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