001// =================== DO NOT EDIT THIS FILE ==================== 002// Generated by Modello 1.8.3, 003// any modifications will be overwritten. 004// ============================================================== 005 006package org.apache.maven.toolchain.model.io.xpp3; 007 008 //---------------------------------/ 009 //- Imported classes and packages -/ 010//---------------------------------/ 011 012import java.io.IOException; 013import java.io.InputStream; 014import java.io.Reader; 015import java.text.DateFormat; 016import org.apache.maven.toolchain.model.PersistedToolchains; 017import org.apache.maven.toolchain.model.ToolchainModel; 018import org.apache.maven.toolchain.model.TrackableBase; 019import org.codehaus.plexus.util.ReaderFactory; 020import org.codehaus.plexus.util.xml.pull.EntityReplacementMap; 021import org.codehaus.plexus.util.xml.pull.MXParser; 022import org.codehaus.plexus.util.xml.pull.XmlPullParser; 023import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 024 025/** 026 * Class MavenToolchainsXpp3Reader. 027 * 028 * @version $Revision: 965443 $ $Date: 2015-09-14 22:18:52 +0000 (Mon, 14 Sep 2015) $ 029 */ 030@SuppressWarnings( "all" ) 031public class MavenToolchainsXpp3Reader 032{ 033 034 //--------------------------/ 035 //- Class/Member Variables -/ 036 //--------------------------/ 037 038 /** 039 * If set the parser will be loaded with all single characters 040 * from the XHTML specification. 041 * The entities used: 042 * <ul> 043 * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</li> 044 * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li> 045 * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li> 046 * </ul> 047 */ 048 private boolean addDefaultEntities = true; 049 050 /** 051 * Field contentTransformer. 052 */ 053 public final ContentTransformer contentTransformer; 054 055 056 //----------------/ 057 //- Constructors -/ 058 //----------------/ 059 060 public MavenToolchainsXpp3Reader() 061 { 062 this( new ContentTransformer() 063 { 064 public String transform( String source, String fieldName ) 065 { 066 return source; 067 } 068 } ); 069 } //-- org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader() 070 071 public MavenToolchainsXpp3Reader(ContentTransformer contentTransformer) 072 { 073 this.contentTransformer = contentTransformer; 074 } //-- org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader(ContentTransformer) 075 076 077 //-----------/ 078 //- Methods -/ 079 //-----------/ 080 081 /** 082 * Method checkFieldWithDuplicate. 083 * 084 * @param parser 085 * @param parsed 086 * @param alias 087 * @param tagName 088 * @throws XmlPullParserException 089 * @return boolean 090 */ 091 private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set parsed ) 092 throws XmlPullParserException 093 { 094 if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) ) 095 { 096 return false; 097 } 098 if ( !parsed.add( tagName ) ) 099 { 100 throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null ); 101 } 102 return true; 103 } //-- boolean checkFieldWithDuplicate( XmlPullParser, String, String, java.util.Set ) 104 105 /** 106 * Method checkUnknownAttribute. 107 * 108 * @param parser 109 * @param strict 110 * @param tagName 111 * @param attribute 112 * @throws XmlPullParserException 113 * @throws IOException 114 */ 115 private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict ) 116 throws XmlPullParserException, IOException 117 { 118 // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too 119 if ( strict ) 120 { 121 throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null ); 122 } 123 } //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean ) 124 125 /** 126 * Method checkUnknownElement. 127 * 128 * @param parser 129 * @param strict 130 * @throws XmlPullParserException 131 * @throws IOException 132 */ 133 private void checkUnknownElement( XmlPullParser parser, boolean strict ) 134 throws XmlPullParserException, IOException 135 { 136 if ( strict ) 137 { 138 throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null ); 139 } 140 141 for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; ) 142 { 143 int eventType = parser.next(); 144 if ( eventType == XmlPullParser.START_TAG ) 145 { 146 unrecognizedTagCount++; 147 } 148 else if ( eventType == XmlPullParser.END_TAG ) 149 { 150 unrecognizedTagCount--; 151 } 152 } 153 } //-- void checkUnknownElement( XmlPullParser, boolean ) 154 155 /** 156 * Returns the state of the "add default entities" flag. 157 * 158 * @return boolean 159 */ 160 public boolean getAddDefaultEntities() 161 { 162 return addDefaultEntities; 163 } //-- boolean getAddDefaultEntities() 164 165 /** 166 * Method getBooleanValue. 167 * 168 * @param s 169 * @param parser 170 * @param attribute 171 * @throws XmlPullParserException 172 * @return boolean 173 */ 174 private boolean getBooleanValue( String s, String attribute, XmlPullParser parser ) 175 throws XmlPullParserException 176 { 177 return getBooleanValue( s, attribute, parser, null ); 178 } //-- boolean getBooleanValue( String, String, XmlPullParser ) 179 180 /** 181 * Method getBooleanValue. 182 * 183 * @param s 184 * @param defaultValue 185 * @param parser 186 * @param attribute 187 * @throws XmlPullParserException 188 * @return boolean 189 */ 190 private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, String defaultValue ) 191 throws XmlPullParserException 192 { 193 if ( s != null && s.length() != 0 ) 194 { 195 return Boolean.valueOf( s ).booleanValue(); 196 } 197 if ( defaultValue != null ) 198 { 199 return Boolean.valueOf( defaultValue ).booleanValue(); 200 } 201 return false; 202 } //-- boolean getBooleanValue( String, String, XmlPullParser, String ) 203 204 /** 205 * Method getByteValue. 206 * 207 * @param s 208 * @param strict 209 * @param parser 210 * @param attribute 211 * @throws XmlPullParserException 212 * @return byte 213 */ 214 private byte getByteValue( String s, String attribute, XmlPullParser parser, boolean strict ) 215 throws XmlPullParserException 216 { 217 if ( s != null ) 218 { 219 try 220 { 221 return Byte.valueOf( s ).byteValue(); 222 } 223 catch ( NumberFormatException nfe ) 224 { 225 if ( strict ) 226 { 227 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a byte", parser, nfe ); 228 } 229 } 230 } 231 return 0; 232 } //-- byte getByteValue( String, String, XmlPullParser, boolean ) 233 234 /** 235 * Method getCharacterValue. 236 * 237 * @param s 238 * @param parser 239 * @param attribute 240 * @throws XmlPullParserException 241 * @return char 242 */ 243 private char getCharacterValue( String s, String attribute, XmlPullParser parser ) 244 throws XmlPullParserException 245 { 246 if ( s != null ) 247 { 248 return s.charAt( 0 ); 249 } 250 return 0; 251 } //-- char getCharacterValue( String, String, XmlPullParser ) 252 253 /** 254 * Method getDateValue. 255 * 256 * @param s 257 * @param parser 258 * @param attribute 259 * @throws XmlPullParserException 260 * @return Date 261 */ 262 private java.util.Date getDateValue( String s, String attribute, XmlPullParser parser ) 263 throws XmlPullParserException 264 { 265 return getDateValue( s, attribute, null, parser ); 266 } //-- java.util.Date getDateValue( String, String, XmlPullParser ) 267 268 /** 269 * Method getDateValue. 270 * 271 * @param s 272 * @param parser 273 * @param dateFormat 274 * @param attribute 275 * @throws XmlPullParserException 276 * @return Date 277 */ 278 private java.util.Date getDateValue( String s, String attribute, String dateFormat, XmlPullParser parser ) 279 throws XmlPullParserException 280 { 281 if ( s != null ) 282 { 283 String effectiveDateFormat = dateFormat; 284 if ( dateFormat == null ) 285 { 286 effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"; 287 } 288 if ( "long".equals( effectiveDateFormat ) ) 289 { 290 try 291 { 292 return new java.util.Date( Long.parseLong( s ) ); 293 } 294 catch ( NumberFormatException e ) 295 { 296 throw new XmlPullParserException( e.getMessage(), parser, e ); 297 } 298 } 299 else 300 { 301 try 302 { 303 DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US ); 304 return dateParser.parse( s ); 305 } 306 catch ( java.text.ParseException e ) 307 { 308 throw new XmlPullParserException( e.getMessage(), parser, e ); 309 } 310 } 311 } 312 return null; 313 } //-- java.util.Date getDateValue( String, String, String, XmlPullParser ) 314 315 /** 316 * Method getDoubleValue. 317 * 318 * @param s 319 * @param strict 320 * @param parser 321 * @param attribute 322 * @throws XmlPullParserException 323 * @return double 324 */ 325 private double getDoubleValue( String s, String attribute, XmlPullParser parser, boolean strict ) 326 throws XmlPullParserException 327 { 328 if ( s != null ) 329 { 330 try 331 { 332 return Double.valueOf( s ).doubleValue(); 333 } 334 catch ( NumberFormatException nfe ) 335 { 336 if ( strict ) 337 { 338 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe ); 339 } 340 } 341 } 342 return 0; 343 } //-- double getDoubleValue( String, String, XmlPullParser, boolean ) 344 345 /** 346 * Method getFloatValue. 347 * 348 * @param s 349 * @param strict 350 * @param parser 351 * @param attribute 352 * @throws XmlPullParserException 353 * @return float 354 */ 355 private float getFloatValue( String s, String attribute, XmlPullParser parser, boolean strict ) 356 throws XmlPullParserException 357 { 358 if ( s != null ) 359 { 360 try 361 { 362 return Float.valueOf( s ).floatValue(); 363 } 364 catch ( NumberFormatException nfe ) 365 { 366 if ( strict ) 367 { 368 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe ); 369 } 370 } 371 } 372 return 0; 373 } //-- float getFloatValue( String, String, XmlPullParser, boolean ) 374 375 /** 376 * Method getIntegerValue. 377 * 378 * @param s 379 * @param strict 380 * @param parser 381 * @param attribute 382 * @throws XmlPullParserException 383 * @return int 384 */ 385 private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict ) 386 throws XmlPullParserException 387 { 388 if ( s != null ) 389 { 390 try 391 { 392 return Integer.valueOf( s ).intValue(); 393 } 394 catch ( NumberFormatException nfe ) 395 { 396 if ( strict ) 397 { 398 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe ); 399 } 400 } 401 } 402 return 0; 403 } //-- int getIntegerValue( String, String, XmlPullParser, boolean ) 404 405 /** 406 * Method getLongValue. 407 * 408 * @param s 409 * @param strict 410 * @param parser 411 * @param attribute 412 * @throws XmlPullParserException 413 * @return long 414 */ 415 private long getLongValue( String s, String attribute, XmlPullParser parser, boolean strict ) 416 throws XmlPullParserException 417 { 418 if ( s != null ) 419 { 420 try 421 { 422 return Long.valueOf( s ).longValue(); 423 } 424 catch ( NumberFormatException nfe ) 425 { 426 if ( strict ) 427 { 428 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a long integer", parser, nfe ); 429 } 430 } 431 } 432 return 0; 433 } //-- long getLongValue( String, String, XmlPullParser, boolean ) 434 435 /** 436 * Method getRequiredAttributeValue. 437 * 438 * @param s 439 * @param strict 440 * @param parser 441 * @param attribute 442 * @throws XmlPullParserException 443 * @return String 444 */ 445 private String getRequiredAttributeValue( String s, String attribute, XmlPullParser parser, boolean strict ) 446 throws XmlPullParserException 447 { 448 if ( s == null ) 449 { 450 if ( strict ) 451 { 452 throw new XmlPullParserException( "Missing required value for attribute '" + attribute + "'", parser, null ); 453 } 454 } 455 return s; 456 } //-- String getRequiredAttributeValue( String, String, XmlPullParser, boolean ) 457 458 /** 459 * Method getShortValue. 460 * 461 * @param s 462 * @param strict 463 * @param parser 464 * @param attribute 465 * @throws XmlPullParserException 466 * @return short 467 */ 468 private short getShortValue( String s, String attribute, XmlPullParser parser, boolean strict ) 469 throws XmlPullParserException 470 { 471 if ( s != null ) 472 { 473 try 474 { 475 return Short.valueOf( s ).shortValue(); 476 } 477 catch ( NumberFormatException nfe ) 478 { 479 if ( strict ) 480 { 481 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a short integer", parser, nfe ); 482 } 483 } 484 } 485 return 0; 486 } //-- short getShortValue( String, String, XmlPullParser, boolean ) 487 488 /** 489 * Method getTrimmedValue. 490 * 491 * @param s 492 * @return String 493 */ 494 private String getTrimmedValue( String s ) 495 { 496 if ( s != null ) 497 { 498 s = s.trim(); 499 } 500 return s; 501 } //-- String getTrimmedValue( String ) 502 503 /** 504 * Method interpolatedTrimmed. 505 * 506 * @param value 507 * @param context 508 * @return String 509 */ 510 private String interpolatedTrimmed( String value, String context ) 511 { 512 return getTrimmedValue( contentTransformer.transform( value, context ) ); 513 } //-- String interpolatedTrimmed( String, String ) 514 515 /** 516 * Method nextTag. 517 * 518 * @param parser 519 * @throws IOException 520 * @throws XmlPullParserException 521 * @return int 522 */ 523 private int nextTag( XmlPullParser parser ) 524 throws IOException, XmlPullParserException 525 { 526 int eventType = parser.next(); 527 if ( eventType == XmlPullParser.TEXT ) 528 { 529 eventType = parser.next(); 530 } 531 if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG ) 532 { 533 throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null ); 534 } 535 return eventType; 536 } //-- int nextTag( XmlPullParser ) 537 538 /** 539 * @see ReaderFactory#newXmlReader 540 * 541 * @param reader 542 * @param strict 543 * @throws IOException 544 * @throws XmlPullParserException 545 * @return PersistedToolchains 546 */ 547 public PersistedToolchains read( Reader reader, boolean strict ) 548 throws IOException, XmlPullParserException 549 { 550 XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( ); 551 552 parser.setInput( reader ); 553 554 555 return read( parser, strict ); 556 } //-- PersistedToolchains read( Reader, boolean ) 557 558 /** 559 * @see ReaderFactory#newXmlReader 560 * 561 * @param reader 562 * @throws IOException 563 * @throws XmlPullParserException 564 * @return PersistedToolchains 565 */ 566 public PersistedToolchains read( Reader reader ) 567 throws IOException, XmlPullParserException 568 { 569 return read( reader, true ); 570 } //-- PersistedToolchains read( Reader ) 571 572 /** 573 * Method read. 574 * 575 * @param in 576 * @param strict 577 * @throws IOException 578 * @throws XmlPullParserException 579 * @return PersistedToolchains 580 */ 581 public PersistedToolchains read( InputStream in, boolean strict ) 582 throws IOException, XmlPullParserException 583 { 584 return read( ReaderFactory.newXmlReader( in ), strict ); 585 } //-- PersistedToolchains read( InputStream, boolean ) 586 587 /** 588 * Method read. 589 * 590 * @param in 591 * @throws IOException 592 * @throws XmlPullParserException 593 * @return PersistedToolchains 594 */ 595 public PersistedToolchains read( InputStream in ) 596 throws IOException, XmlPullParserException 597 { 598 return read( ReaderFactory.newXmlReader( in ) ); 599 } //-- PersistedToolchains read( InputStream ) 600 601 /** 602 * Method parsePersistedToolchains. 603 * 604 * @param parser 605 * @param strict 606 * @throws IOException 607 * @throws XmlPullParserException 608 * @return PersistedToolchains 609 */ 610 private PersistedToolchains parsePersistedToolchains( XmlPullParser parser, boolean strict ) 611 throws IOException, XmlPullParserException 612 { 613 String tagName = parser.getName(); 614 PersistedToolchains persistedToolchains = new PersistedToolchains(); 615 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- ) 616 { 617 String name = parser.getAttributeName( i ); 618 String value = parser.getAttributeValue( i ); 619 620 if ( name.indexOf( ':' ) >= 0 ) 621 { 622 // just ignore attributes with non-default namespace (for example: xmlns:xsi) 623 } 624 else if ( "xmlns".equals( name ) ) 625 { 626 // ignore xmlns attribute in root class, which is a reserved attribute name 627 } 628 else 629 { 630 checkUnknownAttribute( parser, name, tagName, strict ); 631 } 632 } 633 java.util.Set parsed = new java.util.HashSet(); 634 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG ) 635 { 636 if ( "toolchain".equals( parser.getName() ) ) 637 { 638 java.util.List toolchains = persistedToolchains.getToolchains(); 639 if ( toolchains == null ) 640 { 641 toolchains = new java.util.ArrayList/*<ToolchainModel>*/(); 642 persistedToolchains.setToolchains( toolchains ); 643 } 644 toolchains.add( parseToolchainModel( parser, strict ) ); 645 } 646 else 647 { 648 checkUnknownElement( parser, strict ); 649 } 650 } 651 return persistedToolchains; 652 } //-- PersistedToolchains parsePersistedToolchains( XmlPullParser, boolean ) 653 654 /** 655 * Method parseToolchainModel. 656 * 657 * @param parser 658 * @param strict 659 * @throws IOException 660 * @throws XmlPullParserException 661 * @return ToolchainModel 662 */ 663 private ToolchainModel parseToolchainModel( XmlPullParser parser, boolean strict ) 664 throws IOException, XmlPullParserException 665 { 666 String tagName = parser.getName(); 667 ToolchainModel toolchainModel = new ToolchainModel(); 668 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- ) 669 { 670 String name = parser.getAttributeName( i ); 671 String value = parser.getAttributeValue( i ); 672 673 if ( name.indexOf( ':' ) >= 0 ) 674 { 675 // just ignore attributes with non-default namespace (for example: xmlns:xsi) 676 } 677 else 678 { 679 checkUnknownAttribute( parser, name, tagName, strict ); 680 } 681 } 682 java.util.Set parsed = new java.util.HashSet(); 683 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG ) 684 { 685 if ( checkFieldWithDuplicate( parser, "type", null, parsed ) ) 686 { 687 toolchainModel.setType( interpolatedTrimmed( parser.nextText(), "type" ) ); 688 } 689 else if ( checkFieldWithDuplicate( parser, "provides", null, parsed ) ) 690 { 691 while ( parser.nextTag() == XmlPullParser.START_TAG ) 692 { 693 String key = parser.getName(); 694 String value = parser.nextText().trim(); 695 toolchainModel.addProvide( key, value ); 696 } 697 } 698 else if ( checkFieldWithDuplicate( parser, "configuration", null, parsed ) ) 699 { 700 toolchainModel.setConfiguration( org.codehaus.plexus.util.xml.Xpp3DomBuilder.build( parser, true ) ); 701 } 702 else 703 { 704 checkUnknownElement( parser, strict ); 705 } 706 } 707 return toolchainModel; 708 } //-- ToolchainModel parseToolchainModel( XmlPullParser, boolean ) 709 710 /** 711 * Method parseTrackableBase. 712 * 713 * @param parser 714 * @param strict 715 * @throws IOException 716 * @throws XmlPullParserException 717 * @return TrackableBase 718 */ 719 private TrackableBase parseTrackableBase( XmlPullParser parser, boolean strict ) 720 throws IOException, XmlPullParserException 721 { 722 String tagName = parser.getName(); 723 TrackableBase trackableBase = new TrackableBase(); 724 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- ) 725 { 726 String name = parser.getAttributeName( i ); 727 String value = parser.getAttributeValue( i ); 728 729 if ( name.indexOf( ':' ) >= 0 ) 730 { 731 // just ignore attributes with non-default namespace (for example: xmlns:xsi) 732 } 733 else 734 { 735 checkUnknownAttribute( parser, name, tagName, strict ); 736 } 737 } 738 java.util.Set parsed = new java.util.HashSet(); 739 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG ) 740 { 741 checkUnknownElement( parser, strict ); 742 } 743 return trackableBase; 744 } //-- TrackableBase parseTrackableBase( XmlPullParser, boolean ) 745 746 /** 747 * Method read. 748 * 749 * @param parser 750 * @param strict 751 * @throws IOException 752 * @throws XmlPullParserException 753 * @return PersistedToolchains 754 */ 755 private PersistedToolchains read( XmlPullParser parser, boolean strict ) 756 throws IOException, XmlPullParserException 757 { 758 int eventType = parser.getEventType(); 759 while ( eventType != XmlPullParser.END_DOCUMENT ) 760 { 761 if ( eventType == XmlPullParser.START_TAG ) 762 { 763 if ( strict && ! "toolchains".equals( parser.getName() ) ) 764 { 765 throw new XmlPullParserException( "Expected root element 'toolchains' but found '" + parser.getName() + "'", parser, null ); 766 } 767 PersistedToolchains persistedToolchains = parsePersistedToolchains( parser, strict ); 768 persistedToolchains.setModelEncoding( parser.getInputEncoding() ); 769 return persistedToolchains; 770 } 771 eventType = parser.next(); 772 } 773 throw new XmlPullParserException( "Expected root element 'toolchains' but found no element at all: invalid XML document", parser, null ); 774 } //-- PersistedToolchains read( XmlPullParser, boolean ) 775 776 /** 777 * Sets the state of the "add default entities" flag. 778 * 779 * @param addDefaultEntities 780 */ 781 public void setAddDefaultEntities( boolean addDefaultEntities ) 782 { 783 this.addDefaultEntities = addDefaultEntities; 784 } //-- void setAddDefaultEntities( boolean ) 785 786 public static interface ContentTransformer 787{ 788 /** 789 * Interpolate the value read from the xpp3 document 790 * @param source The source value 791 * @param fieldName A description of the field being interpolated. The implementation may use this to 792 * log stuff. 793 * @return The interpolated value. 794 */ 795 String transform( String source, String fieldName ); 796} 797 798}