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.Branch;
018    import com.liferay.portal.kernel.xml.Comment;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.kernel.xml.Node;
021    import com.liferay.portal.kernel.xml.ProcessingInstruction;
022    import com.liferay.portal.kernel.xml.QName;
023    import com.liferay.util.xml.XMLFormatter;
024    
025    import java.io.IOException;
026    
027    import java.util.Iterator;
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class BranchImpl extends NodeImpl implements Branch {
034    
035            public BranchImpl(org.dom4j.Branch branch) {
036                    super(branch);
037    
038                    _branch = branch;
039            }
040    
041            public void add(Comment comment) {
042                    CommentImpl commentImpl = (CommentImpl)comment;
043    
044                    _branch.add(commentImpl.getWrappedComment());
045            }
046    
047            public void add(Element element) {
048                    ElementImpl elementImpl = (ElementImpl)element;
049    
050                    _branch.add(elementImpl.getWrappedElement());
051            }
052    
053            public void add(Node node) {
054                    NodeImpl nodeImpl = (NodeImpl)node;
055    
056                    _branch.add(nodeImpl.getWrappedNode());
057            }
058    
059            public void add(ProcessingInstruction processingInstruction) {
060                    ProcessingInstructionImpl processingInstructionImpl =
061                            (ProcessingInstructionImpl)processingInstruction;
062    
063                    _branch.add(
064                            processingInstructionImpl.getWrappedProcessingInstruction());
065            }
066    
067            public Element addElement(QName qName) {
068                    QNameImpl qNameImpl = (QNameImpl)qName;
069    
070                    return new ElementImpl(_branch.addElement(qNameImpl.getWrappedQName()));
071            }
072    
073            public Element addElement(String name) {
074                    return new ElementImpl(_branch.addElement(name));
075            }
076    
077            public Element addElement(String qualifiedName, String namespaceURI) {
078                    return new ElementImpl(_branch.addElement(qualifiedName, namespaceURI));
079            }
080    
081            public void appendContent(Branch branch) {
082                    BranchImpl branchImpl = (BranchImpl)branch;
083    
084                    _branch.appendContent(branchImpl.getWrappedBranch());
085            }
086    
087            public void clearContent() {
088                    _branch.clearContent();
089            }
090    
091            public List<Node> content() {
092                    return SAXReaderImpl.toNewNodes(_branch.content());
093            }
094    
095            public Element elementByID(String elementID) {
096                    return new ElementImpl(_branch.elementByID(elementID));
097            }
098    
099            @Override
100            public boolean equals(Object obj) {
101                    org.dom4j.Branch branch = ((BranchImpl)obj).getWrappedBranch();
102    
103                    return _branch.equals(branch);
104            }
105    
106            @Override
107            public String formattedString() throws IOException {
108                    return XMLFormatter.toString(_branch);
109            }
110    
111            @Override
112            public String formattedString(String indent) throws IOException {
113                    return XMLFormatter.toString(_branch, indent);
114            }
115    
116            @Override
117            public String formattedString(String indent, boolean expandEmptyElements)
118                    throws IOException {
119    
120                    return XMLFormatter.toString(_branch, indent, expandEmptyElements);
121            }
122    
123            public org.dom4j.Branch getWrappedBranch() {
124                    return _branch;
125            }
126    
127            @Override
128            public int hashCode() {
129                    return _branch.hashCode();
130            }
131    
132            public int indexOf(Node node) {
133                    NodeImpl nodeImpl = (NodeImpl)node;
134    
135                    return _branch.indexOf(nodeImpl.getWrappedNode());
136            }
137    
138            public Node node(int index) {
139                    org.dom4j.Node node = _branch.node(index);
140    
141                    if (node == null) {
142                            return null;
143                    }
144                    else {
145                            if (node instanceof org.dom4j.CDATA) {
146                                    return new CDATAImpl((org.dom4j.CDATA)node);
147                            }
148                            else if (node instanceof org.dom4j.Comment) {
149                                    return new CommentImpl((org.dom4j.Comment)node);
150                            }
151                            else if (node instanceof org.dom4j.Element) {
152                                    return new ElementImpl((org.dom4j.Element)node);
153                            }
154                            else if (node instanceof org.dom4j.Entity) {
155                                    return new EntityImpl((org.dom4j.Entity)node);
156                            }
157                            else if (node instanceof org.dom4j.Namespace) {
158                                    return new NamespaceImpl((org.dom4j.Namespace)node);
159                            }
160                            else if (node instanceof org.dom4j.Text) {
161                                    return new TextImpl((org.dom4j.Text)node);
162                            }
163                            else {
164                                    return new NodeImpl(node);
165                            }
166                    }
167            }
168    
169            public int nodeCount() {
170                    return _branch.nodeCount();
171            }
172    
173            public Iterator<Node> nodeIterator() {
174                    return content().iterator();
175            }
176    
177            public void normalize() {
178                    _branch.normalize();
179            }
180    
181            public ProcessingInstruction processingInstruction(String target) {
182                    org.dom4j.ProcessingInstruction processingInstruction =
183                            _branch.processingInstruction(target);
184    
185                    if (processingInstruction == null) {
186                            return null;
187                    }
188                    else {
189                            return new ProcessingInstructionImpl(processingInstruction);
190                    }
191            }
192    
193            public List<ProcessingInstruction> processingInstructions() {
194                    return SAXReaderImpl.toNewProcessingInstructions(
195                            _branch.processingInstructions());
196            }
197    
198            public List<ProcessingInstruction> processingInstructions(String target) {
199                    return SAXReaderImpl.toNewProcessingInstructions(
200                            _branch.processingInstructions(target));
201            }
202    
203            public boolean remove(Comment comment) {
204                    CommentImpl commentImpl = (CommentImpl)comment;
205    
206                    return _branch.remove(commentImpl.getWrappedComment());
207            }
208    
209            public boolean remove(Element element) {
210                    ElementImpl elementImpl = (ElementImpl)element;
211    
212                    return _branch.remove(elementImpl.getWrappedElement());
213            }
214    
215            public boolean remove(Node node) {
216                    NodeImpl nodeImpl = (NodeImpl)node;
217    
218                    return _branch.remove(nodeImpl.getWrappedNode());
219            }
220    
221            public boolean remove(ProcessingInstruction processingInstruction) {
222                    ProcessingInstructionImpl processingInstructionImpl =
223                            (ProcessingInstructionImpl)processingInstruction;
224    
225                    return _branch.remove(
226                            processingInstructionImpl.getWrappedProcessingInstruction());
227            }
228    
229            public boolean removeProcessingInstruction(String target) {
230                    return _branch.removeProcessingInstruction(target);
231            }
232    
233            public void setContent(List<Node> content) {
234                    _branch.setContent(SAXReaderImpl.toOldNodes(content));
235            }
236    
237            public void setProcessingInstructions(
238                    List<ProcessingInstruction> processingInstructions) {
239    
240                    _branch.setProcessingInstructions(
241                            SAXReaderImpl.toOldProcessingInstructions(processingInstructions));
242            }
243    
244            @Override
245            public String toString() {
246                    return _branch.toString();
247            }
248    
249            private org.dom4j.Branch _branch;
250    
251    }