use of javax.xml.transform.stax.StAXSource in project teiid by teiid.
the class XMLSystemFunctions method convertReader.
private static void convertReader(Writer writer, XMLEventWriter eventWriter, Reader r, Type type, XMLType xml) throws XMLStreamException, IOException, FactoryConfigurationError, SQLException {
switch(type) {
case CONTENT:
case ELEMENT:
case PI:
case COMMENT:
{
// write the value directly to the writer
eventWriter.flush();
StorageMode storageMode = InputStreamFactory.getStorageMode(xml);
if ((storageMode == StorageMode.PERSISTENT || storageMode == StorageMode.OTHER) && writer instanceof ExtendedWriter) {
ExtendedWriter ew = (ExtendedWriter) writer;
if (ew.include(xml)) {
break;
}
}
char[] buf = new char[1 << 13];
int read = -1;
if (r == null) {
r = xml.getCharacterStream();
}
while ((read = r.read(buf)) != -1) {
writer.write(buf, 0, read);
}
break;
}
// assume a document
case UNKNOWN:
case // filter the doc declaration
DOCUMENT:
XMLEventReader eventReader = null;
XMLInputFactory inputFactory = XMLType.getXmlInputFactory();
if (r != null) {
if (!(r instanceof BufferedReader)) {
r = new BufferedReader(r);
}
eventReader = inputFactory.createXMLEventReader(r);
} else {
StAXSource staxSource = xml.getSource(StAXSource.class);
eventReader = staxSource.getXMLEventReader();
if (eventReader == null) {
eventReader = inputFactory.createXMLEventReader(staxSource.getXMLStreamReader());
}
}
eventReader = inputFactory.createFilteredReader(eventReader, declarationOmittingFilter);
eventWriter.add(eventReader);
break;
case TEXT:
if (r == null) {
r = xml.getCharacterStream();
}
XMLEventFactory eventFactory = threadLocalEventtFactory.get();
char[] buf = new char[1 << 13];
int read = -1;
while ((read = r.read(buf)) != -1) {
eventWriter.add(eventFactory.createCharacters(new String(buf, 0, read)));
}
break;
}
}
use of javax.xml.transform.stax.StAXSource 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);
}
use of javax.xml.transform.stax.StAXSource in project syndesis by syndesisio.
the class PayloadConverter method convertAsXml.
private static void convertAsXml(final Message in) {
final String body = bodyAsString(in);
if (body == null) {
return;
}
try {
final XMLStreamReader bodyReader = XML_INPUT_FACTORY.createXMLStreamReader(new StringReader(body));
final XMLStreamReader eventReader = XML_INPUT_FACTORY.createFilteredReader(bodyReader, new XmlPayloadProcessor(in.getHeaders()));
final Source source = new StAXSource(eventReader);
final ByteArrayOutputStream out = new ByteArrayOutputStream(body.length());
final Result result = new StreamResult(out);
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
final Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
in.setBody(new String(out.toByteArray(), StandardCharsets.UTF_8));
} catch (XMLStreamException | TransformerFactoryConfigurationError | TransformerException e) {
LOG.warn("Unable to parse payload, continuing without conversion", e);
return;
}
}
use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.
the class StaxUtilsTests method isStaxSourceJaxp14.
@Test
void isStaxSourceJaxp14() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String expected = "<element/>";
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
StAXSource source = new StAXSource(streamReader);
assertThat(StaxUtils.isStaxSource(source)).as("Not a StAX Source").isTrue();
}
use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.
the class XMLEventStreamReaderTests method readCorrect.
@Test
void readCorrect() throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StAXSource source = new StAXSource(streamReader);
StringWriter writer = new StringWriter();
transformer.transform(source, new StreamResult(writer));
Predicate<Node> nodeFilter = n -> n.getNodeType() != Node.DOCUMENT_TYPE_NODE && n.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE;
assertThat(XmlContent.from(writer)).isSimilarTo(XML, nodeFilter);
}
Aggregations