001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.xml;
016    
017    import com.liferay.portal.kernel.xml.Document;
018    import com.liferay.portal.kernel.xml.Element;
019    import com.liferay.portal.kernel.xml.Node;
020    
021    import java.io.IOException;
022    import java.io.Writer;
023    
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class NodeImpl implements Node {
030    
031            public NodeImpl(org.dom4j.Node node) {
032                    _node = node;
033            }
034    
035            public String asXML() {
036                    return _node.asXML();
037            }
038    
039            public Node asXPathResult(Element parent) {
040                    ElementImpl parentImpl = (ElementImpl)parent;
041    
042                    org.dom4j.Node node = _node.asXPathResult(
043                            parentImpl.getWrappedElement());
044    
045                    if (node == null) {
046                            return null;
047                    }
048                    if (node instanceof org.dom4j.Element) {
049                            return new ElementImpl((org.dom4j.Element)node);
050                    }
051                    else {
052                            return new NodeImpl(node);
053                    }
054            }
055    
056            public Node detach() {
057                    org.dom4j.Node node = _node.detach();
058    
059                    if (node == null) {
060                            return null;
061                    }
062                    if (node instanceof org.dom4j.Element) {
063                            return new ElementImpl((org.dom4j.Element)node);
064                    }
065                    else {
066                            return new NodeImpl(node);
067                    }
068            }
069    
070            public boolean equals(Object obj) {
071                    org.dom4j.Node node = ((NodeImpl)obj).getWrappedNode();
072    
073                    return _node.equals(node);
074            }
075    
076            public Document getDocument() {
077                    org.dom4j.Document document = _node.getDocument();
078    
079                    if (document == null) {
080                            return null;
081                    }
082                    else {
083                            return new DocumentImpl(document);
084                    }
085            }
086    
087            public String getName() {
088                    return _node.getName();
089            }
090    
091            public Element getParent() {
092                    org.dom4j.Element element = _node.getParent();
093    
094                    if (element == null) {
095                            return null;
096                    }
097                    else {
098                            return new ElementImpl(element);
099                    }
100            }
101    
102            public String getPath() {
103                    return _node.getPath();
104            }
105    
106            public String getPath(Element context) {
107                    ElementImpl contextImpl = (ElementImpl)context;
108    
109                    return _node.getPath(contextImpl.getWrappedElement());
110            }
111    
112            public String getStringValue() {
113                    return _node.getStringValue();
114            }
115    
116            public String getText() {
117                    return _node.getText();
118            }
119    
120            public String getUniquePath() {
121                    return _node.getUniquePath();
122            }
123    
124            public String getUniquePath(Element context) {
125                    ElementImpl contextImpl = (ElementImpl)context;
126    
127                    return _node.getUniquePath(contextImpl.getWrappedElement());
128            }
129    
130            public org.dom4j.Node getWrappedNode() {
131                    return _node;
132            }
133    
134            public boolean hasContent() {
135                    return _node.hasContent();
136            }
137    
138            public int hashCode() {
139                    return _node.hashCode();
140            }
141    
142            public boolean isReadOnly() {
143                    return _node.isReadOnly();
144            }
145    
146            public boolean matches(String xpathExpression) {
147                    return _node.matches(xpathExpression);
148            }
149    
150            public Number numberValueOf(String xpathExpression) {
151                    return _node.numberValueOf(xpathExpression);
152            }
153    
154            public List<Node> selectNodes(String xpathExpression) {
155                    return SAXReaderImpl.toNewNodes(_node.selectNodes(xpathExpression));
156            }
157    
158            public List<Node> selectNodes(
159                    String xpathExpression, String comparisonXPathExpression) {
160    
161                    return SAXReaderImpl.toNewNodes(
162                            _node.selectNodes(xpathExpression, comparisonXPathExpression));
163            }
164    
165            public List<Node> selectNodes(
166                    String xpathExpression, String comparisonXPathExpression,
167                    boolean removeDuplicates) {
168    
169                    return SAXReaderImpl.toNewNodes(
170                            _node.selectNodes(
171                                    xpathExpression, comparisonXPathExpression, removeDuplicates));
172            }
173    
174            public Object selectObject(String xpathExpression) {
175                    Object obj = _node.selectObject(xpathExpression);
176    
177                    if (obj == null) {
178                            return null;
179                    }
180                    else if (obj instanceof List<?>) {
181                            return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)obj);
182                    }
183                    else {
184                            return obj;
185                    }
186            }
187    
188            public Node selectSingleNode(String xpathExpression) {
189                    org.dom4j.Node node = _node.selectSingleNode(xpathExpression);
190    
191                    if (node == null) {
192                            return null;
193                    }
194                    if (node instanceof org.dom4j.Element) {
195                            return new ElementImpl((org.dom4j.Element)node);
196                    }
197                    else {
198                            return new NodeImpl(node);
199                    }
200            }
201    
202            public void setName(String name) {
203                    _node.setName(name);
204            }
205    
206            public void setText(String text) {
207                    _node.setText(text);
208            }
209    
210            public boolean supportsParent() {
211                    return _node.supportsParent();
212            }
213    
214            public String toString() {
215                    return _node.toString();
216            }
217    
218            public String valueOf(String xpathExpression) {
219                    return _node.valueOf(xpathExpression);
220            }
221    
222            public void write(Writer writer) throws IOException {
223                    _node.write(writer);
224            }
225    
226            private org.dom4j.Node _node;
227    
228    }