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