Search in sources :

Example 46 with SAXException

use of org.xml.sax.SAXException 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 47 with SAXException

use of org.xml.sax.SAXException 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 48 with SAXException

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

the class HtmlToSpannedConverter method convert.

public Spanned convert() {
    mReader.setContentHandler(this);
    try {
        mReader.parse(new InputSource(new StringReader(mSource)));
    } catch (IOException e) {
        // We are reading from a string. There should not be IO problems.
        throw new RuntimeException(e);
    } catch (SAXException e) {
        // TagSoup doesn't throw parse exceptions.
        throw new RuntimeException(e);
    }
    // Fix flags and range for paragraph-type markup.
    Object[] obj = mSpannableStringBuilder.getSpans(0, mSpannableStringBuilder.length(), ParagraphStyle.class);
    for (int i = 0; i < obj.length; i++) {
        int start = mSpannableStringBuilder.getSpanStart(obj[i]);
        int end = mSpannableStringBuilder.getSpanEnd(obj[i]);
        // If the last line of the range is blank, back off by one.
        if (end - 2 >= 0) {
            if (mSpannableStringBuilder.charAt(end - 1) == '\n' && mSpannableStringBuilder.charAt(end - 2) == '\n') {
                end--;
            }
        }
        if (end == start) {
            mSpannableStringBuilder.removeSpan(obj[i]);
        } else {
            mSpannableStringBuilder.setSpan(obj[i], start, end, Spannable.SPAN_PARAGRAPH);
        }
    }
    return mSpannableStringBuilder;
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 49 with SAXException

use of org.xml.sax.SAXException 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 50 with SAXException

use of org.xml.sax.SAXException in project OpenAM by OpenRock.

the class UnmarshallerImpl method unmarshal.

protected Object unmarshal(XMLReader reader, InputSource source) throws JAXBException {
    SAXLocator locator = new SAXLocator();
    SAXUnmarshallerHandler handler = createUnmarshallerHandler(locator);
    reader = InterningXMLReader.adapt(reader);
    reader.setContentHandler(handler);
    // saxErrorHandler will be set by the createUnmarshallerHandler method.
    // configure XMLReader so that the error will be sent to it.
    // This is essential for the UnmarshallerHandler to be able to abort
    // unmarshalling when an error is found.
    //
    // Note that when this XMLReader is provided by the client code,
    // it might be already configured to call a client error handler.
    // This will clobber such handler, if any.
    //
    // Ryan noted that we might want to report errors to such a client
    // error handler as well.
    reader.setErrorHandler(new ErrorHandlerAdaptor(handler, locator));
    try {
        reader.parse(source);
    } catch (IOException e) {
        throw new JAXBException(e);
    } catch (SAXException e) {
        throw createUnmarshalException(e);
    }
    Object result = handler.getResult();
    // avoid keeping unnecessary references too long to let the GC
    // reclaim more memory.
    // setting null upsets some parsers, so use a dummy instance instead.
    reader.setContentHandler(dummyHandler);
    reader.setErrorHandler(dummyHandler);
    return result;
}
Also used : SAXLocator(com.sun.xml.bind.validator.SAXLocator) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

SAXException (org.xml.sax.SAXException)2465 IOException (java.io.IOException)1622 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1049 Document (org.w3c.dom.Document)682 DocumentBuilder (javax.xml.parsers.DocumentBuilder)537 InputSource (org.xml.sax.InputSource)518 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)415 InputStream (java.io.InputStream)317 Element (org.w3c.dom.Element)311 NodeList (org.w3c.dom.NodeList)292 File (java.io.File)274 Node (org.w3c.dom.Node)247 ByteArrayInputStream (java.io.ByteArrayInputStream)235 StringReader (java.io.StringReader)224 SAXParser (javax.xml.parsers.SAXParser)209 SAXParseException (org.xml.sax.SAXParseException)196 TransformerException (javax.xml.transform.TransformerException)180 ArrayList (java.util.ArrayList)169 SAXParserFactory (javax.xml.parsers.SAXParserFactory)159 XMLReader (org.xml.sax.XMLReader)151