001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.xml;
016    
017    import com.liferay.portal.kernel.util.StringUtil;
018    import com.liferay.portal.kernel.xml.Attribute;
019    import com.liferay.portal.kernel.xml.CDATA;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.kernel.xml.Entity;
022    import com.liferay.portal.kernel.xml.Namespace;
023    import com.liferay.portal.kernel.xml.Node;
024    import com.liferay.portal.kernel.xml.QName;
025    import com.liferay.portal.kernel.xml.Text;
026    
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Map;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ElementImpl extends BranchImpl implements Element {
035    
036            public ElementImpl(org.dom4j.Element element) {
037                    super(element);
038    
039                    _element = element;
040            }
041    
042            public void add(Attribute attribute) {
043                    AttributeImpl attributeImpl = (AttributeImpl)attribute;
044    
045                    _element.add(attributeImpl.getWrappedAttribute());
046            }
047    
048            public void add(CDATA cdata) {
049                    CDATAImpl cdataImpl = (CDATAImpl)cdata;
050    
051                    _element.add(cdataImpl.getWrappedCDATA());
052            }
053    
054            public void add(Entity entity) {
055                    EntityImpl entityImpl = (EntityImpl)entity;
056    
057                    _element.add(entityImpl.getWrappedEntity());
058            }
059    
060            public void add(Namespace namespace) {
061                    NamespaceImpl namespaceImpl = (NamespaceImpl)namespace;
062    
063                    _element.add(namespaceImpl.getWrappedNamespace());
064            }
065    
066            public void add(Text text) {
067                    TextImpl textImpl = (TextImpl)text;
068    
069                    _element.add(textImpl.getWrappedText());
070            }
071    
072            public Element addAttribute(QName qName, String value) {
073                    QNameImpl qNameImpl = (QNameImpl)qName;
074    
075                    return new ElementImpl(
076                            _element.addAttribute(qNameImpl.getWrappedQName(), value));
077            }
078    
079            public Element addAttribute(String name, String value) {
080                    return new ElementImpl(_element.addAttribute(name, value));
081            }
082    
083            public Element addCDATA(String cdata) {
084                    cdata = StringUtil.replace(cdata, "]]>", "]]]]><![CDATA[>");
085    
086                    return new ElementImpl(_element.addCDATA(cdata));
087            }
088    
089            public Element addComment(String comment) {
090                    return new ElementImpl(_element.addComment(comment));
091            }
092    
093            public Element addEntity(String name, String text) {
094                    return new ElementImpl(_element.addEntity(name, text));
095            }
096    
097            public Element addNamespace(String prefix, String uri) {
098                    return new ElementImpl(_element.addNamespace(prefix, uri));
099            }
100    
101            public Element addProcessingInstruction(
102                    String target, Map<String, String> data) {
103    
104                    return new ElementImpl(_element.addProcessingInstruction(target, data));
105            }
106    
107            public Element addProcessingInstruction(String target, String data) {
108                    return new ElementImpl(_element.addProcessingInstruction(target, data));
109            }
110    
111            public Element addText(String text) {
112                    return new ElementImpl(_element.addText(text));
113            }
114    
115            public List<Namespace> additionalNamespaces() {
116                    return SAXReaderImpl.toNewNamespaces(_element.additionalNamespaces());
117            }
118    
119            public void appendAttributes(Element element) {
120                    ElementImpl elementImpl = (ElementImpl)element;
121    
122                    _element.appendAttributes(elementImpl.getWrappedElement());
123            }
124    
125            public Attribute attribute(int index) {
126                    org.dom4j.Attribute attribute = _element.attribute(index);
127    
128                    if (attribute == null) {
129                            return null;
130                    }
131                    else {
132                            return new AttributeImpl(attribute);
133                    }
134            }
135    
136            public Attribute attribute(QName qName) {
137                    QNameImpl qNameImpl = (QNameImpl)qName;
138    
139                    org.dom4j.Attribute attribute = _element.attribute(
140                            qNameImpl.getWrappedQName());
141    
142                    if (attribute == null) {
143                            return null;
144                    }
145                    else {
146                            return new AttributeImpl(attribute);
147                    }
148            }
149    
150            public Attribute attribute(String name) {
151                    org.dom4j.Attribute attribute = _element.attribute(name);
152    
153                    if (attribute == null) {
154                            return null;
155                    }
156                    else {
157                            return new AttributeImpl(attribute);
158                    }
159            }
160    
161            public int attributeCount() {
162                    return _element.attributeCount();
163            }
164    
165            public Iterator<Attribute> attributeIterator() {
166                    return attributes().iterator();
167            }
168    
169            public String attributeValue(QName qName) {
170                    QNameImpl qNameImpl = (QNameImpl)qName;
171    
172                    return _element.attributeValue(qNameImpl.getWrappedQName());
173            }
174    
175            public String attributeValue(QName qName, String defaultValue) {
176                    QNameImpl qNameImpl = (QNameImpl)qName;
177    
178                    return _element.attributeValue(
179                            qNameImpl.getWrappedQName(), defaultValue);
180            }
181    
182            public String attributeValue(String name) {
183                    return _element.attributeValue(name);
184            }
185    
186            public String attributeValue(String name, String defaultValue) {
187                    return _element.attributeValue(name, defaultValue);
188            }
189    
190            public List<Attribute> attributes() {
191                    return SAXReaderImpl.toNewAttributes(_element.attributes());
192            }
193    
194            public Element createCopy() {
195                    return new ElementImpl(_element.createCopy());
196            }
197    
198            public Element createCopy(QName qName) {
199                    QNameImpl qNameImpl = (QNameImpl)qName;
200    
201                    return new ElementImpl(
202                            _element.createCopy(qNameImpl.getWrappedQName()));
203            }
204    
205            public Element createCopy(String name) {
206                    return new ElementImpl(_element.createCopy(name));
207            }
208    
209            public List<Namespace> declaredNamespaces() {
210                    return SAXReaderImpl.toNewNamespaces(_element.declaredNamespaces());
211            }
212    
213            public Element element(QName qName) {
214                    QNameImpl qNameImpl = (QNameImpl)qName;
215    
216                    org.dom4j.Element element = _element.element(
217                            qNameImpl.getWrappedQName());
218    
219                    if (element == null) {
220                            return null;
221                    }
222                    else {
223                            return new ElementImpl(element);
224                    }
225            }
226    
227            public Element element(String name) {
228                    org.dom4j.Element element = _element.element(name);
229    
230                    if (element == null) {
231                            return null;
232                    }
233                    else {
234                            return new ElementImpl(element);
235                    }
236            }
237    
238            public Iterator<Element> elementIterator() {
239                    return elements().iterator();
240            }
241    
242            public Iterator<Element> elementIterator(QName qName) {
243                    return elements(qName).iterator();
244            }
245    
246            public Iterator<Element> elementIterator(String name) {
247                    return elements(name).iterator();
248            }
249    
250            public String elementText(QName qName) {
251                    QNameImpl qNameImpl = (QNameImpl)qName;
252    
253                    return _element.elementText(qNameImpl.getWrappedQName());
254            }
255    
256            public String elementText(String name) {
257                    return _element.elementText(name);
258            }
259    
260            public String elementTextTrim(QName qName) {
261                    QNameImpl qNameImpl = (QNameImpl)qName;
262    
263                    return _element.elementTextTrim(qNameImpl.getWrappedQName());
264            }
265    
266            public String elementTextTrim(String name) {
267                    return _element.elementTextTrim(name);
268            }
269    
270            public List<Element> elements() {
271                    return SAXReaderImpl.toNewElements(_element.elements());
272            }
273    
274            public List<Element> elements(QName qName) {
275                    QNameImpl qNameImpl = (QNameImpl)qName;
276    
277                    return SAXReaderImpl.toNewElements(
278                            _element.elements(qNameImpl.getWrappedQName()));
279            }
280    
281            public List<Element> elements(String name) {
282                    return SAXReaderImpl.toNewElements(_element.elements(name));
283            }
284    
285            public boolean equals(Object obj) {
286                    org.dom4j.Element element = ((ElementImpl)obj).getWrappedElement();
287    
288                    return _element.equals(element);
289            }
290    
291            public Object getData() {
292                    return _element.getData();
293            }
294    
295            public Namespace getNamespace() {
296                    org.dom4j.Namespace namespace = _element.getNamespace();
297    
298                    if (namespace == null) {
299                            return null;
300                    }
301                    else {
302                            return new NamespaceImpl(namespace);
303                    }
304            }
305    
306            public Namespace getNamespaceForPrefix(String prefix) {
307                    org.dom4j.Namespace namespace = _element.getNamespaceForPrefix(prefix);
308    
309                    if (namespace == null) {
310                            return null;
311                    }
312                    else {
313                            return new NamespaceImpl(namespace);
314                    }
315            }
316    
317            public Namespace getNamespaceForURI(String uri) {
318                    org.dom4j.Namespace namespace = _element.getNamespaceForURI(uri);
319    
320                    if (namespace == null) {
321                            return null;
322                    }
323                    else {
324                            return new NamespaceImpl(namespace);
325                    }
326            }
327    
328            public String getNamespacePrefix() {
329                    return _element.getNamespacePrefix();
330            }
331    
332            public String getNamespaceURI() {
333                    return _element.getNamespaceURI();
334            }
335    
336            public List<Namespace> getNamespacesForURI(String uri) {
337                    return SAXReaderImpl.toNewNamespaces(_element.getNamespacesForURI(uri));
338            }
339    
340            public QName getQName() {
341                    org.dom4j.QName qName = _element.getQName();
342    
343                    if (qName == null) {
344                            return null;
345                    }
346                    else {
347                            return new QNameImpl(qName);
348                    }
349            }
350    
351            public QName getQName(String qualifiedName) {
352                    org.dom4j.QName qName = _element.getQName(qualifiedName);
353    
354                    if (qName == null) {
355                            return null;
356                    }
357                    else {
358                            return new QNameImpl(qName);
359                    }
360            }
361    
362            public String getQualifiedName() {
363                    return _element.getQualifiedName();
364            }
365    
366            public String getTextTrim() {
367                    return _element.getTextTrim();
368            }
369    
370            public org.dom4j.Element getWrappedElement() {
371                    return _element;
372            }
373    
374            public Node getXPathResult(int index) {
375                    org.dom4j.Node node = _element.getXPathResult(index);
376    
377                    if (node == null) {
378                            return null;
379                    }
380                    else {
381                            return new NodeImpl(node);
382                    }
383            }
384    
385            public int hashCode() {
386                    return _element.hashCode();
387            }
388    
389            public boolean hasMixedContent() {
390                    return _element.hasMixedContent();
391            }
392    
393            public boolean isRootElement() {
394                    return _element.isRootElement();
395            }
396    
397            public boolean isTextOnly() {
398                    return _element.isTextOnly();
399            }
400    
401            public boolean remove(Attribute attribute) {
402                    AttributeImpl attributeImpl = (AttributeImpl)attribute;
403    
404                    return _element.remove(attributeImpl.getWrappedAttribute());
405            }
406    
407            public boolean remove(CDATA cdata) {
408                    CDATAImpl cdataImpl = (CDATAImpl)cdata;
409    
410                    return _element.remove(cdataImpl.getWrappedCDATA());
411            }
412    
413            public boolean remove(Entity entity) {
414                    EntityImpl entityImpl = (EntityImpl)entity;
415    
416                    return _element.remove(entityImpl.getWrappedEntity());
417            }
418    
419            public boolean remove(Namespace namespace) {
420                    NamespaceImpl namespaceImpl = (NamespaceImpl)namespace;
421    
422                    return _element.remove(namespaceImpl.getWrappedNamespace());
423            }
424    
425            public boolean remove(Text text) {
426                    TextImpl textImpl = (TextImpl)text;
427    
428                    return _element.remove(textImpl.getWrappedText());
429            }
430    
431            public void setAttributes(List<Attribute> attributes) {
432                    _element.setAttributes(SAXReaderImpl.toOldAttributes(attributes));
433            }
434    
435            public void setData(Object data) {
436                    _element.setData(data);
437            }
438    
439            public void setQName(QName qName) {
440                    QNameImpl qNameImpl = (QNameImpl)qName;
441    
442                    _element.setQName(qNameImpl.getWrappedQName());
443            }
444    
445            public String toString() {
446                    return _element.toString();
447            }
448    
449            private org.dom4j.Element _element;
450    
451    }