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