001// =================== DO NOT EDIT THIS FILE ====================
002// Generated by Modello 2.1.2,
003// any modifications will be overwritten.
004// ==============================================================
005
006package org.apache.maven.scm.providers.svn.settings.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.providers.svn.settings.Settings;
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 SvnXpp3Reader.
025 * 
026 * @version $Revision$ $Date$
027 */
028@SuppressWarnings( "all" )
029public class SvnXpp3Reader
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 SvnXpp3Reader()
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.providers.svn.settings.io.xpp3.SvnXpp3Reader()
068
069    public SvnXpp3Reader(ContentTransformer contentTransformer)
070    {
071        this.contentTransformer = contentTransformer;
072    } //-- org.apache.maven.scm.providers.svn.settings.io.xpp3.SvnXpp3Reader(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     * Method read.
554     * 
555     * @param parser a parser object.
556     * @param strict a strict object.
557     * @throws IOException IOException if any.
558     * @throws XmlPullParserException XmlPullParserException if
559     * any.
560     * @return Settings
561     */
562    public Settings read( XmlPullParser parser, boolean strict )
563        throws IOException, XmlPullParserException
564    {
565        Settings settings = null;
566        int eventType = parser.getEventType();
567        boolean parsed = false;
568        while ( eventType != XmlPullParser.END_DOCUMENT )
569        {
570            if ( eventType == XmlPullParser.START_TAG )
571            {
572                if ( strict && ! "svn-settings".equals( parser.getName() ) )
573                {
574                    throw new XmlPullParserException( "Expected root element 'svn-settings' but found '" + parser.getName() + "'", parser, null );
575                }
576                else if ( parsed )
577                {
578                    // fallback, already expected a XmlPullParserException due to invalid XML
579                    throw new XmlPullParserException( "Duplicated tag: 'svn-settings'", parser, null );
580                }
581                settings = parseSettings( parser, strict );
582                settings.setModelEncoding( parser.getInputEncoding() );
583                parsed = true;
584            }
585            eventType = parser.next();
586        }
587        if ( parsed )
588        {
589            return settings;
590        }
591        throw new XmlPullParserException( "Expected root element 'svn-settings' but found no element at all: invalid XML document", parser, null );
592    } //-- Settings read( XmlPullParser, boolean )
593
594    /**
595     * @see ReaderFactory#newXmlReader
596     * 
597     * @param reader a reader object.
598     * @param strict a strict object.
599     * @throws IOException IOException if any.
600     * @throws XmlPullParserException XmlPullParserException if
601     * any.
602     * @return Settings
603     */
604    public Settings read( Reader reader, boolean strict )
605        throws IOException, XmlPullParserException
606    {
607        XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( );
608
609        parser.setInput( reader );
610
611
612        return read( parser, strict );
613    } //-- Settings read( Reader, boolean )
614
615    /**
616     * @see ReaderFactory#newXmlReader
617     * 
618     * @param reader a reader object.
619     * @throws IOException IOException if any.
620     * @throws XmlPullParserException XmlPullParserException if
621     * any.
622     * @return Settings
623     */
624    public Settings read( Reader reader )
625        throws IOException, XmlPullParserException
626    {
627        return read( reader, true );
628    } //-- Settings read( Reader )
629
630    /**
631     * Method read.
632     * 
633     * @param in a in object.
634     * @param strict a strict object.
635     * @throws IOException IOException if any.
636     * @throws XmlPullParserException XmlPullParserException if
637     * any.
638     * @return Settings
639     */
640    public Settings read( InputStream in, boolean strict )
641        throws IOException, XmlPullParserException
642    {
643        return read( ReaderFactory.newXmlReader( in ), strict );
644    } //-- Settings read( InputStream, boolean )
645
646    /**
647     * Method read.
648     * 
649     * @param in a in object.
650     * @throws IOException IOException if any.
651     * @throws XmlPullParserException XmlPullParserException if
652     * any.
653     * @return Settings
654     */
655    public Settings read( InputStream in )
656        throws IOException, XmlPullParserException
657    {
658        return read( ReaderFactory.newXmlReader( in ) );
659    } //-- Settings read( InputStream )
660
661    /**
662     * Method parseSettings.
663     * 
664     * @param parser a parser object.
665     * @param strict a strict object.
666     * @throws IOException IOException if any.
667     * @throws XmlPullParserException XmlPullParserException if
668     * any.
669     * @return Settings
670     */
671    private Settings parseSettings( XmlPullParser parser, boolean strict )
672        throws IOException, XmlPullParserException
673    {
674        String tagName = parser.getName();
675        Settings settings = new Settings();
676        for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
677        {
678            String name = parser.getAttributeName( i );
679            String value = parser.getAttributeValue( i );
680
681            if ( name.indexOf( ':' ) >= 0 )
682            {
683                // just ignore attributes with non-default namespace (for example: xmlns:xsi)
684            }
685            else if ( "xmlns".equals( name ) )
686            {
687                // ignore xmlns attribute in root class, which is a reserved attribute name
688            }
689            else
690            {
691                checkUnknownAttribute( parser, name, tagName, strict );
692            }
693        }
694        java.util.Set parsed = new java.util.HashSet();
695        while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
696        {
697            if ( checkFieldWithDuplicate( parser, "configDirectory", null, parsed ) )
698            {
699                settings.setConfigDirectory( interpolatedTrimmed( parser.nextText(), "configDirectory" ) );
700            }
701            else if ( checkFieldWithDuplicate( parser, "useCygwinPath", null, parsed ) )
702            {
703                settings.setUseCygwinPath( getBooleanValue( interpolatedTrimmed( parser.nextText(), "useCygwinPath" ), "useCygwinPath", parser, "false" ) );
704            }
705            else if ( checkFieldWithDuplicate( parser, "cygwinMountPath", null, parsed ) )
706            {
707                settings.setCygwinMountPath( interpolatedTrimmed( parser.nextText(), "cygwinMountPath" ) );
708            }
709            else if ( checkFieldWithDuplicate( parser, "useNonInteractive", null, parsed ) )
710            {
711                settings.setUseNonInteractive( getBooleanValue( interpolatedTrimmed( parser.nextText(), "useNonInteractive" ), "useNonInteractive", parser, "true" ) );
712            }
713            else if ( checkFieldWithDuplicate( parser, "useAuthCache", null, parsed ) )
714            {
715                settings.setUseAuthCache( getBooleanValue( interpolatedTrimmed( parser.nextText(), "useAuthCache" ), "useAuthCache", parser, "false" ) );
716            }
717            else if ( checkFieldWithDuplicate( parser, "trustServerCert", null, parsed ) )
718            {
719                settings.setTrustServerCert( getBooleanValue( interpolatedTrimmed( parser.nextText(), "trustServerCert" ), "trustServerCert", parser, "false" ) );
720            }
721            else
722            {
723                checkUnknownElement( parser, strict );
724            }
725        }
726        return settings;
727    } //-- Settings parseSettings( XmlPullParser, boolean )
728
729    /**
730     * Sets the state of the "add default entities" flag.
731     * 
732     * @param addDefaultEntities a addDefaultEntities object.
733     */
734    public void setAddDefaultEntities( boolean addDefaultEntities )
735    {
736        this.addDefaultEntities = addDefaultEntities;
737    } //-- void setAddDefaultEntities( boolean )
738
739    public static interface ContentTransformer
740{
741    /**
742     * Interpolate the value read from the xpp3 document
743     * @param source The source value
744     * @param fieldName A description of the field being interpolated. The implementation may use this to
745     *                           log stuff.
746     * @return The interpolated value.
747     */
748    String transform( String source, String fieldName );
749}
750
751}