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.Map;
20  import javax.xml.transform.Source;
21  import javax.xml.transform.TransformerException;
22  import javax.xml.transform.dom.DOMResult;
23  
24  import org.springframework.xml.transform.TransformerObjectSupport;
25  
26  import org.w3c.dom.DOMException;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  import org.w3c.dom.Node;
30  
31  /**
32   * Abstract base class for implementations of {@link XPathOperations}. Contains a namespaces property.
33   *
34   * @author Arjen Poutsma
35   * @since 1.0.0
36   */
37  public abstract class AbstractXPathTemplate extends TransformerObjectSupport implements XPathOperations {
38  
39      private Map<String, String> namespaces;
40  
41      /** Returns namespaces used in the XPath expression. */
42      public Map<String, String> getNamespaces() {
43          return namespaces;
44      }
45  
46      /** Sets namespaces used in the XPath expression. Maps prefixes to namespaces. */
47      public void setNamespaces(Map<String, String> namespaces) {
48          this.namespaces = namespaces;
49      }
50  
51      public final void evaluate(String expression, Source context, NodeCallbackHandler callbackHandler)
52              throws XPathException {
53          evaluate(expression, context, new NodeCallbackHandlerNodeMapper(callbackHandler));
54      }
55  
56      /** Static inner class that adapts a {@link NodeCallbackHandler} to the interface of {@link NodeMapper}. */
57      private static class NodeCallbackHandlerNodeMapper implements NodeMapper<Object> {
58  
59          private final NodeCallbackHandler callbackHandler;
60  
61          public NodeCallbackHandlerNodeMapper(NodeCallbackHandler callbackHandler) {
62              this.callbackHandler = callbackHandler;
63          }
64  
65          public Object mapNode(Node node, int nodeNum) throws DOMException {
66              callbackHandler.processNode(node);
67              return null;
68          }
69      }
70  
71      /**
72       * Returns the root element of the given source.
73       *
74       * @param source the source to get the root element from
75       * @return the root element
76       */
77      protected Element getRootElement(Source source) throws TransformerException {
78          DOMResult domResult = new DOMResult();
79          transform(source, domResult);
80          Document document = (Document) domResult.getNode();
81          return document.getDocumentElement();
82      }
83  
84  }