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