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.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            public <T, V extends Visitor<T>> T accept(V visitor) {
043                    return visitor.visitNode(this);
044            }
045    
046            public String asXML() {
047                    return _node.asXML();
048            }
049    
050            public Node asXPathResult(Element parent) {
051                    ElementImpl parentImpl = (ElementImpl)parent;
052    
053                    org.dom4j.Node node = _node.asXPathResult(
054                            parentImpl.getWrappedElement());
055    
056                    if (node == null) {
057                            return null;
058                    }
059    
060                    if (node instanceof org.dom4j.Element) {
061                            return new ElementImpl((org.dom4j.Element)node);
062                    }
063                    else {
064                            return new NodeImpl(node);
065                    }
066            }
067    
068            public String compactString() throws IOException {
069                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
070                            new UnsyncByteArrayOutputStream();
071    
072                    OutputFormat outputFormat = OutputFormat.createCompactFormat();
073    
074                    XMLWriter xmlWriter = new XMLWriter(
075                            unsyncByteArrayOutputStream, outputFormat);
076    
077                    xmlWriter.write(_node);
078    
079                    return unsyncByteArrayOutputStream.toString(StringPool.UTF8);
080            }
081    
082            public Node detach() {
083                    org.dom4j.Node node = _node.detach();
084    
085                    if (node == null) {
086                            return null;
087                    }
088    
089                    if (node instanceof org.dom4j.Element) {
090                            return new ElementImpl((org.dom4j.Element)node);
091                    }
092                    else {
093                            return new NodeImpl(node);
094                    }
095            }
096    
097            @Override
098            public boolean equals(Object obj) {
099                    org.dom4j.Node node = ((NodeImpl)obj).getWrappedNode();
100    
101                    return _node.equals(node);
102            }
103    
104            public String formattedString() throws IOException {
105                    return XMLFormatter.toString(_node);
106            }
107    
108            public String formattedString(String indent) throws IOException {
109                    return XMLFormatter.toString(_node, indent);
110            }
111    
112            public String formattedString(String indent, boolean expandEmptyElements)
113                    throws IOException {
114    
115                    return XMLFormatter.toString(_node, indent, expandEmptyElements);
116            }
117    
118            public String formattedString(
119                            String indent, boolean expandEmptyElements, boolean trimText)
120                    throws IOException {
121    
122                    return XMLFormatter.toString(
123                            _node, indent, expandEmptyElements, trimText);
124            }
125    
126            public Document getDocument() {
127                    org.dom4j.Document document = _node.getDocument();
128    
129                    if (document == null) {
130                            return null;
131                    }
132                    else {
133                            return new DocumentImpl(document);
134                    }
135            }
136    
137            public String getName() {
138                    return _node.getName();
139            }
140    
141            public Element getParent() {
142                    org.dom4j.Element element = _node.getParent();
143    
144                    if (element == null) {
145                            return null;
146                    }
147                    else {
148                            return new ElementImpl(element);
149                    }
150            }
151    
152            public String getPath() {
153                    return _node.getPath();
154            }
155    
156            public String getPath(Element context) {
157                    ElementImpl contextImpl = (ElementImpl)context;
158    
159                    return _node.getPath(contextImpl.getWrappedElement());
160            }
161    
162            public String getStringValue() {
163                    return _node.getStringValue();
164            }
165    
166            public String getText() {
167                    return _node.getText();
168            }
169    
170            public String getUniquePath() {
171                    return _node.getUniquePath();
172            }
173    
174            public String getUniquePath(Element context) {
175                    ElementImpl contextImpl = (ElementImpl)context;
176    
177                    return _node.getUniquePath(contextImpl.getWrappedElement());
178            }
179    
180            public org.dom4j.Node getWrappedNode() {
181                    return _node;
182            }
183    
184            public boolean hasContent() {
185                    return _node.hasContent();
186            }
187    
188            @Override
189            public int hashCode() {
190                    return _node.hashCode();
191            }
192    
193            public boolean isReadOnly() {
194                    return _node.isReadOnly();
195            }
196    
197            public boolean matches(String xPathExpression) {
198                    return _node.matches(xPathExpression);
199            }
200    
201            public Number numberValueOf(String xPathExpression) {
202                    return _node.numberValueOf(xPathExpression);
203            }
204    
205            public List<Node> selectNodes(String xPathExpression) {
206                    return SAXReaderImpl.toNewNodes(_node.selectNodes(xPathExpression));
207            }
208    
209            public List<Node> selectNodes(
210                    String xPathExpression, String comparisonXPathExpression) {
211    
212                    return SAXReaderImpl.toNewNodes(
213                            _node.selectNodes(xPathExpression, comparisonXPathExpression));
214            }
215    
216            public List<Node> selectNodes(
217                    String xPathExpression, String comparisonXPathExpression,
218                    boolean removeDuplicates) {
219    
220                    return SAXReaderImpl.toNewNodes(
221                            _node.selectNodes(
222                                    xPathExpression, comparisonXPathExpression, removeDuplicates));
223            }
224    
225            public Object selectObject(String xPathExpression) {
226                    Object obj = _node.selectObject(xPathExpression);
227    
228                    if (obj == null) {
229                            return null;
230                    }
231                    else if (obj instanceof List<?>) {
232                            return SAXReaderImpl.toNewNodes((List<org.dom4j.Node>)obj);
233                    }
234                    else {
235                            return obj;
236                    }
237            }
238    
239            public Node selectSingleNode(String xPathExpression) {
240                    org.dom4j.Node node = _node.selectSingleNode(xPathExpression);
241    
242                    if (node == null) {
243                            return null;
244                    }
245    
246                    if (node instanceof org.dom4j.Element) {
247                            return new ElementImpl((org.dom4j.Element)node);
248                    }
249                    else {
250                            return new NodeImpl(node);
251                    }
252            }
253    
254            public void setName(String name) {
255                    _node.setName(name);
256            }
257    
258            public void setText(String text) {
259                    _node.setText(text);
260            }
261    
262            public boolean supportsParent() {
263                    return _node.supportsParent();
264            }
265    
266            @Override
267            public String toString() {
268                    return _node.toString();
269            }
270    
271            public String valueOf(String xPathExpression) {
272                    return _node.valueOf(xPathExpression);
273            }
274    
275            public void write(Writer writer) throws IOException {
276                    _node.write(writer);
277            }
278    
279            private org.dom4j.Node _node;
280    
281    }