View Javadoc
1   package org.codehaus.plexus.metadata.merge.support;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.util.Collection;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import org.codehaus.plexus.metadata.merge.MergeException;
32  import org.codehaus.plexus.metadata.merge.MergeStrategy;
33  import org.jdom2.Attribute;
34  import org.jdom2.Content;
35  import org.jdom2.Document;
36  import org.jdom2.Element;
37  import org.jdom2.Namespace;
38  import org.jdom2.Parent;
39  import org.jdom2.filter.Filter;
40  
41  /**
42   * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
43   */
44  /**
45   * @author khmarbaise
46   *
47   */
48  public abstract class AbstractMergeableSupport implements Mergeable {
49      /**
50       * Wrapped JDOM element.
51       */
52      protected Element element;
53  
54      /**
55       * The default merging strategy used.
56       */
57      private static final MergeStrategy DEFAULT_MERGE_STRATEGY = MergeStrategies.DEEP;
58  
59      /**
60       * @param element {@link Element}
61       */
62      public AbstractMergeableSupport(Element element) {
63          this.element = element;
64      }
65  
66      /** {@inheritDoc} */
67      public abstract void merge(Mergeable me) throws MergeException;
68  
69      /**
70       * Determines if the passed in {@link Mergeable} was of same type as this
71       * class.
72       *
73       * @param me {@link Mergeable} instance to test.
74       * @return <code>true</code> if the passed in Mergeable can be merged with
75       *         the current Mergeable.
76       */
77      protected abstract boolean isExpectedElementType(Mergeable me);
78  
79      // ----------------------------------------------------------------------
80      // Methods delegated on wrapped JDOM element.
81      // ----------------------------------------------------------------------
82  
83      /**
84       * @param collection {@link Collection}
85       * @return {@link Element}.
86       */
87      public Element addContent(Collection collection) {
88          return element.addContent(collection);
89      }
90  
91      /**
92       * @param child {@link Content}
93       * @return {@link Element}.
94       */
95      public Element addContent(Content child) {
96          return element.addContent(child);
97      }
98  
99      /**
100      * @param index The index.
101      * @param c {@link Collection}
102      * @return {@link Element}.
103      */
104     public Element addContent(int index, Collection c) {
105         return element.addContent(index, c);
106     }
107 
108     /**
109      * @param index The index.
110      * @param child {@link Content}
111      * @return {@link Element}.
112      */
113     public Element addContent(int index, Content child) {
114         return element.addContent(index, child);
115     }
116 
117     /**
118      * @param str The content to be added.
119      * @return {@link Element}.
120      */
121     public Element addContent(String str) {
122         return element.addContent(str);
123     }
124 
125     /**
126      * @param additional {@link Namespace}
127      */
128     public void addNamespaceDeclaration(Namespace additional) {
129         element.addNamespaceDeclaration(additional);
130     }
131 
132     public Object clone() {
133         return element.clone();
134     }
135 
136     public List cloneContent() {
137         return element.cloneContent();
138     }
139 
140     public Content detach() {
141         return element.detach();
142     }
143 
144     /** {@inheritDoc} */
145     public boolean equals(Object obj) {
146         return element.equals(obj);
147     }
148 
149     /**
150      * @return list of Namespaces.
151      */
152     public List getAdditionalNamespaces() {
153         return element.getAdditionalNamespaces();
154     }
155 
156     /**
157      * @param name The name.
158      * @param ns {@link Namespace}
159      * @return {@link Attribute}
160      */
161     public Attribute getAttribute(String name, Namespace ns) {
162         return element.getAttribute(name, ns);
163     }
164 
165     /**
166      * @param name The name of the attribute.
167      * @return {@link Attribute}
168      */
169     public Attribute getAttribute(String name) {
170         return element.getAttribute(name);
171     }
172 
173     /**
174      * @return list {@link Attribute}
175      */
176     public List getAttributes() {
177         return element.getAttributes();
178     }
179 
180     /**
181      * @see org.jdom2.Element#getAttributeValue(java.lang.String,org.jdom2.Namespace,java.lang.String)
182      * @param name The name of the attribute.
183      * @param ns The {@link Namespace}
184      * @param def the default value.
185      * @return The value of the attribute.
186      */
187     public String getAttributeValue(String name, Namespace ns, String def) {
188         return element.getAttributeValue(name, ns, def);
189     }
190 
191     /**
192      * @see org.jdom2.Element#getAttributeValue(java.lang.String,org.jdom2.Namespace)
193      * @param name The name of the attribute.
194      * @param ns The {@link Namespace}
195      * @return The value of the attribute.
196      */
197     public String getAttributeValue(String name, Namespace ns) {
198         return element.getAttributeValue(name, ns);
199     }
200 
201     /**
202      * @see org.jdom2.Element#getAttributeValue(java.lang.String,java.lang.String)
203      * @param name The name of the attribute.
204      * @param def the default value.
205      * @return The value of the attribute.
206      */
207     public String getAttributeValue(String name, String def) {
208         return element.getAttributeValue(name, def);
209     }
210 
211     /**
212      * @see org.jdom2.Element#getAttributeValue(java.lang.String)
213      * @param name The name of the attribute.
214      * @return The value of the attribute.
215      */
216     public String getAttributeValue(String name) {
217         return element.getAttributeValue(name);
218     }
219 
220     /**
221      * @param name The name of the child.
222      * @param ns {@link Namespace}
223      * @return {@link Element}
224      * @see org.jdom2.Element#getChild(java.lang.String,org.jdom2.Namespace)
225      */
226     public Element getChild(String name, Namespace ns) {
227         return element.getChild(name, ns);
228     }
229 
230     /**
231      * @param name The name of the child.
232      * @return {@link Element}
233      * @see org.jdom2.Element#getChild(java.lang.String)
234      */
235     public Element getChild(String name) {
236         return element.getChild(name);
237     }
238 
239     /**
240      * @return list of {@link Element}
241      * @see org.jdom2.Element#getChildren()
242      */
243     public List getChildren() {
244         return element.getChildren();
245     }
246 
247     /**
248      * @param name The name of the child.
249      * @param ns {@link Namespace}
250      * @return list {@link Element}
251      * @see org.jdom2.Element#getChildren(java.lang.String,org.jdom2.Namespace)
252      */
253     public List getChildren(String name, Namespace ns) {
254         return element.getChildren(name, ns);
255     }
256 
257     /**
258      * @param name The name.
259      * @return list {@link Element}
260      * @see org.jdom2.Element#getChildren(java.lang.String)
261      */
262     public List getChildren(String name) {
263         return element.getChildren(name);
264     }
265 
266     /**
267      * @param name The name of the child.
268      * @param ns {@link Namespace}
269      * @return the child text.
270      * @see org.jdom2.Element#getChildText(java.lang.String,org.jdom2.Namespace)
271      */
272     public String getChildText(String name, Namespace ns) {
273         return element.getChildText(name, ns);
274     }
275 
276     /**
277      * @param name The name of the child.
278      * @return the child text.
279      * @see org.jdom2.Element#getChildText(java.lang.String)
280      */
281     public String getChildText(String name) {
282         return element.getChildText(name);
283     }
284 
285     /**
286      * @param name The name of the child.
287      * @param ns {@link Namespace}
288      * @return the child text.
289      * @see org.jdom2.Element#getChildTextNormalize(java.lang.String,org.jdom2.Namespace)
290      */
291     public String getChildTextNormalize(String name, Namespace ns) {
292         return element.getChildTextNormalize(name, ns);
293     }
294 
295     /**
296      * @param name The name of the child.
297      * @return the child text.
298      * @see org.jdom2.Element#getChildTextNormalize(java.lang.String)
299      */
300     public String getChildTextNormalize(String name) {
301         return element.getChildTextNormalize(name);
302     }
303 
304     /**
305      * @param name The name of the child.
306      * @param ns {@link Namespace}
307      * @return the child text.
308      * @see org.jdom2.Element#getChildTextTrim(java.lang.String,org.jdom2.Namespace)
309      */
310     public String getChildTextTrim(String name, Namespace ns) {
311         return element.getChildTextTrim(name, ns);
312     }
313 
314     /**
315      * @param name The name of the child.
316      * @return the child text.
317      * @see org.jdom2.Element#getChildTextTrim(java.lang.String)
318      */
319     public String getChildTextTrim(String name) {
320         return element.getChildTextTrim(name);
321     }
322 
323     /**
324      * @see org.jdom2.Element#getContent()
325      * @return list of content.
326      */
327     public List getContent() {
328         return element.getContent();
329     }
330 
331     /**
332      * @param filter {@link Filter}
333      * @return list of content.
334      * @see org.jdom2.Element#getContent(org.jdom2.filter.Filter)
335      */
336     public List getContent(Filter filter) {
337         return element.getContent(filter);
338     }
339 
340     /**
341      * @param index The index.
342      * @return the content.
343      * @see org.jdom2.Element#getContent(int)
344      */
345     public Content getContent(int index) {
346         return element.getContent(index);
347     }
348 
349     /**
350      * @return The content size.
351      * @see org.jdom2.Element#getContentSize()
352      */
353     public int getContentSize() {
354         return element.getContentSize();
355     }
356 
357     /**
358      * @return {@link Iterator} of descendants.
359      * @see org.jdom2.Element#getDescendants()
360      */
361     public Iterator getDescendants() {
362         return element.getDescendants();
363     }
364 
365     /**
366      * @param filter {@link Filter}
367      * @return {@link Iterator} of descendants.
368      * @see org.jdom2.Element#getDescendants(org.jdom2.filter.Filter)
369      */
370     public Iterator getDescendants(Filter filter) {
371         return element.getDescendants(filter);
372     }
373 
374     /**
375      * @return the document.
376      * @see org.jdom2.Content#getDocument()
377      */
378     public Document getDocument() {
379         return element.getDocument();
380     }
381 
382     /**
383      * @return The name of the element.
384      * @see org.jdom2.Element#getName()
385      */
386     public String getName() {
387         return element.getName();
388     }
389 
390     /**
391      * @return {@link Namespace}
392      * @see org.jdom2.Element#getNamespace()
393      */
394     public Namespace getNamespace() {
395         return element.getNamespace();
396     }
397 
398     /**
399      * @param prefix The prefix.
400      * @return {@link Namespace}
401      * @see org.jdom2.Element#getNamespace(java.lang.String)
402      */
403     public Namespace getNamespace(String prefix) {
404         return element.getNamespace(prefix);
405     }
406 
407     /**
408      * @return the namespace prefix.
409      * @see org.jdom2.Element#getNamespacePrefix()
410      */
411     public String getNamespacePrefix() {
412         return element.getNamespacePrefix();
413     }
414 
415     /**
416      * @return the namespace URI.
417      * @see org.jdom2.Element#getNamespaceURI()
418      */
419     public String getNamespaceURI() {
420         return element.getNamespaceURI();
421     }
422 
423     /**
424      * @return The parent.
425      * @see org.jdom2.Content#getParent()
426      */
427     public Parent getParent() {
428         return element.getParent();
429     }
430 
431     /**
432      * @return the parent {@link Element}
433      * @see org.jdom2.Content#getParentElement()
434      */
435     public Element getParentElement() {
436         return element.getParentElement();
437     }
438 
439     /**
440      * @return The qualified name.
441      * @see org.jdom2.Element#getQualifiedName()
442      */
443     public String getQualifiedName() {
444         return element.getQualifiedName();
445     }
446 
447     /**
448      * @return The text.
449      * @see org.jdom2.Element#getText()
450      */
451     public String getText() {
452         return element.getText();
453     }
454 
455     /**
456      * @return the normalized text.
457      * @see org.jdom2.Element#getTextNormalize()
458      */
459     public String getTextNormalize() {
460         return element.getTextNormalize();
461     }
462 
463     /**
464      * @return the trimmed text.
465      * @see org.jdom2.Element#getTextTrim()
466      */
467     public String getTextTrim() {
468         return element.getTextTrim();
469     }
470 
471     /**
472      * @return the element value.
473      * @see org.jdom2.Element#getValue()
474      */
475     public String getValue() {
476         return element.getValue();
477     }
478 
479     /**
480      * @see java.lang.Object#hashCode()
481      */
482     public int hashCode() {
483         return element.hashCode();
484     }
485 
486     /**
487      * @param child The child.
488      * @return the index.
489      * @see org.jdom2.Element#indexOf(org.jdom2.Content)
490      */
491     public int indexOf(Content child) {
492         return element.indexOf(child);
493     }
494 
495     /**
496      * @see org.jdom2.Element#isAncestor(org.jdom2.Element)
497      * @param element {@link Element}.
498      * @return true/false.
499      */
500     public boolean isAncestor(Element element) {
501         return element.isAncestor(element);
502     }
503 
504     /**
505      * @see org.jdom2.Element#isRootElement()
506      * @return true/false.
507      */
508     public boolean isRootElement() {
509         return element.isRootElement();
510     }
511 
512     /**
513      * @see org.jdom2.Element#removeAttribute(org.jdom2.Attribute)
514      * @param attribute {@link Attribute}
515      * @return true/false.
516      */
517     public boolean removeAttribute(Attribute attribute) {
518         return element.removeAttribute(attribute);
519     }
520 
521     /**
522      * @see org.jdom2.Element#removeAttribute(java.lang.String,org.jdom2.Namespace)
523      * @param name The name of the attribute.
524      * @param ns The {@link Namespace}
525      * @return true/false.
526      */
527     public boolean removeAttribute(String name, Namespace ns) {
528         return element.removeAttribute(name, ns);
529     }
530 
531     /**
532      * @see org.jdom2.Element#removeAttribute(java.lang.String)
533      * @param name The mame of the attribute.
534      * @return true/false.
535      */
536     public boolean removeAttribute(String name) {
537         return element.removeAttribute(name);
538     }
539 
540     /**
541      * @see org.jdom2.Element#removeChild(java.lang.String,org.jdom2.Namespace)
542      * @param name The name of the child.
543      * @param ns {@link Namespace}
544      * @return true/false.
545      */
546     public boolean removeChild(String name, Namespace ns) {
547         return element.removeChild(name, ns);
548     }
549 
550     /**
551      * @see org.jdom2.Element#removeChild(java.lang.String)
552      * @param name The name of the child.
553      * @return true/false.
554      */
555     public boolean removeChild(String name) {
556         return element.removeChild(name);
557     }
558 
559     /**
560      * @see org.jdom2.Element#removeChildren(java.lang.String,org.jdom2.Namespace)
561      * @param name The name of the child.
562      * @param ns {@link Namespace}
563      * @return true/false.
564      */
565     public boolean removeChildren(String name, Namespace ns) {
566         return element.removeChildren(name, ns);
567     }
568 
569     /**
570      * @see org.jdom2.Element#removeChildren(java.lang.String)
571      * @param name name of the child.
572      * @return true/false.
573      */
574     public boolean removeChildren(String name) {
575         return element.removeChildren(name);
576     }
577 
578     /**
579      * @see org.jdom2.Element#removeContent()
580      * @return list of elements.
581      */
582     public List removeContent() {
583         return element.removeContent();
584     }
585 
586     /**
587      * @see org.jdom2.Element#removeContent(org.jdom2.Content)
588      * @param child {@link Content}
589      * @return true/false.
590      */
591     public boolean removeContent(Content child) {
592         return element.removeContent(child);
593     }
594 
595     /**
596      * @see org.jdom2.Element#removeContent(org.jdom2.filter.Filter)
597      * @param filter {@link Filter}.
598      * @return list of elements.
599      */
600     public List removeContent(Filter filter) {
601         return element.removeContent(filter);
602     }
603 
604     /**
605      * @see org.jdom2.Element#removeContent(int)
606      * @param index The index.
607      * @return {@link Content}
608      */
609     public Content removeContent(int index) {
610         return element.removeContent(index);
611     }
612 
613     /**
614      * @see org.jdom2.Element#removeNamespaceDeclaration(org.jdom2.Namespace)
615      * @param additionalNamespace {@link Namespace}.
616      */
617     public void removeNamespaceDeclaration(Namespace additionalNamespace) {
618         element.removeNamespaceDeclaration(additionalNamespace);
619     }
620 
621     /**
622      * @param attribute {@link Attribute}
623      * @return {@link Element}.
624      * @see org.jdom2.Element#setAttribute(org.jdom2.Attribute)
625      */
626     public Element setAttribute(Attribute attribute) {
627         return element.setAttribute(attribute);
628     }
629 
630     /**
631      * @see org.jdom2.Element#setAttribute(java.lang.String,java.lang.String,org.jdom2.Namespace)
632      * @param name name of the attribute.
633      * @param value The value of the attribute.
634      * @param ns {@link Namespace}.
635      * @return {@link Element}
636      */
637     public Element setAttribute(String name, String value, Namespace ns) {
638         return element.setAttribute(name, value, ns);
639     }
640 
641     /**
642      * @param name name of the attribute.
643      * @param value The value of the attribute.
644      * @return {@link Element}
645      * @see org.jdom2.Element#setAttribute(java.lang.String,java.lang.String)
646      */
647     public Element setAttribute(String name, String value) {
648         return element.setAttribute(name, value);
649     }
650 
651     /**
652      * @param newAttributes list of new attributes.
653      * @return {@link Element}
654      */
655     public Element setAttributes(List newAttributes) {
656         return element.setAttributes(newAttributes);
657     }
658 
659     /**
660      * @param newContent {@link Collection}
661      * @return {@link Element}
662      * @see org.jdom2.Element#setContent(java.util.Collection)
663      */
664     public Element setContent(Collection newContent) {
665         return element.setContent(newContent);
666     }
667 
668     /**
669      * @param child {@link Content}
670      * @return {@link Element}
671      * @see org.jdom2.Element#setContent(org.jdom2.Content)
672      */
673     public Element setContent(Content child) {
674         return element.setContent(child);
675     }
676 
677     /**
678      * @param index The index.
679      * @param collection {@link Collection}
680      * @return {@link Parent}
681      * @see org.jdom2.Element#setContent(int,java.util.Collection)
682      */
683     public Parent setContent(int index, Collection collection) {
684         return element.setContent(index, collection);
685     }
686 
687     /**
688      * @param index index.
689      * @param child {@link Content}
690      * @return {@link Element}
691      * @see org.jdom2.Element#setContent(int,org.jdom2.Content)
692      */
693     public Element setContent(int index, Content child) {
694         return element.setContent(index, child);
695     }
696 
697     /**
698      * @param name The name of the element.
699      * @return {@link Element}
700      * @see org.jdom2.Element#setName(java.lang.String)
701      */
702     public Element setName(String name) {
703         return element.setName(name);
704     }
705 
706     /**
707      * @param namespace {@link Namespace}
708      * @see org.jdom2.Element#setNamespace(org.jdom2.Namespace)
709      * @return {@link Element}
710      */
711     public Element setNamespace(Namespace namespace) {
712         return element.setNamespace(namespace);
713     }
714 
715     /**
716      * @see org.jdom2.Element#setText(java.lang.String)
717      * @param text The text to be set.
718      * @return {@link Element}
719      */
720     public Element setText(String text) {
721         return element.setText(text);
722     }
723 
724     /**
725      * {@link org.jdom2.Element#toString()}
726      * {@inheritDoc}
727      */
728     public String toString() {
729         return element.toString();
730     }
731 
732     /**
733      * Returns the wrapped up JDom {@link Element} instance.
734      * {@inheritDoc}
735      */
736     public Element getElement() {
737         return this.element;
738     }
739 
740     /**
741      * Sub classes should override if they wish to provide a different
742      * combination of composite keys for determining conflicts.
743      * @param defaultList the default list.
744      * @return the default list.
745      */
746     protected List getElementNamesForConflictResolution(List defaultList) {
747         return defaultList;
748     }
749 
750     /**
751      * Returns the default {@link MergeStrategy} instance.
752      * @return {@link MergeStrategy}
753      */
754     protected MergeStrategy getDefaultMergeStrategy() {
755         return DEFAULT_MERGE_STRATEGY;
756     }
757 }