Search in sources :

Example 91 with SAXParser

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

the class QuickServerXmlParser method parse.

public static QuickServerXmlParser parse(final String serverXmlContents) {
    final QuickServerXmlParser handler = new QuickServerXmlParser();
    try {
        final SAXParser parser = FACTORY.newSAXParser();
        parser.parse(new ByteArrayInputStream(serverXmlContents.getBytes()), handler);
    } catch (final Exception e) {
    // no-op: using defaults
    }
    return handler;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) SAXException(org.xml.sax.SAXException)

Example 92 with SAXParser

use of javax.xml.parsers.SAXParser 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 93 with SAXParser

use of javax.xml.parsers.SAXParser 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 94 with SAXParser

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

Example 95 with SAXParser

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

the class JaxbOpenejbJar3 method unmarshal.

public static <T> T unmarshal(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
    final InputSource inputSource = new InputSource(in);
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    final JAXBContext ctx = getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            //                System.out.println(validationEvent);
            return false;
        }
    });
    final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    final SAXSource source = new SAXSource(xmlFilter, inputSource);
    final Object o = unmarshaller.unmarshal(source);
    if (o instanceof JAXBElement) {
        final JAXBElement element = (JAXBElement) o;
        return (T) element.getValue();
    }
    return (T) o;
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9