KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > eip > patterns > XPathSplitter


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.eip.patterns;
18
19 import javax.jbi.management.DeploymentException;
20 import javax.xml.namespace.NamespaceContext JavaDoc;
21 import javax.xml.transform.Source JavaDoc;
22 import javax.xml.transform.dom.DOMSource JavaDoc;
23 import javax.xml.xpath.XPathFactory JavaDoc;
24 import javax.xml.xpath.XPathFunctionResolver JavaDoc;
25
26 import org.apache.servicemix.eip.support.AbstractSplitter;
27 import org.apache.servicemix.expression.JAXPNodeSetXPathExpression;
28 import org.apache.servicemix.expression.MessageVariableResolver;
29 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * The XPathSplitter component implements the
35  * <a HREF="http://www.enterpriseintegrationpatterns.com/Sequencer.html">Splitter</a>
36  * pattern using an xpath expression to split the incoming xml.
37  *
38  * @author gnodet
39  * @version $Revision: 376451 $
40  * @org.apache.xbean.XBean element="xpath-splitter"
41  * description="An XPath Splitter"
42  */

43 public class XPathSplitter extends AbstractSplitter {
44
45     /**
46      * The xpath expression to use to split
47      */

48     private JAXPNodeSetXPathExpression xpathExpression = new JAXPNodeSetXPathExpression();
49
50     /* (non-Javadoc)
51      * @see org.apache.servicemix.eip.EIPEndpoint#validate()
52      */

53     public void validate() throws DeploymentException {
54         super.validate();
55         // Check xpath expression
56
try {
57             xpathExpression.afterPropertiesSet();
58         } catch (Exception JavaDoc e) {
59             throw new DeploymentException("Error validating xpath expression", e);
60         }
61     }
62     
63     /* (non-Javadoc)
64      * @see org.apache.servicemix.components.eip.AbstractSplitter#split(javax.xml.transform.Source)
65      */

66     protected Source JavaDoc[] split(Source JavaDoc main) throws Exception JavaDoc {
67         Node JavaDoc doc = new SourceTransformer().toDOMNode(main);
68         NodeList JavaDoc nodes = (NodeList JavaDoc) xpathExpression.evaluateXPath(doc);
69         Source JavaDoc[] parts = new Source JavaDoc[nodes.getLength()];
70         for (int i = 0; i < parts.length; i++) {
71             parts[i] = new DOMSource JavaDoc(nodes.item(i));
72         }
73         return parts;
74     }
75
76     /* (non-Javadoc)
77      * @see org.apache.servicemix.expression.JAXPXPathExpression#getFactory()
78      */

79     public XPathFactory JavaDoc getFactory() {
80         return xpathExpression.getFactory();
81     }
82
83     /* (non-Javadoc)
84      * @see org.apache.servicemix.expression.JAXPXPathExpression#getFunctionResolver()
85      */

86     public XPathFunctionResolver JavaDoc getFunctionResolver() {
87         return xpathExpression.getFunctionResolver();
88     }
89
90     /* (non-Javadoc)
91      * @see org.apache.servicemix.expression.JAXPXPathExpression#getNamespaceContext()
92      */

93     public NamespaceContext JavaDoc getNamespaceContext() {
94         return xpathExpression.getNamespaceContext();
95     }
96
97     /* (non-Javadoc)
98      * @see org.apache.servicemix.expression.JAXPXPathExpression#getTransformer()
99      */

100     public SourceTransformer getTransformer() {
101         return xpathExpression.getTransformer();
102     }
103
104     /* (non-Javadoc)
105      * @see org.apache.servicemix.expression.JAXPXPathExpression#getVariableResolver()
106      */

107     public MessageVariableResolver getVariableResolver() {
108         return xpathExpression.getVariableResolver();
109     }
110
111     /* (non-Javadoc)
112      * @see org.apache.servicemix.expression.JAXPXPathExpression#getXPath()
113      */

114     public String JavaDoc getXPath() {
115         return xpathExpression.getXPath();
116     }
117
118     /* (non-Javadoc)
119      * @see org.apache.servicemix.expression.JAXPXPathExpression#setFactory(javax.xml.xpath.XPathFactory)
120      */

121     public void setFactory(XPathFactory JavaDoc factory) {
122         xpathExpression.setFactory(factory);
123     }
124
125     /* (non-Javadoc)
126      * @see org.apache.servicemix.expression.JAXPXPathExpression#setFunctionResolver(javax.xml.xpath.XPathFunctionResolver)
127      */

128     public void setFunctionResolver(XPathFunctionResolver JavaDoc functionResolver) {
129         xpathExpression.setFunctionResolver(functionResolver);
130     }
131
132     /* (non-Javadoc)
133      * @see org.apache.servicemix.expression.JAXPXPathExpression#setNamespaceContext(javax.xml.namespace.NamespaceContext)
134      */

135     public void setNamespaceContext(NamespaceContext JavaDoc namespaceContext) {
136         xpathExpression.setNamespaceContext(namespaceContext);
137     }
138
139     /* (non-Javadoc)
140      * @see org.apache.servicemix.expression.JAXPXPathExpression#setTransformer(org.apache.servicemix.jbi.jaxp.SourceTransformer)
141      */

142     public void setTransformer(SourceTransformer transformer) {
143         xpathExpression.setTransformer(transformer);
144     }
145
146     /* (non-Javadoc)
147      * @see org.apache.servicemix.expression.JAXPXPathExpression#setVariableResolver(org.apache.servicemix.expression.MessageVariableResolver)
148      */

149     public void setVariableResolver(MessageVariableResolver variableResolver) {
150         xpathExpression.setVariableResolver(variableResolver);
151     }
152
153     /**
154      * @org.apache.xbean.Property alias="xpath"
155      */

156     public void setXPath(String JavaDoc xpath) {
157         xpathExpression.setXPath(xpath);
158     }
159
160 }
161
Popular Tags