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.usability.plugin.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.usability.plugin.Expression;
017import org.apache.maven.usability.plugin.ExpressionDocumentation;
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 ParamdocXpp3Reader.
026 * 
027 * @version $Revision$ $Date$
028 */
029@SuppressWarnings( "all" )
030public class ParamdocXpp3Reader
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 ParamdocXpp3Reader()
060    {
061        this( new ContentTransformer()
062        {
063            public String transform( String source, String fieldName )
064            {
065                return source;
066            }
067        } );
068    } //-- org.apache.maven.usability.plugin.io.xpp3.ParamdocXpp3Reader()
069
070    public ParamdocXpp3Reader(ContentTransformer contentTransformer)
071    {
072        this.contentTransformer = contentTransformer;
073    } //-- org.apache.maven.usability.plugin.io.xpp3.ParamdocXpp3Reader(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 ExpressionDocumentation
545     */
546    public ExpressionDocumentation 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    } //-- ExpressionDocumentation read( Reader, boolean )
556
557    /**
558     * @see ReaderFactory#newXmlReader
559     * 
560     * @param reader
561     * @throws IOException
562     * @throws XmlPullParserException
563     * @return ExpressionDocumentation
564     */
565    public ExpressionDocumentation read( Reader reader )
566        throws IOException, XmlPullParserException
567    {
568        return read( reader, true );
569    } //-- ExpressionDocumentation read( Reader )
570
571    /**
572     * Method read.
573     * 
574     * @param in
575     * @param strict
576     * @throws IOException
577     * @throws XmlPullParserException
578     * @return ExpressionDocumentation
579     */
580    public ExpressionDocumentation read( InputStream in, boolean strict )
581        throws IOException, XmlPullParserException
582    {
583        return read( ReaderFactory.newXmlReader( in ), strict );
584    } //-- ExpressionDocumentation read( InputStream, boolean )
585
586    /**
587     * Method read.
588     * 
589     * @param in
590     * @throws IOException
591     * @throws XmlPullParserException
592     * @return ExpressionDocumentation
593     */
594    public ExpressionDocumentation read( InputStream in )
595        throws IOException, XmlPullParserException
596    {
597        return read( ReaderFactory.newXmlReader( in ) );
598    } //-- ExpressionDocumentation read( InputStream )
599
600    /**
601     * Method parseExpression.
602     * 
603     * @param parser
604     * @param strict
605     * @throws IOException
606     * @throws XmlPullParserException
607     * @return Expression
608     */
609    private Expression parseExpression( XmlPullParser parser, boolean strict )
610        throws IOException, XmlPullParserException
611    {
612        String tagName = parser.getName();
613        Expression expression = new Expression();
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, "syntax", null, parsed ) )
632            {
633                expression.setSyntax( interpolatedTrimmed( parser.nextText(), "syntax" ) );
634            }
635            else if ( checkFieldWithDuplicate( parser, "description", null, parsed ) )
636            {
637                expression.setDescription( interpolatedTrimmed( parser.nextText(), "description" ) );
638            }
639            else if ( checkFieldWithDuplicate( parser, "configuration", null, parsed ) )
640            {
641                expression.setConfiguration( interpolatedTrimmed( parser.nextText(), "configuration" ) );
642            }
643            else if ( checkFieldWithDuplicate( parser, "cliOptions", null, parsed ) )
644            {
645                while ( parser.nextTag() == XmlPullParser.START_TAG )
646                {
647                    if ( "cliOption".equals( parser.getName() ) )
648                    {
649                        String key = null;
650                        String value = null;
651                        // explode mode.
652                        while ( parser.nextTag() == XmlPullParser.START_TAG )
653                        {
654                            if ( "key".equals( parser.getName() ) )
655                            {
656                                key = parser.nextText();
657                            }
658                            else if ( "value".equals( parser.getName() ) )
659                            {
660                                value = parser.nextText().trim();
661                            }
662                            else
663                            {
664                                parser.nextText();
665                            }
666                        }
667                        expression.addCliOption( key, value );
668                    }
669                    parser.next();
670                }
671            }
672            else if ( checkFieldWithDuplicate( parser, "apiMethods", null, parsed ) )
673            {
674                while ( parser.nextTag() == XmlPullParser.START_TAG )
675                {
676                    if ( "apiMethod".equals( parser.getName() ) )
677                    {
678                        String key = null;
679                        String value = null;
680                        // explode mode.
681                        while ( parser.nextTag() == XmlPullParser.START_TAG )
682                        {
683                            if ( "key".equals( parser.getName() ) )
684                            {
685                                key = parser.nextText();
686                            }
687                            else if ( "value".equals( parser.getName() ) )
688                            {
689                                value = parser.nextText().trim();
690                            }
691                            else
692                            {
693                                parser.nextText();
694                            }
695                        }
696                        expression.addApiMethod( key, value );
697                    }
698                    parser.next();
699                }
700            }
701            else if ( checkFieldWithDuplicate( parser, "deprecation", null, parsed ) )
702            {
703                expression.setDeprecation( interpolatedTrimmed( parser.nextText(), "deprecation" ) );
704            }
705            else if ( checkFieldWithDuplicate( parser, "ban", null, parsed ) )
706            {
707                expression.setBan( interpolatedTrimmed( parser.nextText(), "ban" ) );
708            }
709            else if ( checkFieldWithDuplicate( parser, "editable", null, parsed ) )
710            {
711                expression.setEditable( getBooleanValue( interpolatedTrimmed( parser.nextText(), "editable" ), "editable", parser, "true" ) );
712            }
713            else
714            {
715                checkUnknownElement( parser, strict );
716            }
717        }
718        return expression;
719    } //-- Expression parseExpression( XmlPullParser, boolean )
720
721    /**
722     * Method parseExpressionDocumentation.
723     * 
724     * @param parser
725     * @param strict
726     * @throws IOException
727     * @throws XmlPullParserException
728     * @return ExpressionDocumentation
729     */
730    private ExpressionDocumentation parseExpressionDocumentation( XmlPullParser parser, boolean strict )
731        throws IOException, XmlPullParserException
732    {
733        String tagName = parser.getName();
734        ExpressionDocumentation expressionDocumentation = new ExpressionDocumentation();
735        for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
736        {
737            String name = parser.getAttributeName( i );
738            String value = parser.getAttributeValue( i );
739
740            if ( name.indexOf( ':' ) >= 0 )
741            {
742                // just ignore attributes with non-default namespace (for example: xmlns:xsi)
743            }
744            else if ( "xmlns".equals( name ) )
745            {
746                // ignore xmlns attribute in root class, which is a reserved attribute name
747            }
748            else
749            {
750                checkUnknownAttribute( parser, name, tagName, strict );
751            }
752        }
753        java.util.Set parsed = new java.util.HashSet();
754        while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
755        {
756            if ( checkFieldWithDuplicate( parser, "expressions", null, parsed ) )
757            {
758                java.util.List expressions = new java.util.ArrayList/*<Expression>*/();
759                expressionDocumentation.setExpressions( expressions );
760                while ( parser.nextTag() == XmlPullParser.START_TAG )
761                {
762                    if ( "expression".equals( parser.getName() ) )
763                    {
764                        expressions.add( parseExpression( parser, strict ) );
765                    }
766                    else
767                    {
768                        checkUnknownElement( parser, strict );
769                    }
770                }
771            }
772            else
773            {
774                checkUnknownElement( parser, strict );
775            }
776        }
777        return expressionDocumentation;
778    } //-- ExpressionDocumentation parseExpressionDocumentation( XmlPullParser, boolean )
779
780    /**
781     * Method read.
782     * 
783     * @param parser
784     * @param strict
785     * @throws IOException
786     * @throws XmlPullParserException
787     * @return ExpressionDocumentation
788     */
789    private ExpressionDocumentation read( XmlPullParser parser, boolean strict )
790        throws IOException, XmlPullParserException
791    {
792        int eventType = parser.getEventType();
793        while ( eventType != XmlPullParser.END_DOCUMENT )
794        {
795            if ( eventType == XmlPullParser.START_TAG )
796            {
797                if ( strict && ! "paramdoc".equals( parser.getName() ) )
798                {
799                    throw new XmlPullParserException( "Expected root element 'paramdoc' but found '" + parser.getName() + "'", parser, null );
800                }
801                ExpressionDocumentation expressionDocumentation = parseExpressionDocumentation( parser, strict );
802                expressionDocumentation.setModelEncoding( parser.getInputEncoding() );
803                return expressionDocumentation;
804            }
805            eventType = parser.next();
806        }
807        throw new XmlPullParserException( "Expected root element 'paramdoc' but found no element at all: invalid XML document", parser, null );
808    } //-- ExpressionDocumentation read( XmlPullParser, boolean )
809
810    /**
811     * Sets the state of the "add default entities" flag.
812     * 
813     * @param addDefaultEntities
814     */
815    public void setAddDefaultEntities( boolean addDefaultEntities )
816    {
817        this.addDefaultEntities = addDefaultEntities;
818    } //-- void setAddDefaultEntities( boolean )
819
820    public static interface ContentTransformer
821{
822    /**
823     * Interpolate the value read from the xpp3 document
824     * @param source The source value
825     * @param fieldName A description of the field being interpolated. The implementation may use this to
826     *                           log stuff.
827     * @return The interpolated value.
828     */
829    String transform( String source, String fieldName );
830}
831
832}