Search in sources :

Example 51 with StAXSource

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

the class XMLSystemFunctions method convertReader.

private static void convertReader(Writer writer, XMLEventWriter eventWriter, Reader r, Type type, XMLType xml) throws XMLStreamException, IOException, FactoryConfigurationError, SQLException {
    switch(type) {
        case CONTENT:
        case ELEMENT:
        case PI:
        case COMMENT:
            {
                // write the value directly to the writer
                eventWriter.flush();
                StorageMode storageMode = InputStreamFactory.getStorageMode(xml);
                if ((storageMode == StorageMode.PERSISTENT || storageMode == StorageMode.OTHER) && writer instanceof ExtendedWriter) {
                    ExtendedWriter ew = (ExtendedWriter) writer;
                    if (ew.include(xml)) {
                        break;
                    }
                }
                char[] buf = new char[1 << 13];
                int read = -1;
                if (r == null) {
                    r = xml.getCharacterStream();
                }
                while ((read = r.read(buf)) != -1) {
                    writer.write(buf, 0, read);
                }
                break;
            }
        // assume a document
        case UNKNOWN:
        case // filter the doc declaration
        DOCUMENT:
            XMLEventReader eventReader = null;
            XMLInputFactory inputFactory = XMLType.getXmlInputFactory();
            if (r != null) {
                if (!(r instanceof BufferedReader)) {
                    r = new BufferedReader(r);
                }
                eventReader = inputFactory.createXMLEventReader(r);
            } else {
                StAXSource staxSource = xml.getSource(StAXSource.class);
                eventReader = staxSource.getXMLEventReader();
                if (eventReader == null) {
                    eventReader = inputFactory.createXMLEventReader(staxSource.getXMLStreamReader());
                }
            }
            eventReader = inputFactory.createFilteredReader(eventReader, declarationOmittingFilter);
            eventWriter.add(eventReader);
            break;
        case TEXT:
            if (r == null) {
                r = xml.getCharacterStream();
            }
            XMLEventFactory eventFactory = threadLocalEventtFactory.get();
            char[] buf = new char[1 << 13];
            int read = -1;
            while ((read = r.read(buf)) != -1) {
                eventWriter.add(eventFactory.createCharacters(new String(buf, 0, read)));
            }
            break;
    }
}
Also used : XMLEventFactory(javax.xml.stream.XMLEventFactory) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) StorageMode(org.teiid.core.types.InputStreamFactory.StorageMode) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 52 with StAXSource

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

the class TestSQLXMLProcessing method testStaxComment.

@Test
public void testStaxComment() throws Exception {
    // $NON-NLS-1$
    String sql = "select * from xmltable('/*:Person/phoneNumber' passing cast(? as xml) columns x string path 'type', y string path 'number') as x";
    List<?>[] expected = new List<?>[] { Arrays.asList(null, "8881112222") };
    XMLInputFactory factory = XMLInputFactory.newFactory();
    XMLEventReader reader = factory.createXMLEventReader(new StringReader("<Person><!--hello--><phoneNumber><number>8881112222</number></phoneNumber></Person>"));
    XMLType value = new XMLType(new StAXSQLXML(new StAXSource(reader)));
    value.setType(Type.DOCUMENT);
    Command command = helpParse(sql);
    CommandContext context = createCommandContext();
    QueryMetadataInterface metadata = RealMetadataFactory.example1Cached();
    context.setMetadata(metadata);
    CapabilitiesFinder capFinder = new DefaultCapabilitiesFinder();
    ProcessorPlan plan = helpGetPlan(command, metadata, capFinder, context);
    setParameterValues(Arrays.asList(value), command, context);
    doProcess(plan, dataManager, expected, context);
}
Also used : CommandContext(org.teiid.query.util.CommandContext) StAXSource(javax.xml.transform.stax.StAXSource) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) XMLType(org.teiid.core.types.XMLType) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Command(org.teiid.query.sql.lang.Command) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) List(java.util.List) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) XMLInputFactory(javax.xml.stream.XMLInputFactory) StAXSQLXML(org.teiid.util.StAXSQLXML) Test(org.junit.Test)

Example 53 with StAXSource

use of javax.xml.transform.stax.StAXSource in project syndesis by syndesisio.

the class PayloadConverter method convertAsXml.

private static void convertAsXml(final Message in) {
    final String body = bodyAsString(in);
    if (body == null) {
        return;
    }
    try {
        final XMLStreamReader bodyReader = XML_INPUT_FACTORY.createXMLStreamReader(new StringReader(body));
        final XMLStreamReader eventReader = XML_INPUT_FACTORY.createFilteredReader(bodyReader, new XmlPayloadProcessor(in.getHeaders()));
        final Source source = new StAXSource(eventReader);
        final ByteArrayOutputStream out = new ByteArrayOutputStream(body.length());
        final Result result = new StreamResult(out);
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();
        final Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(source, result);
        in.setBody(new String(out.toByteArray(), StandardCharsets.UTF_8));
    } catch (XMLStreamException | TransformerFactoryConfigurationError | TransformerException e) {
        LOG.warn("Unable to parse payload, continuing without conversion", e);
        return;
    }
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) XMLStreamReader(javax.xml.stream.XMLStreamReader) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StAXSource(javax.xml.transform.stax.StAXSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) TransformerException(javax.xml.transform.TransformerException)

Example 54 with StAXSource

use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.

the class StaxUtilsTests method isStaxSourceJaxp14.

@Test
void isStaxSourceJaxp14() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    String expected = "<element/>";
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
    StAXSource source = new StAXSource(streamReader);
    assertThat(StaxUtils.isStaxSource(source)).as("Not a StAX Source").isTrue();
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.jupiter.api.Test)

Example 55 with StAXSource

use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.

the class XMLEventStreamReaderTests method readCorrect.

@Test
void readCorrect() throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StAXSource source = new StAXSource(streamReader);
    StringWriter writer = new StringWriter();
    transformer.transform(source, new StreamResult(writer));
    Predicate<Node> nodeFilter = n -> n.getNodeType() != Node.DOCUMENT_TYPE_NODE && n.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE;
    assertThat(XmlContent.from(writer)).isSimilarTo(XML, nodeFilter);
}
Also used : XMLEventReader(javax.xml.stream.XMLEventReader) Transformer(javax.xml.transform.Transformer) BeforeEach(org.junit.jupiter.api.BeforeEach) XMLInputFactory(javax.xml.stream.XMLInputFactory) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.jupiter.api.Test) StringReader(java.io.StringReader) StAXSource(javax.xml.transform.stax.StAXSource) Predicate(org.xmlunit.util.Predicate) Node(org.w3c.dom.Node) TransformerFactory(javax.xml.transform.TransformerFactory) XmlContent(org.springframework.core.testfixture.xml.XmlContent) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.jupiter.api.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