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