use of org.xml.sax.InputSource in project j2objc by google.
the class DocumentBuilderTest method testParseInputSource.
/**
* javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream)
* Case 1: Try to parse correct xml document.
* Case 2: Try to call parse() with null argument.
* Case 3: Try to parse a non-existent file.
* Case 4: Try to parse incorrect xml file.
*/
public void testParseInputSource() {
InputStream stream = getClass().getResourceAsStream("/simple.xml");
InputSource is = new InputSource(stream);
// case 1: Trivial use.
try {
Document d = db.parse(is);
assertNotNull(d);
// TBD getXmlEncoding() IS NOT SUPPORTED
// assertEquals("ISO-8859-1", d.getXmlEncoding());
assertEquals(2, d.getChildNodes().getLength());
assertEquals("#comment", d.getChildNodes().item(0).getNodeName());
assertEquals("breakfast_menu", d.getChildNodes().item(1).getNodeName());
} catch (IOException ioe) {
fail("Unexpected IOException " + ioe.toString());
} catch (SAXException sax) {
fail("Unexpected SAXException " + sax.toString());
}
// case 2: Try to call parse with null argument
try {
db.parse((InputSource) null);
fail("Expected IllegalArgumentException was not thrown");
} catch (IllegalArgumentException iae) {
// expected
} catch (IOException ioe) {
fail("Unexpected IOException " + ioe.toString());
} catch (SAXException sax) {
fail("Unexpected SAXException " + sax.toString());
}
// case 3: Try to parse a non-existent file
try {
db.parse(new InputSource(new FileInputStream("_")));
fail("Expected IOException was not thrown");
} catch (IOException ioe) {
// expected
} catch (SAXException sax) {
fail("Unexpected SAXException " + sax.toString());
}
// case 4: Try to parse incorrect xml file
try {
is = new InputSource(getClass().getResourceAsStream("/wrong.xml"));
db.parse(is);
fail("Expected SAXException was not thrown");
} catch (IOException ioe) {
fail("Unexpected IOException " + ioe.toString());
} catch (SAXException sax) {
// expected
}
}
use of org.xml.sax.InputSource in project j2objc by google.
the class DocumentBuilderTest method testReset.
public void testReset() {
// Make sure EntityResolver gets reset
InputStream source = new ByteArrayInputStream("<a>&foo;</a>".getBytes());
InputStream entity = new ByteArrayInputStream("bar".getBytes());
MockResolver resolver = new MockResolver();
resolver.addEntity("foo", "foo", new InputSource(entity));
Document d;
try {
db = dbf.newDocumentBuilder();
db.setEntityResolver(resolver);
db.reset();
d = db.parse(source);
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
Element root = (Element) d.getElementsByTagName("a").item(0);
assertEquals("foo", ((EntityReference) root.getFirstChild()).getNodeName());
// Make sure ErrorHandler gets reset
source = new ByteArrayInputStream("</a>".getBytes());
MethodLogger logger = new MethodLogger();
ErrorHandler handler = new MockHandler(logger);
try {
db = dbf.newDocumentBuilder();
db.setErrorHandler(handler);
db.reset();
d = db.parse(source);
} catch (SAXParseException e) {
// Expected
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
assertEquals(0, logger.size());
}
use of org.xml.sax.InputSource in project CoreNLP by stanfordnlp.
the class XMLUtils method readDocumentFromString.
// end class SAXErrorHandler
public static Document readDocumentFromString(String s) throws Exception {
InputSource in = new InputSource(new StringReader(s));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
return factory.newDocumentBuilder().parse(in);
}
use of org.xml.sax.InputSource in project liquibase by liquibase.
the class LiquibaseEntityResolver method tryResolveFromResourceAccessor.
private InputSource tryResolveFromResourceAccessor(String systemId) {
String path = FilenameUtils.concat(basePath, systemId);
log.debug("Attempting to load " + systemId + " from resourceAccessor as " + path);
try {
InputStream resourceAsStream = StreamUtil.singleInputStream(path, resourceAccessor);
if (resourceAsStream == null) {
log.debug("Could not load " + systemId + " from resourceAccessor as " + path);
return null;
}
return new InputSource(resourceAsStream);
} catch (Exception ex) {
return null;
}
}
use of org.xml.sax.InputSource in project liquibase by liquibase.
the class LiquibaseEntityResolver method resolveEntity.
@Override
public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) throws SAXException, IOException {
log.debug("Resolving XML entity name='" + name + "', publicId='" + publicId + "', baseURI='" + baseURI + "', systemId='" + systemId + "'");
if (systemId == null) {
log.debug("Unable to resolve XML entity locally. Will load from network.");
return null;
}
InputSource resolved = null;
if (systemId.toLowerCase().endsWith(".xsd")) {
if (systemId.startsWith("http://www.liquibase.org/xml/ns/migrator/")) {
systemId = systemId.replace("http://www.liquibase.org/xml/ns/migrator/", "http://www.liquibase.org/xml/ns/dbchangelog/");
}
resolved = tryResolveLiquibaseSchema(systemId, publicId);
}
if (resolved == null && resourceAccessor != null && basePath != null) {
resolved = tryResolveFromResourceAccessor(systemId);
}
if (resolved == null) {
log.debug("Unable to resolve XML entity locally. Will load from network.");
}
return resolved;
}
Aggregations