1 22 package org.jboss.mq.xml; 23 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 import java.util.Vector ; 27 28 import org.xml.sax.Attributes ; 29 30 68 public class XElement { 69 70 private XElement parent = null; 71 private String name = null; 72 private Hashtable metadata = new Hashtable (); 73 private Vector contents = new Vector (); 74 private final static String nl = System.getProperty( "line.separator" ); 75 76 81 public XElement( String objectName ) { 82 if ( objectName == null ) { 83 throw new NullPointerException (); 84 } 85 name = objectName; 86 contents.addElement( new StringBuffer () ); 87 } 88 89 95 public XElement( String objectName, Attributes atts ) { 96 if ( objectName == null ) { 97 throw new NullPointerException (); 98 } 99 if ( atts == null ) { 100 throw new NullPointerException (); 101 } 102 name = objectName; 103 contents.addElement( new StringBuffer () ); 104 for ( int i = 0; i < atts.getLength(); i++ ) { 105 metadata.put( atts.getQName( i ), atts.getValue( i ) ); 106 } 108 } 109 110 117 public void setAttribute( String key, String value ) { 118 metadata.put( key, value ); 119 } 120 121 126 public void setName( String newName ) { 127 name = newName; 128 } 129 130 139 public void setValue( String value ) 140 throws XElementException { 141 if ( !isField() ) { 142 throw new XElementException( "" + getName() + " is not an attribute object" ); 143 } 144 contents.setElementAt( new StringBuffer ( value ), 0 ); 145 } 146 147 148 155 public void setField( String key, String value ) 156 throws XElementException { 157 getElement( key ).setValue( value ); 158 } 159 160 168 public String getAttribute( String key ) { 169 String t = ( String )metadata.get( key ); 170 if ( t == null ) { 171 return ""; 172 } 173 return t; 174 } 175 176 182 public java.lang.String getName() { 183 return name; 184 } 185 186 192 public XElement getParent() { 193 return parent; 194 } 195 196 210 public String getText() { 211 return contents.elementAt( 0 ).toString().trim(); 212 } 213 214 223 public String getValue() 224 throws XElementException { 225 if ( !isField() ) { 226 throw new XElementException( "" + getName() + " is not an attribute object" ); 227 } 228 return contents.elementAt( 0 ).toString(); 229 } 230 231 239 public XElement getElement( String relativeName ) 240 throws XElementException { 241 if ( relativeName == null ) { 242 throw new NullPointerException (); 243 } 244 245 String names[] = {null, relativeName}; 246 247 String split[] = splitFront( relativeName, "/" ); 249 if ( split != null ) { 250 251 if ( split[0].length() == 0 ) { 253 if ( parent == null ) { 255 split[0] = null; 256 } 257 else { 259 return parent.getElement( relativeName ); 260 } 261 } 262 263 if ( split[1].length() == 0 ) { 265 if ( split[0].equals( null ) ) { 267 return this; 268 } 269 270 throw new XElementException( "Invalid name (trailing '/') : " + relativeName ); 273 } 274 275 names = split; 276 } 277 278 if ( names[0] == null ) { 279 for ( int i = 1; i < contents.size(); i++ ) { 280 XElement o = ( XElement )contents.elementAt( i ); 281 if ( names[1].equals( o.getName() ) ) { 282 return o; 283 } 284 } 285 } else { 286 if ( names[0].equals( "." ) ) { 287 return getElement( names[1] ); 288 } else if ( names[0].equals( ".." ) ) { 289 if ( parent != null ) { 290 return parent.getElement( names[1] ); 291 } else { 292 throw new XElementException( "Invalid name (" + getName() + " has no parent) : " + relativeName ); 293 } 294 } else { 295 for ( int i = 1; i < contents.size(); i++ ) { 296 XElement o = ( XElement )contents.elementAt( i ); 297 if ( names[0].equals( o.getName() ) ) { 298 return o.getElement( names[1] ); 299 } 300 } 301 } 302 } 303 304 throw new XElementException( "Invalid name (" + getName() + " does not contain the name) : " + relativeName ); 305 } 306 307 308 317 public String getField( String objectName ) 318 throws XElementException { 319 return getElement( objectName ).getValue(); 320 } 321 322 329 public boolean isField() { 330 return contents.size() == 1; 331 } 332 333 340 public java.util.Enumeration getElementsNamed( String relativeName ) { 341 342 Vector t = new Vector (); 343 addElementsToVector( t, relativeName ); 344 return t.elements(); 345 } 346 347 352 public void add( String data ) { 353 ( ( StringBuffer )contents.elementAt( 0 ) ).append( data ); 354 } 355 356 361 public String toString() { 362 return toString( 0, true ); 363 } 364 365 371 public void addElement( XElement subObject ) { 372 contents.addElement( subObject ); 373 subObject.parent = this; 374 } 375 376 383 public void addField( String key, String value ) { 384 XElement subObject = new XElement( key ); 385 subObject.add( value ); 386 addElement( subObject ); 387 } 388 389 396 public boolean containsElement( String objectName ) { 397 try { 398 getElement( objectName ); 399 return true; 400 } catch ( XElementException e ) { 401 return false; 402 } 403 } 404 405 412 public boolean containsField( String objectName ) { 413 try { 414 XElement obj = getElement( objectName ); 415 return obj.isField(); 416 } catch ( XElementException e ) { 417 return false; 418 } 419 } 420 421 428 public String toString( int nestingLevel, boolean indent ) { 429 try { 430 StringBuffer indentation = new StringBuffer (); 431 StringBuffer rc = new StringBuffer (); 432 if ( indent ) { 433 for ( int i = 0; i < nestingLevel; i++ ) { 434 indentation.append( '\t' ); 435 } 436 } 437 rc.append( indentation.toString() ); 438 rc.append( "<" ); 439 rc.append( getName() ); 440 Enumeration enumeration = metadata.keys(); 441 while ( enumeration.hasMoreElements() ) { 442 String key = ( String )enumeration.nextElement(); 443 String value = ( String )metadata.get( key ); 444 rc.append( ' ' ); 445 rc.append( key ); 446 rc.append( "=\"" ); 447 rc.append( metaValueEncode( value ) ); 448 rc.append( '"' ); 449 } 450 if ( isField() ) { 451 if ( getValue().length() == 0 ) { 452 rc.append( "/>" ); 453 rc.append( nl ); 454 } else { 455 rc.append( '>' ); 456 rc.append( valueEncode( getValue() ) ); 457 rc.append( "</" ); 458 rc.append( getName() ); 459 rc.append( '>' ); 460 rc.append( nl ); 461 } 462 } else { 463 rc.append( '>' ); 464 rc.append( nl ); 465 String text = getText(); 466 if ( text.length() > 0 ) { 467 rc.append( indentation.toString() + "\t" ); 468 rc.append( getText() ); 469 rc.append( nl ); 470 } 471 for ( int i = 1; i < contents.size(); i++ ) { 472 Object o = contents.elementAt( i ); 473 rc.append( ( ( XElement )o ).toString( nestingLevel + 1, indent ) ); 474 } 475 rc.append( indentation.toString() ); 476 rc.append( "</" ); 477 rc.append( getName() ); 478 rc.append( '>' ); 479 rc.append( nl ); 480 } 481 return rc.toString(); 482 } catch ( XElementException e ) { 483 e.printStackTrace(); 485 System.exit( 1 ); 486 return ""; 487 } 488 } 489 490 496 public String toXML( boolean indent ) { 497 return 498 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + nl + 499 toString( 0, indent ); 500 } 501 502 507 public void removeFromParent() 508 throws XElementException { 509 if ( parent == null ) { 510 throw new XElementException( "" + getName() + " does not have a parent" ); 511 } 512 513 parent.contents.remove( this ); 514 parent = null; 515 } 516 517 522 public Enumeration elements() { 523 return getElementsNamed( "*" ); 524 } 525 526 535 private void addElementsToVector( Vector t, String relativeName ) { 536 537 String names[] = {null, relativeName}; 538 539 String split[] = splitFront( relativeName, "/" ); 541 if ( split != null ) { 542 543 if ( split[0].length() == 0 ) { 545 if ( parent == null ) { 547 split[0] = null; 548 } else { 549 parent.addElementsToVector( t, relativeName ); 551 return; 552 } 553 } 554 555 if ( split[1].length() == 0 ) { 557 return; 558 } 559 names = split; 560 } 561 562 if ( names[0] == null ) { 563 if ( names[1].equals( "*" ) ) { 564 for ( int i = 1; i < contents.size(); i++ ) { 565 t.addElement( contents.elementAt( i ) ); 566 } 567 } else { 568 for ( int i = 1; i < contents.size(); i++ ) { 569 XElement o = ( XElement )contents.elementAt( i ); 570 if ( names[1].equals( o.getName() ) ) { 571 t.addElement( o ); 572 } 573 } 574 } 575 } else { 576 if ( names[0].equals( "." ) ) { 577 addElementsToVector( t, names[1] ); 578 return; 579 } else if ( names[0].equals( ".." ) ) { 580 if ( parent != null ) { 581 parent.addElementsToVector( t, names[1] ); 582 } 583 return; 584 } else { 585 for ( int i = 1; i < contents.size(); i++ ) { 586 XElement o = ( XElement )contents.elementAt( i ); 587 if ( names[0].equals( o.getName() ) ) { 588 o.addElementsToVector( t, names[1] ); 589 } 590 } 591 } 592 } 593 } 594 595 603 public static XElement createFrom( java.io.InputStream is ) 604 throws XElementException, java.io.IOException { 605 class MyRecordConsumer implements XElementConsumer { 606 607 XElement root = null; 608 609 public void documentEndEvent() { 610 } 611 612 public void documentStartEvent() { 613 } 614 615 public void recordReadEvent( XElement o ) { 616 root = o; 617 } 618 } 619 620 MyRecordConsumer consumer = new MyRecordConsumer(); 621 XElementProducer producer = new XElementProducer( consumer ); 622 623 try { 624 producer.parse( is ); 625 if ( consumer.root == null ) { 626 throw new XElementException( "No root element" ); 627 } 628 return consumer.root; 629 } catch ( java.io.IOException e ) { 630 throw e; 631 } catch ( Exception e ) { 632 throw new XElementException( "Parse Error: " + e ); 633 } 634 } 635 636 644 public static XElement createFrom( java.net.URL url ) 645 throws XElementException, java.io.IOException { 646 class MyRecordConsumer implements XElementConsumer { 647 648 XElement root = null; 649 650 public void documentEndEvent() { 651 } 652 653 public void documentStartEvent() { 654 } 655 656 public void recordReadEvent( XElement o ) { 657 root = o; 658 } 659 } 660 661 MyRecordConsumer consumer = new MyRecordConsumer(); 662 XElementProducer producer = new XElementProducer( consumer ); 663 664 try { 665 producer.parse( url ); 666 if ( consumer.root == null ) { 667 throw new XElementException( "No root element" ); 668 } 669 return consumer.root; 670 } catch ( java.io.IOException e ) { 671 throw e; 672 } catch ( Exception e ) { 673 throw new XElementException( "Parse Error: " + e ); 674 } 675 } 676 677 678 private static String findAndReplace( String value, String searchStr, String replaceStr ) { 679 StringBuffer buffer = new StringBuffer ( value.length() ); 680 while ( value.length() > 0 ) { 681 int pos = value.indexOf( searchStr ); 682 if ( pos != -1 ) { 683 buffer.append( value.substring( 0, pos ) ); 684 buffer.append( replaceStr ); 685 if ( pos + searchStr.length() < value.length() ) { 686 value = value.substring( pos + searchStr.length() ); 687 } else { 688 value = ""; 689 } 690 } else { 691 buffer.append( value ); 692 value = ""; 693 } 694 } 695 return buffer.toString(); 696 } 697 698 private static String metaValueEncode( String value ) { 699 value = findAndReplace( value, "&", "&" ); 700 value = findAndReplace( value, "\"", """ ); 701 value = findAndReplace( value, "'", "'" ); 702 return utf8Encode( value ); 703 } 704 705 706 private static String utf8Encode( String value ) { 707 try { 708 return new String ( value.getBytes("UTF-8") ); 713 } catch ( Exception e ) { 714 return null; 715 } 716 } 717 718 private static String valueEncode( String value ) { 719 value = findAndReplace( value, "&", "&" ); 720 value = findAndReplace( value, "<", "<" ); 721 value = findAndReplace( value, ">", ">" ); 722 return utf8Encode( value ); 723 } 724 725 726 private static String [] splitFront( String string, String splitMarker ) { 727 728 if ( string == null || splitMarker == null ) { 729 throw new NullPointerException (); 730 } 731 732 String front; 733 String back; 734 735 int pos = string.indexOf( splitMarker ); 736 if ( pos == -1 ) { 737 return null; 738 } 739 740 int l = splitMarker.length(); 741 front = string.substring( 0, pos ); 742 if ( pos + l >= string.length() ) { 743 back = ""; 744 } else { 745 back = string.substring( pos + l ); 746 } 747 748 String rc[] = {front, back}; 749 return rc; 750 } 751 752 public String getOptionalField( String field) throws XElementException { 753 if ( !containsField(field) ) 754 return null; 755 return getField(field); 756 } 757 758 public void setOptionalField( String field, String value) throws XElementException { 759 if ( value == null ) { 760 if( containsField(field) ) 761 getElement(field).removeFromParent(); 762 return; 763 } 764 if( containsField(field) ) 765 setField(field, value); 766 else 767 addField(field, value); 768 } 769 770 } 771 | Popular Tags |