1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.xml;
24  
25  import com.liferay.portal.kernel.xml.Node;
26  import com.liferay.portal.kernel.xml.XPath;
27  
28  import java.util.List;
29  
30  /**
31   * <a href="XPathImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   *
35   */
36  public class XPathImpl implements XPath {
37  
38      public XPathImpl(org.dom4j.XPath xPath) {
39          _xPath = xPath;
40      }
41  
42      public boolean booleanValueOf(Object context) {
43          return _xPath.booleanValueOf(toOldContext(context));
44      }
45  
46      public boolean equals(Object obj) {
47          org.dom4j.XPath xPath = ((XPathImpl)obj).getWrappedXPath();
48  
49          return _xPath.equals(xPath);
50      }
51  
52      public Object evaluate(Object context) {
53          return toNewContext(_xPath.evaluate(toOldContext(context)));
54      }
55  
56      public String getText() {
57          return _xPath.getText();
58      }
59  
60      public org.dom4j.XPath getWrappedXPath() {
61          return _xPath;
62      }
63  
64      public int hashCode() {
65          return _xPath.hashCode();
66      }
67  
68      public boolean matches(Node node) {
69          NodeImpl nodeImpl = (NodeImpl)node;
70  
71          return _xPath.matches(nodeImpl.getWrappedNode());
72      }
73  
74      public Number numberValueOf(Object context) {
75          return _xPath.numberValueOf(toOldContext(context));
76      }
77  
78      public List<Node> selectNodes(Object context) {
79          return SAXReaderImpl.toNewNodes(
80              _xPath.selectNodes(toOldContext(context)));
81      }
82  
83      public List<Node> selectNodes(Object context, XPath sortXPath) {
84          XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
85  
86          return SAXReaderImpl.toNewNodes(
87              _xPath.selectNodes(
88                  toOldContext(context), sortXPathImpl.getWrappedXPath()));
89      }
90  
91      public List<Node> selectNodes(
92          Object context, XPath sortXPath, boolean distinct) {
93  
94          XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
95  
96          return SAXReaderImpl.toNewNodes(
97              _xPath.selectNodes(
98                  toOldContext(context), sortXPathImpl.getWrappedXPath(),
99                  distinct));
100     }
101 
102     public Node selectSingleNode(Object context) {
103         org.dom4j.Node node = _xPath.selectSingleNode(toOldContext(context));
104 
105         if (node == null) {
106             return null;
107         }
108         else {
109             return new NodeImpl(node);
110         }
111     }
112 
113     public void sort(List<Node> nodes) {
114         _xPath.sort(SAXReaderImpl.toOldNodes(nodes));
115     }
116 
117     public void sort(List<Node> nodes, boolean distinct) {
118         _xPath.sort(SAXReaderImpl.toOldNodes(nodes), distinct);
119     }
120 
121     public String valueOf(Object context) {
122         return _xPath.valueOf(toOldContext(context));
123     }
124 
125     protected Object toNewContext(Object context) {
126         if (context == null) {
127             return null;
128         }
129         else if (context instanceof org.dom4j.Document) {
130             org.dom4j.Document document = (org.dom4j.Document)context;
131 
132             return new DocumentImpl(document);
133         }
134         else if (context instanceof org.dom4j.Node) {
135             org.dom4j.Node node = (org.dom4j.Node)context;
136 
137             return new NodeImpl(node);
138         }
139         else if (context instanceof List) {
140             return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)context);
141         }
142         else {
143             return context;
144         }
145     }
146 
147     protected Object toOldContext(Object context) {
148         if (context == null) {
149             return null;
150         }
151         else if (context instanceof DocumentImpl) {
152             DocumentImpl documentImpl = (DocumentImpl)context;
153 
154             return documentImpl.getWrappedDocument();
155         }
156         else if (context instanceof NodeImpl) {
157             NodeImpl nodeImpl = (NodeImpl)context;
158 
159             return nodeImpl.getWrappedNode();
160         }
161         else if (context instanceof List) {
162             return SAXReaderImpl.toOldNodes((List<Node>)context);
163         }
164         else {
165             return context;
166         }
167     }
168 
169     private org.dom4j.XPath _xPath;
170 
171 }