1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 valueOf(Object context) {
113         return _xPath.valueOf(toOldContext(context));
114     }
115 
116     protected Object toNewContext(Object context) {
117         if (context == null) {
118             return null;
119         }
120         else if (context instanceof org.dom4j.Document) {
121             org.dom4j.Document document = (org.dom4j.Document)context;
122 
123             return new DocumentImpl(document);
124         }
125         else if (context instanceof org.dom4j.Node) {
126             org.dom4j.Node node = (org.dom4j.Node)context;
127 
128             return new NodeImpl(node);
129         }
130         else if (context instanceof List<?>) {
131             return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)context);
132         }
133         else {
134             return context;
135         }
136     }
137 
138     protected Object toOldContext(Object context) {
139         if (context == null) {
140             return null;
141         }
142         else if (context instanceof DocumentImpl) {
143             DocumentImpl documentImpl = (DocumentImpl)context;
144 
145             return documentImpl.getWrappedDocument();
146         }
147         else if (context instanceof NodeImpl) {
148             NodeImpl nodeImpl = (NodeImpl)context;
149 
150             return nodeImpl.getWrappedNode();
151         }
152         else if (context instanceof List<?>) {
153             return SAXReaderImpl.toOldNodes((List<Node>)context);
154         }
155         else {
156             return context;
157         }
158     }
159 
160     private org.dom4j.XPath _xPath;
161 
162 }