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