Search in sources :

Example 1 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project camel by apache.

the class XmlConverter method toSAXSourceFromStream.

@Converter
public SAXSource toSAXSourceFromStream(StreamSource source, Exchange exchange) throws SAXException {
    InputSource inputSource;
    if (source.getReader() != null) {
        inputSource = new InputSource(source.getReader());
    } else {
        inputSource = new InputSource(source.getInputStream());
    }
    inputSource.setSystemId(source.getSystemId());
    inputSource.setPublicId(source.getPublicId());
    XMLReader xmlReader = null;
    try {
        // use the SAXPaserFactory which is set from exchange
        if (exchange != null) {
            SAXParserFactory sfactory = exchange.getProperty(Exchange.SAXPARSER_FACTORY, SAXParserFactory.class);
            if (sfactory != null) {
                if (!sfactory.isNamespaceAware()) {
                    sfactory.setNamespaceAware(true);
                }
                xmlReader = sfactory.newSAXParser().getXMLReader();
            }
        }
        if (xmlReader == null) {
            if (xmlReaderPool == null) {
                xmlReaderPool = new XMLReaderPool(createSAXParserFactory());
            }
            xmlReader = xmlReaderPool.createXMLReader();
        }
    } catch (Exception ex) {
        LOG.warn("Cannot create the SAXParser XMLReader, due to {}", ex);
    }
    return new SAXSource(xmlReader, inputSource);
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) XMLStreamException(javax.xml.stream.XMLStreamException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Converter(org.apache.camel.Converter)

Example 2 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project groovy by apache.

the class XmlUtil method newSAXParser.

private static SAXParser newSAXParser(boolean namespaceAware, boolean validating, Schema schema1) throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(validating);
    factory.setNamespaceAware(namespaceAware);
    factory.setSchema(schema1);
    return factory.newSAXParser();
}
Also used : SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 3 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project OpenPanodroid by duerrfk.

the class RESTRequestorXML method parseResponse.

@Override
protected void parseResponse(InputStream is, int contentLength) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        DefaultHandler saxHandler = createSAXHandler();
        if (saxHandler == null) {
            setSuccessState(false);
        } else {
            parser.parse(is, saxHandler);
        }
    } catch (Exception ex) {
        setErrorMsg(ex.getLocalizedMessage());
        setSuccessState(false);
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 4 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project che by eclipse.

the class CheCodeFormatterOptions method getCheDefaultSettings.

private Map<String, String> getCheDefaultSettings() {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    XMLParser parserXML = new XMLParser();
    try {
        SAXParser parser = factory.newSAXParser();
        parser.parse(getClass().getResourceAsStream(DEFAULT_CODESTYLE), parserXML);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        LOG.error("It is not possible to parse file " + DEFAULT_CODESTYLE, e);
    }
    return parserXML.getSettings();
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 5 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project jetty.project by eclipse.

the class XmlParser method setValidating.

/* ------------------------------------------------------------ */
public void setValidating(boolean validating) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(validating);
        _parser = factory.newSAXParser();
        try {
            if (validating)
                _parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", validating);
        } catch (Exception e) {
            if (validating)
                LOG.warn("Schema validation may not be supported: ", e);
            else
                LOG.ignore(e);
        }
        _parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", validating);
        _parser.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", true);
        _parser.getXMLReader().setFeature("http://xml.org/sax/features/namespace-prefixes", false);
        try {
            if (validating)
                _parser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", validating);
        } catch (Exception e) {
            LOG.warn(e.getMessage());
        }
    } catch (Exception e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new Error(e.toString());
    }
}
Also used : NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXParserFactory (javax.xml.parsers.SAXParserFactory)183 SAXParser (javax.xml.parsers.SAXParser)141 InputSource (org.xml.sax.InputSource)76 SAXException (org.xml.sax.SAXException)75 IOException (java.io.IOException)62 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 XMLReader (org.xml.sax.XMLReader)37 DefaultHandler (org.xml.sax.helpers.DefaultHandler)27 InputStream (java.io.InputStream)22 File (java.io.File)21 SAXSource (javax.xml.transform.sax.SAXSource)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringReader (java.io.StringReader)15 Unmarshaller (javax.xml.bind.Unmarshaller)13 Attributes (org.xml.sax.Attributes)13 JAXBContext (javax.xml.bind.JAXBContext)12 SAXParseException (org.xml.sax.SAXParseException)10 InputStreamReader (java.io.InputStreamReader)9 ArrayList (java.util.ArrayList)9 ValidationEvent (javax.xml.bind.ValidationEvent)9