public static XMLType xmlPi(String name, String content) { int start = 0; char[] chars = content.toCharArray(); while (start < chars.length && chars[start] == ' ') { start++; } XMLType result = new XMLType(new SQLXMLImpl("<?" + name + " " + content.substring(start) + "?>")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ result.setType(Type.PI); return result; }
public DocumentInProgress(FileStore store, String encoding) throws TeiidComponentException{ final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(store, encoding); this.xml = new SQLXMLImpl(fsisf); this.xml.setEncoding(encoding); SAXTransformerFactory factory = new TransformerFactoryImpl(); try { //SAX2.0 ContentHandler handler = factory.newTransformerHandler(); handler.setResult(new StreamResult(fsisf.getOuputStream())); } catch (Exception e) { throw new TeiidComponentException(QueryPlugin.Event.TEIID30204, e); } transformer = handler.getTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); }
public String getString() throws SQLException { try { return ObjectConverterUtil.convertToString(getCharacterStream()); } catch (IOException e) { SQLException ex = new SQLException(e.getMessage()); ex.initCause(e); throw ex; } }
@SuppressWarnings("unchecked") public <T extends Source> T getSource(Class<T> sourceClass) throws SQLException { if (sourceClass == null || sourceClass == StreamSource.class) { return (T)new StreamSource(getBinaryStream(), this.getStreamFactory().getSystemId()); } else if (sourceClass == StAXSource.class) { XMLInputFactory factory = XMLType.getXmlInputFactory(); try { return (T) new StAXSource(factory.createXMLStreamReader(getBinaryStream())); } catch (XMLStreamException e) { throw new SQLException(e); return (T) new SAXSource(new InputSource(getBinaryStream())); } else if (sourceClass == DOMSource.class) { try { Node doc = docBuilder.parse(new InputSource(getBinaryStream())); return (T) new DOMSource(doc); } catch (ParserConfigurationException e) {
@Test(expected=SQLException.class) public void testSetBinaryStream() throws Exception { SQLXMLImpl xml = new SQLXMLImpl(testStr); xml.setBinaryStream(); }
if (value instanceof String) { String string = (String)value; result = new SQLXMLImpl(string); result.setCharset(Streamable.CHARSET); if (!xp.isWellFormed()) { Reader r = new StringReader(string); result = new SQLXMLImpl(string.getBytesDirect()); result.setCharset(Streamable.CHARSET); if (!xp.isWellFormed()) { Reader r = result.getCharacterStream(); type = validate(xp, r); Streamable<?> s = (Streamable<?>)value; isf = getInputStreamFactory(s); result = new SQLXMLImpl(isf); if (!xp.isWellFormed()) { Reader r = result.getCharacterStream(); type = validate(xp, r);
try { if (obj instanceof SQLXMLImpl) { streams.add(((SQLXMLImpl)obj).getBinaryStream()); StreamFactoryReference sfr = new SQLXMLImpl(); references.add(sfr); return sfr; } else if (obj instanceof SQLXML) { streams.add(((SQLXML)obj).getBinaryStream()); StreamFactoryReference sfr = new SQLXMLImpl(); references.add(sfr); return sfr;
@Test public void testGetCharacterStream() throws Exception { SQLXMLImpl xml = new SQLXMLImpl(testStr); assertEquals(testStr, ObjectConverterUtil.convertToString(xml.getCharacterStream())); }
@Test public void testGetString() throws Exception { SQLXMLImpl xml = new SQLXMLImpl(testStr); assertEquals(testStr, xml.getString()); }
@Test public void testGetSource() throws Exception { SQLXMLImpl xml = new SQLXMLImpl(testStr); assertTrue(xml.getSource(null) instanceof StreamSource); StreamSource ss = (StreamSource)xml.getSource(null); assertEquals(testStr, new String(ObjectConverterUtil.convertToByteArray(ss.getInputStream()), Streamable.ENCODING)); }
@Test(expected=SQLException.class) public void testSetString() throws Exception { SQLXMLImpl xml = new SQLXMLImpl(testStr); xml.setString(testStr); }
@Test(expected=SQLException.class) public void testSetCharacterStream() throws Exception { SQLXMLImpl xml = new SQLXMLImpl(testStr); xml.setCharacterStream(); }
@Override public Charset getCharset() { Charset cs = super.getCharset(); if (cs != null) { return cs; } String enc = null; try { enc = XMLType.getEncoding(this.getBinaryStream()); } catch (SQLException e) { } if (enc != null) { setEncoding(enc); } else { super.setCharset(Streamable.CHARSET); } return super.getCharset(); }
/** * Constructs a SQLXML from bytes that are already encoded in {@link Streamable#ENCODING} * @param bytes */ public SQLXMLImpl(final byte[] bytes) { super(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); } @Override public StorageMode getStorageMode() { return StorageMode.MEMORY; } @Override public long getLength() { return bytes.length; } }); setEncoding(Streamable.ENCODING); }
public static ClobType xslTransform(CommandContext context, Object xml, Object styleSheet) throws Exception { Source styleSource = null; Source xmlSource = null; try { styleSource = convertToSource(styleSheet); xmlSource = convertToSource(xml); final Source xmlParam = xmlSource; TransformerFactory factory = StandardXMLTranslator.getThreadLocalTransformerFactory(); final Transformer transformer = factory.newTransformer(styleSource); //this creates a non-validated sqlxml - it may not be valid xml/root-less xml SQLXMLImpl result = XMLSystemFunctions.saveToBufferManager(context.getBufferManager(), new XMLTranslator() { @Override public void translate(Writer writer) throws TransformerException { //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$ // Feed the resultant I/O stream into the XSLT processor transformer.transform(xmlParam, new StreamResult(writer)); } }, context); return new ClobType(new ClobImpl(result.getStreamFactory(), -1)); } finally { WSUtil.closeSource(styleSource); WSUtil.closeSource(xmlSource); } }
@SuppressWarnings("unchecked") public <T extends Source> T getSource(Class<T> sourceClass) throws SQLException { if (sourceClass == null || sourceClass == StAXSource.class) { return (T) sourceProvider.getStaxSource(); } return super.getSource(sourceClass); }
if (value instanceof String) { String string = (String)value; result = new SQLXMLImpl(string); result.setCharset(Streamable.CHARSET); if (!xp.isWellFormed()) { Reader r = new StringReader(string); result = new SQLXMLImpl(string.getBytesDirect()); result.setCharset(Streamable.CHARSET); if (!xp.isWellFormed()) { Reader r = result.getCharacterStream(); type = validate(xp, r); Streamable<?> s = (Streamable<?>)value; isf = getInputStreamFactory(s); result = new SQLXMLImpl(isf); if (!xp.isWellFormed()) { Reader r = result.getCharacterStream(); type = validate(xp, r);
try { if (obj instanceof SQLXMLImpl) { streams.add(((SQLXMLImpl)obj).getBinaryStream()); StreamFactoryReference sfr = new SQLXMLImpl(); references.add(sfr); return sfr; } else if (obj instanceof SQLXML) { streams.add(((SQLXML)obj).getBinaryStream()); StreamFactoryReference sfr = new SQLXMLImpl(); references.add(sfr); return sfr;
@Test public void testGetString1() throws Exception { SQLXMLImpl clob = new SQLXMLImpl() { public java.io.Reader getCharacterStream() throws java.sql.SQLException { return new Reader() { int pos = 0; @Override public void close() throws IOException { } @Override public int read(char[] cbuf, int off, int len) throws IOException { if (pos < 5) { cbuf[off] = 'a'; pos++; return 1; } return -1; } }; } }; assertEquals("aaaaa", clob.getString()); }
@Override public Charset getCharset() { Charset cs = super.getCharset(); if (cs != null) { return cs; } String enc = null; try { enc = XMLType.getEncoding(this.getBinaryStream()); } catch (SQLException e) { } if (enc != null) { setEncoding(enc); } else { super.setCharset(Streamable.CHARSET); } return super.getCharset(); }