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