use of org.xml.sax.EntityResolver in project commons-gdx by gemserk.
the class DocumentParser method parse.
/**
* Returns an XML Document from an InputStream using a DocumentBuilder with the specified validation and namespaceaware flags.
*
* @param schema
* The Schema to be used to parse the Document, null if no Schema wanted.
* @param is
* The InputStream to be processed.
* @param validating
* If the document should be validated or not
* @param namespaceaware
* If the document should be processed with name space awareness or not.
*/
public Document parse(Schema schema, InputStream is, boolean validating, boolean namespaceaware) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
if (schema != null)
factory.setSchema(schema);
factory.setValidating(validating);
factory.setNamespaceAware(namespaceaware);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new DefaultErrorHandler());
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
});
return builder.parse(is);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.xml.sax.EntityResolver in project jackrabbit by apache.
the class ParserTest method testCustomEntityResolver.
public void testCustomEntityResolver() throws ParserConfigurationException, SAXException, IOException {
try {
DocumentBuilderFactory dbf = new DocumentBuilderFactory() {
DocumentBuilderFactory def = DocumentBuilderFactory.newInstance();
@Override
public void setFeature(String name, boolean value) throws ParserConfigurationException {
def.setFeature(name, value);
}
@Override
public void setAttribute(String name, Object value) throws IllegalArgumentException {
def.setAttribute(name, value);
}
@Override
public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
DocumentBuilder db = def.newDocumentBuilder();
db.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if ("foo:test".equals(systemId)) {
return new InputSource(new ByteArrayInputStream("foo&bar".getBytes("UTF-8")));
} else {
return null;
}
}
});
return db;
}
@Override
public boolean getFeature(String name) throws ParserConfigurationException {
return def.getFeature(name);
}
@Override
public Object getAttribute(String name) throws IllegalArgumentException {
return def.getAttribute(name);
}
};
DomUtil.setBuilderFactory(dbf);
String testBody = "<?xml version='1.0'?>\n<!DOCTYPE foo [" + " <!ENTITY test SYSTEM \"foo:test\">" + "]>\n<foo>&test;</foo>";
InputStream is = new ByteArrayInputStream(testBody.getBytes("UTF-8"));
Document d = DomUtil.parseDocument(is);
Element root = d.getDocumentElement();
String text = DomUtil.getText(root);
assertEquals("custom entity resolver apparently not called", "foo&bar", text);
} finally {
DomUtil.setBuilderFactory(null);
}
}
use of org.xml.sax.EntityResolver in project jmeter by apache.
the class XPathUtil method makeDocumentBuilder.
/**
* Create a DocumentBuilder using the makeDocumentFactory func.
*
* @param validate should the parser validate documents?
* @param whitespace should the parser eliminate whitespace in element content?
* @param namespace should the parser be namespace aware?
* @param downloadDTDs if true, parser should attempt to resolve external entities
* @return document builder
* @throws ParserConfigurationException if {@link DocumentBuilder} can not be created for the wanted configuration
*/
public static DocumentBuilder makeDocumentBuilder(boolean validate, boolean whitespace, boolean namespace, boolean downloadDTDs) throws ParserConfigurationException {
DocumentBuilder builder = makeDocumentBuilderFactory(validate, whitespace, namespace).newDocumentBuilder();
builder.setErrorHandler(new MyErrorHandler(validate, false));
if (!downloadDTDs) {
EntityResolver er = (publicId, systemId) -> new InputSource(new ByteArrayInputStream(new byte[0]));
builder.setEntityResolver(er);
}
return builder;
}
use of org.xml.sax.EntityResolver in project j2objc by google.
the class ParserAdapterTest method testGetSetEntityResolver.
public void testGetSetEntityResolver() {
EntityResolver resolver = new MockResolver();
adapter.setEntityResolver(resolver);
assertEquals(resolver, adapter.getEntityResolver());
adapter.setEntityResolver(null);
assertEquals(null, adapter.getEntityResolver());
}
use of org.xml.sax.EntityResolver in project j2objc by google.
the class XMLReaderAdapterTest method testSetEntityResolver.
public void testSetEntityResolver() {
EntityResolver resolver = new MockResolver();
// Ordinary case
adapter.setEntityResolver(resolver);
assertEquals(resolver, reader.getEntityResolver());
// null case
adapter.setEntityResolver(null);
assertEquals(null, reader.getEntityResolver());
}
Aggregations