Search in sources :

Example 31 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project openhab1-addons by openhab.

the class IhcProjectFile method parseProject.

static HashMap<Integer, ArrayList<IhcEnumValue>> parseProject(String filePath, String dumpResourcesToFile) throws IhcExecption {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File(filePath));
        return parseProject(doc, dumpResourcesToFile);
    } catch (ParserConfigurationException e) {
        throw new IhcExecption(e);
    } catch (SAXException e) {
        throw new IhcExecption(e);
    } catch (IOException e) {
        throw new IhcExecption(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) File(java.io.File) SAXException(org.xml.sax.SAXException)

Example 32 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project jphp by jphp-compiler.

the class UIReader method read.

public Component read(InputStream inputStream) {
    try {
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document document = builder.parse(inputStream);
        Element root = document.getDocumentElement();
        return readElement(root);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 33 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project midpoint by Evolveum.

the class DOMUtil method parseFile.

public static Document parseFile(File file) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder loader = factory.newDocumentBuilder();
        return loader.parse(file);
    } catch (SAXException | IOException | ParserConfigurationException ex) {
        throw new IllegalStateException("Error parsing XML document " + ex.getMessage(), ex);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 34 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project midpoint by Evolveum.

the class DOMUtil method createDocumentBuilder.

public static DocumentBuilder createDocumentBuilder() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    try {
        return factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("Error creating document builder " + e.getMessage(), e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 35 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException 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)

Aggregations

ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1435 SAXException (org.xml.sax.SAXException)1039 IOException (java.io.IOException)951 Document (org.w3c.dom.Document)751 DocumentBuilder (javax.xml.parsers.DocumentBuilder)687 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)389 InputSource (org.xml.sax.InputSource)260 NodeList (org.w3c.dom.NodeList)248 Node (org.w3c.dom.Node)225 SAXParser (javax.xml.parsers.SAXParser)185 File (java.io.File)171 InputStream (java.io.InputStream)170 TransformerException (javax.xml.transform.TransformerException)167 SAXParserFactory (javax.xml.parsers.SAXParserFactory)144 ByteArrayInputStream (java.io.ByteArrayInputStream)141 StringReader (java.io.StringReader)127 ArrayList (java.util.ArrayList)122 DOMSource (javax.xml.transform.dom.DOMSource)114 StreamResult (javax.xml.transform.stream.StreamResult)98