View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   // Generated by Modello 1.8.3,
3   // any modifications will be overwritten.
4   // ==============================================================
5   
6   package org.apache.maven.archetype.catalog.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.catalog.Archetype;
17  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
18  import org.codehaus.plexus.util.ReaderFactory;
19  import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
20  import org.codehaus.plexus.util.xml.pull.MXParser;
21  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
22  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
23  
24  /**
25   * Class ArchetypeCatalogXpp3Reader.
26   * 
27   * @version $Revision: 942523 $ $Date: 2015-03-06 01:20:33 +0100 (Fri, 06 Mar 2015) $
28   */
29  @SuppressWarnings( "all" )
30  public class ArchetypeCatalogXpp3Reader
31  {
32  
33        //--------------------------/
34       //- Class/Member Variables -/
35      //--------------------------/
36  
37      /**
38       * If set the parser will be loaded with all single characters
39       * from the XHTML specification.
40       * The entities used:
41       * <ul>
42       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</li>;
43       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li>;
44       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li>;
45       * </ul>
46       */
47      private boolean addDefaultEntities = true;
48  
49      /**
50       * Field contentTransformer.
51       */
52      public final ContentTransformer contentTransformer;
53  
54  
55        //----------------/
56       //- Constructors -/
57      //----------------/
58  
59      public ArchetypeCatalogXpp3Reader()
60      {
61          this( new ContentTransformer()
62          {
63              public String transform( String source, String fieldName )
64              {
65                  return source;
66              }
67          } );
68      } //-- org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader()
69  
70      public ArchetypeCatalogXpp3Reader(ContentTransformer contentTransformer)
71      {
72          this.contentTransformer = contentTransformer;
73      } //-- org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader(ContentTransformer)
74  
75  
76        //-----------/
77       //- Methods -/
78      //-----------/
79  
80      /**
81       * Method checkFieldWithDuplicate.
82       * 
83       * @param parser
84       * @param parsed
85       * @param alias
86       * @param tagName
87       * @throws XmlPullParserException
88       * @return boolean
89       */
90      private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set parsed )
91          throws XmlPullParserException
92      {
93          if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
94          {
95              return false;
96          }
97          if ( !parsed.add( tagName ) )
98          {
99              throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
100         }
101         return true;
102     } //-- boolean checkFieldWithDuplicate( XmlPullParser, String, String, java.util.Set )
103 
104     /**
105      * Method checkUnknownAttribute.
106      * 
107      * @param parser
108      * @param strict
109      * @param tagName
110      * @param attribute
111      * @throws XmlPullParserException
112      * @throws IOException
113      */
114     private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict )
115         throws XmlPullParserException, IOException
116     {
117         // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
118         if ( strict )
119         {
120             throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
121         }
122     } //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean )
123 
124     /**
125      * Method checkUnknownElement.
126      * 
127      * @param parser
128      * @param strict
129      * @throws XmlPullParserException
130      * @throws IOException
131      */
132     private void checkUnknownElement( XmlPullParser parser, boolean strict )
133         throws XmlPullParserException, IOException
134     {
135         if ( strict )
136         {
137             throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null );
138         }
139 
140         for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; )
141         {
142             int eventType = parser.next();
143             if ( eventType == XmlPullParser.START_TAG )
144             {
145                 unrecognizedTagCount++;
146             }
147             else if ( eventType == XmlPullParser.END_TAG )
148             {
149                 unrecognizedTagCount--;
150             }
151         }
152     } //-- void checkUnknownElement( XmlPullParser, boolean )
153 
154     /**
155      * Returns the state of the "add default entities" flag.
156      * 
157      * @return boolean
158      */
159     public boolean getAddDefaultEntities()
160     {
161         return addDefaultEntities;
162     } //-- boolean getAddDefaultEntities()
163 
164     /**
165      * Method getBooleanValue.
166      * 
167      * @param s
168      * @param parser
169      * @param attribute
170      * @throws XmlPullParserException
171      * @return boolean
172      */
173     private boolean getBooleanValue( String s, String attribute, XmlPullParser parser )
174         throws XmlPullParserException
175     {
176         return getBooleanValue( s, attribute, parser, null );
177     } //-- boolean getBooleanValue( String, String, XmlPullParser )
178 
179     /**
180      * Method getBooleanValue.
181      * 
182      * @param s
183      * @param defaultValue
184      * @param parser
185      * @param attribute
186      * @throws XmlPullParserException
187      * @return boolean
188      */
189     private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, String defaultValue )
190         throws XmlPullParserException
191     {
192         if ( s != null && s.length() != 0 )
193         {
194             return Boolean.valueOf( s ).booleanValue();
195         }
196         if ( defaultValue != null )
197         {
198             return Boolean.valueOf( defaultValue ).booleanValue();
199         }
200         return false;
201     } //-- boolean getBooleanValue( String, String, XmlPullParser, String )
202 
203     /**
204      * Method getByteValue.
205      * 
206      * @param s
207      * @param strict
208      * @param parser
209      * @param attribute
210      * @throws XmlPullParserException
211      * @return byte
212      */
213     private byte getByteValue( String s, String attribute, XmlPullParser parser, boolean strict )
214         throws XmlPullParserException
215     {
216         if ( s != null )
217         {
218             try
219             {
220                 return Byte.valueOf( s ).byteValue();
221             }
222             catch ( NumberFormatException nfe )
223             {
224                 if ( strict )
225                 {
226                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a byte", parser, nfe );
227                 }
228             }
229         }
230         return 0;
231     } //-- byte getByteValue( String, String, XmlPullParser, boolean )
232 
233     /**
234      * Method getCharacterValue.
235      * 
236      * @param s
237      * @param parser
238      * @param attribute
239      * @throws XmlPullParserException
240      * @return char
241      */
242     private char getCharacterValue( String s, String attribute, XmlPullParser parser )
243         throws XmlPullParserException
244     {
245         if ( s != null )
246         {
247             return s.charAt( 0 );
248         }
249         return 0;
250     } //-- char getCharacterValue( String, String, XmlPullParser )
251 
252     /**
253      * Method getDateValue.
254      * 
255      * @param s
256      * @param parser
257      * @param attribute
258      * @throws XmlPullParserException
259      * @return Date
260      */
261     private java.util.Date getDateValue( String s, String attribute, XmlPullParser parser )
262         throws XmlPullParserException
263     {
264         return getDateValue( s, attribute, null, parser );
265     } //-- java.util.Date getDateValue( String, String, XmlPullParser )
266 
267     /**
268      * Method getDateValue.
269      * 
270      * @param s
271      * @param parser
272      * @param dateFormat
273      * @param attribute
274      * @throws XmlPullParserException
275      * @return Date
276      */
277     private java.util.Date getDateValue( String s, String attribute, String dateFormat, XmlPullParser parser )
278         throws XmlPullParserException
279     {
280         if ( s != null )
281         {
282             String effectiveDateFormat = dateFormat;
283             if ( dateFormat == null )
284             {
285                 effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
286             }
287             if ( "long".equals( effectiveDateFormat ) )
288             {
289                 try
290                 {
291                     return new java.util.Date( Long.parseLong( s ) );
292                 }
293                 catch ( NumberFormatException e )
294                 {
295                     throw new XmlPullParserException( e.getMessage(), parser, e );
296                 }
297             }
298             else
299             {
300                 try
301                 {
302                     DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US );
303                     return dateParser.parse( s );
304                 }
305                 catch ( java.text.ParseException e )
306                 {
307                     throw new XmlPullParserException( e.getMessage(), parser, e );
308                 }
309             }
310         }
311         return null;
312     } //-- java.util.Date getDateValue( String, String, String, XmlPullParser )
313 
314     /**
315      * Method getDoubleValue.
316      * 
317      * @param s
318      * @param strict
319      * @param parser
320      * @param attribute
321      * @throws XmlPullParserException
322      * @return double
323      */
324     private double getDoubleValue( String s, String attribute, XmlPullParser parser, boolean strict )
325         throws XmlPullParserException
326     {
327         if ( s != null )
328         {
329             try
330             {
331                 return Double.valueOf( s ).doubleValue();
332             }
333             catch ( NumberFormatException nfe )
334             {
335                 if ( strict )
336                 {
337                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
338                 }
339             }
340         }
341         return 0;
342     } //-- double getDoubleValue( String, String, XmlPullParser, boolean )
343 
344     /**
345      * Method getFloatValue.
346      * 
347      * @param s
348      * @param strict
349      * @param parser
350      * @param attribute
351      * @throws XmlPullParserException
352      * @return float
353      */
354     private float getFloatValue( String s, String attribute, XmlPullParser parser, boolean strict )
355         throws XmlPullParserException
356     {
357         if ( s != null )
358         {
359             try
360             {
361                 return Float.valueOf( s ).floatValue();
362             }
363             catch ( NumberFormatException nfe )
364             {
365                 if ( strict )
366                 {
367                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
368                 }
369             }
370         }
371         return 0;
372     } //-- float getFloatValue( String, String, XmlPullParser, boolean )
373 
374     /**
375      * Method getIntegerValue.
376      * 
377      * @param s
378      * @param strict
379      * @param parser
380      * @param attribute
381      * @throws XmlPullParserException
382      * @return int
383      */
384     private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict )
385         throws XmlPullParserException
386     {
387         if ( s != null )
388         {
389             try
390             {
391                 return Integer.valueOf( s ).intValue();
392             }
393             catch ( NumberFormatException nfe )
394             {
395                 if ( strict )
396                 {
397                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe );
398                 }
399             }
400         }
401         return 0;
402     } //-- int getIntegerValue( String, String, XmlPullParser, boolean )
403 
404     /**
405      * Method getLongValue.
406      * 
407      * @param s
408      * @param strict
409      * @param parser
410      * @param attribute
411      * @throws XmlPullParserException
412      * @return long
413      */
414     private long getLongValue( String s, String attribute, XmlPullParser parser, boolean strict )
415         throws XmlPullParserException
416     {
417         if ( s != null )
418         {
419             try
420             {
421                 return Long.valueOf( s ).longValue();
422             }
423             catch ( NumberFormatException nfe )
424             {
425                 if ( strict )
426                 {
427                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a long integer", parser, nfe );
428                 }
429             }
430         }
431         return 0;
432     } //-- long getLongValue( String, String, XmlPullParser, boolean )
433 
434     /**
435      * Method getRequiredAttributeValue.
436      * 
437      * @param s
438      * @param strict
439      * @param parser
440      * @param attribute
441      * @throws XmlPullParserException
442      * @return String
443      */
444     private String getRequiredAttributeValue( String s, String attribute, XmlPullParser parser, boolean strict )
445         throws XmlPullParserException
446     {
447         if ( s == null )
448         {
449             if ( strict )
450             {
451                 throw new XmlPullParserException( "Missing required value for attribute '" + attribute + "'", parser, null );
452             }
453         }
454         return s;
455     } //-- String getRequiredAttributeValue( String, String, XmlPullParser, boolean )
456 
457     /**
458      * Method getShortValue.
459      * 
460      * @param s
461      * @param strict
462      * @param parser
463      * @param attribute
464      * @throws XmlPullParserException
465      * @return short
466      */
467     private short getShortValue( String s, String attribute, XmlPullParser parser, boolean strict )
468         throws XmlPullParserException
469     {
470         if ( s != null )
471         {
472             try
473             {
474                 return Short.valueOf( s ).shortValue();
475             }
476             catch ( NumberFormatException nfe )
477             {
478                 if ( strict )
479                 {
480                     throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a short integer", parser, nfe );
481                 }
482             }
483         }
484         return 0;
485     } //-- short getShortValue( String, String, XmlPullParser, boolean )
486 
487     /**
488      * Method getTrimmedValue.
489      * 
490      * @param s
491      * @return String
492      */
493     private String getTrimmedValue( String s )
494     {
495         if ( s != null )
496         {
497             s = s.trim();
498         }
499         return s;
500     } //-- String getTrimmedValue( String )
501 
502     /**
503      * Method interpolatedTrimmed.
504      * 
505      * @param value
506      * @param context
507      * @return String
508      */
509     private String interpolatedTrimmed( String value, String context )
510     {
511         return getTrimmedValue( contentTransformer.transform( value, context ) );
512     } //-- String interpolatedTrimmed( String, String )
513 
514     /**
515      * Method nextTag.
516      * 
517      * @param parser
518      * @throws IOException
519      * @throws XmlPullParserException
520      * @return int
521      */
522     private int nextTag( XmlPullParser parser )
523         throws IOException, XmlPullParserException
524     {
525         int eventType = parser.next();
526         if ( eventType == XmlPullParser.TEXT )
527         {
528             eventType = parser.next();
529         }
530         if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG )
531         {
532             throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null );
533         }
534         return eventType;
535     } //-- int nextTag( XmlPullParser )
536 
537     /**
538      * @see ReaderFactory#newXmlReader
539      * 
540      * @param reader
541      * @param strict
542      * @throws IOException
543      * @throws XmlPullParserException
544      * @return ArchetypeCatalog
545      */
546     public ArchetypeCatalog read( Reader reader, boolean strict )
547         throws IOException, XmlPullParserException
548     {
549         XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( );
550 
551         parser.setInput( reader );
552 
553 
554         return read( parser, strict );
555     } //-- ArchetypeCatalog read( Reader, boolean )
556 
557     /**
558      * @see ReaderFactory#newXmlReader
559      * 
560      * @param reader
561      * @throws IOException
562      * @throws XmlPullParserException
563      * @return ArchetypeCatalog
564      */
565     public ArchetypeCatalog read( Reader reader )
566         throws IOException, XmlPullParserException
567     {
568         return read( reader, true );
569     } //-- ArchetypeCatalog read( Reader )
570 
571     /**
572      * Method read.
573      * 
574      * @param in
575      * @param strict
576      * @throws IOException
577      * @throws XmlPullParserException
578      * @return ArchetypeCatalog
579      */
580     public ArchetypeCatalog read( InputStream in, boolean strict )
581         throws IOException, XmlPullParserException
582     {
583         return read( ReaderFactory.newXmlReader( in ), strict );
584     } //-- ArchetypeCatalog read( InputStream, boolean )
585 
586     /**
587      * Method read.
588      * 
589      * @param in
590      * @throws IOException
591      * @throws XmlPullParserException
592      * @return ArchetypeCatalog
593      */
594     public ArchetypeCatalog read( InputStream in )
595         throws IOException, XmlPullParserException
596     {
597         return read( ReaderFactory.newXmlReader( in ) );
598     } //-- ArchetypeCatalog read( InputStream )
599 
600     /**
601      * Method parseArchetype.
602      * 
603      * @param parser
604      * @param strict
605      * @throws IOException
606      * @throws XmlPullParserException
607      * @return Archetype
608      */
609     private Archetype parseArchetype( XmlPullParser parser, boolean strict )
610         throws IOException, XmlPullParserException
611     {
612         String tagName = parser.getName();
613         Archetype archetype = new Archetype();
614         for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
615         {
616             String name = parser.getAttributeName( i );
617             String value = parser.getAttributeValue( i );
618 
619             if ( name.indexOf( ':' ) >= 0 )
620             {
621                 // just ignore attributes with non-default namespace (for example: xmlns:xsi)
622             }
623             else
624             {
625                 checkUnknownAttribute( parser, name, tagName, strict );
626             }
627         }
628         java.util.Set parsed = new java.util.HashSet();
629         while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
630         {
631             if ( checkFieldWithDuplicate( parser, "groupId", null, parsed ) )
632             {
633                 archetype.setGroupId( interpolatedTrimmed( parser.nextText(), "groupId" ) );
634             }
635             else if ( checkFieldWithDuplicate( parser, "artifactId", null, parsed ) )
636             {
637                 archetype.setArtifactId( interpolatedTrimmed( parser.nextText(), "artifactId" ) );
638             }
639             else if ( checkFieldWithDuplicate( parser, "version", null, parsed ) )
640             {
641                 archetype.setVersion( interpolatedTrimmed( parser.nextText(), "version" ) );
642             }
643             else if ( checkFieldWithDuplicate( parser, "repository", null, parsed ) )
644             {
645                 archetype.setRepository( interpolatedTrimmed( parser.nextText(), "repository" ) );
646             }
647             else if ( checkFieldWithDuplicate( parser, "description", null, parsed ) )
648             {
649                 archetype.setDescription( interpolatedTrimmed( parser.nextText(), "description" ) );
650             }
651             else
652             {
653                 checkUnknownElement( parser, strict );
654             }
655         }
656         return archetype;
657     } //-- Archetype parseArchetype( XmlPullParser, boolean )
658 
659     /**
660      * Method parseArchetypeCatalog.
661      * 
662      * @param parser
663      * @param strict
664      * @throws IOException
665      * @throws XmlPullParserException
666      * @return ArchetypeCatalog
667      */
668     private ArchetypeCatalog parseArchetypeCatalog( XmlPullParser parser, boolean strict )
669         throws IOException, XmlPullParserException
670     {
671         String tagName = parser.getName();
672         ArchetypeCatalog archetypeCatalog = new ArchetypeCatalog();
673         for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
674         {
675             String name = parser.getAttributeName( i );
676             String value = parser.getAttributeValue( i );
677 
678             if ( name.indexOf( ':' ) >= 0 )
679             {
680                 // just ignore attributes with non-default namespace (for example: xmlns:xsi)
681             }
682             else if ( "xmlns".equals( name ) )
683             {
684                 // ignore xmlns attribute in root class, which is a reserved attribute name
685             }
686             else
687             {
688                 checkUnknownAttribute( parser, name, tagName, strict );
689             }
690         }
691         java.util.Set parsed = new java.util.HashSet();
692         while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
693         {
694             if ( checkFieldWithDuplicate( parser, "archetypes", null, parsed ) )
695             {
696                 java.util.List archetypes = new java.util.ArrayList/*<Archetype>*/();
697                 archetypeCatalog.setArchetypes( archetypes );
698                 while ( parser.nextTag() == XmlPullParser.START_TAG )
699                 {
700                     if ( "archetype".equals( parser.getName() ) )
701                     {
702                         archetypes.add( parseArchetype( parser, strict ) );
703                     }
704                     else
705                     {
706                         checkUnknownElement( parser, strict );
707                     }
708                 }
709             }
710             else
711             {
712                 checkUnknownElement( parser, strict );
713             }
714         }
715         return archetypeCatalog;
716     } //-- ArchetypeCatalog parseArchetypeCatalog( XmlPullParser, boolean )
717 
718     /**
719      * Method read.
720      * 
721      * @param parser
722      * @param strict
723      * @throws IOException
724      * @throws XmlPullParserException
725      * @return ArchetypeCatalog
726      */
727     private ArchetypeCatalog read( XmlPullParser parser, boolean strict )
728         throws IOException, XmlPullParserException
729     {
730         int eventType = parser.getEventType();
731         while ( eventType != XmlPullParser.END_DOCUMENT )
732         {
733             if ( eventType == XmlPullParser.START_TAG )
734             {
735                 if ( strict && ! "archetype-catalog".equals( parser.getName() ) )
736                 {
737                     throw new XmlPullParserException( "Expected root element 'archetype-catalog' but found '" + parser.getName() + "'", parser, null );
738                 }
739                 ArchetypeCatalog archetypeCatalog = parseArchetypeCatalog( parser, strict );
740                 archetypeCatalog.setModelEncoding( parser.getInputEncoding() );
741                 return archetypeCatalog;
742             }
743             eventType = parser.next();
744         }
745         throw new XmlPullParserException( "Expected root element 'archetype-catalog' but found no element at all: invalid XML document", parser, null );
746     } //-- ArchetypeCatalog read( XmlPullParser, boolean )
747 
748     /**
749      * Sets the state of the "add default entities" flag.
750      * 
751      * @param addDefaultEntities
752      */
753     public void setAddDefaultEntities( boolean addDefaultEntities )
754     {
755         this.addDefaultEntities = addDefaultEntities;
756     } //-- void setAddDefaultEntities( boolean )
757 
758     public static interface ContentTransformer
759 {
760     /**
761      * Interpolate the value read from the xpp3 document
762      * @param source The source value
763      * @param fieldName A description of the field being interpolated. The implementation may use this to
764      *                           log stuff.
765      * @return The interpolated value.
766      */
767     String transform( String source, String fieldName );
768 }
769 
770 }