Skip to content

Commit

Permalink
expose reason why XML doc is not well-formed #3845
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Aug 14, 2017
1 parent 64e1ee4 commit fb8b597
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
20 changes: 12 additions & 8 deletions src/main/java/edu/harvard/iq/dataverse/util/xml/XmlValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,39 @@ public static boolean validateXml(String fileToValidate, String schemaToValidate
}
}

public static boolean xmlWellFormed(String filename) throws ParserConfigurationException, SAXException, IOException {
/**
* @param filename XML file on disk to check for well-formedness.
* @return true if well-formed or an exception with a message about why if
* not.
* @throws Exception if the XML is not well-formed with a message about why.
*/
public static boolean xmlWellFormed(String filename) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new SimpleErrorHandler());
// the "parse" method also validates XML, will throw an exception if misformatted
try {
Document document = builder.parse(new InputSource(filename));
return true;
} catch (Exception ex) {
logger.info("XML is not well formed: " + ex);
return false;
} catch (SAXException ex) {
throw new Exception("XML is not well formed: " + ex.getMessage(), ex);
}

}

public static class SimpleErrorHandler implements ErrorHandler {

@Override
public void warning(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}

@Override
public void error(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}

@Override
public void fatalError(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.Assert;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
Expand All @@ -21,10 +23,37 @@ public void testValidateXml() throws IOException, SAXException, ParserConfigurat
}

@Test
public void testWellFormedXml() throws ParserConfigurationException, SAXException, IOException {
assertTrue(XmlValidator.xmlWellFormed("pom.xml"));
assertFalse(XmlValidator.xmlWellFormed("scripts/issues/3845/not-well-formed.xml"));
assertTrue(XmlValidator.xmlWellFormed("scripts/issues/3845/sendToDataCite.xml"));
public void testWellFormedXml() {

// well-formed XML
Exception ex1 = null;
try {
assertTrue(XmlValidator.xmlWellFormed("scripts/issues/3845/sendToDataCite.xml"));
} catch (Exception ex) {
ex1 = ex;
}
Assert.assertNull(ex1);

// not well-formed XML
Exception ex2 = null;
try {
XmlValidator.xmlWellFormed("scripts/issues/3845/not-well-formed.xml");
} catch (Exception ex) {
ex2 = ex;
}
Assert.assertNotNull(ex2);
Assert.assertEquals("XML is not well formed: The element type \"br\" must be terminated by the matching end-tag \"</br>\".", ex2.getMessage());

// other exception
Exception ex3 = null;
try {
XmlValidator.xmlWellFormed("path/to/nowhere.xml");
} catch (Exception ex) {
ex3 = ex;
}
Assert.assertNotNull(ex3);
Assert.assertEquals("class java.io.FileNotFoundException", ex3.getClass().toString());

}

}

0 comments on commit fb8b597

Please sign in to comment.