001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    import java.util.TreeMap;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class ElementImpl extends BranchImpl implements Element {
040    
041            public ElementImpl(org.dom4j.Element element) {
042                    super(element);
043    
044                    _element = element;
045            }
046    
047            @Override
048            public <T, V extends Visitor<T>> T accept(V visitor) {
049                    return visitor.visitElement(this);
050            }
051    
052            public void add(Attribute attribute) {
053                    AttributeImpl attributeImpl = (AttributeImpl)attribute;
054    
055                    _element.add(attributeImpl.getWrappedAttribute());
056            }
057    
058            public void add(CDATA cdata) {
059                    CDATAImpl cdataImpl = (CDATAImpl)cdata;
060    
061                    _element.add(cdataImpl.getWrappedCDATA());
062            }
063    
064            public void add(Entity entity) {
065                    EntityImpl entityImpl = (EntityImpl)entity;
066    
067                    _element.add(entityImpl.getWrappedEntity());
068            }
069    
070            public void add(Namespace namespace) {
071                    NamespaceImpl namespaceImpl = (NamespaceImpl)namespace;
072    
073                    _element.add(namespaceImpl.getWrappedNamespace());
074            }
075    
076            public void add(Text text) {
077                    TextImpl textImpl = (TextImpl)text;
078    
079                    _element.add(textImpl.getWrappedText());
080            }
081    
082            public Element addAttribute(QName qName, String value) {
083                    QNameImpl qNameImpl = (QNameImpl)qName;
084    
085                    return new ElementImpl(
086                            _element.addAttribute(qNameImpl.getWrappedQName(), value));
087            }
088    
089            public Element addAttribute(String name, String value) {
090                    return new ElementImpl(_element.addAttribute(name, value));
091            }
092    
093            public Element addCDATA(String cdata) {
094                    cdata = StringUtil.replace(cdata, "]]>", "]]]]><![CDATA[>");
095    
096                    return new ElementImpl(_element.addCDATA(cdata));
097            }
098    
099            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            @Override
296            public boolean equals(Object obj) {
297                    org.dom4j.Element element = ((ElementImpl)obj).getWrappedElement();
298    
299                    return _element.equals(element);
300            }
301    
302            @Override
303            public String formattedString() throws IOException {
304                    return XMLFormatter.toString(_element);
305            }
306    
307            @Override
308            public String formattedString(String indent) throws IOException {
309                    return XMLFormatter.toString(_element, indent);
310            }
311    
312            @Override
313            public String formattedString(String indent, boolean expandEmptyElements)
314                    throws IOException {
315    
316                    return XMLFormatter.toString(_element, indent, expandEmptyElements);
317            }
318    
319            public Object getData() {
320                    return _element.getData();
321            }
322    
323            public Namespace getNamespace() {
324                    org.dom4j.Namespace namespace = _element.getNamespace();
325    
326                    if (namespace == null) {
327                            return null;
328                    }
329                    else {
330                            return new NamespaceImpl(namespace);
331                    }
332            }
333    
334            public Namespace getNamespaceForPrefix(String prefix) {
335                    org.dom4j.Namespace namespace = _element.getNamespaceForPrefix(prefix);
336    
337                    if (namespace == null) {
338                            return null;
339                    }
340                    else {
341                            return new NamespaceImpl(namespace);
342                    }
343            }
344    
345            public Namespace getNamespaceForURI(String uri) {
346                    org.dom4j.Namespace namespace = _element.getNamespaceForURI(uri);
347    
348                    if (namespace == null) {
349                            return null;
350                    }
351                    else {
352                            return new NamespaceImpl(namespace);
353                    }
354            }
355    
356            public String getNamespacePrefix() {
357                    return _element.getNamespacePrefix();
358            }
359    
360            public String getNamespaceURI() {
361                    return _element.getNamespaceURI();
362            }
363    
364            public List<Namespace> getNamespacesForURI(String uri) {
365                    return SAXReaderImpl.toNewNamespaces(_element.getNamespacesForURI(uri));
366            }
367    
368            public QName getQName() {
369                    org.dom4j.QName qName = _element.getQName();
370    
371                    if (qName == null) {
372                            return null;
373                    }
374                    else {
375                            return new QNameImpl(qName);
376                    }
377            }
378    
379            public QName getQName(String qualifiedName) {
380                    org.dom4j.QName qName = _element.getQName(qualifiedName);
381    
382                    if (qName == null) {
383                            return null;
384                    }
385                    else {
386                            return new QNameImpl(qName);
387                    }
388            }
389    
390            public String getQualifiedName() {
391                    return _element.getQualifiedName();
392            }
393    
394            public String getTextTrim() {
395                    return _element.getTextTrim();
396            }
397    
398            public org.dom4j.Element getWrappedElement() {
399                    return _element;
400            }
401    
402            public Node getXPathResult(int index) {
403                    org.dom4j.Node node = _element.getXPathResult(index);
404    
405                    if (node == null) {
406                            return null;
407                    }
408                    else {
409                            return new NodeImpl(node);
410                    }
411            }
412    
413            @Override
414            public int hashCode() {
415                    return _element.hashCode();
416            }
417    
418            public boolean hasMixedContent() {
419                    return _element.hasMixedContent();
420            }
421    
422            public boolean isRootElement() {
423                    return _element.isRootElement();
424            }
425    
426            public boolean isTextOnly() {
427                    return _element.isTextOnly();
428            }
429    
430            public boolean remove(Attribute attribute) {
431                    AttributeImpl attributeImpl = (AttributeImpl)attribute;
432    
433                    return _element.remove(attributeImpl.getWrappedAttribute());
434            }
435    
436            public boolean remove(CDATA cdata) {
437                    CDATAImpl cdataImpl = (CDATAImpl)cdata;
438    
439                    return _element.remove(cdataImpl.getWrappedCDATA());
440            }
441    
442            public boolean remove(Entity entity) {
443                    EntityImpl entityImpl = (EntityImpl)entity;
444    
445                    return _element.remove(entityImpl.getWrappedEntity());
446            }
447    
448            public boolean remove(Namespace namespace) {
449                    NamespaceImpl namespaceImpl = (NamespaceImpl)namespace;
450    
451                    return _element.remove(namespaceImpl.getWrappedNamespace());
452            }
453    
454            public boolean remove(Text text) {
455                    TextImpl textImpl = (TextImpl)text;
456    
457                    return _element.remove(textImpl.getWrappedText());
458            }
459    
460            public void setAttributes(List<Attribute> attributes) {
461                    _element.setAttributes(SAXReaderImpl.toOldAttributes(attributes));
462            }
463    
464            public void setData(Object data) {
465                    _element.setData(data);
466            }
467    
468            public void setQName(QName qName) {
469                    QNameImpl qNameImpl = (QNameImpl)qName;
470    
471                    _element.setQName(qNameImpl.getWrappedQName());
472            }
473    
474            public void sortAttributes(boolean recursive) {
475                    Map<String, Attribute> attributesMap = new TreeMap<String, Attribute>();
476    
477                    List<Attribute> attributes = attributes();
478    
479                    for (Attribute attribute : attributes) {
480                            attribute.detach();
481    
482                            attributesMap.put(attribute.getName(), attribute);
483                    }
484    
485                    for (Map.Entry<String, Attribute> entry : attributesMap.entrySet()) {
486                            Attribute attribute = entry.getValue();
487    
488                            add(attribute);
489                    }
490    
491                    if (!recursive) {
492                            return;
493                    }
494    
495                    List<Element> elements = elements();
496    
497                    for (Element element : elements) {
498                            element.sortAttributes(true);
499                    }
500            }
501    
502            public void sortElementsByAttribute(
503                    String elementName, String attributeName) {
504    
505                    Map<String, Element> elementsMap = new TreeMap<String, Element>();
506    
507                    List<Element> elements = elements(elementName);
508    
509                    for (Element element : elements) {
510                            element.detach();
511    
512                            String attributeValue = element.attributeValue(attributeName);
513    
514                            elementsMap.put(attributeValue, element);
515                    }
516    
517                    for (Map.Entry<String, Element> entry : elementsMap.entrySet()) {
518                            Element element = entry.getValue();
519    
520                            add(element);
521                    }
522            }
523    
524            public void sortElementsByChildElement(
525                    String elementName, String childElementName) {
526    
527                    Map<String, Element> elementsMap = new TreeMap<String, Element>();
528    
529                    List<Element> elements = elements(elementName);
530    
531                    for (Element element : elements) {
532                            element.detach();
533    
534                            String childElementValue = element.elementText(childElementName);
535    
536                            elementsMap.put(childElementValue, element);
537                    }
538    
539                    for (Map.Entry<String, Element> entry : elementsMap.entrySet()) {
540                            Element element = entry.getValue();
541    
542                            add(element);
543                    }
544            }
545    
546            @Override
547            public String toString() {
548                    return _element.toString();
549            }
550    
551            private org.dom4j.Element _element;
552    
553    }