001    /**
002     * Copyright (c) 2000-2012 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    import com.liferay.portal.kernel.xml.Visitor;
021    import com.liferay.util.xml.XMLFormatter;
022    
023    import java.io.IOException;
024    import java.io.Writer;
025    
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class NodeImpl implements Node {
032    
033            public NodeImpl(org.dom4j.Node node) {
034                    _node = node;
035            }
036    
037            public <T, V extends Visitor<T>> T accept(V visitor) {
038                    return visitor.visitNode(this);
039            }
040    
041            public String asXML() {
042                    return _node.asXML();
043            }
044    
045            public Node asXPathResult(Element parent) {
046                    ElementImpl parentImpl = (ElementImpl)parent;
047    
048                    org.dom4j.Node node = _node.asXPathResult(
049                            parentImpl.getWrappedElement());
050    
051                    if (node == null) {
052                            return null;
053                    }
054                    if (node instanceof org.dom4j.Element) {
055                            return new ElementImpl((org.dom4j.Element)node);
056                    }
057                    else {
058                            return new NodeImpl(node);
059                    }
060            }
061    
062            public Node detach() {
063                    org.dom4j.Node node = _node.detach();
064    
065                    if (node == null) {
066                            return null;
067                    }
068                    if (node instanceof org.dom4j.Element) {
069                            return new ElementImpl((org.dom4j.Element)node);
070                    }
071                    else {
072                            return new NodeImpl(node);
073                    }
074            }
075    
076            @Override
077            public boolean equals(Object obj) {
078                    org.dom4j.Node node = ((NodeImpl)obj).getWrappedNode();
079    
080                    return _node.equals(node);
081            }
082    
083            public String formattedString() throws IOException {
084                    return XMLFormatter.toString(_node);
085            }
086    
087            public String formattedString(String indent) throws IOException {
088                    return XMLFormatter.toString(_node, indent);
089            }
090    
091            public String formattedString(String indent, boolean expandEmptyElements)
092                    throws IOException {
093    
094                    return XMLFormatter.toString(_node, indent, expandEmptyElements);
095            }
096    
097            public Document getDocument() {
098                    org.dom4j.Document document = _node.getDocument();
099    
100                    if (document == null) {
101                            return null;
102                    }
103                    else {
104                            return new DocumentImpl(document);
105                    }
106            }
107    
108            public String getName() {
109                    return _node.getName();
110            }
111    
112            public Element getParent() {
113                    org.dom4j.Element element = _node.getParent();
114    
115                    if (element == null) {
116                            return null;
117                    }
118                    else {
119                            return new ElementImpl(element);
120                    }
121            }
122    
123            public String getPath() {
124                    return _node.getPath();
125            }
126    
127            public String getPath(Element context) {
128                    ElementImpl contextImpl = (ElementImpl)context;
129    
130                    return _node.getPath(contextImpl.getWrappedElement());
131            }
132    
133            public String getStringValue() {
134                    return _node.getStringValue();
135            }
136    
137            public String getText() {
138                    return _node.getText();
139            }
140    
141            public String getUniquePath() {
142                    return _node.getUniquePath();
143            }
144    
145            public String getUniquePath(Element context) {
146                    ElementImpl contextImpl = (ElementImpl)context;
147    
148                    return _node.getUniquePath(contextImpl.getWrappedElement());
149            }
150    
151            public org.dom4j.Node getWrappedNode() {
152                    return _node;
153            }
154    
155            public boolean hasContent() {
156                    return _node.hasContent();
157            }
158    
159            @Override
160            public int hashCode() {
161                    return _node.hashCode();
162            }
163    
164            public boolean isReadOnly() {
165                    return _node.isReadOnly();
166            }
167    
168            public boolean matches(String xPathExpression) {
169                    return _node.matches(xPathExpression);
170            }
171    
172            public Number numberValueOf(String xPathExpression) {
173                    return _node.numberValueOf(xPathExpression);
174            }
175    
176            public List<Node> selectNodes(String xPathExpression) {
177                    return SAXReaderImpl.toNewNodes(_node.selectNodes(xPathExpression));
178            }
179    
180            public List<Node> selectNodes(
181                    String xPathExpression, String comparisonXPathExpression) {
182    
183                    return SAXReaderImpl.toNewNodes(
184                            _node.selectNodes(xPathExpression, comparisonXPathExpression));
185            }
186    
187            public List<Node> selectNodes(
188                    String xPathExpression, String comparisonXPathExpression,
189                    boolean removeDuplicates) {
190    
191                    return SAXReaderImpl.toNewNodes(
192                            _node.selectNodes(
193                                    xPathExpression, comparisonXPathExpression, removeDuplicates));
194            }
195    
196            public Object selectObject(String xPathExpression) {
197                    Object obj = _node.selectObject(xPathExpression);
198    
199                    if (obj == null) {
200                            return null;
201                    }
202                    else if (obj instanceof List<?>) {
203                            return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)obj);
204                    }
205                    else {
206                            return obj;
207                    }
208            }
209    
210            public Node selectSingleNode(String xPathExpression) {
211                    org.dom4j.Node node = _node.selectSingleNode(xPathExpression);
212    
213                    if (node == null) {
214                            return null;
215                    }
216                    if (node instanceof org.dom4j.Element) {
217                            return new ElementImpl((org.dom4j.Element)node);
218                    }
219                    else {
220                            return new NodeImpl(node);
221                    }
222            }
223    
224            public void setName(String name) {
225                    _node.setName(name);
226            }
227    
228            public void setText(String text) {
229                    _node.setText(text);
230            }
231    
232            public boolean supportsParent() {
233                    return _node.supportsParent();
234            }
235    
236            @Override
237            public String toString() {
238                    return _node.toString();
239            }
240    
241            public String valueOf(String xPathExpression) {
242                    return _node.valueOf(xPathExpression);
243            }
244    
245            public void write(Writer writer) throws IOException {
246                    _node.write(writer);
247            }
248    
249            private org.dom4j.Node _node;
250    
251    }