View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   // Generated by Modello 1.9.1,
3   // any modifications will be overwritten.
4   // ==============================================================
5   
6   package org.apache.maven.scm.providers.svn.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.svn.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 SvnXpp3Reader.
25   * 
26   * @version $Revision$ $Date$
27   */
28  @SuppressWarnings( "all" )
29  public class SvnXpp3Reader
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       * Field contentTransformer.
50       */
51      public final ContentTransformer contentTransformer;
52  
53  
54        //----------------/
55       //- Constructors -/
56      //----------------/
57  
58      public SvnXpp3Reader()
59      {
60          this( new ContentTransformer()
61          {
62              public String transform( String source, String fieldName )
63              {
64                  return source;
65              }
66          } );
67      } //-- org.apache.maven.scm.providers.svn.settings.io.xpp3.SvnXpp3Reader()
68  
69      public SvnXpp3Reader(ContentTransformer contentTransformer)
70      {
71          this.contentTransformer = contentTransformer;
72      } //-- org.apache.maven.scm.providers.svn.settings.io.xpp3.SvnXpp3Reader(ContentTransformer)
73  
74  
75        //-----------/
76       //- Methods -/
77      //-----------/
78  
79      /**
80       * Method checkFieldWithDuplicate.
81       * 
82       * @param parser
83       * @param parsed
84       * @param alias
85       * @param tagName
86       * @throws XmlPullParserException
87       * @return boolean
88       */
89      private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set parsed )
90          throws XmlPullParserException
91      {
92          if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
93          {
94              return false;
95          }
96          if ( !parsed.add( tagName ) )
97          {
98              throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
99          }
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, "configDirectory", null, parsed ) )
635             {
636                 settings.setConfigDirectory( interpolatedTrimmed( parser.nextText(), "configDirectory" ) );
637             }
638             else if ( checkFieldWithDuplicate( parser, "useCygwinPath", null, parsed ) )
639             {
640                 settings.setUseCygwinPath( getBooleanValue( interpolatedTrimmed( parser.nextText(), "useCygwinPath" ), "useCygwinPath", parser, "false" ) );
641             }
642             else if ( checkFieldWithDuplicate( parser, "cygwinMountPath", null, parsed ) )
643             {
644                 settings.setCygwinMountPath( interpolatedTrimmed( parser.nextText(), "cygwinMountPath" ) );
645             }
646             else if ( checkFieldWithDuplicate( parser, "useNonInteractive", null, parsed ) )
647             {
648                 settings.setUseNonInteractive( getBooleanValue( interpolatedTrimmed( parser.nextText(), "useNonInteractive" ), "useNonInteractive", parser, "true" ) );
649             }
650             else if ( checkFieldWithDuplicate( parser, "useAuthCache", null, parsed ) )
651             {
652                 settings.setUseAuthCache( getBooleanValue( interpolatedTrimmed( parser.nextText(), "useAuthCache" ), "useAuthCache", parser, "false" ) );
653             }
654             else if ( checkFieldWithDuplicate( parser, "trustServerCert", null, parsed ) )
655             {
656                 settings.setTrustServerCert( getBooleanValue( interpolatedTrimmed( parser.nextText(), "trustServerCert" ), "trustServerCert", parser, "false" ) );
657             }
658             else
659             {
660                 checkUnknownElement( parser, strict );
661             }
662         }
663         return settings;
664     } //-- Settings parseSettings( XmlPullParser, boolean )
665 
666     /**
667      * Method read.
668      * 
669      * @param parser
670      * @param strict
671      * @throws IOException
672      * @throws XmlPullParserException
673      * @return Settings
674      */
675     private Settings read( XmlPullParser parser, boolean strict )
676         throws IOException, XmlPullParserException
677     {
678         Settings settings = null;
679         int eventType = parser.getEventType();
680         boolean parsed = false;
681         while ( eventType != XmlPullParser.END_DOCUMENT )
682         {
683             if ( eventType == XmlPullParser.START_TAG )
684             {
685                 if ( strict && ! "svn-settings".equals( parser.getName() ) )
686                 {
687                     throw new XmlPullParserException( "Expected root element 'svn-settings' but found '" + parser.getName() + "'", parser, null );
688                 }
689                 else if ( parsed )
690                 {
691                     // fallback, already expected a XmlPullParserException due to invalid XML
692                     throw new XmlPullParserException( "Duplicated tag: 'svn-settings'", parser, null );
693                 }
694                 settings = parseSettings( parser, strict );
695                 settings.setModelEncoding( parser.getInputEncoding() );
696                 parsed = true;
697             }
698             eventType = parser.next();
699         }
700         if ( parsed )
701         {
702             return settings;
703         }
704         throw new XmlPullParserException( "Expected root element 'svn-settings' but found no element at all: invalid XML document", parser, null );
705     } //-- Settings read( XmlPullParser, boolean )
706 
707     /**
708      * Sets the state of the "add default entities" flag.
709      * 
710      * @param addDefaultEntities
711      */
712     public void setAddDefaultEntities( boolean addDefaultEntities )
713     {
714         this.addDefaultEntities = addDefaultEntities;
715     } //-- void setAddDefaultEntities( boolean )
716 
717     public static interface ContentTransformer
718 {
719     /**
720      * Interpolate the value read from the xpp3 document
721      * @param source The source value
722      * @param fieldName A description of the field being interpolated. The implementation may use this to
723      *                           log stuff.
724      * @return The interpolated value.
725      */
726     String transform( String source, String fieldName );
727 }
728 
729 }