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