Search in sources :

Example 1 with StAXSQLXML

use of org.teiid.util.StAXSQLXML 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 2 with StAXSQLXML

use of org.teiid.util.StAXSQLXML 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 3 with StAXSQLXML

use of org.teiid.util.StAXSQLXML in project teiid by teiid.

the class WSProcedureExecution method getOutputParameterValues.

@Override
public List<?> getOutputParameterValues() throws TranslatorException {
    Object result = returnValue;
    if (returnValue != null && (returnValue instanceof StAXSource) && procedure.getArguments().size() > 4 && procedure.getArguments().get(4).getDirection() == Direction.IN && Boolean.TRUE.equals(procedure.getArguments().get(4).getArgumentValue().getValue())) {
        SQLXMLImpl sqlXml = new StAXSQLXML((StAXSource) returnValue);
        XMLType xml = new XMLType(sqlXml);
        xml.setType(Type.DOCUMENT);
        result = xml;
    } else if (returnValue != null && returnValue instanceof DOMSource) {
        final DOMSource xmlSource = (DOMSource) returnValue;
        SQLXMLImpl sqlXml = new SQLXMLImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                Result outputTarget = new StreamResult(outputStream);
                try {
                    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
                } catch (Exception e) {
                    throw new IOException(e);
                }
                return new ByteArrayInputStream(outputStream.toByteArray());
            }
        });
        XMLType xml = new XMLType(sqlXml);
        xml.setType(Type.DOCUMENT);
        result = xml;
    }
    return Arrays.asList(result);
}
Also used : SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) StAXSource(javax.xml.transform.stax.StAXSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) TransformerException(javax.xml.transform.TransformerException) SQLException(java.sql.SQLException) XMLStreamException(javax.xml.stream.XMLStreamException) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) XMLType(org.teiid.core.types.XMLType) ByteArrayInputStream(java.io.ByteArrayInputStream) StAXSQLXML(org.teiid.util.StAXSQLXML)

Example 4 with StAXSQLXML

use of org.teiid.util.StAXSQLXML in project teiid by teiid.

the class WSWSDLProcedureExecution method getOutputParameterValues.

@Override
public List<?> getOutputParameterValues() throws TranslatorException {
    Object result = returnValue;
    if (returnValue != null && procedure.getArguments().size() > 1 && Boolean.TRUE.equals(procedure.getArguments().get(1).getArgumentValue().getValue())) {
        SQLXMLImpl sqlXml = new StAXSQLXML(returnValue);
        XMLType xml = new XMLType(sqlXml);
        xml.setType(Type.DOCUMENT);
        result = xml;
    }
    return Arrays.asList(result);
}
Also used : XMLType(org.teiid.core.types.XMLType) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) StAXSQLXML(org.teiid.util.StAXSQLXML)

Example 5 with StAXSQLXML

use of org.teiid.util.StAXSQLXML 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)

Aggregations

StAXSQLXML (org.teiid.util.StAXSQLXML)5 StAXSource (javax.xml.transform.stax.StAXSource)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 XMLType (org.teiid.core.types.XMLType)3 SQLException (java.sql.SQLException)2 XMLEventReader (javax.xml.stream.XMLEventReader)2 TransformerException (javax.xml.transform.TransformerException)2 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Charset (java.nio.charset.Charset)1 List (java.util.List)1 XMLEventFactory (javax.xml.stream.XMLEventFactory)1 XMLEventWriter (javax.xml.stream.XMLEventWriter)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 Result (javax.xml.transform.Result)1