1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class BranchImpl extends NodeImpl implements Branch {
41  
42      public BranchImpl(org.dom4j.Branch branch) {
43          super(branch);
44  
45          _branch = branch;
46      }
47  
48      public void add(Comment comment) {
49          CommentImpl commentImpl = (CommentImpl)comment;
50  
51          _branch.add(commentImpl.getWrappedComment());
52      }
53  
54      public void add(Element element) {
55          ElementImpl elementImpl = (ElementImpl)element;
56  
57          _branch.add(elementImpl.getWrappedElement());
58      }
59  
60      public void add(Node node) {
61          NodeImpl nodeImpl = (NodeImpl)node;
62  
63          _branch.add(nodeImpl.getWrappedNode());
64      }
65  
66      public void add(ProcessingInstruction processingInstruction) {
67          ProcessingInstructionImpl processingInstructionImpl =
68              (ProcessingInstructionImpl)processingInstruction;
69  
70          _branch.add(
71              processingInstructionImpl.getWrappedProcessingInstruction());
72      }
73  
74      public Element addElement(QName qName) {
75          QNameImpl qNameImpl = (QNameImpl)qName;
76  
77          return new ElementImpl(_branch.addElement(qNameImpl.getWrappedQName()));
78      }
79  
80      public Element addElement(String name) {
81          return new ElementImpl(_branch.addElement(name));
82      }
83  
84      public Element addElement(String qualifiedName, String namespaceURI) {
85          return new ElementImpl(_branch.addElement(qualifiedName, namespaceURI));
86      }
87  
88      public void appendContent(Branch branch) {
89          BranchImpl branchImpl = (BranchImpl)branch;
90  
91          _branch.appendContent(branchImpl.getWrappedBranch());
92      }
93  
94      public void clearContent() {
95          _branch.clearContent();
96      }
97  
98      public List<Node> content() {
99          return SAXReaderImpl.toNewNodes(_branch.content());
100     }
101 
102     public Element elementByID(String elementID) {
103         return new ElementImpl(_branch.elementByID(elementID));
104     }
105 
106     public boolean equals(Object obj) {
107         org.dom4j.Branch branch = ((BranchImpl)obj).getWrappedBranch();
108 
109         return _branch.equals(branch);
110     }
111 
112     public org.dom4j.Branch getWrappedBranch() {
113         return _branch;
114     }
115 
116     public int hashCode() {
117         return _branch.hashCode();
118     }
119 
120     public int indexOf(Node node) {
121         NodeImpl nodeImpl = (NodeImpl)node;
122 
123         return _branch.indexOf(nodeImpl.getWrappedNode());
124     }
125 
126     public Node node(int index) {
127         org.dom4j.Node node = _branch.node(index);
128 
129         if (node == null) {
130             return null;
131         }
132         else {
133             if (node instanceof org.dom4j.Element) {
134                 return new ElementImpl((org.dom4j.Element)node);
135             }
136             else {
137                 return new NodeImpl(node);
138             }
139         }
140     }
141 
142     public int nodeCount() {
143         return _branch.nodeCount();
144     }
145 
146     public Iterator<Node> nodeIterator() {
147         return content().iterator();
148     }
149 
150     public void normalize() {
151         _branch.normalize();
152     }
153 
154     public ProcessingInstruction processingInstruction(String target) {
155         org.dom4j.ProcessingInstruction processingInstruction =
156             _branch.processingInstruction(target);
157 
158         if (processingInstruction == null) {
159             return null;
160         }
161         else {
162             return new ProcessingInstructionImpl(processingInstruction);
163         }
164     }
165 
166     public List<ProcessingInstruction> processingInstructions() {
167         return SAXReaderImpl.toNewProcessingInstructions(
168             _branch.processingInstructions());
169     }
170 
171     public List<ProcessingInstruction> processingInstructions(String target) {
172         return SAXReaderImpl.toNewProcessingInstructions(
173             _branch.processingInstructions(target));
174     }
175 
176     public boolean remove(Comment comment) {
177         CommentImpl commentImpl = (CommentImpl)comment;
178 
179         return _branch.remove(commentImpl.getWrappedComment());
180     }
181 
182     public boolean remove(Element element) {
183         ElementImpl elementImpl = (ElementImpl)element;
184 
185         return _branch.remove(elementImpl.getWrappedElement());
186     }
187 
188     public boolean remove(Node node) {
189         NodeImpl nodeImpl = (NodeImpl)node;
190 
191         return _branch.remove(nodeImpl.getWrappedNode());
192     }
193 
194     public boolean remove(ProcessingInstruction processingInstruction) {
195         ProcessingInstructionImpl processingInstructionImpl =
196             (ProcessingInstructionImpl)processingInstruction;
197 
198         return _branch.remove(
199             processingInstructionImpl.getWrappedProcessingInstruction());
200     }
201 
202     public boolean removeProcessingInstruction(String target) {
203         return _branch.removeProcessingInstruction(target);
204     }
205 
206     public void setContent(List<Node> content) {
207         _branch.setContent(SAXReaderImpl.toOldNodes(content));
208     }
209 
210     public void setProcessingInstructions(
211         List<ProcessingInstruction> processingInstructions) {
212 
213         _branch.setProcessingInstructions(
214             SAXReaderImpl.toOldProcessingInstructions(processingInstructions));
215     }
216 
217     private org.dom4j.Branch _branch;
218 
219 }