View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.xml.xpath;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import javax.xml.transform.Source;
22  import javax.xml.transform.TransformerException;
23  
24  import org.jaxen.JaxenException;
25  import org.jaxen.SimpleNamespaceContext;
26  import org.jaxen.XPath;
27  import org.jaxen.dom.DOMXPath;
28  import org.w3c.dom.DOMException;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.Node;
31  
32  /**
33   * Implementation of [email protected] XPathOperations} that uses Jaxen.
34   * <p/>
35   * Namespaces can be set using the <code>namespaces</code> property.
36   *
37   * @author Arjen Poutsma
38   * @see <a href="http://www.jaxen.org/">Jaxen</a>
39   * @since 1.0.0
40   */
41  public class JaxenXPathTemplate extends AbstractXPathTemplate {
42  
43      public boolean evaluateAsBoolean(String expression, Source context) throws XPathException {
44          try {
45              XPath xpath = createXPath(expression);
46              Element element = getRootElement(context);
47              return xpath.booleanValueOf(element);
48          }
49          catch (JaxenException ex) {
50              throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
51          }
52          catch (TransformerException ex) {
53              throw new XPathException("Could not transform context to DOM Node", ex);
54          }
55      }
56  
57      public Node evaluateAsNode(String expression, Source context) throws XPathException {
58          try {
59              XPath xpath = createXPath(expression);
60              Element element = getRootElement(context);
61              return (Node) xpath.selectSingleNode(element);
62          }
63          catch (JaxenException ex) {
64              throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
65          }
66          catch (TransformerException ex) {
67              throw new XPathException("Could not transform context to DOM Node", ex);
68          }
69      }
70  
71      @SuppressWarnings("unchecked")
72      public List<Node> evaluateAsNodeList(String expression, Source context) throws XPathException {
73          try {
74              XPath xpath = createXPath(expression);
75              Element element = getRootElement(context);
76              return xpath.selectNodes(element);
77          }
78          catch (JaxenException ex) {
79              throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
80          }
81          catch (TransformerException ex) {
82              throw new XPathException("Could not transform context to DOM Node", ex);
83          }
84      }
85  
86      public double evaluateAsDouble(String expression, Source context) throws XPathException {
87          try {
88              XPath xpath = createXPath(expression);
89              Element element = getRootElement(context);
90              return xpath.numberValueOf(element).doubleValue();
91          }
92          catch (JaxenException ex) {
93              throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
94          }
95          catch (TransformerException ex) {
96              throw new XPathException("Could not transform context to DOM Node", ex);
97          }
98      }
99  
100     public String evaluateAsString(String expression, Source context) throws XPathException {
101         try {
102             XPath xpath = createXPath(expression);
103             Element element = getRootElement(context);
104             return xpath.stringValueOf(element);
105         }
106         catch (JaxenException ex) {
107             throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
108         }
109         catch (TransformerException ex) {
110             throw new XPathException("Could not transform context to DOM Node", ex);
111         }
112     }
113 
114     public <T> T evaluateAsObject(String expression, Source context, NodeMapper<T> nodeMapper) throws XPathException {
115         try {
116             XPath xpath = createXPath(expression);
117             Element element = getRootElement(context);
118             Node node = (Node) xpath.selectSingleNode(element);
119             if (node != null) {
120                 try {
121                     return nodeMapper.mapNode(node, 0);
122                 }
123                 catch (DOMException ex) {
124                     throw new XPathException("Mapping resulted in DOMException", ex);
125                 }
126             }
127             else {
128                 return null;
129             }
130 
131         }
132         catch (JaxenException ex) {
133             throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
134         }
135         catch (TransformerException ex) {
136             throw new XPathException("Could not transform context to DOM Node", ex);
137         }
138     }
139 
140     public <T> List<T> evaluate(String expression, Source context, NodeMapper<T> nodeMapper) throws XPathException {
141         try {
142             XPath xpath = createXPath(expression);
143             Element element = getRootElement(context);
144             List<?> nodes = xpath.selectNodes(element);
145             List<T> results = new ArrayList<T>(nodes.size());
146             for (int i = 0; i < nodes.size(); i++) {
147                 Node node = (Node) nodes.get(i);
148                 try {
149                     results.add(nodeMapper.mapNode(node, i));
150                 }
151                 catch (DOMException ex) {
152                     throw new XPathException("Mapping resulted in DOMException", ex);
153                 }
154             }
155             return results;
156         }
157         catch (JaxenException ex) {
158             throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
159         }
160         catch (TransformerException ex) {
161             throw new XPathException("Could not transform context to DOM Node", ex);
162         }
163     }
164 
165     private XPath createXPath(String expression) throws JaxenException {
166         XPath xpath = new DOMXPath(expression);
167         if (getNamespaces() != null && !getNamespaces().isEmpty()) {
168             xpath.setNamespaceContext(new SimpleNamespaceContext(getNamespaces()));
169         }
170         return xpath;
171     }
172 }