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