1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class XPathImpl implements XPath {
36  
37      public XPathImpl(org.dom4j.XPath xPath) {
38          _xPath = xPath;
39      }
40  
41      public boolean booleanValueOf(Object context) {
42          return _xPath.booleanValueOf(toOldContext(context));
43      }
44  
45      public boolean equals(Object obj) {
46          org.dom4j.XPath xPath = ((XPathImpl)obj).getWrappedXPath();
47  
48          return _xPath.equals(xPath);
49      }
50  
51      public Object evaluate(Object context) {
52          return toNewContext(_xPath.evaluate(toOldContext(context)));
53      }
54  
55      public String getText() {
56          return _xPath.getText();
57      }
58  
59      public org.dom4j.XPath getWrappedXPath() {
60          return _xPath;
61      }
62  
63      public int hashCode() {
64          return _xPath.hashCode();
65      }
66  
67      public boolean matches(Node node) {
68          NodeImpl nodeImpl = (NodeImpl)node;
69  
70          return _xPath.matches(nodeImpl.getWrappedNode());
71      }
72  
73      public Number numberValueOf(Object context) {
74          return _xPath.numberValueOf(toOldContext(context));
75      }
76  
77      public List<Node> selectNodes(Object context) {
78          return SAXReaderImpl.toNewNodes(
79              _xPath.selectNodes(toOldContext(context)));
80      }
81  
82      public List<Node> selectNodes(Object context, XPath sortXPath) {
83          XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
84  
85          return SAXReaderImpl.toNewNodes(
86              _xPath.selectNodes(
87                  toOldContext(context), sortXPathImpl.getWrappedXPath()));
88      }
89  
90      public List<Node> selectNodes(
91          Object context, XPath sortXPath, boolean distinct) {
92  
93          XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
94  
95          return SAXReaderImpl.toNewNodes(
96              _xPath.selectNodes(
97                  toOldContext(context), sortXPathImpl.getWrappedXPath(),
98                  distinct));
99      }
100 
101     public Node selectSingleNode(Object context) {
102         org.dom4j.Node node = _xPath.selectSingleNode(toOldContext(context));
103 
104         if (node == null) {
105             return null;
106         }
107         else {
108             return new NodeImpl(node);
109         }
110     }
111 
112     public void sort(List<Node> nodes) {
113         _xPath.sort(SAXReaderImpl.toOldNodes(nodes));
114     }
115 
116     public void sort(List<Node> nodes, boolean distinct) {
117         _xPath.sort(SAXReaderImpl.toOldNodes(nodes), distinct);
118     }
119 
120     public String valueOf(Object context) {
121         return _xPath.valueOf(toOldContext(context));
122     }
123 
124     protected Object toNewContext(Object context) {
125         if (context == null) {
126             return null;
127         }
128         else if (context instanceof org.dom4j.Document) {
129             org.dom4j.Document document = (org.dom4j.Document)context;
130 
131             return new DocumentImpl(document);
132         }
133         else if (context instanceof org.dom4j.Node) {
134             org.dom4j.Node node = (org.dom4j.Node)context;
135 
136             return new NodeImpl(node);
137         }
138         else if (context instanceof List<?>) {
139             return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)context);
140         }
141         else {
142             return context;
143         }
144     }
145 
146     protected Object toOldContext(Object context) {
147         if (context == null) {
148             return null;
149         }
150         else if (context instanceof DocumentImpl) {
151             DocumentImpl documentImpl = (DocumentImpl)context;
152 
153             return documentImpl.getWrappedDocument();
154         }
155         else if (context instanceof NodeImpl) {
156             NodeImpl nodeImpl = (NodeImpl)context;
157 
158             return nodeImpl.getWrappedNode();
159         }
160         else if (context instanceof List<?>) {
161             return SAXReaderImpl.toOldNodes((List<Node>)context);
162         }
163         else {
164             return context;
165         }
166     }
167 
168     private org.dom4j.XPath _xPath;
169 
170 }