1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.xml;
24  
25  import com.liferay.portal.kernel.xml.Branch;
26  import com.liferay.portal.kernel.xml.Comment;
27  import com.liferay.portal.kernel.xml.Element;
28  import com.liferay.portal.kernel.xml.Node;
29  import com.liferay.portal.kernel.xml.ProcessingInstruction;
30  import com.liferay.portal.kernel.xml.QName;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="BranchImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class BranchImpl extends NodeImpl implements Branch {
42  
43      public BranchImpl(org.dom4j.Branch branch) {
44          super(branch);
45  
46          _branch = branch;
47      }
48  
49      public void add(Comment comment) {
50          CommentImpl commentImpl = (CommentImpl)comment;
51  
52          _branch.add(commentImpl.getWrappedComment());
53      }
54  
55      public void add(Element element) {
56          ElementImpl elementImpl = (ElementImpl)element;
57  
58          _branch.add(elementImpl.getWrappedElement());
59      }
60  
61      public void add(Node node) {
62          NodeImpl nodeImpl = (NodeImpl)node;
63  
64          _branch.add(nodeImpl.getWrappedNode());
65      }
66  
67      public void add(ProcessingInstruction processingInstruction) {
68          ProcessingInstructionImpl processingInstructionImpl =
69              (ProcessingInstructionImpl)processingInstruction;
70  
71          _branch.add(
72              processingInstructionImpl.getWrappedProcessingInstruction());
73      }
74  
75      public Element addElement(QName qName) {
76          QNameImpl qNameImpl = (QNameImpl)qName;
77  
78          return new ElementImpl(_branch.addElement(qNameImpl.getWrappedQName()));
79      }
80  
81      public Element addElement(String name) {
82          return new ElementImpl(_branch.addElement(name));
83      }
84  
85      public Element addElement(String qualifiedName, String namespaceURI) {
86          return new ElementImpl(_branch.addElement(qualifiedName, namespaceURI));
87      }
88  
89      public void appendContent(Branch branch) {
90          BranchImpl branchImpl = (BranchImpl)branch;
91  
92          _branch.appendContent(branchImpl.getWrappedBranch());
93      }
94  
95      public void clearContent() {
96          _branch.clearContent();
97      }
98  
99      public List<Node> content() {
100         return SAXReaderImpl.toNewNodes(_branch.content());
101     }
102 
103     public Element elementByID(String elementID) {
104         return new ElementImpl(_branch.elementByID(elementID));
105     }
106 
107     public boolean equals(Object obj) {
108         org.dom4j.Branch branch = ((BranchImpl)obj).getWrappedBranch();
109 
110         return _branch.equals(branch);
111     }
112 
113     public org.dom4j.Branch getWrappedBranch() {
114         return _branch;
115     }
116 
117     public int hashCode() {
118         return _branch.hashCode();
119     }
120 
121     public int indexOf(Node node) {
122         NodeImpl nodeImpl = (NodeImpl)node;
123 
124         return _branch.indexOf(nodeImpl.getWrappedNode());
125     }
126 
127     public Node node(int index) {
128         org.dom4j.Node node = _branch.node(index);
129 
130         if (node == null) {
131             return null;
132         }
133         else {
134             if (node instanceof org.dom4j.Element) {
135                 return new ElementImpl((org.dom4j.Element)node);
136             }
137             else {
138                 return new NodeImpl(node);
139             }
140         }
141     }
142 
143     public int nodeCount() {
144         return _branch.nodeCount();
145     }
146 
147     public Iterator<Node> nodeIterator() {
148         return content().iterator();
149     }
150 
151     public void normalize() {
152         _branch.normalize();
153     }
154 
155     public ProcessingInstruction processingInstruction(String target) {
156         org.dom4j.ProcessingInstruction processingInstruction =
157             _branch.processingInstruction(target);
158 
159         if (processingInstruction == null) {
160             return null;
161         }
162         else {
163             return new ProcessingInstructionImpl(processingInstruction);
164         }
165     }
166 
167     public List<ProcessingInstruction> processingInstructions() {
168         return SAXReaderImpl.toNewProcessingInstructions(
169             _branch.processingInstructions());
170     }
171 
172     public List<ProcessingInstruction> processingInstructions(String target) {
173         return SAXReaderImpl.toNewProcessingInstructions(
174             _branch.processingInstructions(target));
175     }
176 
177     public boolean remove(Comment comment) {
178         CommentImpl commentImpl = (CommentImpl)comment;
179 
180         return _branch.remove(commentImpl.getWrappedComment());
181     }
182 
183     public boolean remove(Element element) {
184         ElementImpl elementImpl = (ElementImpl)element;
185 
186         return _branch.remove(elementImpl.getWrappedElement());
187     }
188 
189     public boolean remove(Node node) {
190         NodeImpl nodeImpl = (NodeImpl)node;
191 
192         return _branch.remove(nodeImpl.getWrappedNode());
193     }
194 
195     public boolean remove(ProcessingInstruction processingInstruction) {
196         ProcessingInstructionImpl processingInstructionImpl =
197             (ProcessingInstructionImpl)processingInstruction;
198 
199         return _branch.remove(
200             processingInstructionImpl.getWrappedProcessingInstruction());
201     }
202 
203     public boolean removeProcessingInstruction(String target) {
204         return _branch.removeProcessingInstruction(target);
205     }
206 
207     public void setContent(List<Node> content) {
208         _branch.setContent(SAXReaderImpl.toOldNodes(content));
209     }
210 
211     public void setProcessingInstructions(
212         List<ProcessingInstruction> processingInstructions) {
213 
214         _branch.setProcessingInstructions(
215             SAXReaderImpl.toOldProcessingInstructions(processingInstructions));
216     }
217 
218     private org.dom4j.Branch _branch;
219 
220 }