1
2
3
4
5
6 package org.apache.maven.toolchain.model.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.toolchain.model.PersistedToolchains;
17 import org.apache.maven.toolchain.model.ToolchainModel;
18 import org.apache.maven.toolchain.model.TrackableBase;
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
27
28
29
30 @SuppressWarnings( "all" )
31 public class MavenToolchainsXpp3Reader
32 {
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 private boolean addDefaultEntities = true;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 }
78
79
80
81
82
83
84
85
86
87
88
89 private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict )
90 throws XmlPullParserException, IOException
91 {
92
93 if ( strict )
94 {
95 throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
96 }
97 }
98
99
100
101
102
103
104
105
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 }
128
129
130
131
132
133
134 public boolean getAddDefaultEntities()
135 {
136 return addDefaultEntities;
137 }
138
139
140
141
142
143
144
145
146
147
148 private boolean getBooleanValue( String s, String attribute, XmlPullParser parser )
149 throws XmlPullParserException
150 {
151 return getBooleanValue( s, attribute, parser, null );
152 }
153
154
155
156
157
158
159
160
161
162
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 }
177
178
179
180
181
182
183
184
185
186
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 }
207
208
209
210
211
212
213
214
215
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 }
226
227
228
229
230
231
232
233
234
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 }
241
242
243
244
245
246
247
248
249
250
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 }
288
289
290
291
292
293
294
295
296
297
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 }
318
319
320
321
322
323
324
325
326
327
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 }
348
349
350
351
352
353
354
355
356
357
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 }
378
379
380
381
382
383
384
385
386
387
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 }
408
409
410
411
412
413
414
415
416
417
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 }
431
432
433
434
435
436
437
438
439
440
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 }
461
462
463
464
465
466
467
468 private String getTrimmedValue( String s )
469 {
470 if ( s != null )
471 {
472 s = s.trim();
473 }
474 return s;
475 }
476
477
478
479
480
481
482
483
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 }
499
500
501
502
503
504
505
506
507
508
509 public PersistedToolchains 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 }
519
520
521
522
523
524
525
526
527
528 public PersistedToolchains read( Reader reader )
529 throws IOException, XmlPullParserException
530 {
531 return read( reader, true );
532 }
533
534
535
536
537
538
539
540
541
542
543 public PersistedToolchains read( InputStream in, boolean strict )
544 throws IOException, XmlPullParserException
545 {
546 return read( ReaderFactory.newXmlReader( in ), strict );
547 }
548
549
550
551
552
553
554
555
556
557 public PersistedToolchains read( InputStream in )
558 throws IOException, XmlPullParserException
559 {
560 return read( ReaderFactory.newXmlReader( in ) );
561 }
562
563
564
565
566
567
568
569
570
571
572 private PersistedToolchains parsePersistedToolchains( XmlPullParser parser, boolean strict )
573 throws IOException, XmlPullParserException
574 {
575 String tagName = parser.getName();
576 PersistedToolchains persistedToolchains = new PersistedToolchains();
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
585 }
586 else if ( "xmlns".equals( name ) )
587 {
588
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 ( "toolchain".equals( parser.getName() ) )
599 {
600 java.util.List toolchains = persistedToolchains.getToolchains();
601 if ( toolchains == null )
602 {
603 toolchains = new java.util.ArrayList();
604 persistedToolchains.setToolchains( toolchains );
605 }
606 toolchains.add( parseToolchainModel( parser, strict ) );
607 }
608 else
609 {
610 checkUnknownElement( parser, strict );
611 }
612 }
613 return persistedToolchains;
614 }
615
616
617
618
619
620
621
622
623
624
625 private ToolchainModel parseToolchainModel( XmlPullParser parser, boolean strict )
626 throws IOException, XmlPullParserException
627 {
628 String tagName = parser.getName();
629 ToolchainModel toolchainModel = new ToolchainModel();
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
638 }
639 else
640 {
641 checkUnknownAttribute( parser, name, tagName, strict );
642 }
643 }
644 java.util.Set parsed = new java.util.HashSet();
645 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
646 {
647 if ( checkFieldWithDuplicate( parser, "type", null, parsed ) )
648 {
649 toolchainModel.setType( getTrimmedValue( parser.nextText() ) );
650 }
651 else if ( checkFieldWithDuplicate( parser, "provides", null, parsed ) )
652 {
653 while ( parser.nextTag() == XmlPullParser.START_TAG )
654 {
655 String key = parser.getName();
656 String value = parser.nextText().trim();
657 toolchainModel.addProvide( key, value );
658 }
659 }
660 else if ( checkFieldWithDuplicate( parser, "configuration", null, parsed ) )
661 {
662 toolchainModel.setConfiguration( org.codehaus.plexus.util.xml.Xpp3DomBuilder.build( parser, true ) );
663 }
664 else
665 {
666 checkUnknownElement( parser, strict );
667 }
668 }
669 return toolchainModel;
670 }
671
672
673
674
675
676
677
678
679
680
681 private TrackableBase parseTrackableBase( XmlPullParser parser, boolean strict )
682 throws IOException, XmlPullParserException
683 {
684 String tagName = parser.getName();
685 TrackableBase trackableBase = new TrackableBase();
686 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
687 {
688 String name = parser.getAttributeName( i );
689 String value = parser.getAttributeValue( i );
690
691 if ( name.indexOf( ':' ) >= 0 )
692 {
693
694 }
695 else
696 {
697 checkUnknownAttribute( parser, name, tagName, strict );
698 }
699 }
700 java.util.Set parsed = new java.util.HashSet();
701 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
702 {
703 checkUnknownElement( parser, strict );
704 }
705 return trackableBase;
706 }
707
708
709
710
711
712
713
714
715
716
717 private PersistedToolchains read( XmlPullParser parser, boolean strict )
718 throws IOException, XmlPullParserException
719 {
720 int eventType = parser.getEventType();
721 while ( eventType != XmlPullParser.END_DOCUMENT )
722 {
723 if ( eventType == XmlPullParser.START_TAG )
724 {
725 if ( strict && ! "toolchains".equals( parser.getName() ) )
726 {
727 throw new XmlPullParserException( "Expected root element 'toolchains' but found '" + parser.getName() + "'", parser, null );
728 }
729 PersistedToolchains persistedToolchains = parsePersistedToolchains( parser, strict );
730 persistedToolchains.setModelEncoding( parser.getInputEncoding() );
731 return persistedToolchains;
732 }
733 eventType = parser.next();
734 }
735 throw new XmlPullParserException( "Expected root element 'toolchains' but found no element at all: invalid XML document", parser, null );
736 }
737
738
739
740
741
742
743 public void setAddDefaultEntities( boolean addDefaultEntities )
744 {
745 this.addDefaultEntities = addDefaultEntities;
746 }
747
748 }