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