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 java.util.Map;
22  
23  import org.jaxen.JaxenException;
24  import org.jaxen.SimpleNamespaceContext;
25  import org.jaxen.XPath;
26  import org.jaxen.dom.DOMXPath;
27  import org.w3c.dom.DOMException;
28  import org.w3c.dom.Node;
29  
30  /**
31   * Jaxen-specific factory for creating <code>XPathExpression</code>s.
32   *
33   * @author Arjen Poutsma
34   * @see #createXPathExpression(String)
35   * @since 1.0.0
36   */
37  abstract class JaxenXPathExpressionFactory {
38  
39      /**
40       * Creates a Jaxen <code>XPathExpression</code> from the given string expression.
41       *
42       * @param expression the XPath expression
43       * @return the compiled <code>XPathExpression</code>
44       * @throws XPathParseException when the given expression cannot be parsed
45       */
46      static XPathExpression createXPathExpression(String expression) {
47          try {
48              XPath xpath = new DOMXPath(expression);
49              return new JaxenXpathExpression(xpath);
50          }
51          catch (JaxenException ex) {
52              throw new org.springframework.xml.xpath.XPathParseException(
53                      "Could not compile [" + expression + "] to a XPathExpression: " + ex.getMessage(), ex);
54          }
55      }
56  
57      /**
58       * Creates a Jaxen <code>XPathExpression</code> from the given string expression and prefixes.
59       *
60       * @param expression the XPath expression
61       * @param namespaces the namespaces
62       * @return the compiled <code>XPathExpression</code>
63       * @throws XPathParseException when the given expression cannot be parsed
64       */
65      public static XPathExpression createXPathExpression(String expression, Map<String, String> namespaces) {
66          try {
67              XPath xpath = new DOMXPath(expression);
68              xpath.setNamespaceContext(new SimpleNamespaceContext(namespaces));
69              return new JaxenXpathExpression(xpath);
70          }
71          catch (JaxenException ex) {
72              throw new org.springframework.xml.xpath.XPathParseException(
73                      "Could not compile [" + expression + "] to a XPathExpression: " + ex.getMessage(), ex);
74          }
75      }
76  
77      /** Jaxen implementation of the <code>XPathExpression</code> interface. */
78      private static class JaxenXpathExpression implements XPathExpression {
79  
80          private XPath xpath;
81  
82          private JaxenXpathExpression(XPath xpath) {
83              this.xpath = xpath;
84          }
85  
86          public Node evaluateAsNode(Node node) {
87              try {
88                  return (Node) xpath.selectSingleNode(node);
89              }
90              catch (JaxenException ex) {
91                  throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
92              }
93          }
94  
95          public boolean evaluateAsBoolean(Node node) {
96              try {
97                  return xpath.booleanValueOf(node);
98              }
99              catch (JaxenException ex) {
100                 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
101             }
102         }
103 
104         public double evaluateAsNumber(Node node) {
105             try {
106                 return xpath.numberValueOf(node).doubleValue();
107             }
108             catch (JaxenException ex) {
109                 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
110             }
111         }
112 
113         public String evaluateAsString(Node node) {
114             try {
115                 return xpath.stringValueOf(node);
116             }
117             catch (JaxenException ex) {
118                 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
119             }
120         }
121 
122         @SuppressWarnings("unchecked")
123         public List<Node> evaluateAsNodeList(Node node) {
124             try {
125                 return xpath.selectNodes(node);
126             }
127             catch (JaxenException ex) {
128                 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
129             }
130         }
131 
132         public <T> T evaluateAsObject(Node context, NodeMapper<T> nodeMapper) throws XPathException {
133             try {
134                 Node result = (Node) xpath.selectSingleNode(context);
135                 if (result != null) {
136                     try {
137                         return nodeMapper.mapNode(result, 0);
138                     }
139                     catch (DOMException ex) {
140                         throw new XPathException("Mapping resulted in DOMException", ex);
141                     }
142                 }
143                 else {
144                     return null;
145                 }
146             }
147             catch (JaxenException ex) {
148                 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
149             }
150         }
151 
152         public <T> List<T> evaluate(Node context, NodeMapper<T> nodeMapper) throws XPathException {
153             try {
154                 List<?> nodes = xpath.selectNodes(context);
155                 List<T> results = new ArrayList<T>(nodes.size());
156                 for (int i = 0; i < nodes.size(); i++) {
157                     Node node = (Node) nodes.get(i);
158                     try {
159                         results.add(nodeMapper.mapNode(node, i));
160                     }
161                     catch (DOMException ex) {
162                         throw new XPathException("Mapping resulted in DOMException", ex);
163                     }
164                 }
165                 return results;
166             }
167             catch (JaxenException ex) {
168                 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
169             }
170         }
171     }
172 }