KickJava   Java API By Example, From Geeks To Geeks.

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


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.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
22 import org.jaxen.FunctionContext;
23 import org.jaxen.JaxenException;
24 import org.jaxen.NamespaceContext;
25 import org.jaxen.XPath;
26 import org.jaxen.dom.DOMXPath;
27 import org.springframework.beans.factory.InitializingBean;
28 import org.w3c.dom.Node JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 import javax.jbi.messaging.MessageExchange;
32 import javax.jbi.messaging.MessagingException;
33 import javax.jbi.messaging.NormalizedMessage;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import javax.xml.transform.TransformerException JavaDoc;
36
37 import java.io.IOException JavaDoc;
38
39
40 /**
41  * Evalutes an XPath expression on the given message using <a HREF="http://jaxen.org/"/>Jaxen</a>
42  *
43  * @version $Revision: 426415 $
44  */

45 public class JaxenXPathExpression implements Expression, InitializingBean {
46     private static final transient Log log = LogFactory.getLog(JaxenXPathExpression.class);
47     
48     private String JavaDoc xpath;
49     private SourceTransformer transformer = new SourceTransformer();
50     private JaxenVariableContext variableContext = new JaxenVariableContext();
51     private XPath xpathObject;
52     private NamespaceContext namespaceContext;
53     private FunctionContext functionContext;
54
55     public JaxenXPathExpression() {
56     }
57
58     /**
59      * A helper constructor to make a fully created expression. This constructor will
60      * call the {@link #afterPropertiesSet()} method to ensure this POJO is properly constructed.
61      */

62     public JaxenXPathExpression(String JavaDoc xpath) throws Exception JavaDoc {
63         this.xpath = xpath;
64         afterPropertiesSet();
65     }
66
67     public void afterPropertiesSet() throws Exception JavaDoc {
68         if (xpathObject == null) {
69             if (xpath == null) {
70                 throw new IllegalArgumentException JavaDoc("You must specify the xpath property");
71             }
72             xpathObject = createXPath(xpath);
73             xpathObject.setVariableContext(variableContext);
74             if (namespaceContext != null) {
75                 xpathObject.setNamespaceContext(namespaceContext);
76             }
77             if (functionContext != null) {
78                 xpathObject.setFunctionContext(functionContext);
79             }
80         }
81     }
82
83     public Object JavaDoc evaluate(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
84         try {
85             Object JavaDoc object = getXMLNode(exchange, message);
86             if (object == null) {
87                 return null;
88             }
89             synchronized (this) {
90                 variableContext.setExchange(exchange);
91                 variableContext.setMessage(message);
92                 return evaluateXPath(object);
93             }
94         }
95         catch (TransformerException JavaDoc e) {
96             throw new MessagingException(e);
97         }
98         catch (JaxenException e) {
99             throw new MessagingException(e);
100         }
101         catch (ParserConfigurationException JavaDoc e) {
102             throw new MessagingException(e);
103         }
104         catch (IOException JavaDoc e) {
105             throw new MessagingException(e);
106         }
107         catch (SAXException JavaDoc e) {
108             throw new MessagingException(e);
109         }
110     }
111
112     public boolean matches(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
113         try {
114             Object JavaDoc object = getXMLNode(exchange, message);
115             if (object == null) {
116                 return false;
117             }
118             synchronized (this) {
119                 variableContext.setExchange(exchange);
120                 variableContext.setMessage(message);
121                 return evaluateXPathAsBoolean(object);
122             }
123         }
124         catch (TransformerException JavaDoc e) {
125             throw new MessagingException(e);
126         }
127         catch (JaxenException e) {
128             throw new MessagingException(e);
129         }
130         catch (ParserConfigurationException JavaDoc e) {
131             throw new MessagingException(e);
132         }
133         catch (IOException JavaDoc e) {
134             throw new MessagingException(e);
135         }
136         catch (SAXException JavaDoc e) {
137             throw new MessagingException(e);
138         }
139     }
140
141     // Properties
142
//-------------------------------------------------------------------------
143
public XPath getXpathObject() {
144         return xpathObject;
145     }
146
147     public void setXpathObject(XPath xpathObject) {
148         this.xpathObject = xpathObject;
149     }
150
151     public String JavaDoc getXpath() {
152         return xpath;
153     }
154
155     public void setXpath(String JavaDoc xpath) {
156         this.xpath = xpath;
157     }
158
159     public SourceTransformer getTransformer() {
160         return transformer;
161     }
162
163     public void setTransformer(SourceTransformer transformer) {
164         this.transformer = transformer;
165     }
166
167     public JaxenVariableContext getVariableContext() {
168         return variableContext;
169     }
170
171     public void setVariableContext(JaxenVariableContext variableContext) {
172         this.variableContext = variableContext;
173     }
174
175     public NamespaceContext getNamespaceContext() {
176         return namespaceContext;
177     }
178
179     public void setNamespaceContext(NamespaceContext namespaceContext) {
180         this.namespaceContext = namespaceContext;
181     }
182
183     public FunctionContext getFunctionContext() {
184         return functionContext;
185     }
186
187     public void setFunctionContext(FunctionContext functionContext) {
188         this.functionContext = functionContext;
189     }
190
191     // Implementation methods
192
//-------------------------------------------------------------------------
193
protected XPath createXPath(String JavaDoc xpath) throws JaxenException {
194         return new DOMXPath(xpath);
195     }
196
197     protected Object JavaDoc evaluateXPath(Object JavaDoc object) throws JaxenException {
198         return xpathObject.evaluate(object);
199     }
200
201     protected boolean evaluateXPathAsBoolean(Object JavaDoc object) throws JaxenException {
202         return xpathObject.booleanValueOf(object);
203     }
204
205
206     protected Object JavaDoc getXMLNode(MessageExchange exchange, NormalizedMessage message) throws TransformerException JavaDoc, MessagingException, ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
207         Node JavaDoc node = null;
208         if (message != null) {
209             node = transformer.toDOMNode(message);
210         }
211         else {
212             log.warn("Null message for exchange: " + exchange);
213         }
214         if (node == null) {
215             // lets make an empty document to avoid Jaxen throwing a NullPointerException
216
node = transformer.createDocument();
217         }
218         return node;
219     }
220 }
221
Popular Tags