1 17 package org.apache.servicemix.components.splitter; 18 19 import java.io.IOException ; 20 21 import javax.jbi.JBIException; 22 import javax.jbi.messaging.ExchangeStatus; 23 import javax.jbi.messaging.InOnly; 24 import javax.jbi.messaging.MessageExchange; 25 import javax.jbi.messaging.MessagingException; 26 import javax.jbi.messaging.NormalizedMessage; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.transform.TransformerException ; 29 import javax.xml.transform.dom.DOMSource ; 30 import javax.xml.xpath.XPath ; 31 import javax.xml.xpath.XPathConstants ; 32 import javax.xml.xpath.XPathExpression ; 33 import javax.xml.xpath.XPathExpressionException ; 34 import javax.xml.xpath.XPathFactory ; 35 36 import org.apache.servicemix.components.util.TransformComponentSupport; 37 import org.apache.servicemix.jbi.MissingPropertyException; 38 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 39 import org.w3c.dom.Node ; 40 import org.w3c.dom.NodeList ; 41 import org.xml.sax.SAXException ; 42 43 49 public class SplitterComponent extends TransformComponentSupport { 50 51 52 private String nodePath; 53 54 private XPathExpression expression; 55 56 private SourceTransformer st = new SourceTransformer(); 57 58 protected void init() throws JBIException { 59 super.init(); 60 61 if (nodePath == null) { 62 throw new MissingPropertyException("nodePath"); 63 } 64 } 65 66 protected boolean transform(MessageExchange me, NormalizedMessage in, 67 NormalizedMessage out) throws MessagingException { 68 NodeList nodes; 69 try { 70 Node doc = st.toDOMNode(in); 71 if (expression == null) { 72 XPath xpath = XPathFactory.newInstance().newXPath(); 73 expression = xpath.compile(nodePath); 74 } 75 nodes = (NodeList ) expression.evaluate(doc, XPathConstants.NODESET); 76 } catch (TransformerException e) { 77 throw new MessagingException(e); 78 } catch (IOException e) { 79 throw new MessagingException(e); 80 } catch (SAXException e) { 81 throw new MessagingException(e); 82 } catch (ParserConfigurationException e) { 83 throw new MessagingException(e); 84 } catch (XPathExpressionException e) { 85 throw new MessagingException(e); 87 } 88 int total = nodes.getLength(); 89 for (int i = 0; i < total; i++) { 90 out.setContent(new DOMSource (nodes.item(i))); 91 InOnly outExchange = getExchangeFactory().createInOnlyExchange(); 92 outExchange.setInMessage(out); 93 getDeliveryChannel().sendSync(outExchange); 94 outExchange.setStatus(ExchangeStatus.DONE); 95 } 96 return false; 97 } 98 99 104 public String getNodePath() { 105 return nodePath; 106 } 107 108 115 public void setNodePath(String nodePath) { 116 this.nodePath = nodePath; 117 this.expression = null; 118 } 119 } 120 | Popular Tags |