View Javadoc
1   package org.apache.maven.plugins.shade.pom;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Objects;
33  
34  import org.apache.maven.model.ActivationFile;
35  import org.apache.maven.model.ActivationOS;
36  import org.apache.maven.model.ActivationProperty;
37  import org.apache.maven.model.Build;
38  import org.apache.maven.model.BuildBase;
39  import org.apache.maven.model.CiManagement;
40  import org.apache.maven.model.ConfigurationContainer;
41  import org.apache.maven.model.Contributor;
42  import org.apache.maven.model.Dependency;
43  import org.apache.maven.model.DependencyManagement;
44  import org.apache.maven.model.DeploymentRepository;
45  import org.apache.maven.model.Developer;
46  import org.apache.maven.model.DistributionManagement;
47  import org.apache.maven.model.Exclusion;
48  import org.apache.maven.model.Extension;
49  import org.apache.maven.model.FileSet;
50  import org.apache.maven.model.IssueManagement;
51  import org.apache.maven.model.License;
52  import org.apache.maven.model.MailingList;
53  import org.apache.maven.model.Model;
54  import org.apache.maven.model.ModelBase;
55  import org.apache.maven.model.Notifier;
56  import org.apache.maven.model.Organization;
57  import org.apache.maven.model.Parent;
58  import org.apache.maven.model.PatternSet;
59  import org.apache.maven.model.Plugin;
60  import org.apache.maven.model.PluginConfiguration;
61  import org.apache.maven.model.PluginContainer;
62  import org.apache.maven.model.PluginExecution;
63  import org.apache.maven.model.PluginManagement;
64  import org.apache.maven.model.Prerequisites;
65  import org.apache.maven.model.Profile;
66  import org.apache.maven.model.Relocation;
67  import org.apache.maven.model.ReportPlugin;
68  import org.apache.maven.model.ReportSet;
69  import org.apache.maven.model.Reporting;
70  import org.apache.maven.model.Repository;
71  import org.apache.maven.model.RepositoryBase;
72  import org.apache.maven.model.RepositoryPolicy;
73  import org.apache.maven.model.Resource;
74  import org.apache.maven.model.Scm;
75  import org.apache.maven.model.Site;
76  import org.codehaus.plexus.util.xml.Xpp3Dom;
77  import org.jdom2.Content;
78  import org.jdom2.DefaultJDOMFactory;
79  import org.jdom2.Document;
80  import org.jdom2.Element;
81  import org.jdom2.Text;
82  import org.jdom2.output.Format;
83  import org.jdom2.output.XMLOutputter;
84  
85  /**
86   * Class MavenJDOMWriter.
87   *
88   */
89  public class MavenJDOMWriter
90  {
91      /**
92       * Field factory
93       */
94      private final DefaultJDOMFactory factory;
95  
96      /**
97       * Field lineSeparator
98       */
99      private final String lineSeparator;
100 
101     public MavenJDOMWriter()
102     {
103         this( "\n" );
104     }
105 
106     public MavenJDOMWriter( final String lineSeparator )
107     {
108         this.factory = new DefaultJDOMFactory();
109         this.lineSeparator = Objects.requireNonNull( lineSeparator );
110     }
111 
112     /**
113      * Method findAndReplaceProperties
114      *
115      * @param counter {@link Counter}
116      * @param props {@link Map}
117      * @param name The name.
118      * @param parent {@link Element}
119      * @return {@link Element}
120      */
121     protected Element findAndReplaceProperties( Counter counter, Element parent, String name, Map props )
122     {
123         Map<String, String> properties = props;
124         boolean shouldExist = properties != null && !properties.isEmpty();
125         Element element = updateElement( counter, parent, name, shouldExist );
126         if ( shouldExist )
127         {
128             Counter innerCounter = new Counter( counter.getDepth() + 1 );
129             for ( Map.Entry<String, String> entry : properties.entrySet() )
130             {
131                 String key = entry.getKey();
132                 findAndReplaceSimpleElement( innerCounter, element, key, entry.getValue(), null );
133             }
134             List<String> lst = new ArrayList<>( properties.keySet() );
135             Iterator<Element> it = element.getChildren().iterator();
136             while ( it.hasNext() )
137             {
138                 Element elem = it.next();
139                 String key = elem.getName();
140                 if ( !lst.contains( key ) )
141                 {
142                     it.remove();
143                 }
144             }
145         }
146         return element;
147     }
148 
149     /**
150      * Method findAndReplaceSimpleElement
151      *
152      * @param counter {@link Counter}
153      * @param defaultValue The default value.
154      * @param text The text.
155      * @param name The name.
156      * @param parent The parent.
157      * @return {@link Element}
158      */
159     protected Element findAndReplaceSimpleElement( Counter counter, Element parent, String name, String text,
160                                                    String defaultValue )
161     {
162         if ( defaultValue != null && text != null && defaultValue.equals( text ) )
163         {
164             Element element = parent.getChild( name, parent.getNamespace() );
165             // if exist and is default value or if doesn't exist.. just keep the way it is..
166             if ( element == null || defaultValue.equals( element.getText() ) )
167             {
168                 return element;
169             }
170         }
171         boolean shouldExist = text != null && text.trim().length() > 0;
172         Element element = updateElement( counter, parent, name, shouldExist );
173         if ( shouldExist )
174         {
175             element.setText( text );
176         }
177         return element;
178     }
179 
180     /**
181      * Method findAndReplaceSimpleLists
182      *
183      * @param counter {@link Counter}
184      * @param childName The childName
185      * @param parentName The parentName
186      * @param list The list of elements.
187      * @param parent The parent.
188      * @return {@link Element}
189      */
190     protected Element findAndReplaceSimpleLists( Counter counter, Element parent, Collection<String> list,
191                                                  String parentName, String childName )
192     {
193         boolean shouldExist = list != null && list.size() > 0;
194         Element element = updateElement( counter, parent, parentName, shouldExist );
195         if ( shouldExist )
196         {
197             Iterator<Element> elIt = element.getChildren( childName, element.getNamespace() ).iterator();
198             if ( !elIt.hasNext() )
199             {
200                 elIt = null;
201             }
202             Counter innerCount = new Counter( counter.getDepth() + 1 );
203             for ( String value : list )
204             {
205                 Element el;
206                 if ( elIt != null && elIt.hasNext() )
207                 {
208                     el = elIt.next();
209                     if ( !elIt.hasNext() )
210                     {
211                         elIt = null;
212                     }
213                 }
214                 else
215                 {
216                     el = factory.element( childName, element.getNamespace() );
217                     insertAtPreferredLocation( element, el, innerCount );
218                 }
219                 el.setText( value );
220                 innerCount.increaseCount();
221             }
222             if ( elIt != null )
223             {
224                 while ( elIt.hasNext() )
225                 {
226                     elIt.next();
227                     elIt.remove();
228                 }
229             }
230         }
231         return element;
232     }
233 
234     /**
235      * Method findAndReplaceXpp3DOM
236      *
237      * @param counter {@link Counter}
238      * @param dom {@link Xpp3Dom}
239      * @param name The name.
240      * @param parent The parent.
241      * @return {@link Element}
242      */
243     protected Element findAndReplaceXpp3DOM( Counter counter, Element parent, String name, Xpp3Dom dom )
244     {
245         boolean shouldExist = dom != null && ( dom.getChildCount() > 0 || dom.getValue() != null );
246         Element element = updateElement( counter, parent, name, shouldExist );
247         if ( shouldExist )
248         {
249             replaceXpp3DOM( element, dom, new Counter( counter.getDepth() + 1 ) );
250         }
251         return element;
252     }
253 
254     /**
255      * Method insertAtPreferredLocation
256      *
257      * @param parent The parent.
258      * @param counter {@link Counter}
259      * @param child {@link Element}
260      */
261     protected void insertAtPreferredLocation( Element parent, Element child, Counter counter )
262     {
263         int contentIndex = 0;
264         int elementCounter = 0;
265         Iterator<Content> it = parent.getContent().iterator();
266         Text lastText = null;
267         int offset = 0;
268         while ( it.hasNext() && elementCounter <= counter.getCurrentIndex() )
269         {
270             Object next = it.next();
271             offset = offset + 1;
272             if ( next instanceof Element )
273             {
274                 elementCounter = elementCounter + 1;
275                 contentIndex = contentIndex + offset;
276                 offset = 0;
277             }
278             if ( next instanceof Text && it.hasNext() )
279             {
280                 lastText = (Text) next;
281             }
282         }
283         if ( lastText != null && lastText.getTextTrim().length() == 0 )
284         {
285             lastText = lastText.clone();
286         }
287         else
288         {
289             StringBuilder starter = new StringBuilder( lineSeparator );
290             for ( int i = 0; i < counter.getDepth(); i++ )
291             {
292                 starter.append( "    " ); // TODO make settable?
293             }
294             lastText = factory.text( starter.toString() );
295         }
296         if ( parent.getContentSize() == 0 )
297         {
298             Text finalText = lastText.clone();
299             finalText.setText( finalText.getText().substring( 0, finalText.getText().length() - "    ".length() ) );
300             parent.addContent( contentIndex, finalText );
301         }
302         parent.addContent( contentIndex, child );
303         parent.addContent( contentIndex, lastText );
304     }
305 
306     /**
307      * Method iterateContributor
308      *
309      * @param counter {@link Counter}
310      * @param childTag The childTag
311      * @param parentTag The parentTag
312      * @param list The list of elements.
313      * @param parent The parent.
314      */
315     protected void iterateContributor( Counter counter, Element parent, Collection<Contributor> list,
316                                        String parentTag, String childTag )
317     {
318         boolean shouldExist = list != null && list.size() > 0;
319         Element element = updateElement( counter, parent, parentTag, shouldExist );
320         if ( shouldExist )
321         {
322             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
323             if ( !elIt.hasNext() )
324             {
325                 elIt = null;
326             }
327             Counter innerCount = new Counter( counter.getDepth() + 1 );
328             for ( Contributor value : list )
329             {
330                 Element el;
331                 if ( elIt != null && elIt.hasNext() )
332                 {
333                     el = elIt.next();
334                     if ( !elIt.hasNext() )
335                     {
336                         elIt = null;
337                     }
338                 }
339                 else
340                 {
341                     el = factory.element( childTag, element.getNamespace() );
342                     insertAtPreferredLocation( element, el, innerCount );
343                 }
344                 updateContributor( value, childTag, innerCount, el );
345                 innerCount.increaseCount();
346             }
347             if ( elIt != null )
348             {
349                 while ( elIt.hasNext() )
350                 {
351                     elIt.next();
352                     elIt.remove();
353                 }
354             }
355         }
356     }
357 
358     /**
359      * Method iterateDependency
360      *
361      * @param counter {@link Counter}
362      * @param childTag The childTag
363      * @param parentTag The parentTag
364      * @param list The list of elements.
365      * @param parent The parent.
366      */
367     protected void iterateDependency( Counter counter, Element parent, Collection<Dependency> list,
368                                       String parentTag, String childTag )
369     {
370         boolean shouldExist = list != null && list.size() > 0;
371         Element element = updateElement( counter, parent, parentTag, shouldExist );
372         if ( shouldExist )
373         {
374             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
375             if ( !elIt.hasNext() )
376             {
377                 elIt = null;
378             }
379             Counter innerCount = new Counter( counter.getDepth() + 1 );
380             for ( Dependency value : list )
381             {
382                 Element el;
383                 if ( elIt != null && elIt.hasNext() )
384                 {
385                     el = elIt.next();
386                     if ( !elIt.hasNext() )
387                     {
388                         elIt = null;
389                     }
390                 }
391                 else
392                 {
393                     el = factory.element( childTag, element.getNamespace() );
394                     insertAtPreferredLocation( element, el, innerCount );
395                 }
396                 updateDependency( value, childTag, innerCount, el );
397                 innerCount.increaseCount();
398             }
399             if ( elIt != null )
400             {
401                 while ( elIt.hasNext() )
402                 {
403                     elIt.next();
404                     elIt.remove();
405                 }
406             }
407         }
408     }
409 
410     /**
411      * Method iterateDeveloper
412      *
413      * @param counter {@link Counter}
414      * @param childTag The childTag
415      * @param parentTag The parentTag
416      * @param list The list of elements.
417      * @param parent The parent.
418      */
419     protected void iterateDeveloper( Counter counter, Element parent, Collection<Developer> list,
420                                      String parentTag, String childTag )
421     {
422         boolean shouldExist = list != null && list.size() > 0;
423         Element element = updateElement( counter, parent, parentTag, shouldExist );
424         if ( shouldExist )
425         {
426             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
427             if ( !elIt.hasNext() )
428             {
429                 elIt = null;
430             }
431             Counter innerCount = new Counter( counter.getDepth() + 1 );
432             for ( Developer value : list )
433             {
434                 Element el;
435                 if ( elIt != null && elIt.hasNext() )
436                 {
437                     el = elIt.next();
438                     if ( !elIt.hasNext() )
439                     {
440                         elIt = null;
441                     }
442                 }
443                 else
444                 {
445                     el = factory.element( childTag, element.getNamespace() );
446                     insertAtPreferredLocation( element, el, innerCount );
447                 }
448                 updateDeveloper( value, childTag, innerCount, el );
449                 innerCount.increaseCount();
450             }
451             if ( elIt != null )
452             {
453                 while ( elIt.hasNext() )
454                 {
455                     elIt.next();
456                     elIt.remove();
457                 }
458             }
459         }
460     }
461 
462     /**
463      * Method iterateExclusion
464      *
465      * @param counter {@link Counter}
466      * @param childTag The childTag
467      * @param parentTag The parentTag
468      * @param list The list of elements.
469      * @param parent The parent.
470      */
471     protected void iterateExclusion( Counter counter, Element parent, Collection<Exclusion> list,
472                                      String parentTag, String childTag )
473     {
474         boolean shouldExist = list != null && list.size() > 0;
475         Element element = updateElement( counter, parent, parentTag, shouldExist );
476         if ( shouldExist )
477         {
478             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
479             if ( !elIt.hasNext() )
480             {
481                 elIt = null;
482             }
483             Counter innerCount = new Counter( counter.getDepth() + 1 );
484             for ( Exclusion value : list )
485             {
486                 Element el;
487                 if ( elIt != null && elIt.hasNext() )
488                 {
489                     el = elIt.next();
490                     if ( !elIt.hasNext() )
491                     {
492                         elIt = null;
493                     }
494                 }
495                 else
496                 {
497                     el = factory.element( childTag, element.getNamespace() );
498                     insertAtPreferredLocation( element, el, innerCount );
499                 }
500                 updateExclusion( value, childTag, innerCount, el );
501                 innerCount.increaseCount();
502             }
503             if ( elIt != null )
504             {
505                 while ( elIt.hasNext() )
506                 {
507                     elIt.next();
508                     elIt.remove();
509                 }
510             }
511         }
512     }
513 
514     /**
515      * Method iterateExtension
516      *
517      * @param counter {@link Counter}
518      * @param childTag The childTag
519      * @param parentTag The parentTag
520      * @param list The list of elements.
521      * @param parent The parent.
522      */
523     protected void iterateExtension( Counter counter, Element parent, Collection<Extension> list,
524                                      String parentTag, String childTag )
525     {
526         boolean shouldExist = list != null && list.size() > 0;
527         Element element = updateElement( counter, parent, parentTag, shouldExist );
528         if ( shouldExist )
529         {
530             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
531             if ( !elIt.hasNext() )
532             {
533                 elIt = null;
534             }
535             Counter innerCount = new Counter( counter.getDepth() + 1 );
536             for ( Extension value : list )
537             {
538                 Element el;
539                 if ( elIt != null && elIt.hasNext() )
540                 {
541                     el = elIt.next();
542                     if ( !elIt.hasNext() )
543                     {
544                         elIt = null;
545                     }
546                 }
547                 else
548                 {
549                     el = factory.element( childTag, element.getNamespace() );
550                     insertAtPreferredLocation( element, el, innerCount );
551                 }
552                 updateExtension( value, childTag, innerCount, el );
553                 innerCount.increaseCount();
554             }
555             if ( elIt != null )
556             {
557                 while ( elIt.hasNext() )
558                 {
559                     elIt.next();
560                     elIt.remove();
561                 }
562             }
563         }
564     }
565 
566     /**
567      * Method iterateLicense
568      *
569      * @param counter {@link Counter}
570      * @param childTag The childTag
571      * @param parentTag The parentTag
572      * @param list The list of elements.
573      * @param parent The parent.
574      */
575     protected void iterateLicense( Counter counter, Element parent, Collection<License> list,
576                                    String parentTag, String childTag )
577     {
578         boolean shouldExist = list != null && list.size() > 0;
579         Element element = updateElement( counter, parent, parentTag, shouldExist );
580         if ( shouldExist )
581         {
582             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
583             if ( !elIt.hasNext() )
584             {
585                 elIt = null;
586             }
587             Counter innerCount = new Counter( counter.getDepth() + 1 );
588             for ( License value : list )
589             {
590                 Element el;
591                 if ( elIt != null && elIt.hasNext() )
592                 {
593                     el = elIt.next();
594                     if ( !elIt.hasNext() )
595                     {
596                         elIt = null;
597                     }
598                 }
599                 else
600                 {
601                     el = factory.element( childTag, element.getNamespace() );
602                     insertAtPreferredLocation( element, el, innerCount );
603                 }
604                 updateLicense( value, childTag, innerCount, el );
605                 innerCount.increaseCount();
606             }
607             if ( elIt != null )
608             {
609                 while ( elIt.hasNext() )
610                 {
611                     elIt.next();
612                     elIt.remove();
613                 }
614             }
615         }
616     }
617 
618     /**
619      * Method iterateMailingList
620      *
621      * @param counter {@link Counter}
622      * @param childTag The childTag
623      * @param parentTag The parentTag
624      * @param list The list of elements.
625      * @param parent The parent.
626      */
627     protected void iterateMailingList( Counter counter, Element parent, Collection<MailingList> list,
628                                        String parentTag, String childTag )
629     {
630         boolean shouldExist = list != null && list.size() > 0;
631         Element element = updateElement( counter, parent, parentTag, shouldExist );
632         if ( shouldExist )
633         {
634             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
635             if ( !elIt.hasNext() )
636             {
637                 elIt = null;
638             }
639             Counter innerCount = new Counter( counter.getDepth() + 1 );
640             for ( MailingList value : list )
641             {
642                 Element el;
643                 if ( elIt != null && elIt.hasNext() )
644                 {
645                     el = elIt.next();
646                     if ( !elIt.hasNext() )
647                     {
648                         elIt = null;
649                     }
650                 }
651                 else
652                 {
653                     el = factory.element( childTag, element.getNamespace() );
654                     insertAtPreferredLocation( element, el, innerCount );
655                 }
656                 updateMailingList( value, childTag, innerCount, el );
657                 innerCount.increaseCount();
658             }
659             if ( elIt != null )
660             {
661                 while ( elIt.hasNext() )
662                 {
663                     elIt.next();
664                     elIt.remove();
665                 }
666             }
667         }
668     }
669 
670     /**
671      * Method iterateNotifier
672      *
673      * @param counter {@link Counter}
674      * @param childTag The childTag
675      * @param parentTag The parentTag
676      * @param list The list of elements.
677      * @param parent The parent.
678      */
679     protected void iterateNotifier( Counter counter, Element parent, Collection<Notifier> list,
680                                     String parentTag, String childTag )
681     {
682         boolean shouldExist = list != null && list.size() > 0;
683         Element element = updateElement( counter, parent, parentTag, shouldExist );
684         if ( shouldExist )
685         {
686             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
687             if ( !elIt.hasNext() )
688             {
689                 elIt = null;
690             }
691             Counter innerCount = new Counter( counter.getDepth() + 1 );
692             for ( Notifier value : list )
693             {
694                 Element el;
695                 if ( elIt != null && elIt.hasNext() )
696                 {
697                     el = elIt.next();
698                     if ( !elIt.hasNext() )
699                     {
700                         elIt = null;
701                     }
702                 }
703                 else
704                 {
705                     el = factory.element( childTag, element.getNamespace() );
706                     insertAtPreferredLocation( element, el, innerCount );
707                 }
708                 updateNotifier( value, childTag, innerCount, el );
709                 innerCount.increaseCount();
710             }
711             if ( elIt != null )
712             {
713                 while ( elIt.hasNext() )
714                 {
715                     elIt.next();
716                     elIt.remove();
717                 }
718             }
719         }
720     }
721 
722     /**
723      * Method iteratePlugin
724      *
725      * @param counter {@link Counter}
726      * @param childTag The childTag
727      * @param parentTag The parentTag
728      * @param list The list of elements.
729      * @param parent The parent.
730      */
731     protected void iteratePlugin( Counter counter, Element parent, Collection<Plugin> list,
732                                   String parentTag, String childTag )
733     {
734         boolean shouldExist = list != null && list.size() > 0;
735         Element element = updateElement( counter, parent, parentTag, shouldExist );
736         if ( shouldExist )
737         {
738             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
739             if ( !elIt.hasNext() )
740             {
741                 elIt = null;
742             }
743             Counter innerCount = new Counter( counter.getDepth() + 1 );
744             for ( Plugin value : list )
745             {
746                 Element el;
747                 if ( elIt != null && elIt.hasNext() )
748                 {
749                     el = elIt.next();
750                     if ( !elIt.hasNext() )
751                     {
752                         elIt = null;
753                     }
754                 }
755                 else
756                 {
757                     el = factory.element( childTag, element.getNamespace() );
758                     insertAtPreferredLocation( element, el, innerCount );
759                 }
760                 updatePlugin( value, childTag, innerCount, el );
761                 innerCount.increaseCount();
762             }
763             if ( elIt != null )
764             {
765                 while ( elIt.hasNext() )
766                 {
767                     elIt.next();
768                     elIt.remove();
769                 }
770             }
771         }
772     }
773 
774     /**
775      * Method iteratePluginExecution
776      *
777      * @param counter {@link Counter}
778      * @param childTag The childTag
779      * @param parentTag The parentTag
780      * @param list The list of elements.
781      * @param parent The parent.
782      */
783     protected void iteratePluginExecution( Counter counter, Element parent, Collection<PluginExecution> list,
784                                            String parentTag, String childTag )
785     {
786         boolean shouldExist = list != null && list.size() > 0;
787         Element element = updateElement( counter, parent, parentTag, shouldExist );
788         if ( shouldExist )
789         {
790             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
791             if ( !elIt.hasNext() )
792             {
793                 elIt = null;
794             }
795             Counter innerCount = new Counter( counter.getDepth() + 1 );
796             for ( PluginExecution value : list )
797             {
798                 Element el;
799                 if ( elIt != null && elIt.hasNext() )
800                 {
801                     el = elIt.next();
802                     if ( !elIt.hasNext() )
803                     {
804                         elIt = null;
805                     }
806                 }
807                 else
808                 {
809                     el = factory.element( childTag, element.getNamespace() );
810                     insertAtPreferredLocation( element, el, innerCount );
811                 }
812                 updatePluginExecution( value, childTag, innerCount, el );
813                 innerCount.increaseCount();
814             }
815             if ( elIt != null )
816             {
817                 while ( elIt.hasNext() )
818                 {
819                     elIt.next();
820                     elIt.remove();
821                 }
822             }
823         }
824     }
825 
826     /**
827      * Method iterateProfile
828      *
829      * @param counter {@link Counter}
830      * @param childTag The childTag
831      * @param parentTag The parentTag
832      * @param list The list of elements.
833      * @param parent The parent.
834      */
835     protected void iterateProfile( Counter counter, Element parent, Collection<Profile> list,
836                                    String parentTag, String childTag )
837     {
838         boolean shouldExist = list != null && list.size() > 0;
839         Element element = updateElement( counter, parent, parentTag, shouldExist );
840         if ( shouldExist )
841         {
842             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
843             if ( !elIt.hasNext() )
844             {
845                 elIt = null;
846             }
847             Counter innerCount = new Counter( counter.getDepth() + 1 );
848             for ( Profile value : list )
849             {
850                 Element el;
851                 if ( elIt != null && elIt.hasNext() )
852                 {
853                     el = elIt.next();
854                     if ( !elIt.hasNext() )
855                     {
856                         elIt = null;
857                     }
858                 }
859                 else
860                 {
861                     el = factory.element( childTag, element.getNamespace() );
862                     insertAtPreferredLocation( element, el, innerCount );
863                 }
864                 updateProfile( value, childTag, innerCount, el );
865                 innerCount.increaseCount();
866             }
867             if ( elIt != null )
868             {
869                 while ( elIt.hasNext() )
870                 {
871                     elIt.next();
872                     elIt.remove();
873                 }
874             }
875         }
876     }
877 
878     /**
879      * Method iterateReportPlugin
880      *
881      * @param counter {@link Counter}
882      * @param childTag The childTag
883      * @param parentTag The parentTag
884      * @param list The list of elements.
885      * @param parent The parent.
886      */
887     protected void iterateReportPlugin( Counter counter, Element parent, Collection<ReportPlugin> list,
888                                         String parentTag, String childTag )
889     {
890         boolean shouldExist = list != null && list.size() > 0;
891         Element element = updateElement( counter, parent, parentTag, shouldExist );
892         if ( shouldExist )
893         {
894             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
895             if ( !elIt.hasNext() )
896             {
897                 elIt = null;
898             }
899             Counter innerCount = new Counter( counter.getDepth() + 1 );
900             for ( ReportPlugin value : list )
901             {
902                 Element el;
903                 if ( elIt != null && elIt.hasNext() )
904                 {
905                     el = elIt.next();
906                     if ( !elIt.hasNext() )
907                     {
908                         elIt = null;
909                     }
910                 }
911                 else
912                 {
913                     el = factory.element( childTag, element.getNamespace() );
914                     insertAtPreferredLocation( element, el, innerCount );
915                 }
916                 updateReportPlugin( value, childTag, innerCount, el );
917                 innerCount.increaseCount();
918             }
919             if ( elIt != null )
920             {
921                 while ( elIt.hasNext() )
922                 {
923                     elIt.next();
924                     elIt.remove();
925                 }
926             }
927         }
928     }
929 
930     /**
931      * Method iterateReportSet
932      *
933      * @param counter {@link Counter}
934      * @param childTag The childTag
935      * @param parentTag The parentTag
936      * @param list The list of elements.
937      * @param parent The parent.
938      */
939     protected void iterateReportSet( Counter counter, Element parent, Collection<ReportSet> list,
940                                      String parentTag, String childTag )
941     {
942         boolean shouldExist = list != null && list.size() > 0;
943         Element element = updateElement( counter, parent, parentTag, shouldExist );
944         if ( shouldExist )
945         {
946             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
947             if ( !elIt.hasNext() )
948             {
949                 elIt = null;
950             }
951             Counter innerCount = new Counter( counter.getDepth() + 1 );
952             for ( ReportSet value : list )
953             {
954                 Element el;
955                 if ( elIt != null && elIt.hasNext() )
956                 {
957                     el = elIt.next();
958                     if ( !elIt.hasNext() )
959                     {
960                         elIt = null;
961                     }
962                 }
963                 else
964                 {
965                     el = factory.element( childTag, element.getNamespace() );
966                     insertAtPreferredLocation( element, el, innerCount );
967                 }
968                 updateReportSet( value, childTag, innerCount, el );
969                 innerCount.increaseCount();
970             }
971             if ( elIt != null )
972             {
973                 while ( elIt.hasNext() )
974                 {
975                     elIt.next();
976                     elIt.remove();
977                 }
978             }
979         }
980     }
981 
982     /**
983      * Method iterateRepository
984      *
985      * @param counter {@link Counter}
986      * @param childTag The childTag
987      * @param parentTag The parentTag
988      * @param list The list of elements.
989      * @param parent The parent.
990      */
991     protected void iterateRepository( Counter counter, Element parent, Collection<Repository> list,
992                                       String parentTag, String childTag )
993     {
994         boolean shouldExist = list != null && list.size() > 0;
995         Element element = updateElement( counter, parent, parentTag, shouldExist );
996         if ( shouldExist )
997         {
998             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
999             if ( !elIt.hasNext() )
1000             {
1001                 elIt = null;
1002             }
1003             Counter innerCount = new Counter( counter.getDepth() + 1 );
1004             for ( Repository value : list )
1005             {
1006                 Element el;
1007                 if ( elIt != null && elIt.hasNext() )
1008                 {
1009                     el = elIt.next();
1010                     if ( !elIt.hasNext() )
1011                     {
1012                         elIt = null;
1013                     }
1014                 }
1015                 else
1016                 {
1017                     el = factory.element( childTag, element.getNamespace() );
1018                     insertAtPreferredLocation( element, el, innerCount );
1019                 }
1020                 updateRepository( value, childTag, innerCount, el );
1021                 innerCount.increaseCount();
1022             }
1023             if ( elIt != null )
1024             {
1025                 while ( elIt.hasNext() )
1026                 {
1027                     elIt.next();
1028                     elIt.remove();
1029                 }
1030             }
1031         }
1032     }
1033 
1034     /**
1035      * Method iterateResource
1036      *
1037      * @param counter {@link Counter}
1038      * @param childTag The childTag
1039      * @param parentTag The parentTag
1040      * @param list The list of elements.
1041      * @param parent The parent.
1042      */
1043     protected void iterateResource( Counter counter, Element parent, Collection<Resource> list,
1044                                     String parentTag, String childTag )
1045     {
1046         boolean shouldExist = list != null && list.size() > 0;
1047         Element element = updateElement( counter, parent, parentTag, shouldExist );
1048         if ( shouldExist )
1049         {
1050             Iterator<Element> elIt = element.getChildren( childTag, element.getNamespace() ).iterator();
1051             if ( !elIt.hasNext() )
1052             {
1053                 elIt = null;
1054             }
1055             Counter innerCount = new Counter( counter.getDepth() + 1 );
1056             for ( Resource value : list )
1057             {
1058                 Element el;
1059                 if ( elIt != null && elIt.hasNext() )
1060                 {
1061                     el = elIt.next();
1062                     if ( !elIt.hasNext() )
1063                     {
1064                         elIt = null;
1065                     }
1066                 }
1067                 else
1068                 {
1069                     el = factory.element( childTag, element.getNamespace() );
1070                     insertAtPreferredLocation( element, el, innerCount );
1071                 }
1072                 updateResource( value, childTag, innerCount, el );
1073                 innerCount.increaseCount();
1074             }
1075             if ( elIt != null )
1076             {
1077                 while ( elIt.hasNext() )
1078                 {
1079                     elIt.next();
1080                     elIt.remove();
1081                 }
1082             }
1083         }
1084     }
1085 
1086     /**
1087      * Method replaceXpp3DOM
1088      *
1089      * @param parent The parent.
1090      * @param counter {@link Counter}
1091      * @param parentDom {@link Element}
1092      */
1093     protected void replaceXpp3DOM( Element parent, Xpp3Dom parentDom, Counter counter )
1094     {
1095         if ( parentDom.getChildCount() > 0 )
1096         {
1097             Xpp3Dom[] childs = parentDom.getChildren();
1098             Collection<Xpp3Dom> domChilds = new ArrayList<>();
1099             Collections.addAll( domChilds, childs );
1100             // int domIndex = 0;
1101             for ( Element elem : parent.getChildren() )
1102             {
1103                 Xpp3Dom corrDom = null;
1104                 for ( Xpp3Dom dm : domChilds )
1105                 {
1106                     if ( dm.getName().equals( elem.getName() ) )
1107                     {
1108                         corrDom = dm;
1109                         break;
1110                     }
1111                 }
1112                 if ( corrDom != null )
1113                 {
1114                     domChilds.remove( corrDom );
1115                     replaceXpp3DOM( elem, corrDom, new Counter( counter.getDepth() + 1 ) );
1116                     counter.increaseCount();
1117                 }
1118                 else
1119                 {
1120                     parent.removeContent( elem );
1121                 }
1122             }
1123             for ( Xpp3Dom dm : domChilds )
1124             {
1125                 Element elem = factory.element( dm.getName(), parent.getNamespace() );
1126                 insertAtPreferredLocation( parent, elem, counter );
1127                 counter.increaseCount();
1128                 replaceXpp3DOM( elem, dm, new Counter( counter.getDepth() + 1 ) );
1129             }
1130         }
1131         else if ( parentDom.getValue() != null )
1132         {
1133             parent.setText( parentDom.getValue() );
1134         }
1135     }
1136 
1137     /**
1138      * Method updateActivationFile
1139      *
1140      * @param value The value.
1141      * @param element {@link Element}
1142      * @param counter {@link Counter}
1143      * @param xmlTag The XMLTag.
1144      */
1145     protected void updateActivationFile( ActivationFile value, String xmlTag, Counter counter, Element element )
1146     {
1147         boolean shouldExist = value != null;
1148         Element root = updateElement( counter, element, xmlTag, shouldExist );
1149         if ( shouldExist )
1150         {
1151             Counter innerCount = new Counter( counter.getDepth() + 1 );
1152             findAndReplaceSimpleElement( innerCount, root, "missing", value.getMissing(), null );
1153             findAndReplaceSimpleElement( innerCount, root, "exists", value.getExists(), null );
1154         }
1155     }
1156 
1157     /**
1158      * Method updateActivationOS
1159      *
1160      * @param value The value.
1161      * @param element {@link Element}
1162      * @param counter {@link Counter}
1163      * @param xmlTag The XMLTag.
1164      */
1165     protected void updateActivationOS( ActivationOS value, String xmlTag, Counter counter, Element element )
1166     {
1167         boolean shouldExist = value != null;
1168         Element root = updateElement( counter, element, xmlTag, shouldExist );
1169         if ( shouldExist )
1170         {
1171             Counter innerCount = new Counter( counter.getDepth() + 1 );
1172             findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1173             findAndReplaceSimpleElement( innerCount, root, "family", value.getFamily(), null );
1174             findAndReplaceSimpleElement( innerCount, root, "arch", value.getArch(), null );
1175             findAndReplaceSimpleElement( innerCount, root, "version", value.getVersion(), null );
1176         }
1177     }
1178 
1179     /**
1180      * Method updateActivationProperty
1181      *
1182      * @param value The value.
1183      * @param element {@link Element}
1184      * @param counter {@link Counter}
1185      * @param xmlTag The XMLTag.
1186      */
1187     protected void updateActivationProperty( ActivationProperty value, String xmlTag, Counter counter, Element element )
1188     {
1189         boolean shouldExist = value != null;
1190         Element root = updateElement( counter, element, xmlTag, shouldExist );
1191         if ( shouldExist )
1192         {
1193             Counter innerCount = new Counter( counter.getDepth() + 1 );
1194             findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1195             findAndReplaceSimpleElement( innerCount, root, "value", value.getValue(), null );
1196         }
1197     }
1198 
1199     /**
1200      * Method updateBuild
1201      *
1202      * @param value The value.
1203      * @param element {@link Element}
1204      * @param counter {@link Counter}
1205      * @param xmlTag The XMLTag.
1206      */
1207     //CHECKSTYLE_OFF: LineLength
1208     protected void updateBuild( Build value, String xmlTag, Counter counter, Element element )
1209     {
1210         boolean shouldExist = value != null;
1211         Element root = updateElement( counter, element, xmlTag, shouldExist );
1212         if ( shouldExist )
1213         {
1214             Counter innerCount = new Counter( counter.getDepth() + 1 );
1215             findAndReplaceSimpleElement( innerCount, root, "sourceDirectory", value.getSourceDirectory(), null );
1216             findAndReplaceSimpleElement( innerCount, root, "scriptSourceDirectory", value.getScriptSourceDirectory(),
1217                                          null );
1218             findAndReplaceSimpleElement( innerCount, root, "testSourceDirectory", value.getTestSourceDirectory(), null );
1219             findAndReplaceSimpleElement( innerCount, root, "outputDirectory", value.getOutputDirectory(), null );
1220             findAndReplaceSimpleElement( innerCount, root, "testOutputDirectory", value.getTestOutputDirectory(), null );
1221             iterateExtension( innerCount, root, value.getExtensions(), "extensions", "extension" );
1222             findAndReplaceSimpleElement( innerCount, root, "defaultGoal", value.getDefaultGoal(), null );
1223             iterateResource( innerCount, root, value.getResources(), "resources", "resource" );
1224             iterateResource( innerCount, root, value.getTestResources(), "testResources", "testResource" );
1225             findAndReplaceSimpleElement( innerCount, root, "directory", value.getDirectory(), null );
1226             findAndReplaceSimpleElement( innerCount, root, "finalName", value.getFinalName(), null );
1227             findAndReplaceSimpleLists( innerCount, root, value.getFilters(), "filters", "filter" );
1228             updatePluginManagement( value.getPluginManagement(), "pluginManagement", innerCount, root );
1229             iteratePlugin( innerCount, root, value.getPlugins(), "plugins", "plugin" );
1230         }
1231     }
1232     //CHECKSTYLE_ON: LineLength
1233 
1234     /**
1235      * Method updateBuildBase
1236      *
1237      * @param value The value.
1238      * @param element {@link Element}
1239      * @param counter {@link Counter}
1240      * @param xmlTag The XMLTag.
1241      */
1242     protected void updateBuildBase( BuildBase value, String xmlTag, Counter counter, Element element )
1243     {
1244         boolean shouldExist = value != null;
1245         Element root = updateElement( counter, element, xmlTag, shouldExist );
1246         if ( shouldExist )
1247         {
1248             Counter innerCount = new Counter( counter.getDepth() + 1 );
1249             findAndReplaceSimpleElement( innerCount, root, "defaultGoal", value.getDefaultGoal(), null );
1250             iterateResource( innerCount, root, value.getResources(), "resources", "resource" );
1251             iterateResource( innerCount, root, value.getTestResources(), "testResources", "testResource" );
1252             findAndReplaceSimpleElement( innerCount, root, "directory", value.getDirectory(), null );
1253             findAndReplaceSimpleElement( innerCount, root, "finalName", value.getFinalName(), null );
1254             findAndReplaceSimpleLists( innerCount, root, value.getFilters(), "filters", "filter" );
1255             updatePluginManagement( value.getPluginManagement(), "pluginManagement", innerCount, root );
1256             iteratePlugin( innerCount, root, value.getPlugins(), "plugins", "plugin" );
1257         }
1258     }
1259 
1260     /**
1261      * Method updateCiManagement
1262      *
1263      * @param value The value.
1264      * @param element {@link Element}
1265      * @param counter {@link Counter}
1266      * @param xmlTag The XMLTag.
1267      */
1268     protected void updateCiManagement( CiManagement value, String xmlTag, Counter counter, Element element )
1269     {
1270         boolean shouldExist = value != null;
1271         Element root = updateElement( counter, element, xmlTag, shouldExist );
1272         if ( shouldExist )
1273         {
1274             Counter innerCount = new Counter( counter.getDepth() + 1 );
1275             findAndReplaceSimpleElement( innerCount, root, "system", value.getSystem(), null );
1276             findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
1277             iterateNotifier( innerCount, root, value.getNotifiers(), "notifiers", "notifier" );
1278         }
1279     }
1280 
1281     /**
1282      * Method updateConfigurationContainer
1283      *
1284      * @param value The value.
1285      * @param element {@link Element}
1286      * @param counter {@link Counter}
1287      * @param xmlTag The XMLTag.
1288      */
1289     protected void updateConfigurationContainer( ConfigurationContainer value, String xmlTag, Counter counter,
1290                                                  Element element )
1291     {
1292         boolean shouldExist = value != null;
1293         Element root = updateElement( counter, element, xmlTag, shouldExist );
1294         if ( shouldExist )
1295         {
1296             Counter innerCount = new Counter( counter.getDepth() + 1 );
1297             findAndReplaceSimpleElement( innerCount, root, "inherited", value.getInherited(), null );
1298             findAndReplaceXpp3DOM( innerCount, root, "configuration", (Xpp3Dom) value.getConfiguration() );
1299         }
1300     }
1301 
1302     /**
1303      * Method updateContributor
1304      *
1305      * @param value The value.
1306      * @param element {@link Element}
1307      * @param counter {@link Counter}
1308      * @param xmlTag The XMLTag.
1309      */
1310     protected void updateContributor( Contributor value, String xmlTag, Counter counter, Element element )
1311     {
1312         Element root = element;
1313         Counter innerCount = new Counter( counter.getDepth() + 1 );
1314         findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1315         findAndReplaceSimpleElement( innerCount, root, "email", value.getEmail(), null );
1316         findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
1317         findAndReplaceSimpleElement( innerCount, root, "organization", value.getOrganization(), null );
1318         findAndReplaceSimpleElement( innerCount, root, "organizationUrl", value.getOrganizationUrl(), null );
1319         findAndReplaceSimpleLists( innerCount, root, value.getRoles(), "roles", "role" );
1320         findAndReplaceSimpleElement( innerCount, root, "timezone", value.getTimezone(), null );
1321         findAndReplaceProperties( innerCount, root, "properties", value.getProperties() );
1322     }
1323 
1324     /**
1325      * Method updateDependency
1326      *
1327      * @param value The value.
1328      * @param element {@link Element}
1329      * @param counter {@link Counter}
1330      * @param xmlTag The XMLTag.
1331      */
1332     protected void updateDependency( Dependency value, String xmlTag, Counter counter, Element element )
1333     {
1334         Element root = element;
1335         Counter innerCount = new Counter( counter.getDepth() + 1 );
1336         findAndReplaceSimpleElement( innerCount, root, "groupId", value.getGroupId(), null );
1337         findAndReplaceSimpleElement( innerCount, root, "artifactId", value.getArtifactId(), null );
1338         findAndReplaceSimpleElement( innerCount, root, "version", value.getVersion(), null );
1339         findAndReplaceSimpleElement( innerCount, root, "type", value.getType(), "jar" );
1340         findAndReplaceSimpleElement( innerCount, root, "classifier", value.getClassifier(), null );
1341         findAndReplaceSimpleElement( innerCount, root, "scope", value.getScope(), null );
1342         findAndReplaceSimpleElement( innerCount, root, "systemPath", value.getSystemPath(), null );
1343         iterateExclusion( innerCount, root, value.getExclusions(), "exclusions", "exclusion" );
1344         findAndReplaceSimpleElement( innerCount, root, "optional",
1345                                      !value.isOptional() ? null : String.valueOf( value.isOptional() ), "false" );
1346     }
1347 
1348     /**
1349      * Method updateDependencyManagement
1350      *
1351      * @param value The value.
1352      * @param element {@link Element}
1353      * @param counter {@link Counter}
1354      * @param xmlTag The XMLTag.
1355      */
1356     protected void updateDependencyManagement( DependencyManagement value, String xmlTag, Counter counter,
1357                                                Element element )
1358     {
1359         boolean shouldExist = value != null;
1360         Element root = updateElement( counter, element, xmlTag, shouldExist );
1361         if ( shouldExist )
1362         {
1363             Counter innerCount = new Counter( counter.getDepth() + 1 );
1364             iterateDependency( innerCount, root, value.getDependencies(), "dependencies", "dependency" );
1365         }
1366     }
1367 
1368     /**
1369      * Method updateDeploymentRepository
1370      *
1371      * @param value The value.
1372      * @param element {@link Element}
1373      * @param counter {@link Counter}
1374      * @param xmlTag The XMLTag.
1375      */
1376     protected void updateDeploymentRepository( DeploymentRepository value, String xmlTag, Counter counter,
1377                                                Element element )
1378     {
1379         boolean shouldExist = value != null;
1380         Element root = updateElement( counter, element, xmlTag, shouldExist );
1381         if ( shouldExist )
1382         {
1383             Counter innerCount = new Counter( counter.getDepth() + 1 );
1384             findAndReplaceSimpleElement( innerCount, root, "uniqueVersion",
1385                                          value.isUniqueVersion() ? null : String.valueOf( value.isUniqueVersion() ),
1386                                          "true" );
1387             findAndReplaceSimpleElement( innerCount, root, "id", value.getId(), null );
1388             findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1389             findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
1390             findAndReplaceSimpleElement( innerCount, root, "layout", value.getLayout(), "default" );
1391         }
1392     }
1393 
1394     /**
1395      * Method updateDeveloper
1396      *
1397      * @param value The value.
1398      * @param element {@link Element}
1399      * @param counter {@link Counter}
1400      * @param xmlTag The XMLTag.
1401      */
1402     protected void updateDeveloper( Developer value, String xmlTag, Counter counter, Element element )
1403     {
1404         Element root = element;
1405         Counter innerCount = new Counter( counter.getDepth() + 1 );
1406         findAndReplaceSimpleElement( innerCount, root, "id", value.getId(), null );
1407         findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1408         findAndReplaceSimpleElement( innerCount, root, "email", value.getEmail(), null );
1409         findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
1410         findAndReplaceSimpleElement( innerCount, root, "organization", value.getOrganization(), null );
1411         findAndReplaceSimpleElement( innerCount, root, "organizationUrl", value.getOrganizationUrl(), null );
1412         findAndReplaceSimpleLists( innerCount, root, value.getRoles(), "roles", "role" );
1413         findAndReplaceSimpleElement( innerCount, root, "timezone", value.getTimezone(), null );
1414         findAndReplaceProperties( innerCount, root, "properties", value.getProperties() );
1415     }
1416 
1417     /**
1418      * Method updateDistributionManagement
1419      *
1420      * @param value The value.
1421      * @param element {@link Element}
1422      * @param counter {@link Counter}
1423      * @param xmlTag The XMLTag.
1424      */
1425     protected void updateDistributionManagement( DistributionManagement value, String xmlTag, Counter counter,
1426                                                  Element element )
1427     {
1428         boolean shouldExist = value != null;
1429         Element root = updateElement( counter, element, xmlTag, shouldExist );
1430         if ( shouldExist )
1431         {
1432             Counter innerCount = new Counter( counter.getDepth() + 1 );
1433             updateDeploymentRepository( value.getRepository(), "repository", innerCount, root );
1434             updateDeploymentRepository( value.getSnapshotRepository(), "snapshotRepository", innerCount, root );
1435             updateSite( value.getSite(), "site", innerCount, root );
1436             findAndReplaceSimpleElement( innerCount, root, "downloadUrl", value.getDownloadUrl(), null );
1437             updateRelocation( value.getRelocation(), "relocation", innerCount, root );
1438             findAndReplaceSimpleElement( innerCount, root, "status", value.getStatus(), null );
1439         }
1440     }
1441 
1442     /**
1443      * Method updateElement
1444      *
1445      * @param counter {@link Counter}
1446      * @param shouldExist should exist.
1447      * @param name The name.
1448      * @param parent The parent.
1449      * @return {@link Element}
1450      */
1451     protected Element updateElement( Counter counter, Element parent, String name, boolean shouldExist )
1452     {
1453         Element element = parent.getChild( name, parent.getNamespace() );
1454         if ( element != null && shouldExist )
1455         {
1456             counter.increaseCount();
1457         }
1458         if ( element == null && shouldExist )
1459         {
1460             element = factory.element( name, parent.getNamespace() );
1461             insertAtPreferredLocation( parent, element, counter );
1462             counter.increaseCount();
1463         }
1464         if ( !shouldExist && element != null )
1465         {
1466             int index = parent.indexOf( element );
1467             if ( index > 0 )
1468             {
1469                 Content previous = parent.getContent( index - 1 );
1470                 if ( previous instanceof Text )
1471                 {
1472                     Text txt = (Text) previous;
1473                     if ( txt.getTextTrim().length() == 0 )
1474                     {
1475                         parent.removeContent( txt );
1476                     }
1477                 }
1478             }
1479             parent.removeContent( element );
1480         }
1481         return element;
1482     }
1483 
1484     /**
1485      * Method updateExclusion
1486      *
1487      * @param value The value.
1488      * @param element {@link Element}
1489      * @param counter {@link Counter}
1490      * @param xmlTag The XMLTag.
1491      */
1492     protected void updateExclusion( Exclusion value, String xmlTag, Counter counter, Element element )
1493     {
1494         Element root = element;
1495         Counter innerCount = new Counter( counter.getDepth() + 1 );
1496         findAndReplaceSimpleElement( innerCount, root, "artifactId", value.getArtifactId(), null );
1497         findAndReplaceSimpleElement( innerCount, root, "groupId", value.getGroupId(), null );
1498     }
1499 
1500     /**
1501      * Method updateExtension
1502      *
1503      * @param value The value.
1504      * @param element {@link Element}
1505      * @param counter {@link Counter}
1506      * @param xmlTag The XMLTag.
1507      */
1508     protected void updateExtension( Extension value, String xmlTag, Counter counter, Element element )
1509     {
1510         Element root = element;
1511         Counter innerCount = new Counter( counter.getDepth() + 1 );
1512         findAndReplaceSimpleElement( innerCount, root, "groupId", value.getGroupId(), null );
1513         findAndReplaceSimpleElement( innerCount, root, "artifactId", value.getArtifactId(), null );
1514         findAndReplaceSimpleElement( innerCount, root, "version", value.getVersion(), null );
1515     }
1516 
1517     /**
1518      * Method updateFileSet
1519      *
1520      * @param value The value.
1521      * @param element {@link Element}
1522      * @param counter {@link Counter}
1523      * @param xmlTag The XMLTag.
1524      */
1525     protected void updateFileSet( FileSet value, String xmlTag, Counter counter, Element element )
1526     {
1527         boolean shouldExist = value != null;
1528         Element root = updateElement( counter, element, xmlTag, shouldExist );
1529         if ( shouldExist )
1530         {
1531             Counter innerCount = new Counter( counter.getDepth() + 1 );
1532             findAndReplaceSimpleElement( innerCount, root, "directory", value.getDirectory(), null );
1533             findAndReplaceSimpleLists( innerCount, root, value.getIncludes(), "includes", "include" );
1534             findAndReplaceSimpleLists( innerCount, root, value.getExcludes(), "excludes", "exclude" );
1535         }
1536     }
1537 
1538     /**
1539      * Method updateIssueManagement
1540      *
1541      * @param value The value.
1542      * @param element {@link Element}
1543      * @param counter {@link Counter}
1544      * @param xmlTag The XMLTag.
1545      */
1546     protected void updateIssueManagement( IssueManagement value, String xmlTag, Counter counter, Element element )
1547     {
1548         boolean shouldExist = value != null;
1549         Element root = updateElement( counter, element, xmlTag, shouldExist );
1550         if ( shouldExist )
1551         {
1552             Counter innerCount = new Counter( counter.getDepth() + 1 );
1553             findAndReplaceSimpleElement( innerCount, root, "system", value.getSystem(), null );
1554             findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
1555         }
1556     }
1557 
1558     /**
1559      * Method updateLicense
1560      *
1561      * @param value The value.
1562      * @param element {@link Element}
1563      * @param counter {@link Counter}
1564      * @param xmlTag The XMLTag.
1565      */
1566     protected void updateLicense( License value, String xmlTag, Counter counter, Element element )
1567     {
1568         Element root = element;
1569         Counter innerCount = new Counter( counter.getDepth() + 1 );
1570         findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1571         findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
1572         findAndReplaceSimpleElement( innerCount, root, "distribution", value.getDistribution(), null );
1573         findAndReplaceSimpleElement( innerCount, root, "comments", value.getComments(), null );
1574     }
1575 
1576     /**
1577      * Method updateMailingList
1578      *
1579      * @param value The value.
1580      * @param element {@link Element}
1581      * @param counter {@link Counter}
1582      * @param xmlTag The XMLTag.
1583      */
1584     protected void updateMailingList( MailingList value, String xmlTag, Counter counter, Element element )
1585     {
1586         Element root = element;
1587         Counter innerCount = new Counter( counter.getDepth() + 1 );
1588         findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1589         findAndReplaceSimpleElement( innerCount, root, "subscribe", value.getSubscribe(), null );
1590         findAndReplaceSimpleElement( innerCount, root, "unsubscribe", value.getUnsubscribe(), null );
1591         findAndReplaceSimpleElement( innerCount, root, "post", value.getPost(), null );
1592         findAndReplaceSimpleElement( innerCount, root, "archive", value.getArchive(), null );
1593         findAndReplaceSimpleLists( innerCount, root, value.getOtherArchives(), "otherArchives", "otherArchive" );
1594     }
1595 
1596     /**
1597      * Method updateModel
1598      *
1599      * @param value The value.
1600      * @param element {@link Element}
1601      * @param counter {@link Counter}
1602      * @param xmlTag The XMLTag.
1603      */
1604     protected void updateModel( Model value, String xmlTag, Counter counter, Element element )
1605     {
1606         Element root = element;
1607         Counter innerCount = new Counter( counter.getDepth() + 1 );
1608         updateParent( value.getParent(), "parent", innerCount, root );
1609         findAndReplaceSimpleElement( innerCount, root, "modelVersion", value.getModelVersion(), null );
1610         findAndReplaceSimpleElement( innerCount, root, "groupId", value.getGroupId(), null );
1611         findAndReplaceSimpleElement( innerCount, root, "artifactId", value.getArtifactId(), null );
1612         findAndReplaceSimpleElement( innerCount, root, "packaging", value.getPackaging(), "jar" );
1613         findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1614         findAndReplaceSimpleElement( innerCount, root, "version", value.getVersion(), null );
1615         findAndReplaceSimpleElement( innerCount, root, "description", value.getDescription(), null );
1616         findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
1617         updatePrerequisites( value.getPrerequisites(), "prerequisites", innerCount, root );
1618         updateIssueManagement( value.getIssueManagement(), "issueManagement", innerCount, root );
1619         updateCiManagement( value.getCiManagement(), "ciManagement", innerCount, root );
1620         findAndReplaceSimpleElement( innerCount, root, "inceptionYear", value.getInceptionYear(), null );
1621         iterateMailingList( innerCount, root, value.getMailingLists(), "mailingLists", "mailingList" );
1622         iterateDeveloper( innerCount, root, value.getDevelopers(), "developers", "developer" );
1623         iterateContributor( innerCount, root, value.getContributors(), "contributors", "contributor" );
1624         iterateLicense( innerCount, root, value.getLicenses(), "licenses", "license" );
1625         updateScm( value.getScm(), "scm", innerCount, root );
1626         updateOrganization( value.getOrganization(), "organization", innerCount, root );
1627         updateBuild( value.getBuild(), "build", innerCount, root );
1628         iterateProfile( innerCount, root, value.getProfiles(), "profiles", "profile" );
1629         findAndReplaceSimpleLists( innerCount, root, value.getModules(), "modules", "module" );
1630         iterateRepository( innerCount, root, value.getRepositories(), "repositories", "repository" );
1631         iterateRepository( innerCount, root, value.getPluginRepositories(), "pluginRepositories", "pluginRepository" );
1632         iterateDependency( innerCount, root, value.getDependencies(), "dependencies", "dependency" );
1633         findAndReplaceXpp3DOM( innerCount, root, "reports", (Xpp3Dom) value.getReports() );
1634         updateReporting( value.getReporting(), "reporting", innerCount, root );
1635         updateDependencyManagement( value.getDependencyManagement(), "dependencyManagement", innerCount, root );
1636         updateDistributionManagement( value.getDistributionManagement(), "distributionManagement", innerCount, root );
1637         findAndReplaceProperties( innerCount, root, "properties", value.getProperties() );
1638     }
1639 
1640     /**
1641      * Method updateModelBase
1642      *
1643      * @param value The value.
1644      * @param element {@link Element}
1645      * @param counter {@link Counter}
1646      * @param xmlTag The XMLTag.
1647      */
1648     //CHECKSTYLE_OFF: LineLength
1649     protected void updateModelBase( ModelBase value, String xmlTag, Counter counter, Element element )
1650     {
1651         boolean shouldExist = value != null;
1652         Element root = updateElement( counter, element, xmlTag, shouldExist );
1653         if ( shouldExist )
1654         {
1655             Counter innerCount = new Counter( counter.getDepth() + 1 );
1656             findAndReplaceSimpleLists( innerCount, root, value.getModules(), "modules", "module" );
1657             iterateRepository( innerCount, root, value.getRepositories(), "repositories", "repository" );
1658             iterateRepository( innerCount, root, value.getPluginRepositories(), "pluginRepositories",
1659                                "pluginRepository" );
1660             iterateDependency( innerCount, root, value.getDependencies(), "dependencies", "dependency" );
1661             findAndReplaceXpp3DOM( innerCount, root, "reports", (Xpp3Dom) value.getReports() );
1662             updateReporting( value.getReporting(), "reporting", innerCount, root );
1663             updateDependencyManagement( value.getDependencyManagement(), "dependencyManagement", innerCount, root );
1664             updateDistributionManagement( value.getDistributionManagement(), "distributionManagement", innerCount, root );
1665             findAndReplaceProperties( innerCount, root, "properties", value.getProperties() );
1666         }
1667     }
1668     //CHECKSTYLE_ON: LineLength
1669 
1670     /**
1671      * Method updateNotifier
1672      *
1673      * @param value The value.
1674      * @param element {@link Element}
1675      * @param counter {@link Counter}
1676      * @param xmlTag The XMLTag.
1677      */
1678     //CHECKSTYLE_OFF: LineLength
1679     protected void updateNotifier( Notifier value, String xmlTag, Counter counter, Element element )
1680     {
1681         Element root = element;
1682         Counter innerCount = new Counter( counter.getDepth() + 1 );
1683         findAndReplaceSimpleElement( innerCount, root, "type", value.getType(), "mail" );
1684         findAndReplaceSimpleElement( innerCount, root, "sendOnError",
1685                                      value.isSendOnError() ? null : String.valueOf( value.isSendOnError() ), "true" );
1686         findAndReplaceSimpleElement( innerCount, root, "sendOnFailure",
1687                                      value.isSendOnFailure() ? null : String.valueOf( value.isSendOnFailure() ), "true" );
1688         findAndReplaceSimpleElement( innerCount, root, "sendOnSuccess",
1689                                      value.isSendOnSuccess() ? null : String.valueOf( value.isSendOnSuccess() ), "true" );
1690         findAndReplaceSimpleElement( innerCount, root, "sendOnWarning",
1691                                      value.isSendOnWarning() ? null : String.valueOf( value.isSendOnWarning() ), "true" );
1692         findAndReplaceSimpleElement( innerCount, root, "address", value.getAddress(), null );
1693         findAndReplaceProperties( innerCount, root, "configuration", value.getConfiguration() );
1694     }
1695     //CHECKSTYLE_ON: LineLength
1696 
1697     /**
1698      * Method updateOrganization
1699      *
1700      * @param value The value.
1701      * @param element {@link Element}
1702      * @param counter {@link Counter}
1703      * @param xmlTag The XMLTag.
1704      */
1705     protected void updateOrganization( Organization value, String xmlTag, Counter counter, Element element )
1706     {
1707         boolean shouldExist = value != null;
1708         Element root = updateElement( counter, element, xmlTag, shouldExist );
1709         if ( shouldExist )
1710         {
1711             Counter innerCount = new Counter( counter.getDepth() + 1 );
1712             findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
1713             findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
1714         }
1715     }
1716 
1717     /**
1718      * Method updateParent
1719      *
1720      * @param value The value.
1721      * @param element {@link Element}
1722      * @param counter {@link Counter}
1723      * @param xmlTag The XMLTag.
1724      */
1725     protected void updateParent( Parent value, String xmlTag, Counter counter, Element element )
1726     {
1727         boolean shouldExist = value != null;
1728         Element root = updateElement( counter, element, xmlTag, shouldExist );
1729         if ( shouldExist )
1730         {
1731             Counter innerCount = new Counter( counter.getDepth() + 1 );
1732             findAndReplaceSimpleElement( innerCount, root, "artifactId", value.getArtifactId(), null );
1733             findAndReplaceSimpleElement( innerCount, root, "groupId", value.getGroupId(), null );
1734             findAndReplaceSimpleElement( innerCount, root, "version", value.getVersion(), null );
1735             findAndReplaceSimpleElement( innerCount, root, "relativePath", value.getRelativePath(), "../pom.xml" );
1736         }
1737     }
1738 
1739     /**
1740      * Method updatePatternSet
1741      *
1742      * @param value The value.
1743      * @param element {@link Element}
1744      * @param counter {@link Counter}
1745      * @param xmlTag The XMLTag.
1746      */
1747     protected void updatePatternSet( PatternSet value, String xmlTag, Counter counter, Element element )
1748     {
1749         boolean shouldExist = value != null;
1750         Element root = updateElement( counter, element, xmlTag, shouldExist );
1751         if ( shouldExist )
1752         {
1753             Counter innerCount = new Counter( counter.getDepth() + 1 );
1754             findAndReplaceSimpleLists( innerCount, root, value.getIncludes(), "includes", "include" );
1755             findAndReplaceSimpleLists( innerCount, root, value.getExcludes(), "excludes", "exclude" );
1756         }
1757     }
1758 
1759     /**
1760      * Method updatePlugin
1761      *
1762      * @param value The value.
1763      * @param element {@link Element}
1764      * @param counter {@link Counter}
1765      * @param xmlTag The XMLTag.
1766      */
1767     protected void updatePlugin( Plugin value, String xmlTag, Counter counter, Element element )
1768     {
1769         Element root = element;
1770         Counter innerCount = new Counter( counter.getDepth() + 1 );
1771         findAndReplaceSimpleElement( innerCount, root, "groupId", value.getGroupId(), "org.apache.maven.plugins" );
1772         findAndReplaceSimpleElement( innerCount, root, "artifactId", value.getArtifactId(), null );
1773         findAndReplaceSimpleElement( innerCount, root, "version", value.getVersion(), null );
1774         findAndReplaceSimpleElement( innerCount, root, "extensions",
1775                                      !value.isExtensions() ? null : String.valueOf( value.isExtensions() ), "false" );
1776         iteratePluginExecution( innerCount, root, value.getExecutions(), "executions", "execution" );
1777         iterateDependency( innerCount, root, value.getDependencies(), "dependencies", "dependency" );
1778         findAndReplaceXpp3DOM( innerCount, root, "goals", (Xpp3Dom) value.getGoals() );
1779         findAndReplaceSimpleElement( innerCount, root, "inherited", value.getInherited(), null );
1780         findAndReplaceXpp3DOM( innerCount, root, "configuration", (Xpp3Dom) value.getConfiguration() );
1781     }
1782 
1783     /**
1784      * Method updatePluginConfiguration
1785      *
1786      * @param value The value.
1787      * @param element {@link Element}
1788      * @param counter {@link Counter}
1789      * @param xmlTag The XMLTag.
1790      */
1791     //CHECKSTYLE_OFF: LineLength
1792     protected void updatePluginConfiguration( PluginConfiguration value, String xmlTag, Counter counter, Element element )
1793     {
1794         boolean shouldExist = value != null;
1795         Element root = updateElement( counter, element, xmlTag, shouldExist );
1796         if ( shouldExist )
1797         {
1798             Counter innerCount = new Counter( counter.getDepth() + 1 );
1799             updatePluginManagement( value.getPluginManagement(), "pluginManagement", innerCount, root );
1800             iteratePlugin( innerCount, root, value.getPlugins(), "plugins", "plugin" );
1801         }
1802     }
1803     //CHECKSTYLE_ON: LineLength
1804 
1805     /**
1806      * Method updatePluginContainer
1807      *
1808      * @param value The value.
1809      * @param element {@link Element}
1810      * @param counter {@link Counter}
1811      * @param xmlTag The XMLTag.
1812      */
1813     protected void updatePluginContainer( PluginContainer value, String xmlTag, Counter counter, Element element )
1814     {
1815         boolean shouldExist = value != null;
1816         Element root = updateElement( counter, element, xmlTag, shouldExist );
1817         if ( shouldExist )
1818         {
1819             Counter innerCount = new Counter( counter.getDepth() + 1 );
1820             iteratePlugin( innerCount, root, value.getPlugins(), "plugins", "plugin" );
1821         }
1822     }
1823 
1824     /**
1825      * Method updatePluginExecution
1826      *
1827      * @param value The value.
1828      * @param element {@link Element}
1829      * @param counter {@link Counter}
1830      * @param xmlTag The XMLTag.
1831      */
1832     protected void updatePluginExecution( PluginExecution value, String xmlTag, Counter counter, Element element )
1833     {
1834         Element root = element;
1835         Counter innerCount = new Counter( counter.getDepth() + 1 );
1836         findAndReplaceSimpleElement( innerCount, root, "id", value.getId(), "default" );
1837         findAndReplaceSimpleElement( innerCount, root, "phase", value.getPhase(), null );
1838         findAndReplaceSimpleLists( innerCount, root, value.getGoals(), "goals", "goal" );
1839         findAndReplaceSimpleElement( innerCount, root, "inherited", value.getInherited(), null );
1840         findAndReplaceXpp3DOM( innerCount, root, "configuration", (Xpp3Dom) value.getConfiguration() );
1841     }
1842 
1843     /**
1844      * Method updatePluginManagement
1845      *
1846      * @param value The value.
1847      * @param element {@link Element}
1848      * @param counter {@link Counter}
1849      * @param xmlTag The XMLTag.
1850      */
1851     protected void updatePluginManagement( PluginManagement value, String xmlTag, Counter counter, Element element )
1852     {
1853         boolean shouldExist = value != null;
1854         Element root = updateElement( counter, element, xmlTag, shouldExist );
1855         if ( shouldExist )
1856         {
1857             Counter innerCount = new Counter( counter.getDepth() + 1 );
1858             iteratePlugin( innerCount, root, value.getPlugins(), "plugins", "plugin" );
1859         }
1860     }
1861 
1862     /**
1863      * Method updatePrerequisites
1864      *
1865      * @param value The value.
1866      * @param element {@link Element}
1867      * @param counter {@link Counter}
1868      * @param xmlTag The XMLTag.
1869      */
1870     protected void updatePrerequisites( Prerequisites value, String xmlTag, Counter counter, Element element )
1871     {
1872         boolean shouldExist = value != null;
1873         Element root = updateElement( counter, element, xmlTag, shouldExist );
1874         if ( shouldExist )
1875         {
1876             Counter innerCount = new Counter( counter.getDepth() + 1 );
1877             findAndReplaceSimpleElement( innerCount, root, "maven", value.getMaven(), "2.0" );
1878         }
1879     }
1880 
1881     /**
1882      * Method updateProfile
1883      *
1884      * @param value The value.
1885      * @param element {@link Element}
1886      * @param counter {@link Counter}
1887      * @param xmlTag The XMLTag.
1888      */
1889     protected void updateProfile( Profile value, String xmlTag, Counter counter, Element element )
1890     {
1891         Element root = element;
1892         Counter innerCount = new Counter( counter.getDepth() + 1 );
1893         findAndReplaceSimpleElement( innerCount, root, "id", value.getId(), "default" );
1894         // updateActivation( value.getActivation(), "activation", innerCount, root);
1895         updateBuildBase( value.getBuild(), "build", innerCount, root );
1896         findAndReplaceSimpleLists( innerCount, root, value.getModules(), "modules", "module" );
1897         iterateRepository( innerCount, root, value.getRepositories(), "repositories", "repository" );
1898         iterateRepository( innerCount, root, value.getPluginRepositories(), "pluginRepositories", "pluginRepository" );
1899         iterateDependency( innerCount, root, value.getDependencies(), "dependencies", "dependency" );
1900         findAndReplaceXpp3DOM( innerCount, root, "reports", (Xpp3Dom) value.getReports() );
1901         updateReporting( value.getReporting(), "reporting", innerCount, root );
1902         updateDependencyManagement( value.getDependencyManagement(), "dependencyManagement", innerCount, root );
1903         updateDistributionManagement( value.getDistributionManagement(), "distributionManagement", innerCount, root );
1904         findAndReplaceProperties( innerCount, root, "properties", value.getProperties() );
1905     }
1906 
1907     /**
1908      * Method updateRelocation
1909      *
1910      * @param value The value.
1911      * @param element {@link Element}
1912      * @param counter {@link Counter}
1913      * @param xmlTag The XMLTag.
1914      */
1915     protected void updateRelocation( Relocation value, String xmlTag, Counter counter, Element element )
1916     {
1917         boolean shouldExist = value != null;
1918         Element root = updateElement( counter, element, xmlTag, shouldExist );
1919         if ( shouldExist )
1920         {
1921             Counter innerCount = new Counter( counter.getDepth() + 1 );
1922             findAndReplaceSimpleElement( innerCount, root, "groupId", value.getGroupId(), null );
1923             findAndReplaceSimpleElement( innerCount, root, "artifactId", value.getArtifactId(), null );
1924             findAndReplaceSimpleElement( innerCount, root, "version", value.getVersion(), null );
1925             findAndReplaceSimpleElement( innerCount, root, "message", value.getMessage(), null );
1926         }
1927     }
1928 
1929     /**
1930      * Method updateReportPlugin
1931      *
1932      * @param value The value.
1933      * @param element {@link Element}
1934      * @param counter {@link Counter}
1935      * @param xmlTag The XMLTag.
1936      */
1937     protected void updateReportPlugin( ReportPlugin value, String xmlTag, Counter counter, Element element )
1938     {
1939         Element root = element;
1940         Counter innerCount = new Counter( counter.getDepth() + 1 );
1941         findAndReplaceSimpleElement( innerCount, root, "groupId", value.getGroupId(), "org.apache.maven.plugins" );
1942         findAndReplaceSimpleElement( innerCount, root, "artifactId", value.getArtifactId(), null );
1943         findAndReplaceSimpleElement( innerCount, root, "version", value.getVersion(), null );
1944         findAndReplaceSimpleElement( innerCount, root, "inherited", value.getInherited(), null );
1945         findAndReplaceXpp3DOM( innerCount, root, "configuration", (Xpp3Dom) value.getConfiguration() );
1946         iterateReportSet( innerCount, root, value.getReportSets(), "reportSets", "reportSet" );
1947     }
1948 
1949     /**
1950      * Method updateReportSet
1951      *
1952      * @param value The value.
1953      * @param element {@link Element}
1954      * @param counter {@link Counter}
1955      * @param xmlTag The XMLTag.
1956      */
1957     protected void updateReportSet( ReportSet value, String xmlTag, Counter counter, Element element )
1958     {
1959         Element root = element;
1960         Counter innerCount = new Counter( counter.getDepth() + 1 );
1961         findAndReplaceSimpleElement( innerCount, root, "id", value.getId(), "default" );
1962         findAndReplaceXpp3DOM( innerCount, root, "configuration", (Xpp3Dom) value.getConfiguration() );
1963         findAndReplaceSimpleElement( innerCount, root, "inherited", value.getInherited(), null );
1964         findAndReplaceSimpleLists( innerCount, root, value.getReports(), "reports", "report" );
1965     }
1966 
1967     /**
1968      * Method updateReporting
1969      *
1970      * @param value The value.
1971      * @param element {@link Element}
1972      * @param counter {@link Counter}
1973      * @param xmlTag The XMLTag.
1974      */
1975     protected void updateReporting( Reporting value, String xmlTag, Counter counter, Element element )
1976     {
1977         boolean shouldExist = value != null;
1978         Element root = updateElement( counter, element, xmlTag, shouldExist );
1979         if ( shouldExist )
1980         {
1981             Counter innerCount = new Counter( counter.getDepth() + 1 );
1982             findAndReplaceSimpleElement( innerCount, root, "excludeDefaults", !value.isExcludeDefaults() ? null
1983                             : String.valueOf( value.isExcludeDefaults() ), "false" );
1984             findAndReplaceSimpleElement( innerCount, root, "outputDirectory", value.getOutputDirectory(), null );
1985             iterateReportPlugin( innerCount, root, value.getPlugins(), "plugins", "plugin" );
1986         }
1987     }
1988 
1989     /**
1990      * Method updateRepository
1991      *
1992      * @param value The value.
1993      * @param element {@link Element}
1994      * @param counter {@link Counter}
1995      * @param xmlTag The XMLTag.
1996      */
1997     protected void updateRepository( Repository value, String xmlTag, Counter counter, Element element )
1998     {
1999         Element root = element;
2000         Counter innerCount = new Counter( counter.getDepth() + 1 );
2001         updateRepositoryPolicy( value.getReleases(), "releases", innerCount, root );
2002         updateRepositoryPolicy( value.getSnapshots(), "snapshots", innerCount, root );
2003         findAndReplaceSimpleElement( innerCount, root, "id", value.getId(), null );
2004         findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
2005         findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
2006         findAndReplaceSimpleElement( innerCount, root, "layout", value.getLayout(), "default" );
2007     }
2008 
2009     /**
2010      * Method updateRepositoryBase
2011      *
2012      * @param value The value.
2013      * @param element {@link Element}
2014      * @param counter {@link Counter}
2015      * @param xmlTag The XMLTag.
2016      */
2017     protected void updateRepositoryBase( RepositoryBase value, String xmlTag, Counter counter, Element element )
2018     {
2019         boolean shouldExist = value != null;
2020         Element root = updateElement( counter, element, xmlTag, shouldExist );
2021         if ( shouldExist )
2022         {
2023             Counter innerCount = new Counter( counter.getDepth() + 1 );
2024             findAndReplaceSimpleElement( innerCount, root, "id", value.getId(), null );
2025             findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
2026             findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
2027             findAndReplaceSimpleElement( innerCount, root, "layout", value.getLayout(), "default" );
2028         }
2029     }
2030 
2031     /**
2032      * Method updateRepositoryPolicy
2033      *
2034      * @param value The value.
2035      * @param element {@link Element}
2036      * @param counter {@link Counter}
2037      * @param xmlTag The XMLTag.
2038      */
2039     protected void updateRepositoryPolicy( RepositoryPolicy value, String xmlTag, Counter counter, Element element )
2040     {
2041         boolean shouldExist = value != null;
2042         Element root = updateElement( counter, element, xmlTag, shouldExist );
2043         if ( shouldExist )
2044         {
2045             Counter innerCount = new Counter( counter.getDepth() + 1 );
2046             findAndReplaceSimpleElement( innerCount, root, "enabled",
2047                                          value.isEnabled() ? null : String.valueOf( value.isEnabled() ), "true" );
2048             findAndReplaceSimpleElement( innerCount, root, "updatePolicy", value.getUpdatePolicy(), null );
2049             findAndReplaceSimpleElement( innerCount, root, "checksumPolicy", value.getChecksumPolicy(), null );
2050         }
2051     }
2052 
2053     /**
2054      * Method updateResource
2055      *
2056      * @param value The value.
2057      * @param element {@link Element}
2058      * @param counter {@link Counter}
2059      * @param xmlTag The XMLTag.
2060      */
2061     protected void updateResource( Resource value, String xmlTag, Counter counter, Element element )
2062     {
2063         Element root = element;
2064         Counter innerCount = new Counter( counter.getDepth() + 1 );
2065         findAndReplaceSimpleElement( innerCount, root, "targetPath", value.getTargetPath(), null );
2066         findAndReplaceSimpleElement( innerCount, root, "filtering",
2067                                      !value.isFiltering() ? null : String.valueOf( value.isFiltering() ), "false" );
2068         findAndReplaceSimpleElement( innerCount, root, "directory", value.getDirectory(), null );
2069         findAndReplaceSimpleLists( innerCount, root, value.getIncludes(), "includes", "include" );
2070         findAndReplaceSimpleLists( innerCount, root, value.getExcludes(), "excludes", "exclude" );
2071     }
2072 
2073     /**
2074      * Method updateScm
2075      *
2076      * @param value The value.
2077      * @param element {@link Element}
2078      * @param counter {@link Counter}
2079      * @param xmlTag The XMLTag.
2080      */
2081     protected void updateScm( Scm value, String xmlTag, Counter counter, Element element )
2082     {
2083         boolean shouldExist = value != null;
2084         Element root = updateElement( counter, element, xmlTag, shouldExist );
2085         if ( shouldExist )
2086         {
2087             //CHECKSTYLE_OFF: LineLength
2088 
2089             Counter innerCount = new Counter( counter.getDepth() + 1 );
2090             findAndReplaceSimpleElement( innerCount, root, "connection", value.getConnection(), null );
2091             findAndReplaceSimpleElement( innerCount, root, "developerConnection", value.getDeveloperConnection(), null );
2092             findAndReplaceSimpleElement( innerCount, root, "tag", value.getTag(), "HEAD" );
2093             findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
2094 
2095             //CHECKSTYLE_ON: LineLength
2096         }
2097     }
2098 
2099     /**
2100      * Method updateSite
2101      *
2102      * @param value The value.
2103      * @param element {@link Element}
2104      * @param counter {@link Counter}
2105      * @param xmlTag The XMLTag.
2106      */
2107     protected void updateSite( Site value, String xmlTag, Counter counter, Element element )
2108     {
2109         boolean shouldExist = value != null;
2110         Element root = updateElement( counter, element, xmlTag, shouldExist );
2111         if ( shouldExist )
2112         {
2113             Counter innerCount = new Counter( counter.getDepth() + 1 );
2114             findAndReplaceSimpleElement( innerCount, root, "id", value.getId(), null );
2115             findAndReplaceSimpleElement( innerCount, root, "name", value.getName(), null );
2116             findAndReplaceSimpleElement( innerCount, root, "url", value.getUrl(), null );
2117         }
2118     }
2119 
2120     /**
2121      * Method write
2122      *
2123      * @param project {@link Model}
2124      * @param stream {@link OutputStream}
2125      * @param document {@link Document}
2126      * @deprecated
2127      * @throws IOException in case of an error.
2128      */
2129     public void write( Model project, Document document, OutputStream stream )
2130         throws IOException
2131     {
2132         updateModel( project, "project", new Counter( 0 ), document.getRootElement() );
2133         XMLOutputter outputter = new XMLOutputter();
2134         Format format = Format.getPrettyFormat();
2135         format.setIndent( "    " ).setLineSeparator( lineSeparator );
2136         outputter.setFormat( format );
2137         outputter.output( document, stream );
2138     }
2139 
2140     /**
2141      * Method write
2142      *
2143      * @param project {@link Model}
2144      * @param writer {@link OutputStreamWriter}
2145      * @param document {@link Document}
2146      * @throws IOException in case of an error.
2147      */
2148     public void write( Model project, Document document, OutputStreamWriter writer )
2149         throws IOException
2150     {
2151         Format format = Format.getRawFormat();
2152         format.setEncoding( writer.getEncoding() ).setLineSeparator( lineSeparator );
2153         write( project, document, writer, format );
2154     }
2155 
2156     /**
2157      * Method write
2158      *
2159      * @param project {@link Model}
2160      * @param jdomFormat {@link Format}
2161      * @param writer {@link Writer}
2162      * @param document {@link Document}
2163      * @throws IOException in case of an error.
2164      */
2165     public void write( Model project, Document document, Writer writer, Format jdomFormat )
2166         throws IOException
2167     {
2168         updateModel( project, "project", new Counter( 0 ), document.getRootElement() );
2169         XMLOutputter outputter = new XMLOutputter();
2170         outputter.setFormat( jdomFormat );
2171         outputter.output( document, writer );
2172     }
2173 
2174 }