use of org.xml.sax.EntityResolver in project musiccabinet by hakko.
the class AbstractSAXParserImpl method getSAXParser.
/*
* From http://docs.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/SAXParserFactory.html
*
* An implementation of the SAXParserFactory class is NOT guaranteed to be thread safe.
* It is up to the user application to make sure about the use of the SAXParserFactory
* from more than one thread. Alternatively the application can have one instance of the
* SAXParserFactory per thread. An application can use the same instance of the factory
* to obtain one or more instances of the SAXParser provided the instance of the factory
* isn't being used in more than one thread at a time.
*
* TODO : woodstox parserfactory synchronization?
*/
protected synchronized SAXParser getSAXParser() throws ParserConfigurationException, SAXException {
SAXParser saxParser = parserFactory.newSAXParser();
saxParser.getXMLReader().setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String arg0, String arg1) throws SAXException, IOException {
return null;
}
});
return saxParser;
}
use of org.xml.sax.EntityResolver in project JFoenix by jfoenixadmin.
the class SVGGlyphBuilder method loadGlyphsFont.
/**
* will load SVG icons from icomoon font file (e.g font.svg)
* @param url of the svg font file
* @throws IOException
*/
public static void loadGlyphsFont(URL url) throws IOException {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
docBuilder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
// System.out.println("Ignoring " + publicId + ", " + systemId);
return new InputSource(new StringReader(""));
}
});
File svgFontFile = new File(url.toURI());
Document doc = docBuilder.parse(svgFontFile);
doc.getDocumentElement().normalize();
NodeList glyphsList = doc.getElementsByTagName("glyph");
for (int i = 0; i < glyphsList.getLength(); i++) {
Node glyph = glyphsList.item(i);
Node glyphName = glyph.getAttributes().getNamedItem("glyph-name");
if (glyphName == null)
continue;
String glyphId = glyphName.getNodeValue();
SVGGlyphBuilder glyphPane = new SVGGlyphBuilder(i, glyphId, (String) glyph.getAttributes().getNamedItem("d").getNodeValue());
glyphsMap.put(svgFontFile.getName() + "." + glyphId, glyphPane);
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.xml.sax.EntityResolver in project JFoenix by jfoenixadmin.
the class SVGGlyphBuilder method loadGlyphsFont.
/**
* will load SVG icons from input stream
* @param stream input stream of svg font file
* @param keyPrefix will be used as a prefix when storing SVG icons in the map
* @throws IOException
*/
public static void loadGlyphsFont(InputStream stream, String keyPrefix) throws IOException {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
docBuilder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
// System.out.println("Ignoring " + publicId + ", " + systemId);
return new InputSource(new StringReader(""));
}
});
Document doc = docBuilder.parse(stream);
doc.getDocumentElement().normalize();
NodeList glyphsList = doc.getElementsByTagName("glyph");
for (int i = 0; i < glyphsList.getLength(); i++) {
Node glyph = glyphsList.item(i);
Node glyphName = glyph.getAttributes().getNamedItem("glyph-name");
if (glyphName == null)
continue;
String glyphId = glyphName.getNodeValue();
SVGGlyphBuilder glyphPane = new SVGGlyphBuilder(i, glyphId, (String) glyph.getAttributes().getNamedItem("d").getNodeValue());
glyphsMap.put(keyPrefix + "." + glyphId, glyphPane);
}
stream.close();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.xml.sax.EntityResolver in project Mycat-Server by MyCATApache.
the class ConfigUtil method getDocument.
public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(dtd);
}
});
builder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException e) {
}
@Override
public void error(SAXParseException e) throws SAXException {
throw e;
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
throw e;
}
});
return builder.parse(xml);
}
use of org.xml.sax.EntityResolver in project robovm by robovm.
the class ExpatParser method handleExternalEntity.
/**
* Handles an external entity.
*
* @param context to be passed back to Expat when we parse the entity
* @param publicId the publicId of the entity
* @param systemId the systemId of the entity
*/
/*package*/
void handleExternalEntity(String context, String publicId, String systemId) throws SAXException, IOException {
EntityResolver entityResolver = xmlReader.entityResolver;
if (entityResolver == null) {
return;
}
/*
* The spec. is terribly under-specified here. It says that if the
* systemId is a URL, we should try to resolve it, but it doesn't
* specify how to tell whether or not the systemId is a URL let alone
* how to resolve it.
*
* Other implementations do various insane things. We try to keep it
* simple: if the systemId parses as a URI and it's relative, we try to
* resolve it against the parent document's systemId. If anything goes
* wrong, we go with the original systemId. If crazybob had designed
* the API, he would have left all resolving to the EntityResolver.
*/
if (this.systemId != null) {
try {
URI systemUri = new URI(systemId);
if (!systemUri.isAbsolute() && !systemUri.isOpaque()) {
// It could be relative (or it may not be a URI at all!)
URI baseUri = new URI(this.systemId);
systemUri = baseUri.resolve(systemUri);
// Replace systemId w/ resolved URI
systemId = systemUri.toString();
}
} catch (Exception e) {
System.logI("Could not resolve '" + systemId + "' relative to" + " '" + this.systemId + "' at " + locator, e);
}
}
InputSource inputSource = entityResolver.resolveEntity(publicId, systemId);
if (inputSource == null) {
/*
* The spec. actually says that we should try to treat systemId
* as a URL and download and parse its contents here, but an
* entity resolver can easily accomplish the same by returning
* new InputSource(systemId).
*
* Downloading external entities by default would result in several
* unwanted DTD downloads, not to mention pose a security risk
* when parsing untrusted XML -- see for example
* http://archive.cert.uni-stuttgart.de/bugtraq/2002/10/msg00421.html --
* so we just do nothing instead. This also enables the user to
* opt out of entity parsing when using
* {@link org.xml.sax.helpers.DefaultHandler}, something that
* wouldn't be possible otherwise.
*/
return;
}
String encoding = pickEncoding(inputSource);
long pointer = createEntityParser(this.pointer, context);
try {
EntityParser entityParser = new EntityParser(encoding, xmlReader, pointer, inputSource.getPublicId(), inputSource.getSystemId());
parseExternalEntity(entityParser, inputSource);
} finally {
releaseParser(pointer);
}
}
Aggregations