KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > expression > JAXPXPathExpression


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.expression;
18
19 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
20 import org.springframework.beans.factory.InitializingBean;
21 import org.xml.sax.SAXException JavaDoc;
22
23 import javax.jbi.messaging.MessageExchange;
24 import javax.jbi.messaging.MessagingException;
25 import javax.jbi.messaging.NormalizedMessage;
26 import javax.xml.namespace.NamespaceContext JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.xpath.XPath JavaDoc;
30 import javax.xml.xpath.XPathException JavaDoc;
31 import javax.xml.xpath.XPathExpression JavaDoc;
32 import javax.xml.xpath.XPathExpressionException JavaDoc;
33 import javax.xml.xpath.XPathFactory JavaDoc;
34 import javax.xml.xpath.XPathFunctionResolver JavaDoc;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Evalutes an XPath expression on the given message using JAXP
40  *
41  * @version $Revision: 426415 $
42  */

43 public class JAXPXPathExpression implements Expression, InitializingBean {
44     private String JavaDoc xpath;
45     private SourceTransformer transformer = new SourceTransformer();
46     private MessageVariableResolver variableResolver = new MessageVariableResolver();
47     private XPathExpression JavaDoc xPathExpression;
48     private XPathFunctionResolver JavaDoc functionResolver;
49     private NamespaceContext JavaDoc namespaceContext;
50     private XPathFactory JavaDoc factory;
51
52     public JAXPXPathExpression() {
53     }
54
55     /**
56      * A helper constructor to make a fully created expression.
57      */

58     public JAXPXPathExpression(String JavaDoc xpath) throws Exception JavaDoc {
59         this.xpath = xpath;
60     }
61
62     /**
63      * Compiles the xpath expression.
64      */

65     public void afterPropertiesSet() throws XPathExpressionException JavaDoc {
66         if (xPathExpression == null) {
67             if (xpath == null) {
68                 throw new IllegalArgumentException JavaDoc("You must specify the xpath property");
69             }
70
71             if (factory == null) {
72                 factory = XPathFactory.newInstance();
73             }
74             XPath JavaDoc xpathObject = factory.newXPath();
75             xpathObject.setXPathVariableResolver(variableResolver);
76             if (functionResolver != null) {
77                 xpathObject.setXPathFunctionResolver(functionResolver);
78             }
79             if (namespaceContext != null) {
80                 xpathObject.setNamespaceContext(namespaceContext);
81             }
82             xPathExpression = xpathObject.compile(xpath);
83         }
84     }
85
86     /**
87      * Before evaluating the xpath expression, it will be compiled by calling
88      * the {@link #afterPropertiesSet()} method.
89      */

90     public Object JavaDoc evaluate(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
91         try {
92             afterPropertiesSet();
93             Object JavaDoc object = getXMLNode(exchange, message);
94             synchronized (this) {
95                 variableResolver.setExchange(exchange);
96                 variableResolver.setMessage(message);
97                 return evaluateXPath(object);
98             }
99         }
100         catch (TransformerException JavaDoc e) {
101             throw new MessagingException(e);
102         }
103         catch (XPathExpressionException JavaDoc e) {
104             throw new MessagingException(e);
105         }
106         catch (ParserConfigurationException JavaDoc e) {
107             throw new MessagingException(e);
108         }
109         catch (IOException JavaDoc e) {
110             throw new MessagingException(e);
111         }
112         catch (SAXException JavaDoc e) {
113             throw new MessagingException(e);
114         }
115     }
116
117     // Properties
118
//-------------------------------------------------------------------------
119
public String JavaDoc getXPath() {
120         return xpath;
121     }
122
123     public void setXPath(String JavaDoc xpath) {
124         this.xpath = xpath;
125     }
126
127     public SourceTransformer getTransformer() {
128         return transformer;
129     }
130
131     public void setTransformer(SourceTransformer transformer) {
132         this.transformer = transformer;
133     }
134
135     public MessageVariableResolver getVariableResolver() {
136         return variableResolver;
137     }
138
139     public void setVariableResolver(MessageVariableResolver variableResolver) {
140         this.variableResolver = variableResolver;
141     }
142
143     public XPathFactory JavaDoc getFactory() {
144         return factory;
145     }
146
147     public void setFactory(XPathFactory JavaDoc factory) {
148         this.factory = factory;
149     }
150
151     public XPathFunctionResolver JavaDoc getFunctionResolver() {
152         return functionResolver;
153     }
154
155     public void setFunctionResolver(XPathFunctionResolver JavaDoc functionResolver) {
156         this.functionResolver = functionResolver;
157     }
158
159     public NamespaceContext JavaDoc getNamespaceContext() {
160         return namespaceContext;
161     }
162
163     public void setNamespaceContext(NamespaceContext JavaDoc namespaceContext) {
164         this.namespaceContext = namespaceContext;
165     }
166
167     // Implementation methods
168
//-------------------------------------------------------------------------
169
protected Object JavaDoc evaluateXPath(Object JavaDoc object) throws XPathExpressionException JavaDoc {
170         return xPathExpression.evaluate(object);
171     }
172
173     protected XPathExpression JavaDoc getXPathExpression() {
174         return xPathExpression;
175     }
176
177     protected Object JavaDoc getXMLNode(MessageExchange exchange, NormalizedMessage message) throws TransformerException JavaDoc, MessagingException, ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
178         return transformer.toDOMNode(message);
179     }
180 }
181
Popular Tags