Search in sources :

Example 51 with SAXSource

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

the class JaxbWls method unmarshal.

public static <T> Object 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 = JaxbWls.getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            System.out.println(validationEvent);
            return false;
        }
    });
    final JaxbWls.NamespaceFilter xmlFilter = new JaxbWls.NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    final SAXSource source = new SAXSource(xmlFilter, inputSource);
    JaxbWls.currentPublicId.set(new TreeSet<String>());
    try {
        return unmarshaller.unmarshal(source, type);
    } finally {
        JaxbWls.currentPublicId.set(null);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBContext(javax.xml.bind.JAXBContext) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 52 with SAXSource

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

the class TestGetSAXSourceWithPushOMDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement sourcedElement = factory.createOMElement(new AbstractPushOMDataSource() {

        @Override
        public void serialize(XMLStreamWriter writer) throws XMLStreamException {
            scenario.serialize(writer);
        }

        @Override
        public boolean isDestructiveWrite() {
            return false;
        }
    });
    Iterator<Map.Entry<String, String>> it = scenario.getNamespaceContext().entrySet().iterator();
    OMElement parent;
    if (it.hasNext()) {
        Map.Entry<String, String> binding = it.next();
        parent = factory.createOMElement("parent", factory.createOMNamespace(binding.getValue(), binding.getKey()));
        while (it.hasNext()) {
            binding = it.next();
            parent.declareNamespace(factory.createOMNamespace(binding.getValue(), binding.getKey()));
        }
    } else {
        parent = factory.createOMElement("parent", null);
    }
    parent.addChild(sourcedElement);
    SAXSource saxSource = (serializeParent ? parent : sourcedElement).getSAXSource(true);
    OMElement element = OMXMLBuilderFactory.createOMBuilder(factory, saxSource, false).getDocumentElement();
    if (serializeParent) {
        element = element.getFirstElement();
    }
    scenario.validate(element, false);
}
Also used : OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMFactory(org.apache.axiom.om.OMFactory) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) AbstractPushOMDataSource(org.apache.axiom.om.ds.AbstractPushOMDataSource) Map(java.util.Map)

Example 53 with SAXSource

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

the class BuilderSpec method from.

static BuilderSpec from(StAXParserConfiguration configuration, Source source) {
    if (source instanceof SAXSource) {
        return from((SAXSource) source, true);
    } else if (source instanceof DOMSource) {
        return from(((DOMSource) source).getNode(), true);
    } else if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        InputSource is = new InputSource();
        is.setByteStream(streamSource.getInputStream());
        is.setCharacterStream(streamSource.getReader());
        is.setPublicId(streamSource.getPublicId());
        is.setSystemId(streamSource.getSystemId());
        return from(configuration, is);
    } else if (source instanceof StAXSource) {
        return from(((StAXSource) source).getXMLStreamReader());
    } else {
        try {
            return new BuilderSpec(new FilteredXmlInput(new StAXPullInput(StAXUtils.getXMLInputFactory().createXMLStreamReader(source), true, null), NamespaceRepairingFilter.DEFAULT), null);
        } catch (XMLStreamException ex) {
            throw new OMException(ex);
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) StAXPullInput(org.apache.axiom.om.impl.stream.stax.pull.StAXPullInput) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) FilteredXmlInput(org.apache.axiom.core.stream.FilteredXmlInput) OMException(org.apache.axiom.om.OMException)

Example 54 with SAXSource

use of javax.xml.transform.sax.SAXSource in project ddf by codice.

the class XacmlClient method addNamespaceAndPrefixes.

/**
     * Adds namespaces and namespace prefixes to the XACML response returned by the XACML PDP. The
     * XACML PDP returns a response with no namespaces, so we need to add them to unmarshal the
     * response.
     *
     * @param xacmlResponse The XACML response as a string.
     * @return DOM representation of the XACML response with namespaces and namespace prefixes.
     * @throws PdpException
     */
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
    XMLReader xmlReader = null;
    try {
        XMLReader xmlParser = XMLReaderFactory.createXMLReader();
        xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xmlReader = new XMLFilterImpl(xmlParser) {

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
            }
        };
    } catch (SAXException e) {
        String message = "Unable to read XACML response:\n" + xacmlResponse;
        LOGGER.info(message);
        throw new PdpException(message, e);
    }
    DOMResult domResult;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(XacmlClient.class.getClassLoader());
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        domResult = new DOMResult();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))), domResult);
    } catch (TransformerException e) {
        String message = "Unable to transform XACML response:\n" + xacmlResponse;
        LOGGER.info(message);
        throw new PdpException(message, e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
    return domResult;
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Attributes(org.xml.sax.Attributes) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 55 with SAXSource

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

the class TidyMarkupDataFormat method asNodeTidyMarkup.

/**
     * Return the HTML Markup as an {@link org.w3c.dom.Node}
     * 
     * @param inputStream
     *            The input Stream to convert
     * @return org.w3c.dom.Node The HTML Markup as a DOM Node
     * @throws CamelException
     */
public Node asNodeTidyMarkup(InputStream inputStream) throws CamelException {
    XMLReader parser = createTagSoupParser();
    StringWriter w = new StringWriter();
    parser.setContentHandler(createContentHandler(w));
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        DOMResult result = new DOMResult();
        transformer.transform(new SAXSource(parser, new InputSource(inputStream)), result);
        return result.getNode();
    } catch (Exception e) {
        throw new CamelException("Failed to convert the HTML to tidy Markup", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) StringWriter(java.io.StringWriter) CamelException(org.apache.camel.CamelException) XMLReader(org.xml.sax.XMLReader) CamelException(org.apache.camel.CamelException)

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