KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > xml > JXPathExtractor


1 /*
2  * $Id: JXPathExtractor.java 3937 2006-11-20 16:04:25Z lajos $
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.transformers.xml;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.apache.commons.jxpath.JXPathContext;
17 import org.dom4j.Document;
18 import org.dom4j.DocumentHelper;
19 import org.dom4j.Node;
20 import org.dom4j.XPath;
21
22 import org.mule.transformers.AbstractTransformer;
23 import org.mule.umo.transformer.TransformerException;
24
25 /**
26  * The JXPathExtractor is a simple transformer that evaluates an xpath expression
27  * against the given bean and that returns the result. <p/> By default, a single
28  * result will be returned. If multiple values are expected, set the
29  * {@link #singleResult} property to <code>false</code>. In this case a
30  * {@link List} of values will be returned. Note the property is currently ignored
31  * for non-String/XML payloads.
32  */

33 public class JXPathExtractor extends AbstractTransformer
34 {
35     /**
36      * Serial version
37      */

38     private static final long serialVersionUID = 8252278715641253900L;
39
40     private String JavaDoc expression;
41
42     private boolean singleResult = true;
43
44     /**
45      * Evaluate the expression in the context of the given object and returns the
46      * result. If the given object is a string, it assumes it is an valid xml and
47      * parses it before evaluating the xpath expression.
48      */

49     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
50     {
51         try
52         {
53             Object JavaDoc result;
54             if (src instanceof String JavaDoc)
55             {
56                 Document doc = DocumentHelper.parseText((String JavaDoc)src);
57                 if (singleResult)
58                 {
59                     result = doc.valueOf(expression);
60                 }
61                 else
62                 {
63                     XPath xpath = doc.createXPath(expression);
64                     // TODO handle non-list cases, see
65
// http://www.dom4j.org/apidocs/org/dom4j/XPath.html#evaluate(java.lang.Object)
66
List JavaDoc obj = (List JavaDoc)xpath.evaluate(doc);
67                     result = new ArrayList JavaDoc(obj.size());
68                     for (int i = 0; i < obj.size(); i++)
69                     {
70                         final Node node = (Node)obj.get(i);
71                         ((List JavaDoc)result).add(node.getText());
72                     }
73                 }
74             }
75             else
76             {
77                 JXPathContext context = JXPathContext.newContext(src);
78                 result = context.getValue(expression);
79             }
80             return result;
81         }
82         catch (Exception JavaDoc e)
83         {
84             throw new TransformerException(this, e);
85         }
86     }
87
88     /**
89      * @return Returns the expression.
90      */

91     public String JavaDoc getExpression()
92     {
93         return expression;
94     }
95
96     /**
97      * @param expression The expression to set.
98      */

99     public void setExpression(String JavaDoc expression)
100     {
101         this.expression = expression;
102     }
103
104     /**
105      * Should a single value be returned.
106      *
107      * @return value
108      */

109     public boolean isSingleResult()
110     {
111         return singleResult;
112     }
113
114     /**
115      * If multiple results are expected from the {@link #expression} evaluation, set
116      * this to false.
117      *
118      * @param singleResult flag
119      */

120     public void setSingleResult(boolean singleResult)
121     {
122         this.singleResult = singleResult;
123     }
124 }
125
Popular Tags