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