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.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.Node;
28  
29  import java.io.IOException;
30  import java.io.Writer;
31  
32  import java.util.List;
33  
34  /**
35   * <a href="NodeImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class NodeImpl implements Node {
40  
41      public NodeImpl(org.dom4j.Node node) {
42          _node = node;
43      }
44  
45      public String asXML() {
46          return _node.asXML();
47      }
48  
49      public Node asXPathResult(Element parent) {
50          ElementImpl parentImpl = (ElementImpl)parent;
51  
52          org.dom4j.Node node = _node.asXPathResult(
53              parentImpl.getWrappedElement());
54  
55          if (node == null) {
56              return null;
57          }
58          if (node instanceof org.dom4j.Element) {
59              return new ElementImpl((org.dom4j.Element)node);
60          }
61          else {
62              return new NodeImpl(node);
63          }
64      }
65  
66      public Node detach() {
67          org.dom4j.Node node = _node.detach();
68  
69          if (node == null) {
70              return null;
71          }
72          if (node instanceof org.dom4j.Element) {
73              return new ElementImpl((org.dom4j.Element)node);
74          }
75          else {
76              return new NodeImpl(node);
77          }
78      }
79  
80      public boolean equals(Object obj) {
81          org.dom4j.Node node = ((NodeImpl)obj).getWrappedNode();
82  
83          return _node.equals(node);
84      }
85  
86      public Document getDocument() {
87          org.dom4j.Document document = _node.getDocument();
88  
89          if (document == null) {
90              return null;
91          }
92          else {
93              return new DocumentImpl(document);
94          }
95      }
96  
97      public String getName() {
98          return _node.getName();
99      }
100 
101     public Element getParent() {
102         org.dom4j.Element element = _node.getParent();
103 
104         if (element == null) {
105             return null;
106         }
107         else {
108             return new ElementImpl(element);
109         }
110     }
111 
112     public String getPath() {
113         return _node.getPath();
114     }
115 
116     public String getPath(Element context) {
117         ElementImpl contextImpl = (ElementImpl)context;
118 
119         return _node.getPath(contextImpl.getWrappedElement());
120     }
121 
122     public String getStringValue() {
123         return _node.getStringValue();
124     }
125 
126     public String getText() {
127         return _node.getText();
128     }
129 
130     public String getUniquePath() {
131         return _node.getUniquePath();
132     }
133 
134     public String getUniquePath(Element context) {
135         ElementImpl contextImpl = (ElementImpl)context;
136 
137         return _node.getUniquePath(contextImpl.getWrappedElement());
138     }
139 
140     public org.dom4j.Node getWrappedNode() {
141         return _node;
142     }
143 
144     public boolean hasContent() {
145         return _node.hasContent();
146     }
147 
148     public int hashCode() {
149         return _node.hashCode();
150     }
151 
152     public boolean isReadOnly() {
153         return _node.isReadOnly();
154     }
155 
156     public boolean matches(String xpathExpression) {
157         return _node.matches(xpathExpression);
158     }
159 
160     public Number numberValueOf(String xpathExpression) {
161         return _node.numberValueOf(xpathExpression);
162     }
163 
164     public List<Node> selectNodes(String xpathExpression) {
165         return SAXReaderImpl.toNewNodes(_node.selectNodes(xpathExpression));
166     }
167 
168     public List<Node> selectNodes(
169         String xpathExpression, String comparisonXPathExpression) {
170 
171         return SAXReaderImpl.toNewNodes(
172             _node.selectNodes(xpathExpression, comparisonXPathExpression));
173     }
174 
175     public List<Node> selectNodes(
176         String xpathExpression, String comparisonXPathExpression,
177         boolean removeDuplicates) {
178 
179         return SAXReaderImpl.toNewNodes(
180             _node.selectNodes(
181                 xpathExpression, comparisonXPathExpression, removeDuplicates));
182     }
183 
184     public Object selectObject(String xpathExpression) {
185         Object obj = _node.selectObject(xpathExpression);
186 
187         if (obj == null) {
188             return null;
189         }
190         else if (obj instanceof List<?>) {
191             return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)obj);
192         }
193         else {
194             return obj;
195         }
196     }
197 
198     public Node selectSingleNode(String xpathExpression) {
199         org.dom4j.Node node = _node.selectSingleNode(xpathExpression);
200 
201         if (node == null) {
202             return null;
203         }
204         if (node instanceof org.dom4j.Element) {
205             return new ElementImpl((org.dom4j.Element)node);
206         }
207         else {
208             return new NodeImpl(node);
209         }
210     }
211 
212     public void setName(String name) {
213         _node.setName(name);
214     }
215 
216     public void setText(String text) {
217         _node.setText(text);
218     }
219 
220     public boolean supportsParent() {
221         return _node.supportsParent();
222     }
223 
224     public String valueOf(String xpathExpression) {
225         return _node.valueOf(xpathExpression);
226     }
227 
228     public void write(Writer writer) throws IOException {
229         _node.write(writer);
230     }
231 
232     private org.dom4j.Node _node;
233 
234 }