KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > filters > xml > JXPathFilter


1 /*
2  * $Id: JXPathFilter.java 3185 2006-09-23 09:55:31Z holger $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.routing.filters.xml;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.apache.commons.jxpath.AbstractFactory;
17 import org.apache.commons.jxpath.JXPathContext;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.dom4j.Document;
21 import org.dom4j.DocumentException;
22 import org.dom4j.DocumentHelper;
23 import org.dom4j.XPath;
24 import org.mule.umo.UMOFilter;
25 import org.mule.umo.UMOMessage;
26 import org.mule.util.StringMessageUtils;
27
28 /**
29  * <code>JXPathFilter</code> evaluates an XPath expression against a W3C Document,
30  * XML string, or Java bean and returns true if the result is as expected.
31  */

32 public class JXPathFilter implements UMOFilter
33 {
34
35     protected transient Log logger = LogFactory.getLog(getClass());
36
37     private String JavaDoc expression;
38     private String JavaDoc expectedValue;
39     private Map JavaDoc namespaces = null;
40     private Map JavaDoc contextProperties = null;
41     private AbstractFactory factory;
42     private boolean lenient = true;
43
44     public JXPathFilter()
45     {
46         super();
47     }
48
49     public JXPathFilter(String JavaDoc expression)
50     {
51         this.expression = expression;
52     }
53
54     public JXPathFilter(String JavaDoc expression, String JavaDoc expectedValue)
55     {
56         this.expression = expression;
57         this.expectedValue = expectedValue;
58     }
59
60     public boolean accept(UMOMessage obj)
61     {
62         return accept(obj.getPayload());
63     }
64
65     private boolean accept(Object JavaDoc obj)
66     {
67         if (obj == null)
68         {
69             logger.warn("Applying JXPathFilter to null object.");
70             return false;
71         }
72         if (expression == null)
73         {
74             logger.warn("Expression for JXPathFilter is not set.");
75             return false;
76         }
77         if (expectedValue == null)
78         {
79             // Handle the special case where the expected value really is null.
80
if (expression.endsWith("= null") || expression.endsWith("=null"))
81             {
82                 expectedValue = "null";
83                 expression = expression.substring(0, expression.lastIndexOf("="));
84             }
85             else
86             {
87                 if (logger.isInfoEnabled())
88                 {
89                     logger.info("Expected value for JXPathFilter is not set, using 'true' by default");
90                 }
91                 expectedValue = Boolean.TRUE.toString();
92             }
93         }
94
95         Object JavaDoc xpathResult = null;
96         boolean accept = false;
97
98         // Payload is a DOM Document
99
if (obj instanceof Document)
100         {
101             if (namespaces == null)
102             {
103                 // no namespace defined, let's perform a direct evaluation
104
xpathResult = ((Document)obj).valueOf(expression);
105             }
106             else
107             {
108                 // create an xpath expression with namespaces and evaluate it
109
XPath xpath = DocumentHelper.createXPath(expression);
110                 xpath.setNamespaceURIs(namespaces);
111                 xpathResult = xpath.valueOf(obj);
112             }
113
114         }
115         // Payload is a String of XML
116
else if (obj instanceof String JavaDoc)
117         {
118             try
119             {
120                 return accept(DocumentHelper.parseText((String JavaDoc)obj));
121             }
122             catch (DocumentException e)
123             {
124                 logger.warn("JXPathFilter unable to parse XML document: " + e.getMessage(), e);
125                 if (logger.isDebugEnabled())
126                     logger.debug("XML = " + StringMessageUtils.truncate((String JavaDoc)obj, 200, false));
127                 return false;
128             }
129         }
130         // Payload is a Java object
131
else
132         {
133             if (logger.isDebugEnabled())
134             {
135                 logger.debug("Passing object of type " + obj.getClass().getName() + " to JXPathContext");
136             }
137             JXPathContext context = JXPathContext.newContext(obj);
138             initialise(context);
139             xpathResult = context.getValue(expression);
140         }
141
142         if (logger.isDebugEnabled())
143         {
144             logger.debug("JXPathFilter Expression result = '" + xpathResult + "' - Expected value = '"
145                          + expectedValue + "'");
146         }
147         // Compare the XPath result with the expected result.
148
if (xpathResult != null)
149         {
150             accept = xpathResult.toString().equals(expectedValue);
151         }
152         else
153         {
154             // A null result was actually expected.
155
if (expectedValue.equals("null"))
156             {
157                 accept = true;
158             }
159             // A null result was not expected, something probably went wrong.
160
else
161             {
162                 logger.warn("JXPathFilter expression evaluates to null: " + expression);
163             }
164         }
165
166         if (logger.isDebugEnabled())
167         {
168             logger.debug("JXPathFilter accept object : " + accept);
169         }
170
171         return accept;
172     }
173
174     /**
175      * Initializes the JXPathContext based on any relevant properties set for the
176      * filter.
177      *
178      * @param the JXPathContext to initialize
179      */

180     protected void initialise(JXPathContext context)
181     {
182         Map.Entry JavaDoc entry = null;
183         if (namespaces != null)
184         {
185             if (logger.isDebugEnabled())
186             {
187                 logger.debug("Initializing JXPathContext with namespaces: " + namespaces);
188             }
189
190             for (Iterator JavaDoc iterator = namespaces.entrySet().iterator(); iterator.hasNext();)
191             {
192                 entry = (Map.Entry JavaDoc)iterator.next();
193                 context.registerNamespace(entry.getKey().toString(), entry.getValue().toString());
194             }
195         }
196
197         if (contextProperties != null)
198         {
199             if (logger.isDebugEnabled())
200             {
201                 logger.debug("Initializing JXPathContext with properties: " + contextProperties);
202             }
203
204             for (Iterator JavaDoc iterator = contextProperties.entrySet().iterator(); iterator.hasNext();)
205             {
206                 entry = (Map.Entry JavaDoc)iterator.next();
207                 context.setValue(entry.getKey().toString(), entry.getValue());
208             }
209         }
210
211         if (factory != null)
212         {
213             context.setFactory(factory);
214         }
215
216         context.setLenient(lenient);
217     }
218
219     /**
220      * @return XPath expression
221      */

222     public String JavaDoc getExpression()
223     {
224         return expression;
225     }
226
227     /**
228      * @param expression The XPath expression
229      */

230     public void setExpression(String JavaDoc expression)
231     {
232         this.expression = expression;
233     }
234
235     /**
236      * @return The expected result value of the XPath expression
237      */

238     public String JavaDoc getExpectedValue()
239     {
240         return expectedValue;
241     }
242
243     /**
244      * Sets the expected result value of the XPath expression
245      */

246     public void setExpectedValue(String JavaDoc expectedValue)
247     {
248         this.expectedValue = expectedValue;
249     }
250
251     /**
252      * @return The expected result value of the XPath expression
253      * @deprecated Use <code>getExpectedValue()</code>.
254      */

255     public String JavaDoc getValue()
256     {
257         return getExpectedValue();
258     }
259
260     /**
261      * Sets the expected result value of the XPath expression
262      *
263      * @deprecated Use <code>setExpectedValue(String expectedValue)</code>.
264      */

265     public void setValue(String JavaDoc value)
266     {
267         setExpectedValue(value);
268     }
269
270     public Map JavaDoc getNamespaces()
271     {
272         return namespaces;
273     }
274
275     public void setNamespaces(Map JavaDoc namespaces)
276     {
277         this.namespaces = namespaces;
278     }
279
280     public Map JavaDoc getContextProperties()
281     {
282         return contextProperties;
283     }
284
285     public void setContextProperties(Map JavaDoc contextProperties)
286     {
287         this.contextProperties = contextProperties;
288     }
289
290     public AbstractFactory getFactory()
291     {
292         return factory;
293     }
294
295     public void setFactory(AbstractFactory factory)
296     {
297         this.factory = factory;
298     }
299
300     public boolean isLenient()
301     {
302         return lenient;
303     }
304
305     public void setLenient(boolean lenient)
306     {
307         this.lenient = lenient;
308     }
309 }
310
Popular Tags