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