KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > splitter > SplitterComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.splitter;
18
19 import java.io.IOException JavaDoc;
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 JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.dom.DOMSource JavaDoc;
30 import javax.xml.xpath.XPath JavaDoc;
31 import javax.xml.xpath.XPathConstants JavaDoc;
32 import javax.xml.xpath.XPathExpression JavaDoc;
33 import javax.xml.xpath.XPathExpressionException JavaDoc;
34 import javax.xml.xpath.XPathFactory JavaDoc;
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 JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  * This Component splits a message according to a XPath expression.
45  *
46  * @author george
47  * @deprecated use the XPathSplitter pattern from the EIP component instead
48  */

49 public class SplitterComponent extends TransformComponentSupport {
50
51     /** Holds value of property nodePath. */
52     private String JavaDoc nodePath;
53
54     private XPathExpression JavaDoc 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 JavaDoc nodes;
69         try {
70             Node JavaDoc doc = st.toDOMNode(in);
71             if (expression == null) {
72                 XPath JavaDoc xpath = XPathFactory.newInstance().newXPath();
73                 expression = xpath.compile(nodePath);
74             }
75             nodes = (NodeList JavaDoc) expression.evaluate(doc, XPathConstants.NODESET);
76         } catch (TransformerException JavaDoc e) {
77             throw new MessagingException(e);
78         } catch (IOException JavaDoc e) {
79             throw new MessagingException(e);
80         } catch (SAXException JavaDoc e) {
81             throw new MessagingException(e);
82         } catch (ParserConfigurationException JavaDoc e) {
83             throw new MessagingException(e);
84         } catch (XPathExpressionException JavaDoc e) {
85             // If XPath Expression is mal formed
86
throw new MessagingException(e);
87         }
88         int total = nodes.getLength();
89         for (int i = 0; i < total; i++) {
90             out.setContent(new DOMSource JavaDoc(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     /**
100      * Getter for property nodePath.
101      *
102      * @return Value of property nodePath.
103      */

104     public String JavaDoc getNodePath() {
105         return nodePath;
106     }
107
108     /**
109      * @org.xbean.Property alias="select"
110      *
111      * Setter for property nodePath.
112      * @param nodePath
113      * New value of property nodePath.
114      */

115     public void setNodePath(String JavaDoc nodePath) {
116         this.nodePath = nodePath;
117         this.expression = null;
118     }
119 }
120
Popular Tags