001// =================== DO NOT EDIT THIS FILE ====================
002// Generated by Modello 2.0.0,
003// any modifications will be overwritten.
004// ==============================================================
005
006package org.apache.maven.scm.provider.local.metadata.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.scm.provider.local.metadata.LocalScmMetadata;
017import org.codehaus.plexus.util.ReaderFactory;
018import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
019import org.codehaus.plexus.util.xml.pull.MXParser;
020import org.codehaus.plexus.util.xml.pull.XmlPullParser;
021import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
022
023/**
024 * Class LocalScmMetadataXpp3Reader.
025 * 
026 * @version $Revision$ $Date$
027 */
028@SuppressWarnings( "all" )
029public class LocalScmMetadataXpp3Reader
030{
031
032      //--------------------------/
033     //- Class/Member Variables -/
034    //--------------------------/
035
036    /**
037     * If set the parser will be loaded with all single characters
038     * from the XHTML specification.
039     * The entities used:
040     * <ul>
041     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</li>
042     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li>
043     * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li>
044     * </ul>
045     */
046    private boolean addDefaultEntities = true;
047
048    /**
049     * Field contentTransformer.
050     */
051    public final ContentTransformer contentTransformer;
052
053
054      //----------------/
055     //- Constructors -/
056    //----------------/
057
058    public LocalScmMetadataXpp3Reader()
059    {
060        this( new ContentTransformer()
061        {
062            public String transform( String source, String fieldName )
063            {
064                return source;
065            }
066        } );
067    } //-- org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader()
068
069    public LocalScmMetadataXpp3Reader(ContentTransformer contentTransformer)
070    {
071        this.contentTransformer = contentTransformer;
072    } //-- org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader(ContentTransformer)
073
074
075      //-----------/
076     //- Methods -/
077    //-----------/
078
079    /**
080     * Method checkFieldWithDuplicate.
081     * 
082     * @param parser a parser object.
083     * @param parsed a parsed object.
084     * @param alias a alias object.
085     * @param tagName a tagName object.
086     * @throws XmlPullParserException XmlPullParserException if
087     * any.
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 a parser object.
108     * @param strict a strict object.
109     * @param tagName a tagName object.
110     * @param attribute a attribute object.
111     * @throws XmlPullParserException XmlPullParserException if
112     * any.
113     * @throws IOException IOException if any.
114     */
115    private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict )
116        throws XmlPullParserException, IOException
117    {
118        // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
119        if ( strict )
120        {
121            throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
122        }
123    } //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean )
124
125    /**
126     * Method checkUnknownElement.
127     * 
128     * @param parser a parser object.
129     * @param strict a strict object.
130     * @throws XmlPullParserException XmlPullParserException if
131     * any.
132     * @throws IOException IOException if any.
133     */
134    private void checkUnknownElement( XmlPullParser parser, boolean strict )
135        throws XmlPullParserException, IOException
136    {
137        if ( strict )
138        {
139            throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null );
140        }
141
142        for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; )
143        {
144            int eventType = parser.next();
145            if ( eventType == XmlPullParser.START_TAG )
146            {
147                unrecognizedTagCount++;
148            }
149            else if ( eventType == XmlPullParser.END_TAG )
150            {
151                unrecognizedTagCount--;
152            }
153        }
154    } //-- void checkUnknownElement( XmlPullParser, boolean )
155
156    /**
157     * Returns the state of the "add default entities" flag.
158     * 
159     * @return boolean
160     */
161    public boolean getAddDefaultEntities()
162    {
163        return addDefaultEntities;
164    } //-- boolean getAddDefaultEntities()
165
166    /**
167     * Method getBooleanValue.
168     * 
169     * @param s a s object.
170     * @param parser a parser object.
171     * @param attribute a attribute object.
172     * @throws XmlPullParserException XmlPullParserException if
173     * any.
174     * @return boolean
175     */
176    private boolean getBooleanValue( String s, String attribute, XmlPullParser parser )
177        throws XmlPullParserException
178    {
179        return getBooleanValue( s, attribute, parser, null );
180    } //-- boolean getBooleanValue( String, String, XmlPullParser )
181
182    /**
183     * Method getBooleanValue.
184     * 
185     * @param s a s object.
186     * @param defaultValue a defaultValue object.
187     * @param parser a parser object.
188     * @param attribute a attribute object.
189     * @throws XmlPullParserException XmlPullParserException if
190     * any.
191     * @return boolean
192     */
193    private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, String defaultValue )
194        throws XmlPullParserException
195    {
196        if ( s != null && s.length() != 0 )
197        {
198            return Boolean.valueOf( s ).booleanValue();
199        }
200        if ( defaultValue != null )
201        {
202            return Boolean.valueOf( defaultValue ).booleanValue();
203        }
204        return false;
205    } //-- boolean getBooleanValue( String, String, XmlPullParser, String )
206
207    /**
208     * Method getByteValue.
209     * 
210     * @param s a s object.
211     * @param strict a strict object.
212     * @param parser a parser object.
213     * @param attribute a attribute object.
214     * @throws XmlPullParserException XmlPullParserException if
215     * any.
216     * @return byte
217     */
218    private byte getByteValue( String s, String attribute, XmlPullParser parser, boolean strict )
219        throws XmlPullParserException
220    {
221        if ( s != null )
222        {
223            try
224            {
225                return Byte.valueOf( s ).byteValue();
226            }
227            catch ( NumberFormatException nfe )
228            {
229                if ( strict )
230                {
231                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a byte", parser, nfe );
232                }
233            }
234        }
235        return 0;
236    } //-- byte getByteValue( String, String, XmlPullParser, boolean )
237
238    /**
239     * Method getCharacterValue.
240     * 
241     * @param s a s object.
242     * @param parser a parser object.
243     * @param attribute a attribute object.
244     * @throws XmlPullParserException XmlPullParserException if
245     * any.
246     * @return char
247     */
248    private char getCharacterValue( String s, String attribute, XmlPullParser parser )
249        throws XmlPullParserException
250    {
251        if ( s != null )
252        {
253            return s.charAt( 0 );
254        }
255        return 0;
256    } //-- char getCharacterValue( String, String, XmlPullParser )
257
258    /**
259     * Method getDateValue.
260     * 
261     * @param s a s object.
262     * @param parser a parser object.
263     * @param attribute a attribute object.
264     * @throws XmlPullParserException XmlPullParserException if
265     * any.
266     * @return Date
267     */
268    private java.util.Date getDateValue( String s, String attribute, XmlPullParser parser )
269        throws XmlPullParserException
270    {
271        return getDateValue( s, attribute, null, parser );
272    } //-- java.util.Date getDateValue( String, String, XmlPullParser )
273
274    /**
275     * Method getDateValue.
276     * 
277     * @param s a s object.
278     * @param parser a parser object.
279     * @param dateFormat a dateFormat object.
280     * @param attribute a attribute object.
281     * @throws XmlPullParserException XmlPullParserException if
282     * any.
283     * @return Date
284     */
285    private java.util.Date getDateValue( String s, String attribute, String dateFormat, XmlPullParser parser )
286        throws XmlPullParserException
287    {
288        if ( s != null )
289        {
290            String effectiveDateFormat = dateFormat;
291            if ( dateFormat == null )
292            {
293                effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
294            }
295            if ( "long".equals( effectiveDateFormat ) )
296            {
297                try
298                {
299                    return new java.util.Date( Long.parseLong( s ) );
300                }
301                catch ( NumberFormatException e )
302                {
303                    throw new XmlPullParserException( e.getMessage(), parser, e );
304                }
305            }
306            else
307            {
308                try
309                {
310                    DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US );
311                    return dateParser.parse( s );
312                }
313                catch ( java.text.ParseException e )
314                {
315                    throw new XmlPullParserException( e.getMessage(), parser, e );
316                }
317            }
318        }
319        return null;
320    } //-- java.util.Date getDateValue( String, String, String, XmlPullParser )
321
322    /**
323     * Method getDoubleValue.
324     * 
325     * @param s a s object.
326     * @param strict a strict object.
327     * @param parser a parser object.
328     * @param attribute a attribute object.
329     * @throws XmlPullParserException XmlPullParserException if
330     * any.
331     * @return double
332     */
333    private double getDoubleValue( String s, String attribute, XmlPullParser parser, boolean strict )
334        throws XmlPullParserException
335    {
336        if ( s != null )
337        {
338            try
339            {
340                return Double.valueOf( s ).doubleValue();
341            }
342            catch ( NumberFormatException nfe )
343            {
344                if ( strict )
345                {
346                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
347                }
348            }
349        }
350        return 0;
351    } //-- double getDoubleValue( String, String, XmlPullParser, boolean )
352
353    /**
354     * Method getFloatValue.
355     * 
356     * @param s a s object.
357     * @param strict a strict object.
358     * @param parser a parser object.
359     * @param attribute a attribute object.
360     * @throws XmlPullParserException XmlPullParserException if
361     * any.
362     * @return float
363     */
364    private float getFloatValue( String s, String attribute, XmlPullParser parser, boolean strict )
365        throws XmlPullParserException
366    {
367        if ( s != null )
368        {
369            try
370            {
371                return Float.valueOf( s ).floatValue();
372            }
373            catch ( NumberFormatException nfe )
374            {
375                if ( strict )
376                {
377                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
378                }
379            }
380        }
381        return 0;
382    } //-- float getFloatValue( String, String, XmlPullParser, boolean )
383
384    /**
385     * Method getIntegerValue.
386     * 
387     * @param s a s object.
388     * @param strict a strict object.
389     * @param parser a parser object.
390     * @param attribute a attribute object.
391     * @throws XmlPullParserException XmlPullParserException if
392     * any.
393     * @return int
394     */
395    private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict )
396        throws XmlPullParserException
397    {
398        if ( s != null )
399        {
400            try
401            {
402                return Integer.valueOf( s ).intValue();
403            }
404            catch ( NumberFormatException nfe )
405            {
406                if ( strict )
407                {
408                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe );
409                }
410            }
411        }
412        return 0;
413    } //-- int getIntegerValue( String, String, XmlPullParser, boolean )
414
415    /**
416     * Method getLongValue.
417     * 
418     * @param s a s object.
419     * @param strict a strict object.
420     * @param parser a parser object.
421     * @param attribute a attribute object.
422     * @throws XmlPullParserException XmlPullParserException if
423     * any.
424     * @return long
425     */
426    private long getLongValue( String s, String attribute, XmlPullParser parser, boolean strict )
427        throws XmlPullParserException
428    {
429        if ( s != null )
430        {
431            try
432            {
433                return Long.valueOf( s ).longValue();
434            }
435            catch ( NumberFormatException nfe )
436            {
437                if ( strict )
438                {
439                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a long integer", parser, nfe );
440                }
441            }
442        }
443        return 0;
444    } //-- long getLongValue( String, String, XmlPullParser, boolean )
445
446    /**
447     * Method getRequiredAttributeValue.
448     * 
449     * @param s a s object.
450     * @param strict a strict object.
451     * @param parser a parser object.
452     * @param attribute a attribute object.
453     * @throws XmlPullParserException XmlPullParserException if
454     * any.
455     * @return String
456     */
457    private String getRequiredAttributeValue( String s, String attribute, XmlPullParser parser, boolean strict )
458        throws XmlPullParserException
459    {
460        if ( s == null )
461        {
462            if ( strict )
463            {
464                throw new XmlPullParserException( "Missing required value for attribute '" + attribute + "'", parser, null );
465            }
466        }
467        return s;
468    } //-- String getRequiredAttributeValue( String, String, XmlPullParser, boolean )
469
470    /**
471     * Method getShortValue.
472     * 
473     * @param s a s object.
474     * @param strict a strict object.
475     * @param parser a parser object.
476     * @param attribute a attribute object.
477     * @throws XmlPullParserException XmlPullParserException if
478     * any.
479     * @return short
480     */
481    private short getShortValue( String s, String attribute, XmlPullParser parser, boolean strict )
482        throws XmlPullParserException
483    {
484        if ( s != null )
485        {
486            try
487            {
488                return Short.valueOf( s ).shortValue();
489            }
490            catch ( NumberFormatException nfe )
491            {
492                if ( strict )
493                {
494                    throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a short integer", parser, nfe );
495                }
496            }
497        }
498        return 0;
499    } //-- short getShortValue( String, String, XmlPullParser, boolean )
500
501    /**
502     * Method getTrimmedValue.
503     * 
504     * @param s a s object.
505     * @return String
506     */
507    private String getTrimmedValue( String s )
508    {
509        if ( s != null )
510        {
511            s = s.trim();
512        }
513        return s;
514    } //-- String getTrimmedValue( String )
515
516    /**
517     * Method interpolatedTrimmed.
518     * 
519     * @param value a value object.
520     * @param context a context object.
521     * @return String
522     */
523    private String interpolatedTrimmed( String value, String context )
524    {
525        return getTrimmedValue( contentTransformer.transform( value, context ) );
526    } //-- String interpolatedTrimmed( String, String )
527
528    /**
529     * Method nextTag.
530     * 
531     * @param parser a parser object.
532     * @throws IOException IOException if any.
533     * @throws XmlPullParserException XmlPullParserException if
534     * any.
535     * @return int
536     */
537    private int nextTag( XmlPullParser parser )
538        throws IOException, XmlPullParserException
539    {
540        int eventType = parser.next();
541        if ( eventType == XmlPullParser.TEXT )
542        {
543            eventType = parser.next();
544        }
545        if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG )
546        {
547            throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null );
548        }
549        return eventType;
550    } //-- int nextTag( XmlPullParser )
551
552    /**
553     * @see ReaderFactory#newXmlReader
554     * 
555     * @param reader a reader object.
556     * @param strict a strict object.
557     * @throws IOException IOException if any.
558     * @throws XmlPullParserException XmlPullParserException if
559     * any.
560     * @return LocalScmMetadata
561     */
562    public LocalScmMetadata read( Reader reader, boolean strict )
563        throws IOException, XmlPullParserException
564    {
565        XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( );
566
567        parser.setInput( reader );
568
569
570        return read( parser, strict );
571    } //-- LocalScmMetadata read( Reader, boolean )
572
573    /**
574     * @see ReaderFactory#newXmlReader
575     * 
576     * @param reader a reader object.
577     * @throws IOException IOException if any.
578     * @throws XmlPullParserException XmlPullParserException if
579     * any.
580     * @return LocalScmMetadata
581     */
582    public LocalScmMetadata read( Reader reader )
583        throws IOException, XmlPullParserException
584    {
585        return read( reader, true );
586    } //-- LocalScmMetadata read( Reader )
587
588    /**
589     * Method read.
590     * 
591     * @param in a in object.
592     * @param strict a strict object.
593     * @throws IOException IOException if any.
594     * @throws XmlPullParserException XmlPullParserException if
595     * any.
596     * @return LocalScmMetadata
597     */
598    public LocalScmMetadata read( InputStream in, boolean strict )
599        throws IOException, XmlPullParserException
600    {
601        return read( ReaderFactory.newXmlReader( in ), strict );
602    } //-- LocalScmMetadata read( InputStream, boolean )
603
604    /**
605     * Method read.
606     * 
607     * @param in a in object.
608     * @throws IOException IOException if any.
609     * @throws XmlPullParserException XmlPullParserException if
610     * any.
611     * @return LocalScmMetadata
612     */
613    public LocalScmMetadata read( InputStream in )
614        throws IOException, XmlPullParserException
615    {
616        return read( ReaderFactory.newXmlReader( in ) );
617    } //-- LocalScmMetadata read( InputStream )
618
619    /**
620     * Method parseLocalScmMetadata.
621     * 
622     * @param parser a parser object.
623     * @param strict a strict object.
624     * @throws IOException IOException if any.
625     * @throws XmlPullParserException XmlPullParserException if
626     * any.
627     * @return LocalScmMetadata
628     */
629    private LocalScmMetadata parseLocalScmMetadata( XmlPullParser parser, boolean strict )
630        throws IOException, XmlPullParserException
631    {
632        String tagName = parser.getName();
633        LocalScmMetadata localScmMetadata = new LocalScmMetadata();
634        for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
635        {
636            String name = parser.getAttributeName( i );
637            String value = parser.getAttributeValue( i );
638
639            if ( name.indexOf( ':' ) >= 0 )
640            {
641                // just ignore attributes with non-default namespace (for example: xmlns:xsi)
642            }
643            else if ( "xmlns".equals( name ) )
644            {
645                // ignore xmlns attribute in root class, which is a reserved attribute name
646            }
647            else
648            {
649                checkUnknownAttribute( parser, name, tagName, strict );
650            }
651        }
652        java.util.Set parsed = new java.util.HashSet();
653        while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
654        {
655            if ( checkFieldWithDuplicate( parser, "repositoryFileNames", null, parsed ) )
656            {
657                java.util.List<String> repositoryFileNames = new java.util.ArrayList<String>();
658                localScmMetadata.setRepositoryFileNames( repositoryFileNames );
659                while ( parser.nextTag() == XmlPullParser.START_TAG )
660                {
661                    if ( "repositoryFileName".equals( parser.getName() ) )
662                    {
663                        repositoryFileNames.add( interpolatedTrimmed( parser.nextText(), "repositoryFileNames" ) );
664                    }
665                    else
666                    {
667                        checkUnknownElement( parser, strict );
668                    }
669                }
670            }
671            else
672            {
673                checkUnknownElement( parser, strict );
674            }
675        }
676        return localScmMetadata;
677    } //-- LocalScmMetadata parseLocalScmMetadata( XmlPullParser, boolean )
678
679    /**
680     * Method read.
681     * 
682     * @param parser a parser object.
683     * @param strict a strict object.
684     * @throws IOException IOException if any.
685     * @throws XmlPullParserException XmlPullParserException if
686     * any.
687     * @return LocalScmMetadata
688     */
689    private LocalScmMetadata read( XmlPullParser parser, boolean strict )
690        throws IOException, XmlPullParserException
691    {
692        LocalScmMetadata localScmMetadata = null;
693        int eventType = parser.getEventType();
694        boolean parsed = false;
695        while ( eventType != XmlPullParser.END_DOCUMENT )
696        {
697            if ( eventType == XmlPullParser.START_TAG )
698            {
699                if ( strict && ! "localScmMetadata".equals( parser.getName() ) )
700                {
701                    throw new XmlPullParserException( "Expected root element 'localScmMetadata' but found '" + parser.getName() + "'", parser, null );
702                }
703                else if ( parsed )
704                {
705                    // fallback, already expected a XmlPullParserException due to invalid XML
706                    throw new XmlPullParserException( "Duplicated tag: 'localScmMetadata'", parser, null );
707                }
708                localScmMetadata = parseLocalScmMetadata( parser, strict );
709                localScmMetadata.setModelEncoding( parser.getInputEncoding() );
710                parsed = true;
711            }
712            eventType = parser.next();
713        }
714        if ( parsed )
715        {
716            return localScmMetadata;
717        }
718        throw new XmlPullParserException( "Expected root element 'localScmMetadata' but found no element at all: invalid XML document", parser, null );
719    } //-- LocalScmMetadata read( XmlPullParser, boolean )
720
721    /**
722     * Sets the state of the "add default entities" flag.
723     * 
724     * @param addDefaultEntities a addDefaultEntities object.
725     */
726    public void setAddDefaultEntities( boolean addDefaultEntities )
727    {
728        this.addDefaultEntities = addDefaultEntities;
729    } //-- void setAddDefaultEntities( boolean )
730
731    public static interface ContentTransformer
732{
733    /**
734     * Interpolate the value read from the xpp3 document
735     * @param source The source value
736     * @param fieldName A description of the field being interpolated. The implementation may use this to
737     *                           log stuff.
738     * @return The interpolated value.
739     */
740    String transform( String source, String fieldName );
741}
742
743}