001    /**
002     * Copyright (c) 2000-2010 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            public boolean equals(Object obj) {
097                    org.dom4j.Branch branch = ((BranchImpl)obj).getWrappedBranch();
098    
099                    return _branch.equals(branch);
100            }
101    
102            public org.dom4j.Branch getWrappedBranch() {
103                    return _branch;
104            }
105    
106            public int hashCode() {
107                    return _branch.hashCode();
108            }
109    
110            public int indexOf(Node node) {
111                    NodeImpl nodeImpl = (NodeImpl)node;
112    
113                    return _branch.indexOf(nodeImpl.getWrappedNode());
114            }
115    
116            public Node node(int index) {
117                    org.dom4j.Node node = _branch.node(index);
118    
119                    if (node == null) {
120                            return null;
121                    }
122                    else {
123                            if (node instanceof org.dom4j.Element) {
124                                    return new ElementImpl((org.dom4j.Element)node);
125                            }
126                            else {
127                                    return new NodeImpl(node);
128                            }
129                    }
130            }
131    
132            public int nodeCount() {
133                    return _branch.nodeCount();
134            }
135    
136            public Iterator<Node> nodeIterator() {
137                    return content().iterator();
138            }
139    
140            public void normalize() {
141                    _branch.normalize();
142            }
143    
144            public ProcessingInstruction processingInstruction(String target) {
145                    org.dom4j.ProcessingInstruction processingInstruction =
146                            _branch.processingInstruction(target);
147    
148                    if (processingInstruction == null) {
149                            return null;
150                    }
151                    else {
152                            return new ProcessingInstructionImpl(processingInstruction);
153                    }
154            }
155    
156            public List<ProcessingInstruction> processingInstructions() {
157                    return SAXReaderImpl.toNewProcessingInstructions(
158                            _branch.processingInstructions());
159            }
160    
161            public List<ProcessingInstruction> processingInstructions(String target) {
162                    return SAXReaderImpl.toNewProcessingInstructions(
163                            _branch.processingInstructions(target));
164            }
165    
166            public boolean remove(Comment comment) {
167                    CommentImpl commentImpl = (CommentImpl)comment;
168    
169                    return _branch.remove(commentImpl.getWrappedComment());
170            }
171    
172            public boolean remove(Element element) {
173                    ElementImpl elementImpl = (ElementImpl)element;
174    
175                    return _branch.remove(elementImpl.getWrappedElement());
176            }
177    
178            public boolean remove(Node node) {
179                    NodeImpl nodeImpl = (NodeImpl)node;
180    
181                    return _branch.remove(nodeImpl.getWrappedNode());
182            }
183    
184            public boolean remove(ProcessingInstruction processingInstruction) {
185                    ProcessingInstructionImpl processingInstructionImpl =
186                            (ProcessingInstructionImpl)processingInstruction;
187    
188                    return _branch.remove(
189                            processingInstructionImpl.getWrappedProcessingInstruction());
190            }
191    
192            public boolean removeProcessingInstruction(String target) {
193                    return _branch.removeProcessingInstruction(target);
194            }
195    
196            public void setContent(List<Node> content) {
197                    _branch.setContent(SAXReaderImpl.toOldNodes(content));
198            }
199    
200            public void setProcessingInstructions(
201                    List<ProcessingInstruction> processingInstructions) {
202    
203                    _branch.setProcessingInstructions(
204                            SAXReaderImpl.toOldProcessingInstructions(processingInstructions));
205            }
206    
207            public String toString() {
208                    return _branch.toString();
209            }
210    
211            private org.dom4j.Branch _branch;
212    
213    }