Search in sources :

Example 1 with StAXSourceProvider

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

Aggregations

Charset (java.nio.charset.Charset)1 SQLException (java.sql.SQLException)1 XMLEventFactory (javax.xml.stream.XMLEventFactory)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 StAXSource (javax.xml.transform.stax.StAXSource)1 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)1 Type (org.teiid.core.types.XMLType.Type)1 ReaderInputStream (org.teiid.core.util.ReaderInputStream)1 StAXSQLXML (org.teiid.util.StAXSQLXML)1 StAXSourceProvider (org.teiid.util.StAXSQLXML.StAXSourceProvider)1