001    /**
002     * Copyright (c) 2000-2012 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 List<Namespace> additionalNamespaces() {
108                    return SAXReaderImpl.toNewNamespaces(_element.additionalNamespaces());
109            }
110    
111            public Element addNamespace(String prefix, String uri) {
112                    return new ElementImpl(_element.addNamespace(prefix, uri));
113            }
114    
115            public Element addProcessingInstruction(
116                    String target, Map<String, String> data) {
117    
118                    return new ElementImpl(_element.addProcessingInstruction(target, data));
119            }
120    
121            public Element addProcessingInstruction(String target, String data) {
122                    return new ElementImpl(_element.addProcessingInstruction(target, data));
123            }
124    
125            public Element addText(String text) {
126                    return new ElementImpl(_element.addText(text));
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 List<Attribute> attributes() {
180                    return SAXReaderImpl.toNewAttributes(_element.attributes());
181            }
182    
183            public String attributeValue(QName qName) {
184                    QNameImpl qNameImpl = (QNameImpl)qName;
185    
186                    return _element.attributeValue(qNameImpl.getWrappedQName());
187            }
188    
189            public String attributeValue(QName qName, String defaultValue) {
190                    QNameImpl qNameImpl = (QNameImpl)qName;
191    
192                    return _element.attributeValue(
193                            qNameImpl.getWrappedQName(), defaultValue);
194            }
195    
196            public String attributeValue(String name) {
197                    return _element.attributeValue(name);
198            }
199    
200            public String attributeValue(String name, String defaultValue) {
201                    return _element.attributeValue(name, defaultValue);
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 List<Element> elements() {
261                    return SAXReaderImpl.toNewElements(_element.elements());
262            }
263    
264            public List<Element> elements(QName qName) {
265                    QNameImpl qNameImpl = (QNameImpl)qName;
266    
267                    return SAXReaderImpl.toNewElements(
268                            _element.elements(qNameImpl.getWrappedQName()));
269            }
270    
271            public List<Element> elements(String name) {
272                    return SAXReaderImpl.toNewElements(_element.elements(name));
273            }
274    
275            public String elementText(QName qName) {
276                    QNameImpl qNameImpl = (QNameImpl)qName;
277    
278                    return _element.elementText(qNameImpl.getWrappedQName());
279            }
280    
281            public String elementText(String name) {
282                    return _element.elementText(name);
283            }
284    
285            public String elementTextTrim(QName qName) {
286                    QNameImpl qNameImpl = (QNameImpl)qName;
287    
288                    return _element.elementTextTrim(qNameImpl.getWrappedQName());
289            }
290    
291            public String elementTextTrim(String name) {
292                    return _element.elementTextTrim(name);
293            }
294    
295            @Override
296            public boolean equals(Object obj) {
297                    if (obj instanceof NodeImpl) {
298                            NodeImpl nodeImpl = (NodeImpl)obj;
299    
300                            if (nodeImpl.getWrappedNode() instanceof org.dom4j.Element) {
301                                    obj = new ElementImpl(
302                                            (org.dom4j.Element)nodeImpl.getWrappedNode());
303                            }
304                            else {
305                                    return false;
306                            }
307                    }
308    
309                    org.dom4j.Element element = ((ElementImpl)obj).getWrappedElement();
310    
311                    return _element.equals(element);
312            }
313    
314            @Override
315            public String formattedString() throws IOException {
316                    return XMLFormatter.toString(_element);
317            }
318    
319            @Override
320            public String formattedString(String indent) throws IOException {
321                    return XMLFormatter.toString(_element, indent);
322            }
323    
324            @Override
325            public String formattedString(String indent, boolean expandEmptyElements)
326                    throws IOException {
327    
328                    return XMLFormatter.toString(_element, indent, expandEmptyElements);
329            }
330    
331            public Object getData() {
332                    return _element.getData();
333            }
334    
335            public Namespace getNamespace() {
336                    org.dom4j.Namespace namespace = _element.getNamespace();
337    
338                    if (namespace == null) {
339                            return null;
340                    }
341                    else {
342                            return new NamespaceImpl(namespace);
343                    }
344            }
345    
346            public Namespace getNamespaceForPrefix(String prefix) {
347                    org.dom4j.Namespace namespace = _element.getNamespaceForPrefix(prefix);
348    
349                    if (namespace == null) {
350                            return null;
351                    }
352                    else {
353                            return new NamespaceImpl(namespace);
354                    }
355            }
356    
357            public Namespace getNamespaceForURI(String uri) {
358                    org.dom4j.Namespace namespace = _element.getNamespaceForURI(uri);
359    
360                    if (namespace == null) {
361                            return null;
362                    }
363                    else {
364                            return new NamespaceImpl(namespace);
365                    }
366            }
367    
368            public String getNamespacePrefix() {
369                    return _element.getNamespacePrefix();
370            }
371    
372            public List<Namespace> getNamespacesForURI(String uri) {
373                    return SAXReaderImpl.toNewNamespaces(_element.getNamespacesForURI(uri));
374            }
375    
376            public String getNamespaceURI() {
377                    return _element.getNamespaceURI();
378            }
379    
380            public QName getQName() {
381                    org.dom4j.QName qName = _element.getQName();
382    
383                    if (qName == null) {
384                            return null;
385                    }
386                    else {
387                            return new QNameImpl(qName);
388                    }
389            }
390    
391            public QName getQName(String qualifiedName) {
392                    org.dom4j.QName qName = _element.getQName(qualifiedName);
393    
394                    if (qName == null) {
395                            return null;
396                    }
397                    else {
398                            return new QNameImpl(qName);
399                    }
400            }
401    
402            public String getQualifiedName() {
403                    return _element.getQualifiedName();
404            }
405    
406            public String getTextTrim() {
407                    return _element.getTextTrim();
408            }
409    
410            public org.dom4j.Element getWrappedElement() {
411                    return _element;
412            }
413    
414            public Node getXPathResult(int index) {
415                    org.dom4j.Node node = _element.getXPathResult(index);
416    
417                    if (node == null) {
418                            return null;
419                    }
420                    else {
421                            return new NodeImpl(node);
422                    }
423            }
424    
425            @Override
426            public int hashCode() {
427                    return _element.hashCode();
428            }
429    
430            public boolean hasMixedContent() {
431                    return _element.hasMixedContent();
432            }
433    
434            public boolean isRootElement() {
435                    return _element.isRootElement();
436            }
437    
438            public boolean isTextOnly() {
439                    return _element.isTextOnly();
440            }
441    
442            public boolean remove(Attribute attribute) {
443                    AttributeImpl attributeImpl = (AttributeImpl)attribute;
444    
445                    return _element.remove(attributeImpl.getWrappedAttribute());
446            }
447    
448            public boolean remove(CDATA cdata) {
449                    CDATAImpl cdataImpl = (CDATAImpl)cdata;
450    
451                    return _element.remove(cdataImpl.getWrappedCDATA());
452            }
453    
454            public boolean remove(Entity entity) {
455                    EntityImpl entityImpl = (EntityImpl)entity;
456    
457                    return _element.remove(entityImpl.getWrappedEntity());
458            }
459    
460            public boolean remove(Namespace namespace) {
461                    NamespaceImpl namespaceImpl = (NamespaceImpl)namespace;
462    
463                    return _element.remove(namespaceImpl.getWrappedNamespace());
464            }
465    
466            public boolean remove(Text text) {
467                    TextImpl textImpl = (TextImpl)text;
468    
469                    return _element.remove(textImpl.getWrappedText());
470            }
471    
472            public void setAttributes(List<Attribute> attributes) {
473                    _element.setAttributes(SAXReaderImpl.toOldAttributes(attributes));
474            }
475    
476            public void setData(Object data) {
477                    _element.setData(data);
478            }
479    
480            public void setQName(QName qName) {
481                    QNameImpl qNameImpl = (QNameImpl)qName;
482    
483                    _element.setQName(qNameImpl.getWrappedQName());
484            }
485    
486            public void sortAttributes(boolean recursive) {
487                    Map<String, Attribute> attributesMap = new TreeMap<String, Attribute>();
488    
489                    List<Attribute> attributes = attributes();
490    
491                    for (Attribute attribute : attributes) {
492                            attribute.detach();
493    
494                            attributesMap.put(attribute.getName(), attribute);
495                    }
496    
497                    for (Map.Entry<String, Attribute> entry : attributesMap.entrySet()) {
498                            Attribute attribute = entry.getValue();
499    
500                            add(attribute);
501                    }
502    
503                    if (!recursive) {
504                            return;
505                    }
506    
507                    List<Element> elements = elements();
508    
509                    for (Element element : elements) {
510                            element.sortAttributes(true);
511                    }
512            }
513    
514            public void sortElementsByAttribute(
515                    String elementName, String attributeName) {
516    
517                    Map<String, Element> elementsMap = new TreeMap<String, Element>();
518    
519                    List<Element> elements = elements();
520    
521                    for (Element element : elements) {
522                            element.detach();
523    
524                            if (elementName.equals(element.getName())) {
525                                    String attributeValue = element.attributeValue(attributeName);
526    
527                                    elementsMap.put(attributeValue, element);
528                            }
529                    }
530    
531                    for (Element element : elements) {
532                            if (elementName.equals(element.getName())) {
533                                    break;
534                            }
535    
536                            add(element);
537                    }
538    
539                    for (Map.Entry<String, Element> entry : elementsMap.entrySet()) {
540                            Element element = entry.getValue();
541    
542                            add(element);
543                    }
544    
545                    boolean foundLastElementWithElementName = false;
546    
547                    for (int i = 0; i < elements.size(); i++) {
548                            Element element = elements.get(i);
549    
550                            if (!foundLastElementWithElementName) {
551                                    if (elementName.equals(element.getName())) {
552                                            if ((i + 1) < elements.size()) {
553                                                    Element nextElement = elements.get(i + 1);
554    
555                                                    if (!elementName.equals(nextElement.getName())) {
556                                                            foundLastElementWithElementName = true;
557                                                    }
558                                            }
559                                    }
560                            }
561                            else {
562                                    add(element);
563                            }
564                    }
565            }
566    
567            public void sortElementsByChildElement(
568                    String elementName, String childElementName) {
569    
570                    Map<String, Element> elementsMap = new TreeMap<String, Element>();
571    
572                    List<Element> elements = elements();
573    
574                    for (Element element : elements) {
575                            element.detach();
576    
577                            if (elementName.equals(element.getName())) {
578                                    String childElementValue = element.elementText(
579                                            childElementName);
580    
581                                    elementsMap.put(childElementValue, element);
582                            }
583                    }
584    
585                    for (Element element : elements) {
586                            if (elementName.equals(element.getName())) {
587                                    break;
588                            }
589    
590                            add(element);
591                    }
592    
593                    for (Map.Entry<String, Element> entry : elementsMap.entrySet()) {
594                            Element element = entry.getValue();
595    
596                            add(element);
597                    }
598    
599                    boolean foundLastElementWithElementName = false;
600    
601                    for (int i = 0; i < elements.size(); i++) {
602                            Element element = elements.get(i);
603    
604                            if (!foundLastElementWithElementName) {
605                                    if (elementName.equals(element.getName())) {
606                                            if ((i + 1) < elements.size()) {
607                                                    Element nextElement = elements.get(i + 1);
608    
609                                                    if (!elementName.equals(nextElement.getName())) {
610                                                            foundLastElementWithElementName = true;
611                                                    }
612                                            }
613                                    }
614                            }
615                            else {
616                                    add(element);
617                            }
618                    }
619            }
620    
621            @Override
622            public String toString() {
623                    return _element.toString();
624            }
625    
626            private org.dom4j.Element _element;
627    
628    }