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