use of java.io.StringReader in project camel by apache.
the class XmlConverter method toSAXSourceFromStAX.
@Converter
public SAXSource toSAXSourceFromStAX(StAXSource source, Exchange exchange) throws TransformerException {
String str = toString(source, exchange);
StringReader reader = new StringReader(str);
return new SAXSource(new InputSource(reader));
}
use of java.io.StringReader in project camel by apache.
the class XmlConverter method toSAXSourceFromDOM.
@Converter
public SAXSource toSAXSourceFromDOM(DOMSource source, Exchange exchange) throws TransformerException {
String str = toString(source, exchange);
StringReader reader = new StringReader(str);
return new SAXSource(new InputSource(reader));
}
use of java.io.StringReader in project camel by apache.
the class StaxConverterTest method testToInputSreamByXmlStreamReader.
public void testToInputSreamByXmlStreamReader() throws Exception {
StringReader src = new StringReader(TEST_XML_7000);
XMLStreamReader xreader = null;
InputStream in = null;
try {
xreader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, src);
in = context.getTypeConverter().mandatoryConvertTo(InputStream.class, xreader);
// verify
InputStream expected = new ByteArrayInputStream(TEST_XML_7000.getBytes("utf-8"));
byte[] tmp1 = new byte[512];
byte[] tmp2 = new byte[512];
for (; ; ) {
int n1 = 0;
int n2 = 0;
try {
n1 = expected.read(tmp1, 0, tmp1.length);
n2 = in.read(tmp2, 0, tmp2.length);
} catch (IOException e) {
fail("unable to read data");
}
assertEquals(n1, n2);
if (n2 < 0) {
break;
}
assertTrue(Arrays.equals(tmp1, tmp2));
}
} finally {
if (xreader != null) {
xreader.close();
}
if (in != null) {
in.close();
}
}
}
use of java.io.StringReader in project camel by apache.
the class StAX2SAXSourceTest method testDefaultPrefixInRootElementWithCopyTransformer.
public void testDefaultPrefixInRootElementWithCopyTransformer() throws Exception {
TransformerFactory trf = TransformerFactory.newInstance();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamReader reader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, new StringReader(TEST_XML));
// ensure UTF-8 encoding
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
XMLStreamWriter writer = context.getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class, exchange, baos);
StAX2SAXSource staxSource = new StAX2SAXSource(reader);
StreamSource templateSource = new StreamSource(getClass().getResourceAsStream("/xslt/common/copy.xsl"));
Transformer transformer = trf.newTransformer(templateSource);
log.info("Used transformer: {}", transformer.getClass().getName());
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(staxSource, new StreamResult(baos));
writer.flush();
baos.flush();
assertThat(new String(baos.toByteArray()), equalTo(TEST_XML));
}
use of java.io.StringReader in project camel by apache.
the class IOConverterTest method testToPropertiesFromReader.
public void testToPropertiesFromReader() throws Exception {
Reader br = IOHelper.buffered(new StringReader("foo=123\nbar=456"));
Properties p = IOConverter.toProperties(br);
assertNotNull(p);
assertEquals(2, p.size());
assertEquals("123", p.get("foo"));
assertEquals("456", p.get("bar"));
}
Aggregations