001    /**
002     * Copyright (c) 2000-2013 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.XMLFormatter;
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 XMLFormatter.toString(_node);
120            }
121    
122            @Override
123            public String formattedString(String indent) throws IOException {
124                    return XMLFormatter.toString(_node, indent);
125            }
126    
127            @Override
128            public String formattedString(String indent, boolean expandEmptyElements)
129                    throws IOException {
130    
131                    return XMLFormatter.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 XMLFormatter.toString(
140                            _node, indent, expandEmptyElements, trimText);
141            }
142    
143            @Override
144            public Document getDocument() {
145                    org.dom4j.Document document = _node.getDocument();
146    
147                    if (document == null) {
148                            return null;
149                    }
150                    else {
151                            return new DocumentImpl(document);
152                    }
153            }
154    
155            @Override
156            public String getName() {
157                    return _node.getName();
158            }
159    
160            @Override
161            public Element getParent() {
162                    org.dom4j.Element element = _node.getParent();
163    
164                    if (element == null) {
165                            return null;
166                    }
167                    else {
168                            return new ElementImpl(element);
169                    }
170            }
171    
172            @Override
173            public String getPath() {
174                    return _node.getPath();
175            }
176    
177            @Override
178            public String getPath(Element context) {
179                    ElementImpl contextImpl = (ElementImpl)context;
180    
181                    return _node.getPath(contextImpl.getWrappedElement());
182            }
183    
184            @Override
185            public String getStringValue() {
186                    return _node.getStringValue();
187            }
188    
189            @Override
190            public String getText() {
191                    return _node.getText();
192            }
193    
194            @Override
195            public String getUniquePath() {
196                    return _node.getUniquePath();
197            }
198    
199            @Override
200            public String getUniquePath(Element context) {
201                    ElementImpl contextImpl = (ElementImpl)context;
202    
203                    return _node.getUniquePath(contextImpl.getWrappedElement());
204            }
205    
206            public org.dom4j.Node getWrappedNode() {
207                    return _node;
208            }
209    
210            @Override
211            public boolean hasContent() {
212                    return _node.hasContent();
213            }
214    
215            @Override
216            public int hashCode() {
217                    return _node.hashCode();
218            }
219    
220            @Override
221            public boolean isReadOnly() {
222                    return _node.isReadOnly();
223            }
224    
225            @Override
226            public boolean matches(String xPathExpression) {
227                    return _node.matches(xPathExpression);
228            }
229    
230            @Override
231            public Number numberValueOf(String xPathExpression) {
232                    return _node.numberValueOf(xPathExpression);
233            }
234    
235            @Override
236            public List<Node> selectNodes(String xPathExpression) {
237                    return SAXReaderImpl.toNewNodes(_node.selectNodes(xPathExpression));
238            }
239    
240            @Override
241            public List<Node> selectNodes(
242                    String xPathExpression, String comparisonXPathExpression) {
243    
244                    return SAXReaderImpl.toNewNodes(
245                            _node.selectNodes(xPathExpression, comparisonXPathExpression));
246            }
247    
248            @Override
249            public List<Node> selectNodes(
250                    String xPathExpression, String comparisonXPathExpression,
251                    boolean removeDuplicates) {
252    
253                    return SAXReaderImpl.toNewNodes(
254                            _node.selectNodes(
255                                    xPathExpression, comparisonXPathExpression, removeDuplicates));
256            }
257    
258            @Override
259            public Object selectObject(String xPathExpression) {
260                    Object obj = _node.selectObject(xPathExpression);
261    
262                    if (obj == null) {
263                            return null;
264                    }
265                    else if (obj instanceof List<?>) {
266                            return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)obj);
267                    }
268                    else {
269                            return obj;
270                    }
271            }
272    
273            @Override
274            public Node selectSingleNode(String xPathExpression) {
275                    org.dom4j.Node node = _node.selectSingleNode(xPathExpression);
276    
277                    if (node == null) {
278                            return null;
279                    }
280    
281                    if (node instanceof org.dom4j.Element) {
282                            return new ElementImpl((org.dom4j.Element)node);
283                    }
284                    else {
285                            return new NodeImpl(node);
286                    }
287            }
288    
289            @Override
290            public void setName(String name) {
291                    _node.setName(name);
292            }
293    
294            @Override
295            public void setText(String text) {
296                    _node.setText(text);
297            }
298    
299            @Override
300            public boolean supportsParent() {
301                    return _node.supportsParent();
302            }
303    
304            @Override
305            public String toString() {
306                    return _node.toString();
307            }
308    
309            @Override
310            public String valueOf(String xPathExpression) {
311                    return _node.valueOf(xPathExpression);
312            }
313    
314            @Override
315            public void write(Writer writer) throws IOException {
316                    _node.write(writer);
317            }
318    
319            private org.dom4j.Node _node;
320    
321    }