Search in sources :

Example 1 with XMLType

use of org.teiid.core.types.XMLType 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)

Example 2 with XMLType

use of org.teiid.core.types.XMLType in project teiid by teiid.

the class TeiidServiceHandler method handleLobResult.

private void handleLobResult(String charSet, Object result, ServiceResponse response) throws SQLException {
    if (result == null) {
        // or should this be an empty result?
        return;
    }
    if (result instanceof SQLXML) {
        if (charSet != null) {
            XMLSerialize serialize = new XMLSerialize();
            // $NON-NLS-1$
            serialize.setTypeString("blob");
            serialize.setDeclaration(true);
            serialize.setEncoding(charSet);
            serialize.setDocument(true);
            try {
                InputStream content = ((BlobType) XMLSystemFunctions.serialize(serialize, new XMLType((SQLXML) result))).getBinaryStream();
                response.writeContent(content, 200, false);
                response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
            } catch (TransformationException e) {
                throw new SQLException(e);
            }
        } else {
            InputStream content = ((SQLXML) result).getBinaryStream();
            response.writeContent(content, 200, false);
            response.writeOK(ContentType.APPLICATION_XML);
        }
    } else if (result instanceof Blob) {
        InputStream content = ((Blob) result).getBinaryStream();
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
    } else if (result instanceof Clob) {
        InputStream content = new ReaderInputStream(((Clob) result).getCharacterStream(), charSet == null ? Charset.defaultCharset() : Charset.forName(charSet));
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.TEXT_PLAIN);
    } else {
        InputStream content = new ByteArrayInputStream(result.toString().getBytes(charSet == null ? Charset.defaultCharset() : Charset.forName(charSet)));
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
    }
}
Also used : XMLType(org.teiid.core.types.XMLType) SQLXML(java.sql.SQLXML) XMLSerialize(org.teiid.query.sql.symbol.XMLSerialize) BlobType(org.teiid.core.types.BlobType) TransformationException(org.teiid.core.types.TransformationException) Blob(java.sql.Blob) ReaderInputStream(org.teiid.core.util.ReaderInputStream) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) Clob(java.sql.Clob)

Example 3 with XMLType

use of org.teiid.core.types.XMLType in project teiid by teiid.

the class TestXMLSystemFunctions method testXMLInput.

// simulate what would happen if someone passed the output of an XML query to the xpathvalue function
@Test
public void testXMLInput() throws Exception {
    // $NON-NLS-1$
    XMLType doc = new XMLType(new SQLXMLImpl("<foo/>"));
    // $NON-NLS-1$
    String xpath = "a/b/c";
    String value = XMLSystemFunctions.xpathValue(doc, xpath);
    assertNull(value);
}
Also used : XMLType(org.teiid.core.types.XMLType) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) Test(org.junit.Test)

Example 4 with XMLType

use of org.teiid.core.types.XMLType in project teiid by teiid.

the class TestFunctionLibrary method testInvokeXmlConcat.

@Test
public void testInvokeXmlConcat() throws Exception {
    CommandContext c = new CommandContext();
    c.setBufferManager(BufferManagerFactory.getStandaloneBufferManager());
    XMLType result = (XMLType) helpInvokeMethod("xmlconcat", new Class<?>[] { DataTypeManager.DefaultDataClasses.XML, DataTypeManager.DefaultDataClasses.XML }, new Object[] { DataTypeManager.transformValue("<bar/>", DataTypeManager.DefaultDataClasses.XML), DataTypeManager.transformValue("<Catalogs xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Catalog><Items><Item ItemID=\"001\"><Name>Lamp</Name><Quantity>5</Quantity></Item></Items></Catalog></Catalogs>", DataTypeManager.DefaultDataClasses.XML) }, c);
    String xml = ObjectConverterUtil.convertToString(result.getCharacterStream());
    assertEquals("<bar/><Catalogs xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Catalog><Items><Item ItemID=\"001\"><Name>Lamp</Name><Quantity>5</Quantity></Item></Items></Catalog></Catalogs>", xml);
}
Also used : XMLType(org.teiid.core.types.XMLType) CommandContext(org.teiid.query.util.CommandContext) Test(org.junit.Test)

Example 5 with XMLType

use of org.teiid.core.types.XMLType in project teiid by teiid.

the class SQLXMLToStringTransform method transformDirect.

/**
 * This method transforms a value of the source type into a value
 * of the target type.
 * @param value Incoming value of source type
 * @return Outgoing value of target type
 * @throws TransformationException if value is an incorrect input type or
 * the transformation fails
 */
public Object transformDirect(Object value) throws TransformationException {
    XMLType source = (XMLType) value;
    Reader reader = null;
    try {
        char[] result = new char[DataTypeManager.MAX_STRING_LENGTH];
        reader = source.getCharacterStream();
        int read = reader.read(result);
        return new String(result, 0, read);
    } catch (SQLException e) {
        throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, new Object[] { getSourceType().getName(), getTargetType().getName() }));
    } catch (IOException e) {
        throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, new Object[] { getSourceType().getName(), getTargetType().getName() }));
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
        }
    }
}
Also used : XMLType(org.teiid.core.types.XMLType) TransformationException(org.teiid.core.types.TransformationException) SQLException(java.sql.SQLException) Reader(java.io.Reader) IOException(java.io.IOException)

Aggregations

XMLType (org.teiid.core.types.XMLType)25 SQLException (java.sql.SQLException)9 IOException (java.io.IOException)8 Test (org.junit.Test)7 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)7 Clob (java.sql.Clob)6 BlobType (org.teiid.core.types.BlobType)6 StringReader (java.io.StringReader)5 StAXSource (javax.xml.transform.stax.StAXSource)5 ClobType (org.teiid.core.types.ClobType)4 TranslatorException (org.teiid.translator.TranslatorException)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 TransformerException (javax.xml.transform.TransformerException)3 StreamResult (javax.xml.transform.stream.StreamResult)3 WebServiceException (javax.xml.ws.WebServiceException)3 Item (net.sf.saxon.om.Item)3 InputStreamFactory (org.teiid.core.types.InputStreamFactory)3 TransformationException (org.teiid.core.types.TransformationException)3 Type (org.teiid.core.types.XMLType.Type)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2