001    /**
002     * Copyright (c) 2000-present 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.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.xml.Document;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.kernel.xml.Node;
022    import com.liferay.portal.kernel.xml.Visitor;
023    import com.liferay.util.xml.Dom4jUtil;
024    
025    import java.io.IOException;
026    import java.io.Writer;
027    
028    import java.util.List;
029    
030    import org.dom4j.io.OutputFormat;
031    import org.dom4j.io.XMLWriter;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class NodeImpl implements Node {
037    
038            public NodeImpl(org.dom4j.Node node) {
039                    _node = node;
040            }
041    
042            @Override
043            public <T, V extends Visitor<T>> T accept(V visitor) {
044                    return visitor.visitNode(this);
045            }
046    
047            @Override
048            public String asXML() {
049                    return _node.asXML();
050            }
051    
052            @Override
053            public Node asXPathResult(Element parent) {
054                    ElementImpl parentImpl = (ElementImpl)parent;
055    
056                    org.dom4j.Node node = _node.asXPathResult(
057                            parentImpl.getWrappedElement());
058    
059                    if (node == null) {
060                            return null;
061                    }
062    
063                    if (node instanceof org.dom4j.Element) {
064                            return new ElementImpl((org.dom4j.Element)node);
065                    }
066                    else {
067                            return new NodeImpl(node);
068                    }
069            }
070    
071            @Override
072            public String compactString() throws IOException {
073                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
074                            new UnsyncByteArrayOutputStream();
075    
076                    OutputFormat outputFormat = OutputFormat.createCompactFormat();
077    
078                    XMLWriter xmlWriter = new XMLWriter(
079                            unsyncByteArrayOutputStream, outputFormat);
080    
081                    xmlWriter.write(_node);
082    
083                    return unsyncByteArrayOutputStream.toString(StringPool.UTF8);
084            }
085    
086            @Override
087            public Node detach() {
088                    org.dom4j.Node node = _node.detach();
089    
090                    if (node == null) {
091                            return null;
092                    }
093    
094                    if (node instanceof org.dom4j.Element) {
095                            return new ElementImpl((org.dom4j.Element)node);
096                    }
097                    else {
098                            return new NodeImpl(node);
099                    }
100            }
101    
102            @Override
103            public boolean equals(Object obj) {
104                    if (this == obj) {
105                            return true;
106                    }
107    
108                    if (!(obj instanceof NodeImpl)) {
109                            return false;
110                    }
111    
112                    org.dom4j.Node node = ((NodeImpl)obj).getWrappedNode();
113    
114                    return _node.equals(node);
115            }
116    
117            @Override
118            public String formattedString() throws IOException {
119                    return Dom4jUtil.toString(_node);
120            }
121    
122            @Override
123            public String formattedString(String indent) throws IOException {
124                    return Dom4jUtil.toString(_node, indent);
125            }
126    
127            @Override
128            public String formattedString(String indent, boolean expandEmptyElements)
129                    throws IOException {
130    
131                    return Dom4jUtil.toString(_node, indent, expandEmptyElements);
132            }
133    
134            @Override
135            public String formattedString(
136                            String indent, boolean expandEmptyElements, boolean trimText)
137                    throws IOException {
138    
139                    return Dom4jUtil.toString(_node, indent, expandEmptyElements, trimText);
140            }
141    
142            @Override
143            public Document getDocument() {
144                    org.dom4j.Document document = _node.getDocument();
145    
146                    if (document == null) {
147                            return null;
148                    }
149                    else {
150                            return new DocumentImpl(document);
151                    }
152            }
153    
154            @Override
155            public String getName() {
156                    return _node.getName();
157            }
158    
159            @Override
160            public Element getParent() {
161                    org.dom4j.Element element = _node.getParent();
162    
163                    if (element == null) {
164                            return null;
165                    }
166                    else {
167                            return new ElementImpl(element);
168                    }
169            }
170    
171            @Override
172            public String getPath() {
173                    return _node.getPath();
174            }
175    
176            @Override
177            public String getPath(Element context) {
178                    ElementImpl contextImpl = (ElementImpl)context;
179    
180                    return _node.getPath(contextImpl.getWrappedElement());
181            }
182    
183            @Override
184            public String getStringValue() {
185                    return _node.getStringValue();
186            }
187    
188            @Override
189            public String getText() {
190                    return _node.getText();
191            }
192    
193            @Override
194            public String getUniquePath() {
195                    return _node.getUniquePath();
196            }
197    
198            @Override
199            public String getUniquePath(Element context) {
200                    ElementImpl contextImpl = (ElementImpl)context;
201    
202                    return _node.getUniquePath(contextImpl.getWrappedElement());
203            }
204    
205            public org.dom4j.Node getWrappedNode() {
206                    return _node;
207            }
208    
209            @Override
210            public boolean hasContent() {
211                    return _node.hasContent();
212            }
213    
214            @Override
215            public int hashCode() {
216                    return _node.hashCode();
217            }
218    
219            @Override
220            public boolean isReadOnly() {
221                    return _node.isReadOnly();
222            }
223    
224            @Override
225            public boolean matches(String xPathExpression) {
226                    return _node.matches(xPathExpression);
227            }
228    
229            @Override
230            public Number numberValueOf(String xPathExpression) {
231                    return _node.numberValueOf(xPathExpression);
232            }
233    
234            @Override
235            public List<Node> selectNodes(String xPathExpression) {
236                    return SAXReaderImpl.toNewNodes(_node.selectNodes(xPathExpression));
237            }
238    
239            @Override
240            public List<Node> selectNodes(
241                    String xPathExpression, String comparisonXPathExpression) {
242    
243                    return SAXReaderImpl.toNewNodes(
244                            _node.selectNodes(xPathExpression, comparisonXPathExpression));
245            }
246    
247            @Override
248            public List<Node> selectNodes(
249                    String xPathExpression, String comparisonXPathExpression,
250                    boolean removeDuplicates) {
251    
252                    return SAXReaderImpl.toNewNodes(
253                            _node.selectNodes(
254                                    xPathExpression, comparisonXPathExpression, removeDuplicates));
255            }
256    
257            @Override
258            public Object selectObject(String xPathExpression) {
259                    Object obj = _node.selectObject(xPathExpression);
260    
261                    if (obj == null) {
262                            return null;
263                    }
264                    else if (obj instanceof List<?>) {
265                            return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)obj);
266                    }
267                    else {
268                            return obj;
269                    }
270            }
271    
272            @Override
273            public Node selectSingleNode(String xPathExpression) {
274                    org.dom4j.Node node = _node.selectSingleNode(xPathExpression);
275    
276                    if (node == null) {
277                            return null;
278                    }
279    
280                    if (node instanceof org.dom4j.Element) {
281                            return new ElementImpl((org.dom4j.Element)node);
282                    }
283                    else {
284                            return new NodeImpl(node);
285                    }
286            }
287    
288            @Override
289            public void setName(String name) {
290                    _node.setName(name);
291            }
292    
293            @Override
294            public void setText(String text) {
295                    _node.setText(text);
296            }
297    
298            @Override
299            public boolean supportsParent() {
300                    return _node.supportsParent();
301            }
302    
303            @Override
304            public String toString() {
305                    return _node.toString();
306            }
307    
308            @Override
309            public String valueOf(String xPathExpression) {
310                    return _node.valueOf(xPathExpression);
311            }
312    
313            @Override
314            public void write(Writer writer) throws IOException {
315                    _node.write(writer);
316            }
317    
318            private final org.dom4j.Node _node;
319    
320    }