001// =================== DO NOT EDIT THIS FILE ====================
002// Generated by Modello 1.8.3,
003// any modifications will be overwritten.
004// ==============================================================
005
006package org.apache.maven.cli.internal.extension.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.cli.internal.extension.model.CoreExtension;
017import org.apache.maven.cli.internal.extension.model.CoreExtensions;
018import org.codehaus.plexus.util.ReaderFactory;
019import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
020import org.codehaus.plexus.util.xml.pull.MXParser;
021import org.codehaus.plexus.util.xml.pull.XmlPullParser;
022import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
023
024/**
025 * Class CoreExtensionsXpp3Reader.
026 * 
027 * @version $Revision: 965443 $ $Date: 2015-09-14 22:18:52 +0000 (Mon, 14 Sep 2015) $
028 */
029@SuppressWarnings( "all" )
030public class CoreExtensionsXpp3Reader
031{
032
033      //--------------------------/
034     //- Class/Member Variables -/
035    //--------------------------/
036
037    /**
038     * If set the parser will be loaded with all single characters
039     * from the XHTML specification.
040     * The entities used:
041     * <ul>
042     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</li>
043     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li>
044     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li>
045     * </ul>
046     */
047    private boolean addDefaultEntities = true;
048
049    /**
050     * Field contentTransformer.
051     */
052    public final ContentTransformer contentTransformer;
053
054
055      //----------------/
056     //- Constructors -/
057    //----------------/
058
059    public CoreExtensionsXpp3Reader()
060    {
061        this( new ContentTransformer()
062        {
063            public String transform( String source, String fieldName )
064            {
065                return source;
066            }
067        } );
068    } //-- org.apache.maven.cli.internal.extension.model.io.xpp3.CoreExtensionsXpp3Reader()
069
070    public CoreExtensionsXpp3Reader(ContentTransformer contentTransformer)
071    {
072        this.contentTransformer = contentTransformer;
073    } //-- org.apache.maven.cli.internal.extension.model.io.xpp3.CoreExtensionsXpp3Reader(ContentTransformer)
074
075
076      //-----------/
077     //- Methods -/
078    //-----------/
079
080    /**
081     * Method checkFieldWithDuplicate.
082     * 
083     * @param parser
084     * @param parsed
085     * @param alias
086     * @param tagName
087     * @throws XmlPullParserException
088     * @return boolean
089     */
090    private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set parsed )
091        throws XmlPullParserException
092    {
093        if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
094        {
095            return false;
096        }
097        if ( !parsed.add( tagName ) )
098        {
099            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 CoreExtensions
545     */
546    public CoreExtensions 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    } //-- CoreExtensions read( Reader, boolean )
556
557    /**
558     * @see ReaderFactory#newXmlReader
559     * 
560     * @param reader
561     * @throws IOException
562     * @throws XmlPullParserException
563     * @return CoreExtensions
564     */
565    public CoreExtensions read( Reader reader )
566        throws IOException, XmlPullParserException
567    {
568        return read( reader, true );
569    } //-- CoreExtensions read( Reader )
570
571    /**
572     * Method read.
573     * 
574     * @param in
575     * @param strict
576     * @throws IOException
577     * @throws XmlPullParserException
578     * @return CoreExtensions
579     */
580    public CoreExtensions read( InputStream in, boolean strict )
581        throws IOException, XmlPullParserException
582    {
583        return read( ReaderFactory.newXmlReader( in ), strict );
584    } //-- CoreExtensions read( InputStream, boolean )
585
586    /**
587     * Method read.
588     * 
589     * @param in
590     * @throws IOException
591     * @throws XmlPullParserException
592     * @return CoreExtensions
593     */
594    public CoreExtensions read( InputStream in )
595        throws IOException, XmlPullParserException
596    {
597        return read( ReaderFactory.newXmlReader( in ) );
598    } //-- CoreExtensions read( InputStream )
599
600    /**
601     * Method parseCoreExtension.
602     * 
603     * @param parser
604     * @param strict
605     * @throws IOException
606     * @throws XmlPullParserException
607     * @return CoreExtension
608     */
609    private CoreExtension parseCoreExtension( XmlPullParser parser, boolean strict )
610        throws IOException, XmlPullParserException
611    {
612        String tagName = parser.getName();
613        CoreExtension coreExtension = new CoreExtension();
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                coreExtension.setGroupId( interpolatedTrimmed( parser.nextText(), "groupId" ) );
634            }
635            else if ( checkFieldWithDuplicate( parser, "artifactId", null, parsed ) )
636            {
637                coreExtension.setArtifactId( interpolatedTrimmed( parser.nextText(), "artifactId" ) );
638            }
639            else if ( checkFieldWithDuplicate( parser, "version", null, parsed ) )
640            {
641                coreExtension.setVersion( interpolatedTrimmed( parser.nextText(), "version" ) );
642            }
643            else
644            {
645                checkUnknownElement( parser, strict );
646            }
647        }
648        return coreExtension;
649    } //-- CoreExtension parseCoreExtension( XmlPullParser, boolean )
650
651    /**
652     * Method parseCoreExtensions.
653     * 
654     * @param parser
655     * @param strict
656     * @throws IOException
657     * @throws XmlPullParserException
658     * @return CoreExtensions
659     */
660    private CoreExtensions parseCoreExtensions( XmlPullParser parser, boolean strict )
661        throws IOException, XmlPullParserException
662    {
663        String tagName = parser.getName();
664        CoreExtensions coreExtensions = new CoreExtensions();
665        for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
666        {
667            String name = parser.getAttributeName( i );
668            String value = parser.getAttributeValue( i );
669
670            if ( name.indexOf( ':' ) >= 0 )
671            {
672                // just ignore attributes with non-default namespace (for example: xmlns:xsi)
673            }
674            else if ( "xmlns".equals( name ) )
675            {
676                // ignore xmlns attribute in root class, which is a reserved attribute name
677            }
678            else
679            {
680                checkUnknownAttribute( parser, name, tagName, strict );
681            }
682        }
683        java.util.Set parsed = new java.util.HashSet();
684        while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
685        {
686            if ( "extension".equals( parser.getName() ) )
687            {
688                java.util.List extensions = coreExtensions.getExtensions();
689                if ( extensions == null )
690                {
691                    extensions = new java.util.ArrayList/*<CoreExtension>*/();
692                    coreExtensions.setExtensions( extensions );
693                }
694                extensions.add( parseCoreExtension( parser, strict ) );
695            }
696            else
697            {
698                checkUnknownElement( parser, strict );
699            }
700        }
701        return coreExtensions;
702    } //-- CoreExtensions parseCoreExtensions( XmlPullParser, boolean )
703
704    /**
705     * Method read.
706     * 
707     * @param parser
708     * @param strict
709     * @throws IOException
710     * @throws XmlPullParserException
711     * @return CoreExtensions
712     */
713    private CoreExtensions read( XmlPullParser parser, boolean strict )
714        throws IOException, XmlPullParserException
715    {
716        int eventType = parser.getEventType();
717        while ( eventType != XmlPullParser.END_DOCUMENT )
718        {
719            if ( eventType == XmlPullParser.START_TAG )
720            {
721                if ( strict && ! "extensions".equals( parser.getName() ) )
722                {
723                    throw new XmlPullParserException( "Expected root element 'extensions' but found '" + parser.getName() + "'", parser, null );
724                }
725                CoreExtensions coreExtensions = parseCoreExtensions( parser, strict );
726                coreExtensions.setModelEncoding( parser.getInputEncoding() );
727                return coreExtensions;
728            }
729            eventType = parser.next();
730        }
731        throw new XmlPullParserException( "Expected root element 'extensions' but found no element at all: invalid XML document", parser, null );
732    } //-- CoreExtensions read( XmlPullParser, boolean )
733
734    /**
735     * Sets the state of the "add default entities" flag.
736     * 
737     * @param addDefaultEntities
738     */
739    public void setAddDefaultEntities( boolean addDefaultEntities )
740    {
741        this.addDefaultEntities = addDefaultEntities;
742    } //-- void setAddDefaultEntities( boolean )
743
744    public static interface ContentTransformer
745{
746    /**
747     * Interpolate the value read from the xpp3 document
748     * @param source The source value
749     * @param fieldName A description of the field being interpolated. The implementation may use this to
750     *                           log stuff.
751     * @return The interpolated value.
752     */
753    String transform( String source, String fieldName );
754}
755
756}