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