Search in sources :

Example 66 with SAXParserFactory

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

the class ParseContext method getSAXParserFactory.

/**
     * Returns the SAX parser factory specified in this parsing context.
     * If a factory is not explicitly specified, then a default factory
     * instance is created and returned. The default factory instance is
     * configured to be namespace-aware, not validating, and to use
     * {@link XMLConstants#FEATURE_SECURE_PROCESSING secure XML processing}.
     *
     * @since Apache Tika 0.8
     * @return SAX parser factory
     */
public SAXParserFactory getSAXParserFactory() {
    SAXParserFactory factory = get(SAXParserFactory.class);
    if (factory == null) {
        factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        try {
            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        } catch (ParserConfigurationException e) {
        } catch (SAXNotSupportedException e) {
        } catch (SAXNotRecognizedException e) {
        // TIKA-271: Some XML parsers do not support the
        // secure-processing feature, even though it's required by
        // JAXP in Java 5. Ignoring the exception is fine here, as
        // deployments without this feature are inherently vulnerable
        // to XML denial-of-service attacks.
        }
    }
    return factory;
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 67 with SAXParserFactory

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

the class FelixJettySourceReferenceFinder method findSourceReferences.

@Override
public List<SourceReference> findSourceReferences(Bundle bundle) throws SourceReferenceException {
    // so infer them from the X-Jetty-Version header
    if (!bundle.getSymbolicName().equals("org.apache.felix.http.jetty")) {
        return Collections.emptyList();
    }
    final Object jettyVersion = bundle.getHeaders().get("X-Jetty-Version");
    if (!(jettyVersion instanceof String)) {
        log.warn("Could not retrieve Jetty version from bundle '{}' because header 'X-Jetty-Version' is not set!", bundle);
        return Collections.emptyList();
    }
    Enumeration<URL> entries = bundle.findEntries("META-INF/maven", "pom.xml", true);
    if (entries != null && entries.hasMoreElements()) {
        URL entry = entries.nextElement();
        InputStream pom = null;
        try {
            pom = entry.openStream();
            try {
                SAXParserFactory parserFactory = SAXParserFactory.newInstance();
                SAXParser parser = parserFactory.newSAXParser();
                PomHandler handler = new PomHandler((String) jettyVersion);
                parser.parse(new InputSource(pom), handler);
                return handler.getReferences();
            } catch (SAXException e) {
                throw new SourceReferenceException(e);
            } catch (ParserConfigurationException e) {
                throw new SourceReferenceException(e);
            } finally {
                IOUtils.closeQuietly(pom);
            }
        } catch (IOException e) {
            throw new SourceReferenceException(e);
        } finally {
            IOUtils.closeQuietly(pom);
        }
    } else {
        log.warn("Could not find a pom.xml in bundle '{}'!", bundle);
        return Collections.emptyList();
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 68 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project webservices-axiom by apache.

the class SAXBuilderFactory method getBuilder.

@Override
public OMXMLParserWrapper getBuilder(OMMetaFactory metaFactory, InputSource inputSource) throws Exception {
    SAXParserFactory factory = implementation.newSAXParserFactory();
    factory.setNamespaceAware(true);
    factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    SAXParser parser = factory.newSAXParser();
    SAXSource source = new SAXSource(new CoalescingXMLFilter(parser.getXMLReader()), inputSource);
    return OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source, false);
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 69 with SAXParserFactory

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

the class JaxbJavaee method validateJavaee.

/**
     * validate the inputStream, which should be a Java EE standard deployment descriptor against its schema type
     * Note, this method will use the new Java EE 6 schema to validate the old descriptors after changing their namespace and version.
     *
     * @param type
     * @param in
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
public static void validateJavaee(final JavaeeSchema type, final InputStream in) throws ParserConfigurationException, SAXException, IOException {
    final URL javaeeSchemaURL = resolveJavaeeSchemaURL(type);
    if (javaeeSchemaURL == null) {
        throw new IllegalArgumentException("Can not find the xsd file against type:" + type);
    }
    final URL xmlSchemaURL = JaxbJavaee.getSchemaURL("xml.xsd");
    if (xmlSchemaURL == null) {
        throw new IllegalArgumentException("Can not find the xml.xsd file");
    }
    // get the parser
    final SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);
    parserfactory.setValidating(false);
    final SAXParser parser = parserfactory.newSAXParser();
    // get the xml filter
    final Javaee6SchemaFilter xmlFilter = new Javaee6SchemaFilter(parser.getXMLReader());
    // get the source
    final SAXSource sourceForValidate = new SAXSource(xmlFilter, new InputSource(in));
    // get the schema
    final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final JaxbJavaeeSchemaResourceResolver resourceResolver = new JaxbJavaeeSchemaResourceResolver();
    schemaFactory.setResourceResolver(resourceResolver);
    final Schema schema = schemaFactory.newSchema(new Source[] { new StreamSource(xmlSchemaURL.openStream()), new StreamSource(javaeeSchemaURL.openStream()) });
    // validate
    schema.newValidator().validate(sourceForValidate);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) SAXParser(javax.xml.parsers.SAXParser) URL(java.net.URL) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 70 with SAXParserFactory

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

the class JaxbPersistenceFactory method getPersistence.

public static <T> T getPersistence(final Class<T> clazz, final InputStream persistenceDescriptor) throws Exception {
    final JAXBContext jc = clazz.getClassLoader() == JaxbPersistenceFactory.class.getClassLoader() ? JaxbJavaee.getContext(clazz) : JAXBContextFactory.newInstance(clazz);
    final Unmarshaller u = jc.createUnmarshaller();
    final UnmarshallerHandler uh = u.getUnmarshallerHandler();
    // create a new XML parser
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);
    final SAXParser parser = factory.newSAXParser();
    final XMLReader xmlReader = parser.getXMLReader();
    // Create a filter to intercept events
    final PersistenceFilter xmlFilter = new PersistenceFilter(xmlReader);
    // Be sure the filter has the JAXB content handler set (or it wont work)
    xmlFilter.setContentHandler(uh);
    final SAXSource source = new SAXSource(xmlFilter, new InputSource(persistenceDescriptor));
    return (T) u.unmarshal(source);
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) UnmarshallerHandler(javax.xml.bind.UnmarshallerHandler) Unmarshaller(javax.xml.bind.Unmarshaller) XMLReader(org.xml.sax.XMLReader) 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