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