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