Search in sources :

Example 16 with EntityResolver

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;
}
Also used : InputSource(org.xml.sax.InputSource) SAXParser(javax.xml.parsers.SAXParser) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 17 with EntityResolver

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();
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EntityResolver(org.xml.sax.EntityResolver) URISyntaxException(java.net.URISyntaxException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 18 with EntityResolver

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();
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EntityResolver(org.xml.sax.EntityResolver) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 19 with EntityResolver

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);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) EntityResolver(org.xml.sax.EntityResolver) SAXException(org.xml.sax.SAXException)

Example 20 with EntityResolver

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) EntityResolver(org.xml.sax.EntityResolver) URI(java.net.URI) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Aggregations

EntityResolver (org.xml.sax.EntityResolver)24 InputSource (org.xml.sax.InputSource)19 SAXException (org.xml.sax.SAXException)16 IOException (java.io.IOException)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)10 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 SAXParseException (org.xml.sax.SAXParseException)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 Document (org.w3c.dom.Document)6 ErrorHandler (org.xml.sax.ErrorHandler)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StringReader (java.io.StringReader)4 SAXBuilder (org.jdom.input.SAXBuilder)4 NodeList (org.w3c.dom.NodeList)4 Document (org.jdom.Document)3 Element (org.w3c.dom.Element)3 MockResolver (tests.api.org.xml.sax.support.MockResolver)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 SAXParser (javax.xml.parsers.SAXParser)2