001// =================== DO NOT EDIT THIS FILE ====================
002// Generated by Modello 1.8.1,
003// any modifications will be overwritten.
004// ==============================================================
005
006package org.apache.maven.toolchain.model.io.xpp3;
007
008  //---------------------------------/
009 //- Imported classes and packages -/
010//---------------------------------/
011
012import java.io.IOException;
013import java.io.InputStream;
014import java.io.Reader;
015import java.text.DateFormat;
016import org.apache.maven.toolchain.model.PersistedToolchains;
017import org.apache.maven.toolchain.model.ToolchainModel;
018import org.apache.maven.toolchain.model.TrackableBase;
019import org.codehaus.plexus.util.ReaderFactory;
020import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
021import org.codehaus.plexus.util.xml.pull.MXParser;
022import org.codehaus.plexus.util.xml.pull.XmlPullParser;
023import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
024
025/**
026 * Class MavenToolchainsXpp3Reader.
027 * 
028 * @version $Revision$ $Date$
029 */
030@SuppressWarnings( "all" )
031public class MavenToolchainsXpp3Reader
032{
033
034      //--------------------------/
035     //- Class/Member Variables -/
036    //--------------------------/
037
038    /**
039     * If set the parser will be loaded with all single characters
040     * from the XHTML specification.
041     * The entities used:
042     * <ul>
043     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</li>
044     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li>
045     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li>
046     * </ul>
047     */
048    private boolean addDefaultEntities = true;
049
050
051      //-----------/
052     //- Methods -/
053    //-----------/
054
055    /**
056     * Method checkFieldWithDuplicate.
057     * 
058     * @param parser
059     * @param parsed
060     * @param alias
061     * @param tagName
062     * @throws XmlPullParserException
063     * @return boolean
064     */
065    private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set parsed )
066        throws XmlPullParserException
067    {
068        if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
069        {
070            return false;
071        }
072        if ( !parsed.add( tagName ) )
073        {
074            throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
075        }
076        return true;
077    } //-- boolean checkFieldWithDuplicate( XmlPullParser, String, String, java.util.Set )
078
079    /**
080     * Method checkUnknownAttribute.
081     * 
082     * @param parser
083     * @param strict
084     * @param tagName
085     * @param attribute
086     * @throws XmlPullParserException
087     * @throws IOException
088     */
089    private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict )
090        throws XmlPullParserException, IOException
091    {
092        // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
093        if ( strict )
094        {
095            throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
096        }
097    } //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean )
098
099    /**
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}