use of org.apache.camel.Converter in project camel by apache.
the class XmlConverter method toSAXSourceFromStream.
@Converter
public SAXSource toSAXSourceFromStream(StreamSource source, Exchange exchange) throws SAXException {
InputSource inputSource;
if (source.getReader() != null) {
inputSource = new InputSource(source.getReader());
} else {
inputSource = new InputSource(source.getInputStream());
}
inputSource.setSystemId(source.getSystemId());
inputSource.setPublicId(source.getPublicId());
XMLReader xmlReader = null;
try {
// use the SAXPaserFactory which is set from exchange
if (exchange != null) {
SAXParserFactory sfactory = exchange.getProperty(Exchange.SAXPARSER_FACTORY, SAXParserFactory.class);
if (sfactory != null) {
if (!sfactory.isNamespaceAware()) {
sfactory.setNamespaceAware(true);
}
xmlReader = sfactory.newSAXParser().getXMLReader();
}
}
if (xmlReader == null) {
if (xmlReaderPool == null) {
xmlReaderPool = new XMLReaderPool(createSAXParserFactory());
}
xmlReader = xmlReaderPool.createXMLReader();
}
} catch (Exception ex) {
LOG.warn("Cannot create the SAXParser XMLReader, due to {}", ex);
}
return new SAXSource(xmlReader, inputSource);
}
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));
}
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);
}
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);
}
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;
}
Aggregations