Search in sources :

Example 41 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class NodeDataWriter method write.

public void write(Object obj, Node n) {
    try {
        Source s = (Source) obj;
        if (s instanceof DOMSource && ((DOMSource) s).getNode() == null) {
            return;
        }
        XMLStreamWriter writer = new W3CDOMStreamWriter((Element) n);
        StaxUtils.copy(s, writer);
    } catch (XMLStreamException e) {
        throw new Fault("COULD_NOT_WRITE_XML_STREAM_CAUSED_BY", LOG, e, e.getClass().getCanonicalName(), e.getMessage());
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Fault(org.apache.cxf.interceptor.Fault) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source)

Example 42 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class XMLStreamDataReader method validate.

private Element validate(XMLStreamReader input) throws XMLStreamException, IOException {
    DOMSource ds = read(input);
    Element rootElement = null;
    if (ds.getNode() instanceof Document) {
        rootElement = ((Document) ds.getNode()).getDocumentElement();
    } else {
        rootElement = (Element) ds.getNode();
    }
    WoodstoxValidationImpl impl = new WoodstoxValidationImpl();
    XMLStreamWriter nullWriter = null;
    if (impl.canValidate()) {
        nullWriter = StaxUtils.createXMLStreamWriter(new NUllOutputStream());
        impl.setupValidation(nullWriter, message.getExchange().getEndpoint(), message.getExchange().getService().getServiceInfos().get(0));
    }
    // check if the impl can still validate after the setup, possible issue loading schemas or similar
    if (impl.canValidate()) {
        // Can use the MSV libs and woodstox to handle the schema validation during
        // parsing and processing.   Much faster and single traversal
        // filter xop node
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(ds);
        XMLStreamReader filteredReader = StaxUtils.createFilteredReader(reader, new StaxStreamFilter(new QName[] { XOP }));
        StaxUtils.copy(filteredReader, nullWriter);
    } else {
        // MSV not available, use a slower method of cloning the data, replace the xop's, validate
        LOG.fine("NO_MSV_AVAILABLE");
        Element newElement = rootElement;
        if (DOMUtils.hasElementWithName(rootElement, "http://www.w3.org/2004/08/xop/include", "Include")) {
            newElement = (Element) rootElement.cloneNode(true);
            List<Element> elems = DOMUtils.findAllElementsByTagNameNS(newElement, "http://www.w3.org/2004/08/xop/include", "Include");
            for (Element include : elems) {
                Node parentNode = include.getParentNode();
                parentNode.removeChild(include);
                String cid = DOMUtils.getAttribute(include, "href");
                // set the fake base64Binary to validate instead of reading the attachment from message
                parentNode.setTextContent(javax.xml.bind.DatatypeConverter.printBase64Binary(cid.getBytes()));
            }
        }
        try {
            schema.newValidator().validate(new DOMSource(newElement));
        } catch (SAXException e) {
            throw new XMLStreamException(e.getMessage(), e);
        }
    }
    return rootElement;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StaxStreamFilter(org.apache.cxf.staxutils.StaxStreamFilter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) WoodstoxValidationImpl(org.apache.cxf.staxutils.validation.WoodstoxValidationImpl)

Example 43 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class ImportRepairTest method dumpSchema.

private void dumpSchema(Document document) throws Exception {
    if (!dumpSchemas) {
        return;
    }
    XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(System.err);
    xwriter = new PrettyPrintXMLStreamWriter(xwriter, 2);
    StaxUtils.copy(new DOMSource(document), xwriter);
    xwriter.close();
}
Also used : PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter)

Example 44 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class XSLTInterceptorsTest method outXMLStreamTest.

@Test
public void outXMLStreamTest() throws XMLStreamException, SAXException, IOException, ParserConfigurationException {
    CachedWriter cWriter = new CachedWriter();
    cWriter.holdTempFile();
    XMLStreamWriter xWriter = StaxUtils.createXMLStreamWriter(cWriter);
    message.setContent(XMLStreamWriter.class, xWriter);
    outInterceptor.handleMessage(message);
    XMLStreamWriter tXWriter = message.getContent(XMLStreamWriter.class);
    StaxUtils.copy(new StreamSource(messageIS), tXWriter);
    tXWriter.close();
    cWriter.releaseTempFileHold();
    Document doc = StaxUtils.read(cWriter.getReader());
    Assert.assertTrue("Message was not transformed", checkTransformedXML(doc));
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) CachedWriter(org.apache.cxf.io.CachedWriter) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 45 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class StaxUtilsTest method testCXF2468.

@Test
public void testCXF2468() throws Exception {
    Document doc = DOMUtils.newDocument();
    doc.appendChild(doc.createElementNS("http://blah.org/", "blah"));
    Element foo = doc.createElementNS("http://blah.org/", "foo");
    Attr attr = doc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:nil");
    attr.setValue("true");
    foo.setAttributeNodeNS(attr);
    doc.getDocumentElement().appendChild(foo);
    XMLStreamReader sreader = StaxUtils.createXMLStreamReader(doc);
    StringWriter sw = new StringWriter();
    XMLStreamWriter swriter = StaxUtils.createXMLStreamWriter(sw);
    StaxUtils.copy(sreader, swriter, true);
    swriter.flush();
    assertTrue("No xsi namespace: " + sw.toString(), sw.toString().contains("XMLSchema-instance"));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Test(org.junit.Test)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)209 XMLStreamException (javax.xml.stream.XMLStreamException)84 StringWriter (java.io.StringWriter)47 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)40 XMLStreamReader (javax.xml.stream.XMLStreamReader)32 QName (javax.xml.namespace.QName)26 Document (org.w3c.dom.Document)26 Fault (org.apache.cxf.interceptor.Fault)25 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StreamSource (javax.xml.transform.stream.StreamSource)17 StringReader (java.io.StringReader)16 JAXBException (javax.xml.bind.JAXBException)14 DOMSource (javax.xml.transform.dom.DOMSource)14 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)14 Message (org.apache.cxf.message.Message)13 Element (org.w3c.dom.Element)11