1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.xml;
16  
17  import com.liferay.portal.kernel.xml.Node;
18  import com.liferay.portal.kernel.xml.XPath;
19  
20  import java.util.List;
21  
22  /**
23   * <a href="XPathImpl.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Brian Wing Shun Chan
26   */
27  public class XPathImpl implements XPath {
28  
29      public XPathImpl(org.dom4j.XPath xPath) {
30          _xPath = xPath;
31      }
32  
33      public boolean booleanValueOf(Object context) {
34          return _xPath.booleanValueOf(toOldContext(context));
35      }
36  
37      public boolean equals(Object obj) {
38          org.dom4j.XPath xPath = ((XPathImpl)obj).getWrappedXPath();
39  
40          return _xPath.equals(xPath);
41      }
42  
43      public Object evaluate(Object context) {
44          return toNewContext(_xPath.evaluate(toOldContext(context)));
45      }
46  
47      public String getText() {
48          return _xPath.getText();
49      }
50  
51      public org.dom4j.XPath getWrappedXPath() {
52          return _xPath;
53      }
54  
55      public int hashCode() {
56          return _xPath.hashCode();
57      }
58  
59      public boolean matches(Node node) {
60          NodeImpl nodeImpl = (NodeImpl)node;
61  
62          return _xPath.matches(nodeImpl.getWrappedNode());
63      }
64  
65      public Number numberValueOf(Object context) {
66          return _xPath.numberValueOf(toOldContext(context));
67      }
68  
69      public List<Node> selectNodes(Object context) {
70          return SAXReaderImpl.toNewNodes(
71              _xPath.selectNodes(toOldContext(context)));
72      }
73  
74      public List<Node> selectNodes(Object context, XPath sortXPath) {
75          XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
76  
77          return SAXReaderImpl.toNewNodes(
78              _xPath.selectNodes(
79                  toOldContext(context), sortXPathImpl.getWrappedXPath()));
80      }
81  
82      public List<Node> selectNodes(
83          Object context, XPath sortXPath, boolean distinct) {
84  
85          XPathImpl sortXPathImpl = (XPathImpl)sortXPath;
86  
87          return SAXReaderImpl.toNewNodes(
88              _xPath.selectNodes(
89                  toOldContext(context), sortXPathImpl.getWrappedXPath(),
90                  distinct));
91      }
92  
93      public Node selectSingleNode(Object context) {
94          org.dom4j.Node node = _xPath.selectSingleNode(toOldContext(context));
95  
96          if (node == null) {
97              return null;
98          }
99          else {
100             return new NodeImpl(node);
101         }
102     }
103 
104     public void sort(List<Node> nodes) {
105         _xPath.sort(SAXReaderImpl.toOldNodes(nodes));
106     }
107 
108     public void sort(List<Node> nodes, boolean distinct) {
109         _xPath.sort(SAXReaderImpl.toOldNodes(nodes), distinct);
110     }
111 
112     public String toString() {
113         return _xPath.toString();
114     }
115 
116     public String valueOf(Object context) {
117         return _xPath.valueOf(toOldContext(context));
118     }
119 
120     protected Object toNewContext(Object context) {
121         if (context == null) {
122             return null;
123         }
124         else if (context instanceof org.dom4j.Document) {
125             org.dom4j.Document document = (org.dom4j.Document)context;
126 
127             return new DocumentImpl(document);
128         }
129         else if (context instanceof org.dom4j.Node) {
130             org.dom4j.Node node = (org.dom4j.Node)context;
131 
132             return new NodeImpl(node);
133         }
134         else if (context instanceof List<?>) {
135             return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)context);
136         }
137         else {
138             return context;
139         }
140     }
141 
142     protected Object toOldContext(Object context) {
143         if (context == null) {
144             return null;
145         }
146         else if (context instanceof DocumentImpl) {
147             DocumentImpl documentImpl = (DocumentImpl)context;
148 
149             return documentImpl.getWrappedDocument();
150         }
151         else if (context instanceof NodeImpl) {
152             NodeImpl nodeImpl = (NodeImpl)context;
153 
154             return nodeImpl.getWrappedNode();
155         }
156         else if (context instanceof List<?>) {
157             return SAXReaderImpl.toOldNodes((List<Node>)context);
158         }
159         else {
160             return context;
161         }
162     }
163 
164     private org.dom4j.XPath _xPath;
165 
166 }