Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a local entity resolver for XML, if possible #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/be/ugent/rml/records/XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,28 @@
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XML extends IteratorFormat {

// Avoid downloading external DTDs (and entities) found in XMLs,
// if those files can be found in the classpath.
public class LocalEntityResolver implements EntityResolver {
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
String fileName = systemId.substring(systemId.lastIndexOf("/") + 1);
if (getClass().getClassLoader().getResource(fileName) != null) {
return new InputSource(getClass().getClassLoader().getResourceAsStream(fileName));
} else {
// if a local file is not found, use the default behaviour
return null;
}
}
}

protected String getContentType() {
return "application/xml";
}
Expand All @@ -28,7 +48,9 @@ List<Record> _get(InputStream stream, String iterator) throws IOException {

try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setValidating(false);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
builder.setEntityResolver(new LocalEntityResolver());
Document xmlDocument = builder.parse(stream);

XPath xPath = XPathFactory.newInstance().newXPath();
Expand Down