View Javadoc

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