Search in sources :

Example 21 with Converter

use of org.apache.camel.Converter 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 22 with Converter

use of org.apache.camel.Converter in project camel by apache.

the class XmlConverter method toStAXSource.

/**
     * Converts the source instance to a {@link StAXSource} or returns null if the conversion is not
     * supported (making it easy to derive from this class to add new kinds of conversion).
     * @throws FileNotFoundException
     * @throws XMLStreamException
     */
@Converter
public StAXSource toStAXSource(File file, Exchange exchange) throws FileNotFoundException, XMLStreamException {
    InputStream is = IOHelper.buffered(new FileInputStream(file));
    XMLStreamReader r = new StaxConverter().createXMLStreamReader(is, exchange);
    return new StAXSource(r);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StAXSource(javax.xml.transform.stax.StAXSource) FileInputStream(java.io.FileInputStream) Converter(org.apache.camel.Converter)

Example 23 with Converter

use of org.apache.camel.Converter in project camel by apache.

the class XmlConverter method toDOMSourceFromStream.

@Converter
public DOMSource toDOMSourceFromStream(StreamSource source, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
    Document document;
    String systemId = source.getSystemId();
    DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
    Reader reader = source.getReader();
    if (reader != null) {
        document = builder.parse(new InputSource(reader));
    } else {
        InputStream inputStream = source.getInputStream();
        if (inputStream != null) {
            InputSource inputsource = new InputSource(inputStream);
            inputsource.setSystemId(systemId);
            document = builder.parse(inputsource);
        } else {
            throw new IOException("No input stream or reader available on StreamSource: " + source);
        }
    }
    return new DOMSource(document, systemId);
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) Converter(org.apache.camel.Converter)

Example 24 with Converter

use of org.apache.camel.Converter in project camel by apache.

the class PurchaseOrderConverter method toPurchaseOrder.

@Converter
public PurchaseOrder toPurchaseOrder(byte[] data) {
    String s = converter.convertTo(String.class, data);
    if (s == null || s.length() < 30) {
        throw new IllegalArgumentException("data is invalid");
    }
    s = s.replaceAll("##START##", "");
    s = s.replaceAll("##END##", "");
    String name = s.substring(0, 9).trim();
    String s2 = s.substring(10, 19).trim();
    String s3 = s.substring(20).trim();
    BigDecimal price = new BigDecimal(s2);
    price = price.setScale(2);
    Integer amount = converter.convertTo(Integer.class, s3);
    PurchaseOrder order = new PurchaseOrder(name, price, amount);
    return order;
}
Also used : PurchaseOrder(org.apache.camel.converter.PurchaseOrder) BigDecimal(java.math.BigDecimal) TypeConverter(org.apache.camel.TypeConverter) Converter(org.apache.camel.Converter)

Example 25 with Converter

use of org.apache.camel.Converter in project camel by apache.

the class RecordStringConverter method toString.

@Converter
public static String toString(Record record) {
    Charset charset = Charset.forName("UTF-8");
    ByteBuffer buffer = record.getData();
    if (buffer.hasArray()) {
        byte[] bytes = record.getData().array();
        return new String(bytes, charset);
    } else {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        return new String(bytes, charset);
    }
}
Also used : Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer) Converter(org.apache.camel.Converter)

Aggregations

Converter (org.apache.camel.Converter)71 TypeConverter (org.apache.camel.TypeConverter)11 FallbackConverter (org.apache.camel.FallbackConverter)10 InputStream (java.io.InputStream)8 ByteBuffer (java.nio.ByteBuffer)7 IOConverter (org.apache.camel.converter.IOConverter)7 InputSource (org.xml.sax.InputSource)6 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 List (java.util.List)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 FileInputStream (java.io.FileInputStream)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 SAXSource (javax.xml.transform.sax.SAXSource)3 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)3 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 Document (org.w3c.dom.Document)3 Element (org.w3c.dom.Element)3