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