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.Attribute;
23  import com.liferay.portal.kernel.xml.CDATA;
24  import com.liferay.portal.kernel.xml.Element;
25  import com.liferay.portal.kernel.xml.Entity;
26  import com.liferay.portal.kernel.xml.Namespace;
27  import com.liferay.portal.kernel.xml.Node;
28  import com.liferay.portal.kernel.xml.QName;
29  import com.liferay.portal.kernel.xml.Text;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * <a href="ElementImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class ElementImpl extends BranchImpl implements Element {
42  
43      public ElementImpl(org.dom4j.Element element) {
44          super(element);
45  
46          _element = element;
47      }
48  
49      public void add(Attribute attribute) {
50          AttributeImpl attributeImpl = (AttributeImpl)attribute;
51  
52          _element.add(attributeImpl.getWrappedAttribute());
53      }
54  
55      public void add(CDATA cdata) {
56          CDATAImpl cdataImpl = (CDATAImpl)cdata;
57  
58          _element.add(cdataImpl.getWrappedCDATA());
59      }
60  
61      public void add(Entity entity) {
62          EntityImpl entityImpl = (EntityImpl)entity;
63  
64          _element.add(entityImpl.getWrappedEntity());
65      }
66  
67      public void add(Namespace namespace) {
68          NamespaceImpl namespaceImpl = (NamespaceImpl)namespace;
69  
70          _element.add(namespaceImpl.getWrappedNamespace());
71      }
72  
73      public void add(Text text) {
74          TextImpl textImpl = (TextImpl)text;
75  
76          _element.add(textImpl.getWrappedText());
77      }
78  
79      public Element addAttribute(QName qName, String value) {
80          QNameImpl qNameImpl = (QNameImpl)qName;
81  
82          return new ElementImpl(
83              _element.addAttribute(qNameImpl.getWrappedQName(), value));
84      }
85  
86      public Element addAttribute(String name, String value) {
87          return new ElementImpl(_element.addAttribute(name, value));
88      }
89  
90      public Element addCDATA(String cdata) {
91          return new ElementImpl(_element.addCDATA(cdata));
92      }
93  
94      public Element addComment(String comment) {
95          return new ElementImpl(_element.addComment(comment));
96      }
97  
98      public Element addEntity(String name, String text) {
99          return new ElementImpl(_element.addEntity(name, text));
100     }
101 
102     public Element addNamespace(String prefix, String uri) {
103         return new ElementImpl(_element.addNamespace(prefix, uri));
104     }
105 
106     public Element addProcessingInstruction(
107         String target, Map<String, String> data) {
108 
109         return new ElementImpl(_element.addProcessingInstruction(target, data));
110     }
111 
112     public Element addProcessingInstruction(String target, String data) {
113         return new ElementImpl(_element.addProcessingInstruction(target, data));
114     }
115 
116     public Element addText(String text) {
117         return new ElementImpl(_element.addText(text));
118     }
119 
120     public List<Namespace> additionalNamespaces() {
121         return SAXReaderImpl.toNewNamespaces(_element.additionalNamespaces());
122     }
123 
124     public void appendAttributes(Element element) {
125         ElementImpl elementImpl = (ElementImpl)element;
126 
127         _element.appendAttributes(elementImpl.getWrappedElement());
128     }
129 
130     public Attribute attribute(int index) {
131         org.dom4j.Attribute attribute = _element.attribute(index);
132 
133         if (attribute == null) {
134             return null;
135         }
136         else {
137             return new AttributeImpl(attribute);
138         }
139     }
140 
141     public Attribute attribute(QName qName) {
142         QNameImpl qNameImpl = (QNameImpl)qName;
143 
144         org.dom4j.Attribute attribute = _element.attribute(
145             qNameImpl.getWrappedQName());
146 
147         if (attribute == null) {
148             return null;
149         }
150         else {
151             return new AttributeImpl(attribute);
152         }
153     }
154 
155     public Attribute attribute(String name) {
156         org.dom4j.Attribute attribute = _element.attribute(name);
157 
158         if (attribute == null) {
159             return null;
160         }
161         else {
162             return new AttributeImpl(attribute);
163         }
164     }
165 
166     public int attributeCount() {
167         return _element.attributeCount();
168     }
169 
170     public Iterator<Attribute> attributeIterator() {
171         return attributes().iterator();
172     }
173 
174     public String attributeValue(QName qName) {
175         QNameImpl qNameImpl = (QNameImpl)qName;
176 
177         return _element.attributeValue(qNameImpl.getWrappedQName());
178     }
179 
180     public String attributeValue(QName qName, String defaultValue) {
181         QNameImpl qNameImpl = (QNameImpl)qName;
182 
183         return _element.attributeValue(
184             qNameImpl.getWrappedQName(), defaultValue);
185     }
186 
187     public String attributeValue(String name) {
188         return _element.attributeValue(name);
189     }
190 
191     public String attributeValue(String name, String defaultValue) {
192         return _element.attributeValue(name, defaultValue);
193     }
194 
195     public List<Attribute> attributes() {
196         return SAXReaderImpl.toNewAttributes(_element.attributes());
197     }
198 
199     public Element createCopy() {
200         return new ElementImpl(_element.createCopy());
201     }
202 
203     public Element createCopy(QName qName) {
204         QNameImpl qNameImpl = (QNameImpl)qName;
205 
206         return new ElementImpl(
207             _element.createCopy(qNameImpl.getWrappedQName()));
208     }
209 
210     public Element createCopy(String name) {
211         return new ElementImpl(_element.createCopy(name));
212     }
213 
214     public List<Namespace> declaredNamespaces() {
215         return SAXReaderImpl.toNewNamespaces(_element.declaredNamespaces());
216     }
217 
218     public Element element(QName qName) {
219         QNameImpl qNameImpl = (QNameImpl)qName;
220 
221         org.dom4j.Element element = _element.element(
222             qNameImpl.getWrappedQName());
223 
224         if (element == null) {
225             return null;
226         }
227         else {
228             return new ElementImpl(element);
229         }
230     }
231 
232     public Element element(String name) {
233         org.dom4j.Element element = _element.element(name);
234 
235         if (element == null) {
236             return null;
237         }
238         else {
239             return new ElementImpl(element);
240         }
241     }
242 
243     public Iterator<Element> elementIterator() {
244         return elements().iterator();
245     }
246 
247     public Iterator<Element> elementIterator(QName qName) {
248         return elements(qName).iterator();
249     }
250 
251     public Iterator<Element> elementIterator(String name) {
252         return elements(name).iterator();
253     }
254 
255     public String elementText(QName qName) {
256         QNameImpl qNameImpl = (QNameImpl)qName;
257 
258         return _element.elementText(qNameImpl.getWrappedQName());
259     }
260 
261     public String elementText(String name) {
262         return _element.elementText(name);
263     }
264 
265     public String elementTextTrim(QName qName) {
266         QNameImpl qNameImpl = (QNameImpl)qName;
267 
268         return _element.elementTextTrim(qNameImpl.getWrappedQName());
269     }
270 
271     public String elementTextTrim(String name) {
272         return _element.elementTextTrim(name);
273     }
274 
275     public List<Element> elements() {
276         return SAXReaderImpl.toNewElements(_element.elements());
277     }
278 
279     public List<Element> elements(QName qName) {
280         QNameImpl qNameImpl = (QNameImpl)qName;
281 
282         return SAXReaderImpl.toNewElements(
283             _element.elements(qNameImpl.getWrappedQName()));
284     }
285 
286     public List<Element> elements(String name) {
287         return SAXReaderImpl.toNewElements(_element.elements(name));
288     }
289 
290     public boolean equals(Object obj) {
291         org.dom4j.Element element = ((ElementImpl)obj).getWrappedElement();
292 
293         return _element.equals(element);
294     }
295 
296     public Object getData() {
297         return _element.getData();
298     }
299 
300     public Namespace getNamespace() {
301         org.dom4j.Namespace namespace = _element.getNamespace();
302 
303         if (namespace == null) {
304             return null;
305         }
306         else {
307             return new NamespaceImpl(namespace);
308         }
309     }
310 
311     public Namespace getNamespaceForPrefix(String prefix) {
312         org.dom4j.Namespace namespace = _element.getNamespaceForPrefix(prefix);
313 
314         if (namespace == null) {
315             return null;
316         }
317         else {
318             return new NamespaceImpl(namespace);
319         }
320     }
321 
322     public Namespace getNamespaceForURI(String uri) {
323         org.dom4j.Namespace namespace = _element.getNamespaceForURI(uri);
324 
325         if (namespace == null) {
326             return null;
327         }
328         else {
329             return new NamespaceImpl(namespace);
330         }
331     }
332 
333     public String getNamespacePrefix() {
334         return _element.getNamespacePrefix();
335     }
336 
337     public String getNamespaceURI() {
338         return _element.getNamespaceURI();
339     }
340 
341     public List<Namespace> getNamespacesForURI(String uri) {
342         return SAXReaderImpl.toNewNamespaces(_element.getNamespacesForURI(uri));
343     }
344 
345     public QName getQName() {
346         org.dom4j.QName qName = _element.getQName();
347 
348         if (qName == null) {
349             return null;
350         }
351         else {
352             return new QNameImpl(qName);
353         }
354     }
355 
356     public QName getQName(String qualifiedName) {
357         org.dom4j.QName qName = _element.getQName(qualifiedName);
358 
359         if (qName == null) {
360             return null;
361         }
362         else {
363             return new QNameImpl(qName);
364         }
365     }
366 
367     public String getQualifiedName() {
368         return _element.getQualifiedName();
369     }
370 
371     public String getTextTrim() {
372         return _element.getTextTrim();
373     }
374 
375     public org.dom4j.Element getWrappedElement() {
376         return _element;
377     }
378 
379     public Node getXPathResult(int index) {
380         org.dom4j.Node node = _element.getXPathResult(index);
381 
382         if (node == null) {
383             return null;
384         }
385         else {
386             return new NodeImpl(node);
387         }
388     }
389 
390     public int hashCode() {
391         return _element.hashCode();
392     }
393 
394     public boolean hasMixedContent() {
395         return _element.hasMixedContent();
396     }
397 
398     public boolean isRootElement() {
399         return _element.isRootElement();
400     }
401 
402     public boolean isTextOnly() {
403         return _element.isTextOnly();
404     }
405 
406     public boolean remove(Attribute attribute) {
407         AttributeImpl attributeImpl = (AttributeImpl)attribute;
408 
409         return _element.remove(attributeImpl.getWrappedAttribute());
410     }
411 
412     public boolean remove(CDATA cdata) {
413         CDATAImpl cdataImpl = (CDATAImpl)cdata;
414 
415         return _element.remove(cdataImpl.getWrappedCDATA());
416     }
417 
418     public boolean remove(Entity entity) {
419         EntityImpl entityImpl = (EntityImpl)entity;
420 
421         return _element.remove(entityImpl.getWrappedEntity());
422     }
423 
424     public boolean remove(Namespace namespace) {
425         NamespaceImpl namespaceImpl = (NamespaceImpl)namespace;
426 
427         return _element.remove(namespaceImpl.getWrappedNamespace());
428     }
429 
430     public boolean remove(Text text) {
431         TextImpl textImpl = (TextImpl)text;
432 
433         return _element.remove(textImpl.getWrappedText());
434     }
435 
436     public void setAttributes(List<Attribute> attributes) {
437         _element.setAttributes(SAXReaderImpl.toOldAttributes(attributes));
438     }
439 
440     public void setData(Object data) {
441         _element.setData(data);
442     }
443 
444     public void setQName(QName qName) {
445         QNameImpl qNameImpl = (QNameImpl)qName;
446 
447         _element.setQName(qNameImpl.getWrappedQName());
448     }
449 
450     private org.dom4j.Element _element;
451 
452 }