Skip to content

Commit

Permalink
Fix XML validation, current code doesn't make sense since the project…
Browse files Browse the repository at this point in the history
… is Java 1.7+.
  • Loading branch information
johnmay committed Oct 9, 2016
1 parent e8194ab commit f63e6a1
Showing 1 changed file with 22 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@
*/
public class XMLIsotopeFactoryTest extends CDKTestCase {

boolean standAlone = false;
boolean standAlone = false;

final static AtomTypeFactory atf = AtomTypeFactory.getInstance(new ChemObject().getBuilder());
final static AtomTypeFactory atf = AtomTypeFactory.getInstance(new ChemObject().getBuilder());

private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";

private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";

static File tmpCMLSchema;
static File tmpCMLSchema;

static {
try {
Expand Down Expand Up @@ -220,35 +220,21 @@ public void testXMLValidityHybrid() throws Exception {
private void assertValidCML(String atomTypeList, String shortcut) throws Exception {
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(atomTypeList);
File tmpInput = copyFileToTmp(shortcut, ".cmlinput", ins, "../../io/cml/data/cml25b1.xsd", "file://"
+ tmpCMLSchema.getAbsolutePath());
+ tmpCMLSchema.getAbsolutePath());
Assert.assertNotNull("Could not find the atom type list CML source", ins);

if (System.getProperty("java.version").indexOf("1.6") != -1
|| System.getProperty("java.version").indexOf("1.7") != -1) {

InputStream cmlSchema = new FileInputStream(tmpCMLSchema);
Assert.assertNotNull("Could not find the CML schema", cmlSchema);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, cmlSchema);
factory.setFeature("http://apache.org/xml/features/validation/schema", true);

DocumentBuilder parser = factory.newDocumentBuilder();
parser.setErrorHandler(new SAXValidityErrorHandler(shortcut));
parser.parse(new FileInputStream(tmpInput));
} else if (System.getProperty("java.version").indexOf("1.5") != -1) {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(tmpInput);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(tmpCMLSchema);
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} else {
Assert.fail("Don't know how to validate with Java version: " + System.getProperty("java.version"));
}
InputStream cmlSchema = new FileInputStream(tmpCMLSchema);
Assert.assertNotNull("Could not find the CML schema", cmlSchema);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, cmlSchema);
factory.setFeature("http://apache.org/xml/features/validation/schema", true);

DocumentBuilder parser = factory.newDocumentBuilder();
parser.setErrorHandler(new SAXValidityErrorHandler(shortcut));
parser.parse(new FileInputStream(tmpInput));
}

@Test
Expand All @@ -273,19 +259,19 @@ public void testCanReadCMLSchema() throws Exception {
* @param in InputStream to copy from
* @param toReplace String to replace. Null, if nothing needs to be replaced.
* @param replaceWith String that replaces the toReplace. Null, if nothing needs to be replaced.
*
* @return The temporary file/
* @return The temporary file/
* @throws IOException
*/
private static File copyFileToTmp(String prefix, String suffix, InputStream in, String toReplace, String replaceWith)
private static File copyFileToTmp(String prefix, String suffix, InputStream in, String toReplace,
String replaceWith)
throws IOException {
File tmpFile = File.createTempFile(prefix, suffix);
FileOutputStream out = new FileOutputStream(tmpFile);
byte[] buf = new byte[4096];
int i = 0;
while ((i = in.read(buf)) != -1) {
if (toReplace != null && replaceWith != null && i >= toReplace.length()
&& new String(buf).contains(toReplace)) {
&& new String(buf).contains(toReplace)) {
// a replacement has been defined
String newString = new String(buf).replaceAll(toReplace, replaceWith);
out.write(newString.getBytes());
Expand Down

0 comments on commit f63e6a1

Please sign in to comment.