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