Search in sources :

Example 1 with XMLFilter

use of org.xml.sax.XMLFilter in project opennms by OpenNMS.

the class JaxbUtils method getXMLFilterForClass.

public static <T> XMLFilter getXMLFilterForClass(final Class<T> clazz) throws SAXException {
    final String namespace = getNamespaceForClass(clazz);
    XMLFilter filter = namespace == null ? new SimpleNamespaceFilter("", false) : new SimpleNamespaceFilter(namespace, true);
    LOG.trace("namespace filter for class {}: {}", clazz, filter);
    final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    filter.setParent(xmlReader);
    return filter;
}
Also used : XMLFilter(org.xml.sax.XMLFilter) XMLReader(org.xml.sax.XMLReader)

Example 2 with XMLFilter

use of org.xml.sax.XMLFilter in project fabric8 by jboss-fuse.

the class JaxbUtil method unmarshalNoValidate.

private static Features unmarshalNoValidate(String uri, InputStream stream) {
    try {
        Unmarshaller unmarshaller = FEATURES_CONTEXT.createUnmarshaller();
        XMLFilter xmlFilter = new NoSourceAndNamespaceFilter(XmlUtils.xmlReader());
        xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
        InputSource is = new InputSource(uri);
        if (stream != null) {
            is.setByteStream(stream);
        }
        SAXSource source = new SAXSource(xmlFilter, new InputSource(uri));
        return (Features) unmarshaller.unmarshal(source);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("Unable to load " + uri, e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLFilter(org.xml.sax.XMLFilter) Unmarshaller(javax.xml.bind.Unmarshaller) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException)

Example 3 with XMLFilter

use of org.xml.sax.XMLFilter in project cxf by apache.

the class XSLTJaxbProvider method unmarshalFromInputStream.

@Override
protected Object unmarshalFromInputStream(Unmarshaller unmarshaller, InputStream is, Annotation[] anns, MediaType mt) throws JAXBException {
    try {
        Templates t = createTemplates(getInTemplates(anns, mt), inParamsMap, inProperties);
        if (t == null && supportJaxbOnly) {
            return super.unmarshalFromInputStream(unmarshaller, is, anns, mt);
        }
        if (unmarshaller.getClass().getName().contains("eclipse")) {
            // eclipse MOXy doesn't work properly with the XMLFilter/Reader thing
            // so we need to bounce through a DOM
            Source reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
            DOMResult dom = new DOMResult();
            t.newTransformer().transform(reader, dom);
            return unmarshaller.unmarshal(dom.getNode());
        }
        XMLFilter filter;
        try {
            filter = factory.newXMLFilter(t);
        } catch (TransformerConfigurationException ex) {
            TemplatesImpl ti = (TemplatesImpl) t;
            filter = factory.newXMLFilter(ti.getTemplates());
            trySettingProperties(filter, ti);
        }
        XMLReader reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
        filter.setParent(reader);
        SAXSource source = new SAXSource();
        source.setXMLReader(filter);
        if (systemId != null) {
            source.setSystemId(systemId);
        }
        return unmarshaller.unmarshal(source);
    } catch (TransformerException ex) {
        LOG.warning("Transformation exception : " + ex.getMessage());
        throw ExceptionUtils.toInternalServerErrorException(ex, null);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StaxSource(org.apache.cxf.staxutils.StaxSource) XMLFilter(org.xml.sax.XMLFilter) Templates(javax.xml.transform.Templates) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 4 with XMLFilter

use of org.xml.sax.XMLFilter in project opennms by OpenNMS.

the class XmlTest method validateJaxbXmlAgainstSchema.

@Test
public void validateJaxbXmlAgainstSchema() throws Exception {
    final String schemaFile = getSchemaFile();
    if (schemaFile == null) {
        LOG.warn("Skipping validation.");
        return;
    }
    LOG.debug("Validating against XSD: {}", schemaFile);
    javax.xml.bind.Unmarshaller unmarshaller = JaxbUtils.getUnmarshallerFor(getSampleClass(), null, true);
    final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final Schema schema = factory.newSchema(new StreamSource(schemaFile));
    unmarshaller.setSchema(schema);
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        @Override
        public boolean handleEvent(final ValidationEvent event) {
            LOG.warn("Received validation event: {}", event, event.getLinkedException());
            return false;
        }
    });
    try {
        final InputSource inputSource = new InputSource(getSampleXmlInputStream());
        final XMLFilter filter = JaxbUtils.getXMLFilterForClass(getSampleClass());
        final SAXSource source = new SAXSource(filter, inputSource);
        @SuppressWarnings("unchecked") T obj = (T) unmarshaller.unmarshal(source);
        assertNotNull(obj);
    } finally {
        unmarshaller.setSchema(null);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) InputSource(org.xml.sax.InputSource) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) XMLFilter(org.xml.sax.XMLFilter) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) Test(org.junit.Test)

Example 5 with XMLFilter

use of org.xml.sax.XMLFilter in project opennms by OpenNMS.

the class JaxbUtils method unmarshal.

public static <T> T unmarshal(final Class<T> clazz, final InputSource inputSource, final JAXBContext jaxbContext, final boolean validate) {
    final Unmarshaller um = getUnmarshallerFor(clazz, jaxbContext, validate);
    LOG.trace("unmarshalling class {} from input source {} with unmarshaller {}", clazz.getSimpleName(), inputSource, um);
    try {
        final XMLFilter filter = getXMLFilterForClass(clazz);
        final SAXSource source = new SAXSource(filter, inputSource);
        um.setEventHandler(new LoggingValidationEventHandler());
        final JAXBElement<T> element = um.unmarshal(source, clazz);
        return element.getValue();
    } catch (final SAXException e) {
        throw EXCEPTION_TRANSLATOR.translate("creating an XML reader object", e);
    } catch (final JAXBException e) {
        throw EXCEPTION_TRANSLATOR.translate("unmarshalling an object (" + clazz.getSimpleName() + ")", e);
    }
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) JAXBException(javax.xml.bind.JAXBException) XMLFilter(org.xml.sax.XMLFilter) Unmarshaller(javax.xml.bind.Unmarshaller) SAXException(org.xml.sax.SAXException)

Aggregations

XMLFilter (org.xml.sax.XMLFilter)6 SAXSource (javax.xml.transform.sax.SAXSource)5 StreamSource (javax.xml.transform.stream.StreamSource)3 XMLReader (org.xml.sax.XMLReader)3 JAXBException (javax.xml.bind.JAXBException)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 Source (javax.xml.transform.Source)2 Templates (javax.xml.transform.Templates)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 DOMResult (javax.xml.transform.dom.DOMResult)2 StaxSource (org.apache.cxf.staxutils.StaxSource)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 IOException (java.io.IOException)1 ValidationEvent (javax.xml.bind.ValidationEvent)1 ValidationEventHandler (javax.xml.bind.ValidationEventHandler)1 Schema (javax.xml.validation.Schema)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 Test (org.junit.Test)1