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()));
}
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);
}
}
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);
}
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);
}
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) {
}
}
}
Aggregations