Search in sources :

Example 46 with SAXSource

use of javax.xml.transform.sax.SAXSource in project webservices-axiom by apache.

the class TestDetachWithSAXSource method runTest.

@Override
protected void runTest() throws Throwable {
    DummyXMLReader xmlReader = new DummyXMLReader();
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new SAXSource(xmlReader, new InputSource()), false);
    assertThat(xmlReader.isParsed()).isFalse();
    builder.detach();
    assertThat(xmlReader.isParsed()).isTrue();
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 47 with SAXSource

use of javax.xml.transform.sax.SAXSource 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 48 with SAXSource

use of javax.xml.transform.sax.SAXSource 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 49 with SAXSource

use of javax.xml.transform.sax.SAXSource 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)

Example 50 with SAXSource

use of javax.xml.transform.sax.SAXSource in project tomee by apache.

the class JaxbSun method unmarshal.

public static <T> Object unmarshal(final Class<T> type, final InputStream in, final boolean logErrors) throws ParserConfigurationException, SAXException, JAXBException {
    // create a parser with validation disabled
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    // Get the JAXB context -- this should be cached
    final JAXBContext ctx = JAXBContextFactory.newInstance(type);
    // get the unmarshaller
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    // log errors?
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            if (logErrors) {
                System.out.println(validationEvent);
            }
            return false;
        }
    });
    // add our XMLFilter which disables dtd downloading
    final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    // Wrap the input stream with our filter
    final SAXSource source = new SAXSource(xmlFilter, new InputSource(in));
    // unmarshal the document
    return unmarshaller.unmarshal(source);
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXSource (javax.xml.transform.sax.SAXSource)111 InputSource (org.xml.sax.InputSource)81 XMLReader (org.xml.sax.XMLReader)38 Source (javax.xml.transform.Source)28 StreamSource (javax.xml.transform.stream.StreamSource)28 DOMSource (javax.xml.transform.dom.DOMSource)27 SAXException (org.xml.sax.SAXException)26 TransformerException (javax.xml.transform.TransformerException)24 SAXParserFactory (javax.xml.parsers.SAXParserFactory)20 Unmarshaller (javax.xml.bind.Unmarshaller)17 SAXParser (javax.xml.parsers.SAXParser)17 Transformer (javax.xml.transform.Transformer)17 StreamResult (javax.xml.transform.stream.StreamResult)16 Test (org.junit.Test)16 StringReader (java.io.StringReader)15 IOException (java.io.IOException)14 JAXBContext (javax.xml.bind.JAXBContext)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 ValidationEvent (javax.xml.bind.ValidationEvent)10