View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   // Generated by Modello 1.11,
3   // any modifications will be overwritten.
4   // ==============================================================
5   
6   package org.apache.maven.archetype.metadata.io.xpp3;
7   
8     //---------------------------------/
9    //- Imported classes and packages -/
10  //---------------------------------/
11  
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.io.Reader;
15  import java.text.DateFormat;
16  import org.apache.maven.archetype.metadata.AbstractArchetypeDescriptor;
17  import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
18  import org.apache.maven.archetype.metadata.FileSet;
19  import org.apache.maven.archetype.metadata.ModuleDescriptor;
20  import org.apache.maven.archetype.metadata.RequiredProperty;
21  import org.codehaus.plexus.util.ReaderFactory;
22  import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
23  import org.codehaus.plexus.util.xml.pull.MXParser;
24  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
25  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
26  
27  /**
28   * Class ArchetypeDescriptorXpp3Reader.
29   * 
30   * @version $Revision$ $Date$
31   */
32  @SuppressWarnings( "all" )
33  public class ArchetypeDescriptorXpp3Reader
34  {
35  
36        //--------------------------/
37       //- Class/Member Variables -/
38      //--------------------------/
39  
40      /**
41       * If set the parser will be loaded with all single characters
42       * from the XHTML specification.
43       * The entities used:
44       * <ul>
45       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</li>;
46       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li>;
47       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li>;
48       * </ul>
49       */
50      private boolean addDefaultEntities = true;
51  
52      /**
53       * Field contentTransformer.
54       */
55      public final ContentTransformer contentTransformer;
56  
57  
58        //----------------/
59       //- Constructors -/
60      //----------------/
61  
62      public ArchetypeDescriptorXpp3Reader()
63      {
64          this( new ContentTransformer()
65          {
66              public String transform( String source, String fieldName )
67              {
68                  return source;
69              }
70          } );
71      } //-- org.apache.maven.archetype.metadata.io.xpp3.ArchetypeDescriptorXpp3Reader()
72  
73      public ArchetypeDescriptorXpp3Reader(ContentTransformer contentTransformer)
74      {
75          this.contentTransformer = contentTransformer;
76      } //-- org.apache.maven.archetype.metadata.io.xpp3.ArchetypeDescriptorXpp3Reader(ContentTransformer)
77  
78  
79        //-----------/
80       //- Methods -/
81      //-----------/
82  
83      /**
84       * Method checkFieldWithDuplicate.
85       * 
86       * @param parser
87       * @param parsed
88       * @param alias
89       * @param tagName
90       * @throws XmlPullParserException
91       * @return boolean
92       */
93      private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set parsed )
94          throws XmlPullParserException
95      {
96          if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
97          {
98              return false;
99          }
100         if ( !parsed.add( tagName ) )
101         {
102             throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
103         }
104         return true;
105     } //-- boolean checkFieldWithDuplicate( XmlPullParser, String, String, java.util.Set )
106 
107     /**
108      * Method checkUnknownAttribute.
109      * 
110      * @param parser
111      * @param strict
112      * @param tagName
113      * @param attribute
114      * @throws XmlPullParserException
115      * @throws IOException
116      */
117     private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict )
118         throws XmlPullParserException, IOException
119     {
120         // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
121         if ( strict )
122         {
123             throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
124         }
125     } //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean )
126 
127     /**
128      * Method checkUnknownElement.
129      * 
130      * @param parser
131      * @param strict
132      * @throws XmlPullParserException
133      * @throws IOException
134      */
135     private void checkUnknownElement( XmlPullParser parser, boolean strict )
136         throws XmlPullParserException, IOException
137     {
138         if ( strict )
139         {
140             throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null );
141         }
142 
143         for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; )
144         {
145             int eventType = parser.next();
146             if ( eventType == XmlPullParser.START_TAG )
147             {
148                 unrecognizedTagCount++;
149             }
150             else if ( eventType == XmlPullParser.END_TAG )
151             {
152                 unrecognizedTagCount--;
153             }
154         }
155     } //-- void checkUnknownElement( XmlPullParser, boolean )
156 
157     /**
158      * Returns the state of the "add default entities" flag.
159      * 
160      * @return boolean
161      */
162     public boolean getAddDefaultEntities()
163     {
164         return addDefaultEntities;
165     } //-- boolean getAddDefaultEntities()
166 
167     /**
168      * Method getBooleanValue.
169      * 
170      * @param s
171      * @param parser
172      * @param attribute
173      * @throws XmlPullParserException
174      * @return boolean
175      */
176     private boolean getBooleanValue( String s, String attribute, XmlPullParser parser )
177         throws XmlPullParserException
178     {
179         return getBooleanValue( s, attribute, parser, null );
180     } //-- boolean getBooleanValue( String, String, XmlPullParser )
181 
182     /**
183      * Method getBooleanValue.
184      * 
185      * @param s
186      * @param defaultValue
187      * @param parser
188      * @param attribute
189      * @throws XmlPullParserException
190      * @return boolean
191      */
192     private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, String defaultValue )
193         throws XmlPullParserException
194     {
195         if ( s != null && s.length() != 0 )
196         {
197             return Boolean.valueOf( s ).booleanValue();
198         }
199         if ( defaultValue != null )
200         {
201             return Boolean.valueOf( defaultValue ).booleanValue();
202         }
203         return false;
204     } //-- boolean getBooleanValue( String, String, XmlPullParser, String )
205 
206     /**
207      * Method getByteValue.
208      * 
209      * @param s
210      * @param strict
211      * @param parser
212      * @param attribute
213      * @throws XmlPullParserException
214      * @return byte
215      */
216     private byte getByteValue( String s, String attribute, XmlPullParser parser, boolean strict )
217         throws XmlPullParserException
218     {
219         if ( s != null )
220         {
221             try
222             {
223                 return Byte.valueOf( s ).byteValue();
224             }
225             catch ( NumberFormatException nfe )
226             {
227                 if ( strict )
228                 {
229                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a byte", parser, nfe );
230                 }
231             }
232         }
233         return 0;
234     } //-- byte getByteValue( String, String, XmlPullParser, boolean )
235 
236     /**
237      * Method getCharacterValue.
238      * 
239      * @param s
240      * @param parser
241      * @param attribute
242      * @throws XmlPullParserException
243      * @return char
244      */
245     private char getCharacterValue( String s, String attribute, XmlPullParser parser )
246         throws XmlPullParserException
247     {
248         if ( s != null )
249         {
250             return s.charAt( 0 );
251         }
252         return 0;
253     } //-- char getCharacterValue( String, String, XmlPullParser )
254 
255     /**
256      * Method getDateValue.
257      * 
258      * @param s
259      * @param parser
260      * @param attribute
261      * @throws XmlPullParserException
262      * @return Date
263      */
264     private java.util.Date getDateValue( String s, String attribute, XmlPullParser parser )
265         throws XmlPullParserException
266     {
267         return getDateValue( s, attribute, null, parser );
268     } //-- java.util.Date getDateValue( String, String, XmlPullParser )
269 
270     /**
271      * Method getDateValue.
272      * 
273      * @param s
274      * @param parser
275      * @param dateFormat
276      * @param attribute
277      * @throws XmlPullParserException
278      * @return Date
279      */
280     private java.util.Date getDateValue( String s, String attribute, String dateFormat, XmlPullParser parser )
281         throws XmlPullParserException
282     {
283         if ( s != null )
284         {
285             String effectiveDateFormat = dateFormat;
286             if ( dateFormat == null )
287             {
288                 effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
289             }
290             if ( "long".equals( effectiveDateFormat ) )
291             {
292                 try
293                 {
294                     return new java.util.Date( Long.parseLong( s ) );
295                 }
296                 catch ( NumberFormatException e )
297                 {
298                     throw new XmlPullParserException( e.getMessage(), parser, e );
299                 }
300             }
301             else
302             {
303                 try
304                 {
305                     DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US );
306                     return dateParser.parse( s );
307                 }
308                 catch ( java.text.ParseException e )
309                 {
310                     throw new XmlPullParserException( e.getMessage(), parser, e );
311                 }
312             }
313         }
314         return null;
315     } //-- java.util.Date getDateValue( String, String, String, XmlPullParser )
316 
317     /**
318      * Method getDoubleValue.
319      * 
320      * @param s
321      * @param strict
322      * @param parser
323      * @param attribute
324      * @throws XmlPullParserException
325      * @return double
326      */
327     private double getDoubleValue( String s, String attribute, XmlPullParser parser, boolean strict )
328         throws XmlPullParserException
329     {
330         if ( s != null )
331         {
332             try
333             {
334                 return Double.valueOf( s ).doubleValue();
335             }
336             catch ( NumberFormatException nfe )
337             {
338                 if ( strict )
339                 {
340                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
341                 }
342             }
343         }
344         return 0;
345     } //-- double getDoubleValue( String, String, XmlPullParser, boolean )
346 
347     /**
348      * Method getFloatValue.
349      * 
350      * @param s
351      * @param strict
352      * @param parser
353      * @param attribute
354      * @throws XmlPullParserException
355      * @return float
356      */
357     private float getFloatValue( String s, String attribute, XmlPullParser parser, boolean strict )
358         throws XmlPullParserException
359     {
360         if ( s != null )
361         {
362             try
363             {
364                 return Float.valueOf( s ).floatValue();
365             }
366             catch ( NumberFormatException nfe )
367             {
368                 if ( strict )
369                 {
370                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
371                 }
372             }
373         }
374         return 0;
375     } //-- float getFloatValue( String, String, XmlPullParser, boolean )
376 
377     /**
378      * Method getIntegerValue.
379      * 
380      * @param s
381      * @param strict
382      * @param parser
383      * @param attribute
384      * @throws XmlPullParserException
385      * @return int
386      */
387     private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict )
388         throws XmlPullParserException
389     {
390         if ( s != null )
391         {
392             try
393             {
394                 return Integer.valueOf( s ).intValue();
395             }
396             catch ( NumberFormatException nfe )
397             {
398                 if ( strict )
399                 {
400                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe );
401                 }
402             }
403         }
404         return 0;
405     } //-- int getIntegerValue( String, String, XmlPullParser, boolean )
406 
407     /**
408      * Method getLongValue.
409      * 
410      * @param s
411      * @param strict
412      * @param parser
413      * @param attribute
414      * @throws XmlPullParserException
415      * @return long
416      */
417     private long getLongValue( String s, String attribute, XmlPullParser parser, boolean strict )
418         throws XmlPullParserException
419     {
420         if ( s != null )
421         {
422             try
423             {
424                 return Long.valueOf( s ).longValue();
425             }
426             catch ( NumberFormatException nfe )
427             {
428                 if ( strict )
429                 {
430                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a long integer", parser, nfe );
431                 }
432             }
433         }
434         return 0;
435     } //-- long getLongValue( String, String, XmlPullParser, boolean )
436 
437     /**
438      * Method getRequiredAttributeValue.
439      * 
440      * @param s
441      * @param strict
442      * @param parser
443      * @param attribute
444      * @throws XmlPullParserException
445      * @return String
446      */
447     private String getRequiredAttributeValue( String s, String attribute, XmlPullParser parser, boolean strict )
448         throws XmlPullParserException
449     {
450         if ( s == null )
451         {
452             if ( strict )
453             {
454                 throw new XmlPullParserException( "Missing required value for attribute '" + attribute + "'", parser, null );
455             }
456         }
457         return s;
458     } //-- String getRequiredAttributeValue( String, String, XmlPullParser, boolean )
459 
460     /**
461      * Method getShortValue.
462      * 
463      * @param s
464      * @param strict
465      * @param parser
466      * @param attribute
467      * @throws XmlPullParserException
468      * @return short
469      */
470     private short getShortValue( String s, String attribute, XmlPullParser parser, boolean strict )
471         throws XmlPullParserException
472     {
473         if ( s != null )
474         {
475             try
476             {
477                 return Short.valueOf( s ).shortValue();
478             }
479             catch ( NumberFormatException nfe )
480             {
481                 if ( strict )
482                 {
483                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a short integer", parser, nfe );
484                 }
485             }
486         }
487         return 0;
488     } //-- short getShortValue( String, String, XmlPullParser, boolean )
489 
490     /**
491      * Method getTrimmedValue.
492      * 
493      * @param s
494      * @return String
495      */
496     private String getTrimmedValue( String s )
497     {
498         if ( s != null )
499         {
500             s = s.trim();
501         }
502         return s;
503     } //-- String getTrimmedValue( String )
504 
505     /**
506      * Method interpolatedTrimmed.
507      * 
508      * @param value
509      * @param context
510      * @return String
511      */
512     private String interpolatedTrimmed( String value, String context )
513     {
514         return getTrimmedValue( contentTransformer.transform( value, context ) );
515     } //-- String interpolatedTrimmed( String, String )
516 
517     /**
518      * Method nextTag.
519      * 
520      * @param parser
521      * @throws IOException
522      * @throws XmlPullParserException
523      * @return int
524      */
525     private int nextTag( XmlPullParser parser )
526         throws IOException, XmlPullParserException
527     {
528         int eventType = parser.next();
529         if ( eventType == XmlPullParser.TEXT )
530         {
531             eventType = parser.next();
532         }
533         if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG )
534         {
535             throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null );
536         }
537         return eventType;
538     } //-- int nextTag( XmlPullParser )
539 
540     /**
541      * @see ReaderFactory#newXmlReader
542      * 
543      * @param reader
544      * @param strict
545      * @throws IOException
546      * @throws XmlPullParserException
547      * @return ArchetypeDescriptor
548      */
549     public ArchetypeDescriptor read( Reader reader, boolean strict )
550         throws IOException, XmlPullParserException
551     {
552         XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( );
553 
554         parser.setInput( reader );
555 
556 
557         return read( parser, strict );
558     } //-- ArchetypeDescriptor read( Reader, boolean )
559 
560     /**
561      * @see ReaderFactory#newXmlReader
562      * 
563      * @param reader
564      * @throws IOException
565      * @throws XmlPullParserException
566      * @return ArchetypeDescriptor
567      */
568     public ArchetypeDescriptor read( Reader reader )
569         throws IOException, XmlPullParserException
570     {
571         return read( reader, true );
572     } //-- ArchetypeDescriptor read( Reader )
573 
574     /**
575      * Method read.
576      * 
577      * @param in
578      * @param strict
579      * @throws IOException
580      * @throws XmlPullParserException
581      * @return ArchetypeDescriptor
582      */
583     public ArchetypeDescriptor read( InputStream in, boolean strict )
584         throws IOException, XmlPullParserException
585     {
586         return read( ReaderFactory.newXmlReader( in ), strict );
587     } //-- ArchetypeDescriptor read( InputStream, boolean )
588 
589     /**
590      * Method read.
591      * 
592      * @param in
593      * @throws IOException
594      * @throws XmlPullParserException
595      * @return ArchetypeDescriptor
596      */
597     public ArchetypeDescriptor read( InputStream in )
598         throws IOException, XmlPullParserException
599     {
600         return read( ReaderFactory.newXmlReader( in ) );
601     } //-- ArchetypeDescriptor read( InputStream )
602 
603     /**
604      * Method parseAbstractArchetypeDescriptor.
605      * 
606      * @param parser
607      * @param strict
608      * @throws IOException
609      * @throws XmlPullParserException
610      * @return AbstractArchetypeDescriptor
611      */
612     private AbstractArchetypeDescriptor parseAbstractArchetypeDescriptor( XmlPullParser parser, boolean strict )
613         throws IOException, XmlPullParserException
614     {
615         String tagName = parser.getName();
616         AbstractArchetypeDescriptor abstractArchetypeDescriptor = new AbstractArchetypeDescriptor();
617         for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
618         {
619             String name = parser.getAttributeName( i );
620             String value = parser.getAttributeValue( i );
621 
622             if ( name.indexOf( ':' ) >= 0 )
623             {
624                 // just ignore attributes with non-default namespace (for example: xmlns:xsi)
625             }
626             else
627             {
628                 checkUnknownAttribute( parser, name, tagName, strict );
629             }
630         }
631         java.util.Set parsed = new java.util.HashSet();
632         while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
633         {
634             if ( checkFieldWithDuplicate( parser, "fileSets", null, parsed ) )
635             {
636                 java.util.List<FileSet> fileSets = new java.util.ArrayList<FileSet>();
637                 abstractArchetypeDescriptor.setFileSets( fileSets );
638                 while ( parser.nextTag() == XmlPullParser.START_TAG )
639                 {
640                     if ( "fileSet".equals( parser.getName() ) )
641                     {
642                         fileSets.add( parseFileSet( parser, strict ) );
643                     }
644                     else
645                     {
646                         checkUnknownElement( parser, strict );
647                     }
648                 }
649             }
650             else if ( checkFieldWithDuplicate( parser, "modules", null, parsed ) )
651             {
652                 java.util.List<ModuleDescriptor> modules = new java.util.ArrayList<ModuleDescriptor>();
653                 abstractArchetypeDescriptor.setModules( modules );
654                 while ( parser.nextTag() == XmlPullParser.START_TAG )
655                 {
656                     if ( "module".equals( parser.getName() ) )
657                     {
658                         modules.add( parseModuleDescriptor( parser, strict ) );
659                     }
660                     else
661                     {
662                         checkUnknownElement( parser, strict );
663                     }
664                 }
665             }
666             else
667             {
668                 checkUnknownElement( parser, strict );
669             }
670         }
671         return abstractArchetypeDescriptor;
672     } //-- AbstractArchetypeDescriptor parseAbstractArchetypeDescriptor( XmlPullParser, boolean )
673 
674     /**
675      * Method parseArchetypeDescriptor.
676      * 
677      * @param parser
678      * @param strict
679      * @throws IOException
680      * @throws XmlPullParserException
681      * @return ArchetypeDescriptor
682      */
683     private ArchetypeDescriptor parseArchetypeDescriptor( XmlPullParser parser, boolean strict )
684         throws IOException, XmlPullParserException
685     {
686         String tagName = parser.getName();
687         ArchetypeDescriptor archetypeDescriptor = new ArchetypeDescriptor();
688         for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
689         {
690             String name = parser.getAttributeName( i );
691             String value = parser.getAttributeValue( i );
692 
693             if ( name.indexOf( ':' ) >= 0 )
694             {
695                 // just ignore attributes with non-default namespace (for example: xmlns:xsi)
696             }
697             else if ( "xmlns".equals( name ) )
698             {
699                 // ignore xmlns attribute in root class, which is a reserved attribute name
700             }
701             else if ( "name".equals( name ) )
702             {
703                 archetypeDescriptor.setName( interpolatedTrimmed( value, "name" ) );
704             }
705             else if ( "partial".equals( name ) )
706             {
707                 archetypeDescriptor.setPartial( getBooleanValue( interpolatedTrimmed( value, "partial" ), "partial", parser, "false" ) );
708             }
709             else
710             {
711                 checkUnknownAttribute( parser, name, tagName, strict );
712             }
713         }
714         java.util.Set parsed = new java.util.HashSet();
715         while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
716         {
717             if ( checkFieldWithDuplicate( parser, "requiredProperties", null, parsed ) )
718             {
719                 java.util.List<RequiredProperty> requiredProperties = new java.util.ArrayList<RequiredProperty>();
720                 archetypeDescriptor.setRequiredProperties( requiredProperties );
721                 while ( parser.nextTag() == XmlPullParser.START_TAG )
722                 {
723                     if ( "requiredProperty".equals( parser.getName() ) )
724                     {
725                         requiredProperties.add( parseRequiredProperty( parser, strict ) );
726                     }
727                     else
728                     {
729                         checkUnknownElement( parser, strict );
730                     }
731                 }
732             }
733             else if ( checkFieldWithDuplicate( parser, "fileSets", null, parsed ) )
734             {
735                 java.util.List<FileSet> fileSets = new java.util.ArrayList<FileSet>();
736                 archetypeDescriptor.setFileSets( fileSets );
737                 while ( parser.nextTag() == XmlPullParser.START_TAG )
738                 {
739                     if ( "fileSet".equals( parser.getName() ) )
740                     {
741                         fileSets.add( parseFileSet( parser, strict ) );
742                     }
743                     else
744                     {
745                         checkUnknownElement( parser, strict );
746                     }
747                 }
748             }
749             else if ( checkFieldWithDuplicate( parser, "modules", null, parsed ) )
750             {
751                 java.util.List<ModuleDescriptor> modules = new java.util.ArrayList<ModuleDescriptor>();
752                 archetypeDescriptor.setModules( modules );
753                 while ( parser.nextTag() == XmlPullParser.START_TAG )
754                 {
755                     if ( "module".equals( parser.getName() ) )
756                     {
757                         modules.add( parseModuleDescriptor( parser, strict ) );
758                     }
759                     else
760                     {
761                         checkUnknownElement( parser, strict );
762                     }
763                 }
764             }
765             else
766             {
767                 checkUnknownElement( parser, strict );
768             }
769         }
770         return archetypeDescriptor;
771     } //-- ArchetypeDescriptor parseArchetypeDescriptor( XmlPullParser, boolean )
772 
773     /**
774      * Method parseFileSet.
775      * 
776      * @param parser
777      * @param strict
778      * @throws IOException
779      * @throws XmlPullParserException
780      * @return FileSet
781      */
782     private FileSet parseFileSet( XmlPullParser parser, boolean strict )
783         throws IOException, XmlPullParserException
784     {
785         String tagName = parser.getName();
786         FileSet fileSet = new FileSet();
787         for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
788         {
789             String name = parser.getAttributeName( i );
790             String value = parser.getAttributeValue( i );
791 
792             if ( name.indexOf( ':' ) >= 0 )
793             {
794                 // just ignore attributes with non-default namespace (for example: xmlns:xsi)
795             }
796             else if ( "filtered".equals( name ) )
797             {
798                 fileSet.setFiltered( getBooleanValue( interpolatedTrimmed( value, "filtered" ), "filtered", parser, "false" ) );
799             }
800             else if ( "packaged".equals( name ) )
801             {
802                 fileSet.setPackaged( getBooleanValue( interpolatedTrimmed( value, "packaged" ), "packaged", parser, "false" ) );
803             }
804             else if ( "encoding".equals( name ) )
805             {
806                 fileSet.setEncoding( interpolatedTrimmed( value, "encoding" ) );
807             }
808             else
809             {
810                 checkUnknownAttribute( parser, name, tagName, strict );
811             }
812         }
813         java.util.Set parsed = new java.util.HashSet();
814         while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
815         {
816             if ( checkFieldWithDuplicate( parser, "directory", null, parsed ) )
817             {
818                 fileSet.setDirectory( interpolatedTrimmed( parser.nextText(), "directory" ) );
819             }
820             else if ( checkFieldWithDuplicate( parser, "includes", null, parsed ) )
821             {
822                 java.util.List<String> includes = new java.util.ArrayList<String>();
823                 fileSet.setIncludes( includes );
824                 while ( parser.nextTag() == XmlPullParser.START_TAG )
825                 {
826                     if ( "include".equals( parser.getName() ) )
827                     {
828                         includes.add( interpolatedTrimmed( parser.nextText(), "includes" ) );
829                     }
830                     else
831                     {
832                         checkUnknownElement( parser, strict );
833                     }
834                 }
835             }
836             else if ( checkFieldWithDuplicate( parser, "excludes", null, parsed ) )
837             {
838                 java.util.List<String> excludes = new java.util.ArrayList<String>();
839                 fileSet.setExcludes( excludes );
840                 while ( parser.nextTag() == XmlPullParser.START_TAG )
841                 {
842                     if ( "exclude".equals( parser.getName() ) )
843                     {
844                         excludes.add( interpolatedTrimmed( parser.nextText(), "excludes" ) );
845                     }
846                     else
847                     {
848                         checkUnknownElement( parser, strict );
849                     }
850                 }
851             }
852             else
853             {
854                 checkUnknownElement( parser, strict );
855             }
856         }
857         return fileSet;
858     } //-- FileSet parseFileSet( XmlPullParser, boolean )
859 
860     /**
861      * Method parseModuleDescriptor.
862      * 
863      * @param parser
864      * @param strict
865      * @throws IOException
866      * @throws XmlPullParserException
867      * @return ModuleDescriptor
868      */
869     private ModuleDescriptor parseModuleDescriptor( XmlPullParser parser, boolean strict )
870         throws IOException, XmlPullParserException
871     {
872         String tagName = parser.getName();
873         ModuleDescriptor moduleDescriptor = new ModuleDescriptor();
874         for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
875         {
876             String name = parser.getAttributeName( i );
877             String value = parser.getAttributeValue( i );
878 
879             if ( name.indexOf( ':' ) >= 0 )
880             {
881                 // just ignore attributes with non-default namespace (for example: xmlns:xsi)
882             }
883             else if ( "id".equals( name ) )
884             {
885                 moduleDescriptor.setId( interpolatedTrimmed( value, "id" ) );
886             }
887             else if ( "dir".equals( name ) )
888             {
889                 moduleDescriptor.setDir( interpolatedTrimmed( value, "dir" ) );
890             }
891             else if ( "name".equals( name ) )
892             {
893                 moduleDescriptor.setName( interpolatedTrimmed( value, "name" ) );
894             }
895             else
896             {
897                 checkUnknownAttribute( parser, name, tagName, strict );
898             }
899         }
900         java.util.Set parsed = new java.util.HashSet();
901         while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
902         {
903             if ( checkFieldWithDuplicate( parser, "fileSets", null, parsed ) )
904             {
905                 java.util.List<FileSet> fileSets = new java.util.ArrayList<FileSet>();
906                 moduleDescriptor.setFileSets( fileSets );
907                 while ( parser.nextTag() == XmlPullParser.START_TAG )
908                 {
909                     if ( "fileSet".equals( parser.getName() ) )
910                     {
911                         fileSets.add( parseFileSet( parser, strict ) );
912                     }
913                     else
914                     {
915                         checkUnknownElement( parser, strict );
916                     }
917                 }
918             }
919             else if ( checkFieldWithDuplicate( parser, "modules", null, parsed ) )
920             {
921                 java.util.List<ModuleDescriptor> modules = new java.util.ArrayList<ModuleDescriptor>();
922                 moduleDescriptor.setModules( modules );
923                 while ( parser.nextTag() == XmlPullParser.START_TAG )
924                 {
925                     if ( "module".equals( parser.getName() ) )
926                     {
927                         modules.add( parseModuleDescriptor( parser, strict ) );
928                     }
929                     else
930                     {
931                         checkUnknownElement( parser, strict );
932                     }
933                 }
934             }
935             else
936             {
937                 checkUnknownElement( parser, strict );
938             }
939         }
940         return moduleDescriptor;
941     } //-- ModuleDescriptor parseModuleDescriptor( XmlPullParser, boolean )
942 
943     /**
944      * Method parseRequiredProperty.
945      * 
946      * @param parser
947      * @param strict
948      * @throws IOException
949      * @throws XmlPullParserException
950      * @return RequiredProperty
951      */
952     private RequiredProperty parseRequiredProperty( XmlPullParser parser, boolean strict )
953         throws IOException, XmlPullParserException
954     {
955         String tagName = parser.getName();
956         RequiredProperty requiredProperty = new RequiredProperty();
957         for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
958         {
959             String name = parser.getAttributeName( i );
960             String value = parser.getAttributeValue( i );
961 
962             if ( name.indexOf( ':' ) >= 0 )
963             {
964                 // just ignore attributes with non-default namespace (for example: xmlns:xsi)
965             }
966             else if ( "key".equals( name ) )
967             {
968                 requiredProperty.setKey( interpolatedTrimmed( value, "key" ) );
969             }
970             else
971             {
972                 checkUnknownAttribute( parser, name, tagName, strict );
973             }
974         }
975         java.util.Set parsed = new java.util.HashSet();
976         while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
977         {
978             if ( checkFieldWithDuplicate( parser, "defaultValue", null, parsed ) )
979             {
980                 requiredProperty.setDefaultValue( interpolatedTrimmed( parser.nextText(), "defaultValue" ) );
981             }
982             else if ( checkFieldWithDuplicate( parser, "validationRegex", null, parsed ) )
983             {
984                 requiredProperty.setValidationRegex( interpolatedTrimmed( parser.nextText(), "validationRegex" ) );
985             }
986             else
987             {
988                 checkUnknownElement( parser, strict );
989             }
990         }
991         return requiredProperty;
992     } //-- RequiredProperty parseRequiredProperty( XmlPullParser, boolean )
993 
994     /**
995      * Method read.
996      * 
997      * @param parser
998      * @param strict
999      * @throws IOException
1000      * @throws XmlPullParserException
1001      * @return ArchetypeDescriptor
1002      */
1003     private ArchetypeDescriptor read( XmlPullParser parser, boolean strict )
1004         throws IOException, XmlPullParserException
1005     {
1006         ArchetypeDescriptor archetypeDescriptor = null;
1007         int eventType = parser.getEventType();
1008         boolean parsed = false;
1009         while ( eventType != XmlPullParser.END_DOCUMENT )
1010         {
1011             if ( eventType == XmlPullParser.START_TAG )
1012             {
1013                 if ( strict && ! "archetype-descriptor".equals( parser.getName() ) )
1014                 {
1015                     throw new XmlPullParserException( "Expected root element 'archetype-descriptor' but found '" + parser.getName() + "'", parser, null );
1016                 }
1017                 else if ( parsed )
1018                 {
1019                     // fallback, already expected a XmlPullParserException due to invalid XML
1020                     throw new XmlPullParserException( "Duplicated tag: 'archetype-descriptor'", parser, null );
1021                 }
1022                 archetypeDescriptor = parseArchetypeDescriptor( parser, strict );
1023                 archetypeDescriptor.setModelEncoding( parser.getInputEncoding() );
1024                 parsed = true;
1025             }
1026             eventType = parser.next();
1027         }
1028         if ( parsed )
1029         {
1030             return archetypeDescriptor;
1031         }
1032         throw new XmlPullParserException( "Expected root element 'archetype-descriptor' but found no element at all: invalid XML document", parser, null );
1033     } //-- ArchetypeDescriptor read( XmlPullParser, boolean )
1034 
1035     /**
1036      * Sets the state of the "add default entities" flag.
1037      * 
1038      * @param addDefaultEntities
1039      */
1040     public void setAddDefaultEntities( boolean addDefaultEntities )
1041     {
1042         this.addDefaultEntities = addDefaultEntities;
1043     } //-- void setAddDefaultEntities( boolean )
1044 
1045     public static interface ContentTransformer
1046 {
1047     /**
1048      * Interpolate the value read from the xpp3 document
1049      * @param source The source value
1050      * @param fieldName A description of the field being interpolated. The implementation may use this to
1051      *                           log stuff.
1052      * @return The interpolated value.
1053      */
1054     String transform( String source, String fieldName );
1055 }
1056 
1057 }