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.jdom.Attribute;
34  import org.jdom.Content;
35  import org.jdom.Document;
36  import org.jdom.Element;
37  import org.jdom.Namespace;
38  import org.jdom.Parent;
39  import org.jdom.filter.Filter;
40  
41  /**
42   * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
43   * @version $Id$
44   */
45  public abstract class AbstractMergeableSupport
46      implements Mergeable
47  {
48      /**
49       * Wrapped JDOM element.
50       */
51      protected Element element;
52  
53      /**
54       * The default merging strategy used.
55       */
56      private static final MergeStrategy DEFAULT_MERGE_STRATEGY = MergeStrategies.DEEP;
57  
58      public AbstractMergeableSupport( Element element )
59      {
60          this.element = element;
61      }
62  
63      public abstract void merge( Mergeable me )
64          throws MergeException;
65  
66      /**
67       * Determines if the passed in {@link Mergeable} was of same type as this
68       * class.
69       *
70       * @param me {@link Mergeable} instance to test.
71       * @return <code>true</code> if the passed in Mergeable can be merged with
72       *         the current Mergeable.
73       */
74      protected abstract boolean isExpectedElementType( Mergeable me );
75  
76      // ----------------------------------------------------------------------
77      // Methods delegated on wrapped JDOM element.
78      // ----------------------------------------------------------------------
79  
80      public Element addContent( Collection collection )
81      {
82          return element.addContent( collection );
83      }
84  
85      public Element addContent( Content child )
86      {
87          return element.addContent( child );
88      }
89  
90      public Element addContent( int index, Collection c )
91      {
92          return element.addContent( index, c );
93      }
94  
95      public Element addContent( int index, Content child )
96      {
97          return element.addContent( index, child );
98      }
99  
100     public Element addContent( String str )
101     {
102         return element.addContent( str );
103     }
104 
105     public void addNamespaceDeclaration( Namespace additional )
106     {
107         element.addNamespaceDeclaration( additional );
108     }
109 
110     public Object clone()
111     {
112         return element.clone();
113     }
114 
115     public List cloneContent()
116     {
117         return element.cloneContent();
118     }
119 
120     public Content detach()
121     {
122         return element.detach();
123     }
124 
125     public boolean equals( Object obj )
126     {
127         return element.equals( obj );
128     }
129 
130     public List getAdditionalNamespaces()
131     {
132         return element.getAdditionalNamespaces();
133     }
134 
135     public Attribute getAttribute( String name, Namespace ns )
136     {
137         return element.getAttribute( name, ns );
138     }
139 
140     public Attribute getAttribute( String name )
141     {
142         return element.getAttribute( name );
143     }
144 
145     public List getAttributes()
146     {
147         return element.getAttributes();
148     }
149 
150     /**
151      * @see org.jdom.Element#getAttributeValue(java.lang.String,org.jdom.Namespace,java.lang.String)
152      */
153     public String getAttributeValue( String name, Namespace ns, String def )
154     {
155         return element.getAttributeValue( name, ns, def );
156     }
157 
158     /**
159      * @see org.jdom.Element#getAttributeValue(java.lang.String,org.jdom.Namespace)
160      */
161     public String getAttributeValue( String name, Namespace ns )
162     {
163         return element.getAttributeValue( name, ns );
164     }
165 
166     /**
167      * @see org.jdom.Element#getAttributeValue(java.lang.String,java.lang.String)
168      */
169     public String getAttributeValue( String name, String def )
170     {
171         return element.getAttributeValue( name, def );
172     }
173 
174     /**
175      * @see org.jdom.Element#getAttributeValue(java.lang.String)
176      */
177     public String getAttributeValue( String name )
178     {
179         return element.getAttributeValue( name );
180     }
181 
182     /**
183      * @return
184      * @see org.jdom.Element#getChild(java.lang.String,org.jdom.Namespace)
185      */
186     public Element getChild( String name, Namespace ns )
187     {
188         return element.getChild( name, ns );
189     }
190 
191     /**
192      * @see org.jdom.Element#getChild(java.lang.String)
193      */
194     public Element getChild( String name )
195     {
196         return element.getChild( name );
197     }
198 
199     /**
200      * @see org.jdom.Element#getChildren()
201      */
202     public List getChildren()
203     {
204         return element.getChildren();
205     }
206 
207     /**
208      * @see org.jdom.Element#getChildren(java.lang.String,org.jdom.Namespace)
209      */
210     public List getChildren( String name, Namespace ns )
211     {
212         return element.getChildren( name, ns );
213     }
214 
215     /**
216      * @see org.jdom.Element#getChildren(java.lang.String)
217      */
218     public List getChildren( String name )
219     {
220         return element.getChildren( name );
221     }
222 
223     /**
224      * @see org.jdom.Element#getChildText(java.lang.String,org.jdom.Namespace)
225      */
226     public String getChildText( String name, Namespace ns )
227     {
228         return element.getChildText( name, ns );
229     }
230 
231     /**
232      * @see org.jdom.Element#getChildText(java.lang.String)
233      */
234     public String getChildText( String name )
235     {
236         return element.getChildText( name );
237     }
238 
239     /**
240      * @see org.jdom.Element#getChildTextNormalize(java.lang.String,org.jdom.Namespace)
241      */
242     public String getChildTextNormalize( String name, Namespace ns )
243     {
244         return element.getChildTextNormalize( name, ns );
245     }
246 
247     /**
248      * @see org.jdom.Element#getChildTextNormalize(java.lang.String)
249      */
250     public String getChildTextNormalize( String name )
251     {
252         return element.getChildTextNormalize( name );
253     }
254 
255     /**
256      * @see org.jdom.Element#getChildTextTrim(java.lang.String,org.jdom.Namespace)
257      */
258     public String getChildTextTrim( String name, Namespace ns )
259     {
260         return element.getChildTextTrim( name, ns );
261     }
262 
263     /**
264      * @see org.jdom.Element#getChildTextTrim(java.lang.String)
265      */
266     public String getChildTextTrim( String name )
267     {
268         return element.getChildTextTrim( name );
269     }
270 
271     /**
272      * @see org.jdom.Element#getContent()
273      */
274     public List getContent()
275     {
276         return element.getContent();
277     }
278 
279     /**
280      * @see org.jdom.Element#getContent(org.jdom.filter.Filter)
281      */
282     public List getContent( Filter filter )
283     {
284         return element.getContent( filter );
285     }
286 
287     /**
288      * @see org.jdom.Element#getContent(int)
289      */
290     public Content getContent( int index )
291     {
292         return element.getContent( index );
293     }
294 
295     /**
296      * @return
297      * @see org.jdom.Element#getContentSize()
298      */
299     public int getContentSize()
300     {
301         return element.getContentSize();
302     }
303 
304     /**
305      * @see org.jdom.Element#getDescendants()
306      */
307     public Iterator getDescendants()
308     {
309         return element.getDescendants();
310     }
311 
312     /**
313      * @see org.jdom.Element#getDescendants(org.jdom.filter.Filter)
314      */
315     public Iterator getDescendants( Filter filter )
316     {
317         return element.getDescendants( filter );
318     }
319 
320     /**
321      * @see org.jdom.Content#getDocument()
322      */
323     public Document getDocument()
324     {
325         return element.getDocument();
326     }
327 
328     /**
329      * @see org.jdom.Element#getName()
330      */
331     public String getName()
332     {
333         return element.getName();
334     }
335 
336     /**
337      * @see org.jdom.Element#getNamespace()
338      */
339     public Namespace getNamespace()
340     {
341         return element.getNamespace();
342     }
343 
344     /**
345      * @see org.jdom.Element#getNamespace(java.lang.String)
346      */
347     public Namespace getNamespace( String prefix )
348     {
349         return element.getNamespace( prefix );
350     }
351 
352     /**
353      * @see org.jdom.Element#getNamespacePrefix()
354      */
355     public String getNamespacePrefix()
356     {
357         return element.getNamespacePrefix();
358     }
359 
360     /**
361      * @see org.jdom.Element#getNamespaceURI()
362      */
363     public String getNamespaceURI()
364     {
365         return element.getNamespaceURI();
366     }
367 
368     /**
369      * @see org.jdom.Content#getParent()
370      */
371     public Parent getParent()
372     {
373         return element.getParent();
374     }
375 
376     /**
377      * @see org.jdom.Content#getParentElement()
378      */
379     public Element getParentElement()
380     {
381         return element.getParentElement();
382     }
383 
384     /**
385      * @see org.jdom.Element#getQualifiedName()
386      */
387     public String getQualifiedName()
388     {
389         return element.getQualifiedName();
390     }
391 
392     /**
393      * @see org.jdom.Element#getText()
394      */
395     public String getText()
396     {
397         return element.getText();
398     }
399 
400     /**
401      * @see org.jdom.Element#getTextNormalize()
402      */
403     public String getTextNormalize()
404     {
405         return element.getTextNormalize();
406     }
407 
408     /**
409      * @see org.jdom.Element#getTextTrim()
410      */
411     public String getTextTrim()
412     {
413         return element.getTextTrim();
414     }
415 
416     /**
417      * @see org.jdom.Element#getValue()
418      */
419     public String getValue()
420     {
421         return element.getValue();
422     }
423 
424     /**
425      * @see java.lang.Object#hashCode()
426      */
427     public int hashCode()
428     {
429         return element.hashCode();
430     }
431 
432     /**
433      * @see org.jdom.Element#indexOf(org.jdom.Content)
434      */
435     public int indexOf( Content child )
436     {
437         return element.indexOf( child );
438     }
439 
440     /**
441      * @see org.jdom.Element#isAncestor(org.jdom.Element)
442      */
443     public boolean isAncestor( Element element )
444     {
445         return element.isAncestor( element );
446     }
447 
448     /**
449      * @see org.jdom.Element#isRootElement()
450      */
451     public boolean isRootElement()
452     {
453         return element.isRootElement();
454     }
455 
456     /**
457      * @see org.jdom.Element#removeAttribute(org.jdom.Attribute)
458      */
459     public boolean removeAttribute( Attribute attribute )
460     {
461         return element.removeAttribute( attribute );
462     }
463 
464     /**
465      * @see org.jdom.Element#removeAttribute(java.lang.String,org.jdom.Namespace)
466      */
467     public boolean removeAttribute( String name, Namespace ns )
468     {
469         return element.removeAttribute( name, ns );
470     }
471 
472     /**
473      * @see org.jdom.Element#removeAttribute(java.lang.String)
474      */
475     public boolean removeAttribute( String name )
476     {
477         return element.removeAttribute( name );
478     }
479 
480     /**
481      * @see org.jdom.Element#removeChild(java.lang.String,org.jdom.Namespace)
482      */
483     public boolean removeChild( String name, Namespace ns )
484     {
485         return element.removeChild( name, ns );
486     }
487 
488     /**
489      * @see org.jdom.Element#removeChild(java.lang.String)
490      */
491     public boolean removeChild( String name )
492     {
493         return element.removeChild( name );
494     }
495 
496     /**
497      * @see org.jdom.Element#removeChildren(java.lang.String,org.jdom.Namespace)
498      */
499     public boolean removeChildren( String name, Namespace ns )
500     {
501         return element.removeChildren( name, ns );
502     }
503 
504     /**
505      * @see org.jdom.Element#removeChildren(java.lang.String)
506      */
507     public boolean removeChildren( String name )
508     {
509         return element.removeChildren( name );
510     }
511 
512     /**
513      * @see org.jdom.Element#removeContent()
514      */
515     public List removeContent()
516     {
517         return element.removeContent();
518     }
519 
520     /**
521      * @see org.jdom.Element#removeContent(org.jdom.Content)
522      */
523     public boolean removeContent( Content child )
524     {
525         return element.removeContent( child );
526     }
527 
528     /**
529      * @see org.jdom.Element#removeContent(org.jdom.filter.Filter)
530      */
531     public List removeContent( Filter filter )
532     {
533         return element.removeContent( filter );
534     }
535 
536     /**
537      * @see org.jdom.Element#removeContent(int)
538      */
539     public Content removeContent( int index )
540     {
541         return element.removeContent( index );
542     }
543 
544     /**
545      * @see org.jdom.Element#removeNamespaceDeclaration(org.jdom.Namespace)
546      */
547     public void removeNamespaceDeclaration( Namespace additionalNamespace )
548     {
549         element.removeNamespaceDeclaration( additionalNamespace );
550     }
551 
552     /**
553      * @see org.jdom.Element#setAttribute(org.jdom.Attribute)
554      */
555     public Element setAttribute( Attribute attribute )
556     {
557         return element.setAttribute( attribute );
558     }
559 
560     /**
561      * @see org.jdom.Element#setAttribute(java.lang.String,java.lang.String,org.jdom.Namespace)
562      */
563     public Element setAttribute( String name, String value, Namespace ns )
564     {
565         return element.setAttribute( name, value, ns );
566     }
567 
568     /**
569      * @see org.jdom.Element#setAttribute(java.lang.String,java.lang.String)
570      */
571     public Element setAttribute( String name, String value )
572     {
573         return element.setAttribute( name, value );
574     }
575 
576     /**
577      * @see org.jdom.Element#setAttributes(java.util.List)
578      */
579     public Element setAttributes( List newAttributes )
580     {
581         return element.setAttributes( newAttributes );
582     }
583 
584     /**
585      * @see org.jdom.Element#setContent(java.util.Collection)
586      */
587     public Element setContent( Collection newContent )
588     {
589         return element.setContent( newContent );
590     }
591 
592     /**
593      * @see org.jdom.Element#setContent(org.jdom.Content)
594      */
595     public Element setContent( Content child )
596     {
597         return element.setContent( child );
598     }
599 
600     /**
601      * @see org.jdom.Element#setContent(int,java.util.Collection)
602      */
603     public Parent setContent( int index, Collection collection )
604     {
605         return element.setContent( index, collection );
606     }
607 
608     /**
609      * @see org.jdom.Element#setContent(int,org.jdom.Content)
610      */
611     public Element setContent( int index, Content child )
612     {
613         return element.setContent( index, child );
614     }
615 
616     /**
617      * @see org.jdom.Element#setName(java.lang.String)
618      */
619     public Element setName( String name )
620     {
621         return element.setName( name );
622     }
623 
624     /**
625      * @see org.jdom.Element#setNamespace(org.jdom.Namespace)
626      */
627     public Element setNamespace( Namespace namespace )
628     {
629         return element.setNamespace( namespace );
630     }
631 
632     /**
633      * @see org.jdom.Element#setText(java.lang.String)
634      */
635     public Element setText( String text )
636     {
637         return element.setText( text );
638     }
639 
640     /**
641      * @see org.jdom.Element#toString()
642      */
643     public String toString()
644     {
645         return element.toString();
646     }
647 
648     /**
649      * Returns the wrapped up JDom {@link Element} instance.
650      */
651     public Element getElement()
652     {
653         return this.element;
654     }
655 
656     /**
657      * Sub classes should override if they wish to provide a different
658      * combination of composite keys for determining conflicts.
659      */
660     protected List getElementNamesForConflictResolution( List defaultList )
661     {
662         return defaultList;
663     }
664 
665     /**
666      * Returns the default {@link MergeStrategy} instance.
667      */
668     protected MergeStrategy getDefaultMergeStrategy()
669     {
670         return DEFAULT_MERGE_STRATEGY;
671     }
672 
673 }