Search in sources :

Example 11 with StringReader

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));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StringReader(java.io.StringReader) Converter(org.apache.camel.Converter)

Example 12 with StringReader

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));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StringReader(java.io.StringReader) Converter(org.apache.camel.Converter)

Example 13 with StringReader

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();
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 14 with StringReader

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));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) TransformerFactory(javax.xml.transform.TransformerFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 15 with StringReader

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"));
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Properties(java.util.Properties)

Aggregations

StringReader (java.io.StringReader)3695 Test (org.junit.Test)837 IOException (java.io.IOException)488 Reader (java.io.Reader)402 InputSource (org.xml.sax.InputSource)369 TokenStream (org.apache.lucene.analysis.TokenStream)298 BufferedReader (java.io.BufferedReader)277 Tokenizer (org.apache.lucene.analysis.Tokenizer)238 StringWriter (java.io.StringWriter)226 ArrayList (java.util.ArrayList)225 JSONReader (com.alibaba.fastjson.JSONReader)195 Document (org.w3c.dom.Document)181 DocumentBuilder (javax.xml.parsers.DocumentBuilder)165 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)141 Map (java.util.Map)139 ParserResult (org.jabref.logic.importer.ParserResult)130 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)120 HashMap (java.util.HashMap)117 Element (org.w3c.dom.Element)108 StreamSource (javax.xml.transform.stream.StreamSource)106