View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   // Generated by Modello 1.8.2,
3   // any modifications will be overwritten.
4   // ==============================================================
5   
6   package org.apache.maven.scm.providers.gitlib.settings.io.xpp3;
7   
8     //---------------------------------/
9    //- Imported classes and packages -/
10  //---------------------------------/
11  
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.io.Reader;
15  import java.text.DateFormat;
16  import org.apache.maven.scm.providers.gitlib.settings.Settings;
17  import org.codehaus.plexus.util.ReaderFactory;
18  import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
19  import org.codehaus.plexus.util.xml.pull.MXParser;
20  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
21  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
22  
23  /**
24   * Class GitXpp3Reader.
25   * 
26   * @version $Revision$ $Date$
27   */
28  @SuppressWarnings( "all" )
29  public class GitXpp3Reader
30  {
31  
32        //--------------------------/
33       //- Class/Member Variables -/
34      //--------------------------/
35  
36      /**
37       * If set the parser will be loaded with all single characters
38       * from the XHTML specification.
39       * The entities used:
40       * <ul>
41       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</li>;
42       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li>;
43       * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li>;
44       * </ul>
45       */
46      private boolean addDefaultEntities = true;
47  
48  
49        //-----------/
50       //- Methods -/
51      //-----------/
52  
53      /**
54       * Method checkFieldWithDuplicate.
55       * 
56       * @param parser
57       * @param parsed
58       * @param alias
59       * @param tagName
60       * @throws XmlPullParserException
61       * @return boolean
62       */
63      private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set parsed )
64          throws XmlPullParserException
65      {
66          if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
67          {
68              return false;
69          }
70          if ( !parsed.add( tagName ) )
71          {
72              throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
73          }
74          return true;
75      } //-- boolean checkFieldWithDuplicate( XmlPullParser, String, String, java.util.Set )
76  
77      /**
78       * Method checkUnknownAttribute.
79       * 
80       * @param parser
81       * @param strict
82       * @param tagName
83       * @param attribute
84       * @throws XmlPullParserException
85       * @throws IOException
86       */
87      private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict )
88          throws XmlPullParserException, IOException
89      {
90          // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
91          if ( strict )
92          {
93              throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
94          }
95      } //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean )
96  
97      /**
98       * Method checkUnknownElement.
99       * 
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, "commitNoVerify", null, parsed ) )
605             {
606                 settings.setCommitNoVerify( getBooleanValue( getTrimmedValue( parser.nextText() ), "commitNoVerify", parser, "false" ) );
607             }
608             else
609             {
610                 checkUnknownElement( parser, strict );
611             }
612         }
613         return settings;
614     } //-- Settings parseSettings( XmlPullParser, boolean )
615 
616     /**
617      * Method read.
618      * 
619      * @param parser
620      * @param strict
621      * @throws IOException
622      * @throws XmlPullParserException
623      * @return Settings
624      */
625     private Settings read( XmlPullParser parser, boolean strict )
626         throws IOException, XmlPullParserException
627     {
628         int eventType = parser.getEventType();
629         while ( eventType != XmlPullParser.END_DOCUMENT )
630         {
631             if ( eventType == XmlPullParser.START_TAG )
632             {
633                 if ( strict && ! "git-settings".equals( parser.getName() ) )
634                 {
635                     throw new XmlPullParserException( "Expected root element 'git-settings' but found '" + parser.getName() + "'", parser, null );
636                 }
637                 Settings settings = parseSettings( parser, strict );
638                 settings.setModelEncoding( parser.getInputEncoding() );
639                 return settings;
640             }
641             eventType = parser.next();
642         }
643         throw new XmlPullParserException( "Expected root element 'git-settings' but found no element at all: invalid XML document", parser, null );
644     } //-- Settings read( XmlPullParser, boolean )
645 
646     /**
647      * Sets the state of the "add default entities" flag.
648      * 
649      * @param addDefaultEntities
650      */
651     public void setAddDefaultEntities( boolean addDefaultEntities )
652     {
653         this.addDefaultEntities = addDefaultEntities;
654     } //-- void setAddDefaultEntities( boolean )
655 
656 }