Search in sources :

Example 1 with EntityResolver

use of org.xml.sax.EntityResolver in project XobotOS by xamarin.

the class Properties method loadFromXML.

/**
     * Loads the properties from an {@code InputStream} containing the
     * properties in XML form. The XML document must begin with (and conform to)
     * following DOCTYPE:
     *
     * <pre>
     * &lt;!DOCTYPE properties SYSTEM &quot;http://java.sun.com/dtd/properties.dtd&quot;&gt;
     * </pre>
     *
     * Also the content of the XML data must satisfy the DTD but the xml is not
     * validated against it. The DTD is not loaded from the SYSTEM ID. After
     * this method returns the InputStream is not closed.
     *
     * @param in the InputStream containing the XML document.
     * @throws IOException in case an error occurs during a read operation.
     * @throws InvalidPropertiesFormatException if the XML data is not a valid
     *             properties file.
     */
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
    if (in == null) {
        throw new NullPointerException();
    }
    if (builder == null) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new Error(e);
        }
        builder.setErrorHandler(new ErrorHandler() {

            public void warning(SAXParseException e) throws SAXException {
                throw e;
            }

            public void error(SAXParseException e) throws SAXException {
                throw e;
            }

            public void fatalError(SAXParseException e) throws SAXException {
                throw e;
            }
        });
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                if (systemId.equals(PROP_DTD_NAME)) {
                    InputSource result = new InputSource(new StringReader(PROP_DTD));
                    result.setSystemId(PROP_DTD_NAME);
                    return result;
                }
                throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
            }
        });
    }
    try {
        Document doc = builder.parse(in);
        NodeList entries = doc.getElementsByTagName("entry");
        if (entries == null) {
            return;
        }
        int entriesListLength = entries.getLength();
        for (int i = 0; i < entriesListLength; i++) {
            Element entry = (Element) entries.item(i);
            String key = entry.getAttribute("key");
            String value = entry.getTextContent();
            /*
                 * key != null & value != null but key or(and) value can be
                 * empty String
                 */
            put(key, value);
        }
    } catch (IOException e) {
        throw e;
    } catch (SAXException e) {
        throw new InvalidPropertiesFormatException(e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with EntityResolver

use of org.xml.sax.EntityResolver in project Openfire by igniterealtime.

the class UserSchemaValidator method validateAndParse.

/**
   * Perform a validate and a parse of the specified source document.
   * Validating is done with the specified schemafiles.
   * 
   * @return A Document when the validation s successful.
   */
Document validateAndParse() {
    ValidatorErrorHandler handler = new ValidatorErrorHandler();
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setXIncludeAware(true);
        documentBuilderFactory.setValidating(false);
        // We don't want xml:base and xml:lang attributes in the output.
        documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-language", false);
        if (schemaSources.length > 0) {
            Log.info("Checking Schema's");
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schemaFactory.setErrorHandler(handler);
            Schema schema = schemaFactory.newSchema(schemaSources);
            documentBuilderFactory.setSchema(schema);
            Log.info("Start validating document");
        } else {
            Log.info("Loading document");
        }
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        handler.reset();
        // Communicate some info about the Xincludes in the imported file. These imports should be resolvable by the server. 
        documentBuilder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                Log.info(String.format("resolved Entity:%s %s", (publicId != null ? publicId : ""), systemId));
                return null;
            }
        });
        documentBuilder.setErrorHandler(handler);
        Document result = documentBuilder.parse(source);
        if (result != null && handler.isValid()) {
            return result;
        } else {
            Log.warn(String.format("document is invalid. %1$d errors found.", handler.getNrOfErrors()));
            return null;
        }
    } catch (Exception e) {
        Log.warn(String.format("document validation failed. %1$d errors found.", handler.getNrOfErrors()));
        Log.debug("", e);
        return null;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Schema(javax.xml.validation.Schema) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 3 with EntityResolver

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;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) InputSource(org.xml.sax.InputSource) SAXHandler(org.jdom.input.SAXHandler) EntityResolver(org.xml.sax.EntityResolver) NotNull(org.jetbrains.annotations.NotNull) XMLReader(org.xml.sax.XMLReader)

Example 4 with EntityResolver

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();
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) InputSource(org.xml.sax.InputSource) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) CharArrayReader(java.io.CharArrayReader) XMLEntityResolver(org.apache.xerces.xni.parser.XMLEntityResolver) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.jdom.Document) SAXException(org.xml.sax.SAXException)

Example 5 with EntityResolver

use of org.xml.sax.EntityResolver in project robovm by robovm.

the class Properties method loadFromXML.

/**
     * Loads the properties from an {@code InputStream} containing the
     * properties in XML form. The XML document must begin with (and conform to)
     * following DOCTYPE:
     *
     * <pre>
     * &lt;!DOCTYPE properties SYSTEM &quot;http://java.sun.com/dtd/properties.dtd&quot;&gt;
     * </pre>
     *
     * Also the content of the XML data must satisfy the DTD but the xml is not
     * validated against it. The DTD is not loaded from the SYSTEM ID. After
     * this method returns the InputStream is not closed.
     *
     * @param in the InputStream containing the XML document.
     * @throws IOException in case an error occurs during a read operation.
     * @throws InvalidPropertiesFormatException if the XML data is not a valid
     *             properties file.
     */
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
    if (in == null) {
        throw new NullPointerException("in == null");
    }
    if (builder == null) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new Error(e);
        }
        builder.setErrorHandler(new ErrorHandler() {

            public void warning(SAXParseException e) throws SAXException {
                throw e;
            }

            public void error(SAXParseException e) throws SAXException {
                throw e;
            }

            public void fatalError(SAXParseException e) throws SAXException {
                throw e;
            }
        });
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                if (systemId.equals(PROP_DTD_NAME)) {
                    InputSource result = new InputSource(new StringReader(PROP_DTD));
                    result.setSystemId(PROP_DTD_NAME);
                    return result;
                }
                throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
            }
        });
    }
    try {
        Document doc = builder.parse(in);
        NodeList entries = doc.getElementsByTagName("entry");
        if (entries == null) {
            return;
        }
        int entriesListLength = entries.getLength();
        for (int i = 0; i < entriesListLength; i++) {
            Element entry = (Element) entries.item(i);
            String key = entry.getAttribute("key");
            String value = entry.getTextContent();
            /*
                 * key != null & value != null but key or(and) value can be
                 * empty String
                 */
            put(key, value);
        }
    } catch (IOException e) {
        throw e;
    } catch (SAXException e) {
        throw new InvalidPropertiesFormatException(e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

EntityResolver (org.xml.sax.EntityResolver)85 InputSource (org.xml.sax.InputSource)41 SAXException (org.xml.sax.SAXException)37 IOException (java.io.IOException)32 DefaultHandler (org.xml.sax.helpers.DefaultHandler)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)23 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 Test (org.junit.jupiter.api.Test)22 Document (org.w3c.dom.Document)18 StringReader (java.io.StringReader)15 Field (java.lang.reflect.Field)14 SAXParseException (org.xml.sax.SAXParseException)14 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 NodeList (org.w3c.dom.NodeList)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)9 Node (org.w3c.dom.Node)9 ErrorHandler (org.xml.sax.ErrorHandler)9 Element (org.w3c.dom.Element)8