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