001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.xpath;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021: import javax.xml.transform.Source;
022: import javax.xml.transform.TransformerException;
023:
024: import org.jaxen.JaxenException;
025: import org.jaxen.SimpleNamespaceContext;
026: import org.jaxen.XPath;
027: import org.jaxen.dom.DOMXPath;
028: import org.w3c.dom.DOMException;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031:
032: /**
033: * Implementation of {@link XPathOperations} that uses Jaxen.
034: * <p/>
035: * Namespaces can be set using the <code>namespaces</code> property.
036: *
037: * @author Arjen Poutsma
038: * @see <a href="http://www.jaxen.org/">Jaxen</a>
039: * @since 1.0.0
040: */
041: public class JaxenXPathTemplate extends AbstractXPathTemplate {
042:
043: public boolean evaluateAsBoolean(String expression, Source context)
044: throws XPathException {
045: try {
046: XPath xpath = createXPath(expression);
047: Element element = getRootElement(context);
048: return xpath.booleanValueOf(element);
049: } catch (JaxenException ex) {
050: throw new XPathException(
051: "Could not evaluate XPath expression ["
052: + expression + "]", ex);
053: } catch (TransformerException ex) {
054: throw new XPathException(
055: "Could not transform context to DOM Node", ex);
056: }
057: }
058:
059: public Node evaluateAsNode(String expression, Source context)
060: throws XPathException {
061: try {
062: XPath xpath = createXPath(expression);
063: Element element = getRootElement(context);
064: return (Node) xpath.selectSingleNode(element);
065: } catch (JaxenException ex) {
066: throw new XPathException(
067: "Could not evaluate XPath expression ["
068: + expression + "]", ex);
069: } catch (TransformerException ex) {
070: throw new XPathException(
071: "Could not transform context to DOM Node", ex);
072: }
073: }
074:
075: public List evaluateAsNodeList(String expression, Source context)
076: throws XPathException {
077: try {
078: XPath xpath = createXPath(expression);
079: Element element = getRootElement(context);
080: return xpath.selectNodes(element);
081: } catch (JaxenException ex) {
082: throw new XPathException(
083: "Could not evaluate XPath expression ["
084: + expression + "]", ex);
085: } catch (TransformerException ex) {
086: throw new XPathException(
087: "Could not transform context to DOM Node", ex);
088: }
089: }
090:
091: public double evaluateAsDouble(String expression, Source context)
092: throws XPathException {
093: try {
094: XPath xpath = createXPath(expression);
095: Element element = getRootElement(context);
096: return xpath.numberValueOf(element).doubleValue();
097: } catch (JaxenException ex) {
098: throw new XPathException(
099: "Could not evaluate XPath expression ["
100: + expression + "]", ex);
101: } catch (TransformerException ex) {
102: throw new XPathException(
103: "Could not transform context to DOM Node", ex);
104: }
105: }
106:
107: public String evaluateAsString(String expression, Source context)
108: throws XPathException {
109: try {
110: XPath xpath = createXPath(expression);
111: Element element = getRootElement(context);
112: return xpath.stringValueOf(element);
113: } catch (JaxenException ex) {
114: throw new XPathException(
115: "Could not evaluate XPath expression ["
116: + expression + "]", ex);
117: } catch (TransformerException ex) {
118: throw new XPathException(
119: "Could not transform context to DOM Node", ex);
120: }
121: }
122:
123: public Object evaluateAsObject(String expression, Source context,
124: NodeMapper nodeMapper) throws XPathException {
125: try {
126: XPath xpath = createXPath(expression);
127: Element element = getRootElement(context);
128: Node node = (Node) xpath.selectSingleNode(element);
129: if (node != null) {
130: try {
131: return nodeMapper.mapNode(node, 0);
132: } catch (DOMException ex) {
133: throw new XPathException(
134: "Mapping resulted in DOMException", ex);
135: }
136: } else {
137: return null;
138: }
139:
140: } catch (JaxenException ex) {
141: throw new XPathException(
142: "Could not evaluate XPath expression ["
143: + expression + "]", ex);
144: } catch (TransformerException ex) {
145: throw new XPathException(
146: "Could not transform context to DOM Node", ex);
147: }
148: }
149:
150: public List evaluate(String expression, Source context,
151: NodeMapper nodeMapper) throws XPathException {
152: try {
153: XPath xpath = createXPath(expression);
154: Element element = getRootElement(context);
155: List nodes = (List) xpath.selectNodes(element);
156: List results = new ArrayList(nodes.size());
157: for (int i = 0; i < nodes.size(); i++) {
158: Node node = (Node) nodes.get(i);
159: try {
160: results.add(nodeMapper.mapNode(node, i));
161: } catch (DOMException ex) {
162: throw new XPathException(
163: "Mapping resulted in DOMException", ex);
164: }
165: }
166: return results;
167: } catch (JaxenException ex) {
168: throw new XPathException(
169: "Could not evaluate XPath expression ["
170: + expression + "]", ex);
171: } catch (TransformerException ex) {
172: throw new XPathException(
173: "Could not transform context to DOM Node", ex);
174: }
175: }
176:
177: private XPath createXPath(String expression) throws JaxenException {
178: XPath xpath = new DOMXPath(expression);
179: if (getNamespaces() != null && !getNamespaces().isEmpty()) {
180: xpath.setNamespaceContext(new SimpleNamespaceContext(
181: getNamespaces()));
182: }
183: return xpath;
184: }
185: }
|