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 = new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new ByteArrayInputStream(new byte[] {}));
}
};
builder.setEntityResolver(er);
}
return builder;
}
use of org.xml.sax.EntityResolver in project maven-plugins by apache.
the class XmlAppendingTransformer method processResource.
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
Document r;
try {
SAXBuilder builder = new SAXBuilder(false);
builder.setExpandEntities(false);
if (ignoreDtd) {
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
});
}
r = builder.build(is);
} catch (JDOMException e) {
throw new RuntimeException("Error processing resource " + resource + ": " + e.getMessage(), e);
}
if (doc == null) {
doc = r;
} else {
Element root = r.getRootElement();
for (@SuppressWarnings("unchecked") Iterator<Attribute> itr = root.getAttributes().iterator(); itr.hasNext(); ) {
Attribute a = itr.next();
itr.remove();
Element mergedEl = doc.getRootElement();
Attribute mergedAtt = mergedEl.getAttribute(a.getName(), a.getNamespace());
if (mergedAtt == null) {
mergedEl.setAttribute(a);
}
}
for (@SuppressWarnings("unchecked") Iterator<Content> itr = root.getChildren().iterator(); itr.hasNext(); ) {
Content n = itr.next();
itr.remove();
doc.getRootElement().addContent(n);
}
}
}
use of org.xml.sax.EntityResolver in project intellij-community by JetBrains.
the class JDOMUtil method getSaxBuilder.
private static SAXBuilder getSaxBuilder() {
SoftReference<SAXBuilder> reference = ourSaxBuilder.get();
SAXBuilder saxBuilder = com.intellij.reference.SoftReference.dereference(reference);
if (saxBuilder == null) {
saxBuilder = new SAXBuilder() {
@Override
protected void configureParser(XMLReader parser, SAXHandler contentHandler) throws JDOMException {
super.configureParser(parser, contentHandler);
try {
parser.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (Exception ignore) {
}
}
};
saxBuilder.setEntityResolver(new EntityResolver() {
@Override
@NotNull
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(new CharArrayReader(ArrayUtil.EMPTY_CHAR_ARRAY));
}
});
ourSaxBuilder.set(new SoftReference<SAXBuilder>(saxBuilder));
}
return saxBuilder;
}
use of org.xml.sax.EntityResolver in project intellij-community by JetBrains.
the class ModelGen method loadXml.
public static Element loadXml(File configXml) throws Exception {
SAXBuilder saxBuilder = new SAXBuilder();
saxBuilder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new CharArrayReader(new char[0]));
}
});
final Document document = saxBuilder.build(configXml);
return document.getRootElement();
}
Aggregations