1 58 package org.apache.ecs; 59 60 import java.io.Writer ; 61 import java.io.StringWriter ; 62 import java.io.PrintWriter ; 63 import java.io.OutputStream ; 64 import java.io.BufferedOutputStream ; 65 import java.io.ByteArrayOutputStream ; 66 import java.io.IOException ; 67 import java.io.UnsupportedEncodingException ; 68 import java.io.Serializable ; 69 import org.apache.ecs.filter.CharacterFilter; 70 import java.util.Hashtable ; 71 72 79 public abstract class GenericElement implements Element,Serializable 80 { 81 87 private static Filter _defaultFilter = new CharacterFilter(); 88 89 93 private Filter filter = _defaultFilter; 94 95 99 private int position = ECSDefaults.getDefaultPosition(); 100 101 105 private boolean filter_state = ECSDefaults.getDefaultFilterState(); 106 107 111 private boolean pretty_print = ECSDefaults.getDefaultPrettyPrint(); 112 113 117 private static final String VERSION = "1.4.2"; 118 119 123 private int case_type = ECSDefaults.getDefaultCaseType(); 124 125 129 private Hashtable element = new Hashtable (4); 130 131 135 private boolean end_element = ECSDefaults.getDefaultEndElement(); 136 137 141 private String codeset = ECSDefaults.getDefaultCodeset(); 142 143 147 private char begin_start_modifier = ECSDefaults.getDefaultBeginStartModifier(); 148 149 153 private char end_start_modifier = ECSDefaults.getDefaultEndStartModifier(); 154 155 159 private char begin_end_modifier = ECSDefaults.getDefaultBeginEndModifier(); 160 161 165 private char end_end_modifier = ECSDefaults.getDefaultEndEndModifier(); 166 167 171 private boolean start_modifier_defined = false; 172 173 177 private boolean begin_start_modifier_defined = false; 178 179 183 private boolean end_start_modifier_defined = false; 184 185 189 private boolean begin_end_modifier_defined = false; 190 191 195 private boolean end_end_modifier_defined = false; 196 197 201 private boolean end_modifier_defined = false; 202 203 207 protected int tabLevel = 0; 208 209 210 private char start_tag = ECSDefaults.getDefaultStartTag(); 211 212 private char end_tag = ECSDefaults.getDefaultEndTag(); 213 214 private String __text = new String (); 215 216 220 private String element_type = new String (); 221 222 225 public GenericElement() 226 { 227 } 228 229 232 public boolean getPrettyPrint() 233 { 234 return(pretty_print); 235 } 236 237 241 public Element setPrettyPrint(boolean pretty_print) 242 { 243 this.pretty_print = pretty_print; 244 return(this); 245 } 246 247 250 protected Hashtable getElementHashEntry() 251 { 252 return(element); 253 } 254 255 259 public Element setFilterState(boolean filter_state) 260 { 261 this.filter_state = filter_state; 262 return(this); 263 } 264 265 268 public boolean getFilterState() 269 { 270 return(filter_state); 271 } 272 273 276 public Filter getFilter() 277 { 278 return(filter); 279 } 280 281 284 public void setFilter(Filter filter) 285 { 286 this.filter = filter; 287 } 288 289 292 public String getVersion() 293 { 294 return(VERSION); 295 } 296 297 300 public void setCase(int case_type) 301 { 302 this.case_type = case_type; 303 } 304 305 308 public int getCase() 309 { 310 return(case_type); 311 } 312 313 316 public void setElementType(String element_type) 317 { 318 this.element_type = element_type; 319 } 320 321 324 public String getElementType() 325 { 326 return(element_type); 327 } 328 329 332 public void setCodeSet(String codeset) 333 { 334 this.codeset = codeset; 335 } 336 337 340 public String getCodeSet() 341 { 342 return(codeset); 343 } 344 345 348 public void setNeedClosingTag(boolean close) 349 { 350 end_element = close; 351 } 352 353 356 public boolean getNeedClosingTag() 357 { 358 return(end_element); 359 } 360 361 364 public boolean getNeedLineBreak() 365 { 366 return true; 367 } 368 369 372 public void setTagPosition(int position) 373 { 374 this.position = position; 375 } 376 377 380 public int getTagPosition() 381 { 382 return(position); 383 } 384 385 389 protected String alterCase(String value) 390 { 391 switch (getCase()) 392 { 393 case UPPERCASE: 394 return value.toUpperCase(); 395 case LOWERCASE: 396 return value.toLowerCase(); 397 default: 398 return value; 399 } 400 } 401 402 407 public Element setBeginModifier(char modifier) 408 { 409 setBeginStartModifier(modifier); 410 setBeginEndModifier(modifier); 411 setBeginStartModifierDefined(true); 412 setBeginEndModifierDefined(true); 413 return(this); 414 } 415 416 421 public Element setBeginStartModifier(char modifier) 422 { 423 this.begin_start_modifier = modifier; 424 setBeginStartModifierDefined(true); 425 return(this); 426 } 427 428 433 public Element setBeginEndModifier(char modifier) 434 { 435 this.begin_end_modifier = modifier; 436 setBeginEndModifierDefined(true); 437 return(this); 438 } 439 440 445 public Element setBeginModifier(char start_modifier,char end_modifier) 446 { 447 this.begin_start_modifier = start_modifier; 448 this.begin_end_modifier = end_modifier; 449 setBeginStartModifierDefined(true); 450 setBeginEndModifierDefined(true); 451 return(this); 452 } 453 454 457 public char getBeginStartModifier() 458 { 459 return(begin_start_modifier); 460 } 461 462 465 public char getBeginEndModifier() 466 { 467 return(begin_end_modifier); 468 } 469 470 473 public Element removeBeginStartModifier() 474 { 475 setBeginStartModifierDefined(false); 476 return(this); 477 } 478 479 482 public Element removeBeginEndModifier() 483 { 484 setBeginEndModifierDefined(false); 485 return(this); 486 } 487 488 491 private void setBeginStartModifierDefined(boolean val) 492 { 493 this.begin_start_modifier_defined = val; 494 } 495 496 499 private void setBeginEndModifierDefined(boolean val) 500 { 501 this.begin_end_modifier_defined = val; 502 } 503 504 507 protected boolean getEndStartModifierDefined() 508 { 509 return this.end_start_modifier_defined; 510 } 511 512 515 protected boolean getEndEndModifierDefined() 516 { 517 return this.end_end_modifier_defined; 518 } 519 520 525 public Element setEndModifier(char modifier) 526 { 527 setEndStartModifier(modifier); 528 setEndEndModifier(modifier); 529 setEndStartModifierDefined(true); 530 setEndEndModifierDefined(true); 531 return(this); 532 } 533 534 539 public Element setEndStartModifier(char modifier) 540 { 541 this.end_start_modifier = modifier; 542 setEndStartModifierDefined(true); 543 return(this); 544 } 545 546 551 public Element setEndEndModifier(char modifier) 552 { 553 this.end_end_modifier = modifier; 554 setEndEndModifierDefined(true); 555 return(this); 556 } 557 558 563 public Element setEndModifier(char start_modifier,char end_modifier) 564 { 565 this.end_start_modifier = start_modifier; 566 this.end_end_modifier = end_modifier; 567 setEndStartModifierDefined(true); 568 setEndEndModifierDefined(true); 569 return(this); 570 } 571 572 575 public char getEndStartModifier() 576 { 577 return(end_start_modifier); 578 } 579 580 583 public char getEndEndModifier() 584 { 585 return(end_end_modifier); 586 } 587 588 591 public Element removeEndStartModifier() 592 { 593 setEndStartModifierDefined(false); 594 return(this); 595 } 596 597 600 public Element removeEndEndModifier() 601 { 602 setEndEndModifierDefined(false); 603 return(this); 604 } 605 606 609 private void setEndStartModifierDefined(boolean val) 610 { 611 this.end_start_modifier_defined = val; 612 } 613 614 617 private void setEndEndModifierDefined(boolean val) 618 { 619 this.end_end_modifier_defined = val; 620 } 621 622 625 protected boolean getBeginStartModifierDefined() 626 { 627 return this.begin_start_modifier_defined; 628 } 629 630 633 protected boolean getBeginEndModifierDefined() 634 { 635 return this.begin_end_modifier_defined; 636 } 637 638 641 public void setStartTagChar(char start_tag) 642 { 643 this.start_tag = start_tag; 644 } 645 646 649 public char getStartTagChar() 650 { 651 return(start_tag); 652 } 653 654 657 public void setEndTagChar(char end_tag) 658 { 659 this.end_tag = end_tag; 660 } 661 662 665 public char getEndTagChar() 666 { 667 return(end_tag); 668 } 669 670 public String getTagText() 671 { 672 return(__text); 673 } 674 675 public Element setTagText(String text) 676 { 677 this.__text = (text); 678 return(this); 679 } 680 681 public String createStartTag() 682 { 683 StringBuffer sb = new StringBuffer (); 684 sb.append(getStartTagChar()); 685 686 if(getBeginStartModifierDefined()) 687 { 688 sb.append(getBeginStartModifier()); 689 } 690 sb.append(getElementType()); 691 692 if(getBeginEndModifierDefined()) 693 { 694 sb.append(getBeginEndModifier()); 695 } 696 sb.append(getEndTagChar()); 697 return(sb.toString()); 698 } 699 700 public String createEndTag() 701 { 702 setEndStartModifier('/'); 703 StringBuffer sb = new StringBuffer (); 704 sb.append(getStartTagChar()); 705 706 if(getEndStartModifierDefined()) 707 { 708 sb.append(getEndStartModifier()); 709 } 710 sb.append(getElementType()); 711 712 if(getEndEndModifierDefined()) 713 { 714 sb.append(getEndEndModifier()); 715 } 716 sb.append(getEndTagChar()); 717 return(sb.toString()); 718 } 719 720 723 public final String toString() 724 { 725 StringWriter write = new StringWriter (); 726 String out = null; 727 try 728 { 729 output(write); 730 write.flush(); 731 out = write.toString(); 732 write.close(); 733 } 734 catch (UnsupportedEncodingException use) 735 { 736 use.printStackTrace(); 737 } 738 catch (IOException ioe) 739 { 740 ioe.printStackTrace(); 741 } 742 return(out); 743 } 744 745 748 public final String toString(String codeset) 749 { 750 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 751 BufferedOutputStream bos = new BufferedOutputStream (baos); 752 String out = null; 753 try 754 { 755 output(bos); 756 bos.flush(); 757 out = baos.toString(codeset); 758 bos.close(); 759 baos.close(); 760 } 761 catch (UnsupportedEncodingException use) 762 { 763 } 764 catch (IOException ioe) 765 { 766 } 767 return(out); 768 } 769 770 773 public void output(OutputStream out) 774 { 775 String encoding = null; 776 if ( getCodeSet() != null ) 777 { 778 encoding = getCodeSet(); 779 } 780 else 781 { 782 encoding = "UTF-16BE"; 785 } 786 787 try 788 { 789 out.write(createStartTag().getBytes(encoding)); 790 791 if(getFilterState()) 792 out.write(getFilter().process(getTagText()).getBytes(encoding)); 793 else 794 out.write(getTagText().getBytes(encoding)); 795 796 if (getNeedClosingTag()) 797 out.write(createEndTag().getBytes(encoding)); 798 799 } 800 catch (UnsupportedEncodingException uee) 801 { 802 uee.printStackTrace(new PrintWriter (out)); 803 } 804 catch (IOException ioe) 805 { 806 ioe.printStackTrace(new PrintWriter (out)); 807 } 808 } 809 810 813 public void output(PrintWriter out) 814 { 815 816 out.write(createStartTag()); 817 818 if(getFilterState()) 819 out.write(getFilter().process(getTagText())); 820 else 821 out.write(getTagText()); 822 823 if (getNeedClosingTag()) 824 out.write(createEndTag()); 825 826 } 827 828 831 public void output(Writer out) 832 { 833 output ( new PrintWriter (out) ); 834 } 835 836 839 public void setTabLevel(int tabLevel) 840 { 841 this.tabLevel = tabLevel; 842 } 843 844 public int getTabLevel() 845 { 846 return tabLevel; 847 } 848 849 protected void putTabs(int tabCount, OutputStream out) 850 throws IOException 851 { 852 for (int i = 0; i < tabCount; i++) 853 out.write('\t'); 854 } 855 856 protected void putTabs(int tabCount, PrintWriter out) 857 { 858 for (int i = 0; i < tabCount; i++) 859 out.print('\t'); 860 } 861 862 } 863 | Popular Tags |