View Javadoc

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