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