Search in sources :

Example 16 with StAXSource

use of javax.xml.transform.stax.StAXSource in project cxf by apache.

the class StaxUtils method createXMLStreamReader.

public static XMLStreamReader createXMLStreamReader(Source source) {
    try {
        if (source instanceof DOMSource) {
            DOMSource ds = (DOMSource) source;
            Node nd = ds.getNode();
            Element el = null;
            if (nd instanceof Document) {
                el = ((Document) nd).getDocumentElement();
            } else if (nd instanceof Element) {
                el = (Element) nd;
            }
            if (null != el) {
                return new W3CDOMStreamReader(el, source.getSystemId());
            }
        } else if (source instanceof StAXSource) {
            return ((StAXSource) source).getXMLStreamReader();
        } else if (source instanceof StaxSource) {
            return ((StaxSource) source).getXMLStreamReader();
        } else if (source instanceof SAXSource) {
            SAXSource ss = (SAXSource) source;
            if (ss.getXMLReader() == null) {
                return createXMLStreamReader(((SAXSource) source).getInputSource());
            }
        }
        XMLInputFactory factory = getXMLInputFactory();
        try {
            XMLStreamReader reader = null;
            try {
                reader = factory.createXMLStreamReader(source);
            } catch (UnsupportedOperationException e) {
            // ignore
            }
            if (reader == null && source instanceof StreamSource) {
                // createXMLStreamReader from Source is optional, we'll try and map it
                StreamSource ss = (StreamSource) source;
                if (ss.getInputStream() != null) {
                    reader = factory.createXMLStreamReader(ss.getSystemId(), ss.getInputStream());
                } else {
                    reader = factory.createXMLStreamReader(ss.getSystemId(), ss.getReader());
                }
            }
            return reader;
        } finally {
            returnXMLInputFactory(factory);
        }
    } catch (XMLStreamException e) {
        throw new RuntimeException("Couldn't parse stream.", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) Node(org.w3c.dom.Node) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) StartDocument(javax.xml.stream.events.StartDocument) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 17 with StAXSource

use of javax.xml.transform.stax.StAXSource in project cxf by apache.

the class DispatchClientServerTest method testStAXSourcePAYLOAD.

@Test
public void testStAXSourcePAYLOAD() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    assertNotNull(wsdl);
    Service service = Service.create(wsdl, SERVICE_NAME);
    assertNotNull(service);
    Dispatch<StAXSource> disp = service.createDispatch(PORT_NAME, StAXSource.class, Service.Mode.PAYLOAD);
    disp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:" + greeterPort + "/SOAPDispatchService/SoapDispatchPort");
    QName opQName = new QName("http://apache.org/hello_world_soap_http", "greetMe");
    disp.getRequestContext().put(MessageContext.WSDL_OPERATION, opQName);
    // Test request-response
    InputStream is = getClass().getResourceAsStream("resources/GreetMeDocLiteralSOAPBodyReq.xml");
    StAXSource staxSourceReq = new StAXSource(StaxUtils.createXMLStreamReader(is));
    assertNotNull(staxSourceReq);
    Source resp = disp.invoke(staxSourceReq);
    assertNotNull(resp);
    assertTrue(resp instanceof StAXSource);
    String expected = "Hello TestSOAPInputMessage";
    String actual = StaxUtils.toString(StaxUtils.read(resp));
    assertTrue("Expected: " + expected, actual.contains(expected));
}
Also used : QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) SOAPService(org.apache.hello_world_soap_http.SOAPService) Service(javax.xml.ws.Service) WebService(javax.jws.WebService) StAXSource(javax.xml.transform.stax.StAXSource) URL(java.net.URL) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.Test)

Example 18 with StAXSource

use of javax.xml.transform.stax.StAXSource in project teiid by teiid.

the class XMLSystemFunctions method jsonToXml.

private static SQLXML jsonToXml(CommandContext context, final String rootName, final Reader r, boolean stream) throws TeiidComponentException, TeiidProcessingException {
    JSONParser parser = new JSONParser();
    final JsonToXmlContentHandler reader = new JsonToXmlContentHandler(rootName, r, parser, threadLocalEventtFactory.get());
    SQLXMLImpl sqlXml = null;
    if (stream) {
        try {
            // jre 1.7 event logic does not set a dummy location and throws an NPE in StAXSource, so we explicitly set a location
            // the streaming result will be directly consumed, so there's no danger that we're stepping on another location
            reader.eventFactory.setLocation(dummyLocation);
            sqlXml = new StAXSQLXML(new StAXSource(reader));
        } catch (XMLStreamException e) {
            throw new TeiidProcessingException(e);
        }
    } else {
        sqlXml = XMLSystemFunctions.saveToBufferManager(context.getBufferManager(), new XMLTranslator() {

            @Override
            public void translate(Writer writer) throws TransformerException, IOException {
                try {
                    XMLOutputFactory factory = getOutputFactory();
                    final XMLEventWriter streamWriter = factory.createXMLEventWriter(writer);
                    streamWriter.add(reader);
                    // woodstox needs a flush rather than a close
                    streamWriter.flush();
                } catch (XMLStreamException e) {
                    throw new TransformerException(e);
                } finally {
                    try {
                        r.close();
                    } catch (IOException e) {
                    }
                }
            }
        }, context);
    }
    XMLType result = new XMLType(sqlXml);
    result.setType(Type.DOCUMENT);
    return result;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StAXSource(javax.xml.transform.stax.StAXSource) TeiidProcessingException(org.teiid.core.TeiidProcessingException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventWriter(javax.xml.stream.XMLEventWriter) JSONParser(org.teiid.json.simple.JSONParser) XMLEventWriter(javax.xml.stream.XMLEventWriter) TransformerException(javax.xml.transform.TransformerException) StAXSQLXML(org.teiid.util.StAXSQLXML)

Example 19 with StAXSource

use of javax.xml.transform.stax.StAXSource in project teiid by teiid.

the class XMLSystemFunctions method serialize.

public static Object serialize(XMLSerialize xs, XMLType value) throws TransformationException {
    Type type = value.getType();
    final Charset encoding;
    if (xs.getEncoding() != null) {
        encoding = Charset.forName(xs.getEncoding());
    } else {
        encoding = UTF_8;
    }
    if (Boolean.TRUE.equals(xs.getDeclaration())) {
        // need to replace existing/default declaration
        if (type == Type.ELEMENT || type == Type.DOCUMENT) {
            XMLEventFactory xmlEventFactory = threadLocalEventtFactory.get();
            xmlEventFactory.setLocation(dummyLocation);
            XMLEvent start = null;
            if (xs.getVersion() != null) {
                start = xmlEventFactory.createStartDocument(encoding.name(), xs.getVersion());
            } else {
                // use the encoding regardless as different stax impls have different default
                // behavior
                start = xmlEventFactory.createStartDocument(encoding.name());
            }
            StAXSourceProvider sourceProvider = new DeclarationStaxSourceProvider(start, value);
            value = new XMLType(new StAXSQLXML(sourceProvider, encoding));
            value.setType(type);
        }
    // else just ignore, since the result is likely invalid
    } else if (type == Type.DOCUMENT && Boolean.FALSE.equals(xs.getDeclaration())) {
        final XMLType v = value;
        StAXSourceProvider sourceProvider = new StAXSourceProvider() {

            @Override
            public StAXSource getStaxSource() throws SQLException {
                try {
                    XMLEventReader eventReader = getXMLEventReader(v.getSource(StAXSource.class));
                    eventReader = XMLType.getXmlInputFactory().createFilteredReader(eventReader, declarationOmittingFilter);
                    return new StAXSource(eventReader);
                } catch (XMLStreamException e) {
                    throw new SQLException(e);
                }
            }
        };
        value = new XMLType(new StAXSQLXML(sourceProvider, encoding));
        value.setType(Type.DOCUMENT);
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.STRING) {
        return DataTypeManager.transformValue(value, xs.getType());
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.CLOB) {
        InputStreamFactory isf = Evaluator.getInputStreamFactory(value);
        return new ClobType(new ClobImpl(isf, -1));
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.VARBINARY) {
        try {
            InputStream is = null;
            if (!Charset.forName(value.getEncoding()).equals(encoding)) {
                is = new ReaderInputStream(value.getCharacterStream(), encoding);
            } else {
                is = value.getBinaryStream();
            }
            byte[] bytes = ObjectConverterUtil.convertToByteArray(is, DataTypeManager.MAX_VARBINARY_BYTES);
            return new BinaryType(bytes);
        } catch (SQLException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, "XML", "VARBINARY"));
        } catch (IOException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, "XML", "VARBINARY"));
        }
    }
    InputStreamFactory isf = null;
    if (!Charset.forName(value.getEncoding()).equals(encoding)) {
        // create a wrapper for the input stream
        isf = new InputStreamFactory.SQLXMLInputStreamFactory(value) {

            public InputStream getInputStream() throws IOException {
                try {
                    return new ReaderInputStream(sqlxml.getCharacterStream(), encoding);
                } catch (SQLException e) {
                    throw new IOException(e);
                }
            }
        };
    } else {
        isf = Evaluator.getInputStreamFactory(value);
    }
    return new BlobType(new BlobImpl(isf));
}
Also used : SQLException(java.sql.SQLException) XMLEventFactory(javax.xml.stream.XMLEventFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSQLXML(org.teiid.util.StAXSQLXML) StAXSourceProvider(org.teiid.util.StAXSQLXML.StAXSourceProvider) ReaderInputStream(org.teiid.core.util.ReaderInputStream) Charset(java.nio.charset.Charset) StAXSource(javax.xml.transform.stax.StAXSource) Type(org.teiid.core.types.XMLType.Type) ReaderInputStream(org.teiid.core.util.ReaderInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 20 with StAXSource

use of javax.xml.transform.stax.StAXSource in project teiid by teiid.

the class TestConnectorWorkItem method testTypeConversion.

@Test
public void testTypeConversion() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    String str = "hello world";
    Object source = new StreamSource(new StringReader(str));
    XMLType xml = (XMLType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.XML, null);
    assertEquals(str, xml.getString());
    source = new StAXSource(XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a/>")));
    xml = (XMLType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.XML, null);
    XMLInputFactory in = XMLType.getXmlInputFactory();
    XMLStreamReader reader = in.createXMLStreamReader(new StringReader(xml.getString()));
    assertEquals(XMLEvent.START_DOCUMENT, reader.getEventType());
    assertEquals(XMLEvent.START_ELEMENT, reader.next());
    assertEquals("a", reader.getLocalName());
    assertEquals(XMLEvent.END_ELEMENT, reader.next());
    byte[] bytes = str.getBytes(Streamable.ENCODING);
    source = new InputStreamFactory.BlobInputStreamFactory(BlobType.createBlob(bytes));
    BlobType blob = (BlobType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.BLOB, null);
    assertArrayEquals(bytes, ObjectConverterUtil.convertToByteArray(blob.getBinaryStream()));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) BufferManager(org.teiid.common.buffer.BufferManager) InputStreamFactory(org.teiid.core.types.InputStreamFactory) XMLType(org.teiid.core.types.XMLType) BlobType(org.teiid.core.types.BlobType) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Aggregations

StAXSource (javax.xml.transform.stax.StAXSource)66 XMLStreamReader (javax.xml.stream.XMLStreamReader)27 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringReader (java.io.StringReader)19 XMLInputFactory (javax.xml.stream.XMLInputFactory)19 XMLEventReader (javax.xml.stream.XMLEventReader)16 StreamSource (javax.xml.transform.stream.StreamSource)15 DOMSource (javax.xml.transform.dom.DOMSource)14 Source (javax.xml.transform.Source)13 SAXSource (javax.xml.transform.sax.SAXSource)12 Test (org.junit.Test)12 StreamResult (javax.xml.transform.stream.StreamResult)10 InputStream (java.io.InputStream)9 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)7 Transformer (javax.xml.transform.Transformer)7 Test (org.junit.jupiter.api.Test)7 Document (org.w3c.dom.Document)7 InputSource (org.xml.sax.InputSource)7 StringWriter (java.io.StringWriter)6