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