1 28 29 package com.opencms.htmlconverter; 30 31 import org.opencms.util.CmsStringUtil; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.InputStream ; 35 import java.util.ArrayList ; 36 import java.util.HashSet ; 37 import java.util.StringTokenizer ; 38 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.NamedNodeMap ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.NodeList ; 43 import org.w3c.tidy.Tidy; 44 45 53 final class CmsHtmlConverterConfig { 54 55 56 private static final int C_COMMENT_NODE = 0; 57 58 private static final int C_CONFIG_NODE = 1; 59 60 private static final int C_DEFAULT_NODE = 10; 61 62 private static final int C_REPLACECONTENT_NODE = 11; 63 64 private static final int C_INLINETAG_NODE = 12; 65 66 private static final int C_REMOVETAG_NODE = 13; 67 68 private static final int C_REMOVEBLOCK_NODE = 14; 69 70 private static final int C_REPLACETAG_NODE = 15; 71 72 private static final int C_REPLACEBLOCK_NODE = 16; 73 74 private static final int C_REPLACESTRING_NODE = 17; 75 76 77 private static final int C_XHTMLOUTPUT_NODE = 20; 78 79 private static final int C_GLOBALPREFIX_NODE = 21; 80 81 private static final int C_GLOBALSUFFIX_NODE = 22; 82 83 private static final int C_GLOBALADDEVERYLINE_NODE = 23; 84 85 private static final int C_USEBRACKETS_NODE = 24; 86 87 private static final int C_ENCODEQUOTATIONMARKS_NODE = 25; 88 89 90 private static final int C_ADDEVERYLINE_NODE = 30; 91 92 private static final int C_PREFIX_NODE = 31; 93 94 private static final int C_SUFFIX_NODE = 32; 95 96 private static final int C_TAG_NODE = 33; 97 98 private static final int C_STRING_NODE = 34; 99 100 101 private boolean m_xhtmlOutput; 102 103 private String m_globalPrefix = ""; 104 105 private String m_globalSuffix = ""; 106 107 private boolean m_globalAddEveryLine; 108 109 private String m_openBracket = ""; 110 111 private String m_closeBracket = ""; 112 113 private boolean m_useBrackets; 114 115 private boolean m_encodeQuotationmarks; 116 117 private String m_quotationmark = ""; 118 119 private String m_replaceTagsPrefix = ""; 120 121 private String m_replaceTagsSuffix = ""; 122 123 private boolean m_replaceTagsAddEveryLine; 124 125 private boolean m_replaceTagsUseDefaults; 126 127 128 private String m_replaceBlocksPrefix = ""; 129 130 private String m_replaceBlocksSuffix = ""; 131 132 private boolean m_replaceBlocksAddEveryLine; 133 134 private boolean m_replaceBlocksUseDefaults; 135 136 137 private String m_replaceStringsPrefix = ""; 138 139 private String m_replaceStringsSuffix = ""; 140 141 private boolean m_replaceStringsAddEveryLine; 142 143 private boolean m_replaceStringsUseDefaults; 144 145 146 private ArrayList m_replaceContent = new ArrayList (); 147 148 private HashSet m_removeTags = new HashSet (); 149 150 private HashSet m_removeBlocks = new HashSet (); 151 152 private HashSet m_inlineTags = new HashSet (); 153 154 private ArrayList m_replaceExtendedChars = new ArrayList (); 155 156 private ArrayList m_replaceStrings = new ArrayList (); 157 158 private ArrayList m_replaceTags = new ArrayList (); 159 160 private ArrayList m_replaceBlocks = new ArrayList (); 161 162 163 private ArrayList m_tempReplaceContent = new ArrayList (); 164 165 private ArrayList m_tempReplaceStrings = new ArrayList (); 166 167 private ArrayList m_tempReplaceTags = new ArrayList (); 168 169 private ArrayList m_tempReplaceBlocks = new ArrayList (); 170 171 172 private CmsHtmlConverterTools m_tools = new CmsHtmlConverterTools(); 173 174 175 private boolean m_isConfigured; 176 177 180 protected CmsHtmlConverterConfig() { 181 } 183 184 189 protected CmsHtmlConverterConfig(String confString) { 190 init(confString); 191 } 192 193 198 protected CmsHtmlConverterConfig(InputStream in) { 199 init(in); 200 } 201 202 207 protected HashSet getRemoveTags() { 208 return m_removeTags; 209 } 210 211 216 protected HashSet getRemoveBlocks() { 217 return m_removeBlocks; 218 } 219 220 225 protected HashSet getInlineTags() { 226 return m_inlineTags; 227 } 228 229 234 protected ArrayList getReplaceContent() { 235 return m_replaceContent; 236 } 237 238 243 protected ArrayList getReplaceExtendedChars() { 244 return m_replaceExtendedChars; 245 } 246 247 252 protected ArrayList getReplaceStrings() { 253 return m_replaceStrings; 254 } 255 256 261 protected ArrayList getReplaceTags() { 262 return m_replaceTags; 263 } 264 265 270 protected ArrayList getReplaceBlocks() { 271 return m_replaceBlocks; 272 } 273 274 279 protected boolean getXhtmlOutput() { 280 return m_xhtmlOutput; 281 } 282 283 288 protected String getGlobalPrefix() { 289 return m_globalPrefix; 290 } 291 292 297 protected String getGlobalSuffix() { 298 return m_globalSuffix; 299 } 300 301 306 protected boolean getGlobalAddEveryLine() { 307 return m_globalAddEveryLine; 308 } 309 310 315 protected boolean getUseBrackets() { 316 return m_useBrackets; 317 } 318 319 324 protected boolean getEncodeQuotationmarks() { 325 return m_encodeQuotationmarks; 326 } 327 328 333 protected String getQuotationmark() { 334 return m_quotationmark; 335 } 336 337 354 protected boolean addObjectReplaceTag(String prefix, String tagName, 355 String attributeName, String attributeValue, String replaceStartTag, 356 String replaceEndTag, String suffix, boolean getReplaceFromAttrs, 357 String startAttribute, String endAttribute, String parameter, boolean replaceParamAttr) { 358 359 return m_replaceTags.add( 360 new CmsHtmlConverterObjectReplaceTags( 361 prefix, 362 tagName, 363 attributeName, 364 attributeValue, 365 replaceStartTag, 366 replaceEndTag, 367 suffix, 368 getReplaceFromAttrs, 369 startAttribute, 370 endAttribute, 371 parameter, 372 replaceParamAttr)); 373 } 374 375 380 protected void removeObjectReplaceTag(int index) { 381 m_replaceTags.remove(index); 382 } 383 384 398 protected boolean addObjectReplaceBlock(String prefix, String tagName, 399 String attributeName, String attributeValue, String replaceString, 400 String suffix, boolean getReplaceFromAttrs, String replaceAttribute, String parameter) { 401 402 return m_replaceBlocks.add( 403 new CmsHtmlConverterObjectReplaceBlocks( 404 prefix, 405 tagName, 406 attributeName, 407 attributeValue, 408 replaceString, 409 suffix, 410 getReplaceFromAttrs, 411 replaceAttribute, 412 parameter)); 413 } 414 415 420 protected void removeObjectReplaceBlock(int index) { 421 m_replaceBlocks.remove(index); 422 } 423 424 430 protected String scanBrackets(String content) { 431 content = m_tools.replaceString(content, m_openBracket, "<"); 432 content = m_tools.replaceString(content, m_closeBracket, ">"); 433 return content; 434 } 435 436 441 protected void init(String confString) { 442 InputStream in = new ByteArrayInputStream (confString.getBytes()); 443 init(in); 444 } 445 446 451 protected void init(InputStream in) { 452 Node node; 453 Tidy tidy = new Tidy(); 454 455 tidy.setXmlTags(true); 456 tidy.setQuiet(true); 457 tidy.setOnlyErrors(true); 458 tidy.setShowWarnings(false); 459 460 if (m_isConfigured) { 461 clearConfiguration(); 462 } 463 node = tidy.parseDOM(in, null); 464 parseConfig(node); 465 buildObjects(); 466 m_isConfigured = true; 467 } 468 469 472 private void clearConfiguration() { 473 m_globalAddEveryLine = false; 474 m_useBrackets = false; 475 m_globalPrefix = ""; 476 m_globalSuffix = ""; 477 m_openBracket = ""; 478 m_closeBracket = ""; 479 m_encodeQuotationmarks = false; 480 m_quotationmark = ""; 481 m_replaceTagsPrefix = ""; 482 m_replaceTagsSuffix = ""; 483 m_replaceTagsAddEveryLine = false; 484 m_replaceTagsUseDefaults = false; 485 m_replaceBlocksPrefix = ""; 486 m_replaceBlocksSuffix = ""; 487 m_replaceBlocksAddEveryLine = false; 488 m_replaceBlocksUseDefaults = false; 489 m_replaceStringsPrefix = ""; 490 m_replaceStringsSuffix = ""; 491 m_replaceStringsAddEveryLine = false; 492 m_replaceStringsUseDefaults = false; 493 m_tempReplaceContent.clear(); 494 m_tempReplaceStrings.clear(); 495 m_tempReplaceTags.clear(); 496 m_tempReplaceBlocks.clear(); 497 m_replaceContent.clear(); 498 m_removeTags.clear(); 499 m_removeBlocks.clear(); 500 m_inlineTags.clear(); 501 m_replaceExtendedChars.clear(); 502 m_replaceStrings.clear(); 503 m_replaceTags.clear(); 504 m_replaceBlocks.clear(); 505 m_isConfigured = false; 506 } 507 508 511 private void buildObjects() { 512 boolean added = true; 513 514 if (m_useBrackets) { 515 m_globalPrefix = scanBrackets(m_globalPrefix); 516 m_globalSuffix = scanBrackets(m_globalSuffix); 517 } 518 519 if (!m_encodeQuotationmarks) { 520 m_quotationmark = "\""; 521 } 522 523 buildObjectReplaceContent(); 524 525 buildObjectReplaceTags(); 526 527 buildObjectReplaceBlocks(); 528 529 buildObjectReplaceStrings(); 530 531 String Chars = "\",",<,<,>,>"; 534 StringTokenizer T = new StringTokenizer (Chars, ","); 535 while (T.hasMoreTokens()) { 536 added = m_replaceExtendedChars.add(new CmsHtmlConverterObjectReplaceExtendedChars(T.nextToken(), T.nextToken())); 537 if (!added) { 538 System.err.println("Configuration error: failed adding object to ArrayList m_replaceExtendedChars."); 539 } 540 } 541 542 m_tempReplaceContent.clear(); 543 m_tempReplaceStrings.clear(); 544 m_tempReplaceTags.clear(); 545 m_tempReplaceBlocks.clear(); 546 } 547 548 551 private void buildObjectReplaceContent() { 552 boolean added = true; 553 int len = m_tempReplaceContent.size(); 554 String content, replace; 555 CmsHtmlConverterObjectReplaceContent objectReplaceContent = new CmsHtmlConverterObjectReplaceContent(); 556 557 for (int i=0; i<len; i++) { 558 objectReplaceContent = (CmsHtmlConverterObjectReplaceContent) (m_tempReplaceContent.get(i)); 559 content = objectReplaceContent.getSearchString(); 560 replace = objectReplaceContent.getReplaceItem(); 561 if (m_useBrackets) { 562 replace = scanBrackets(replace); 563 } 564 added = m_replaceContent.add(new CmsHtmlConverterObjectReplaceContent(content, replace)); 565 if (!added) { 566 System.err.println("Configuration error: failed adding object to ArrayList m_replaceContent."); 567 } 568 } 569 added = m_replaceContent.add(new CmsHtmlConverterObjectReplaceContent(" ", "$nbsp$")); 570 if (!added) { 571 System.err.println("Configuration error: failed adding object to ArrayList m_replaceContent."); 572 } 573 } 574 575 578 private void buildObjectReplaceTags() { 579 boolean added = true; 580 int len = m_tempReplaceTags.size(); 581 String name, attrib, value, replaceStartTag, replaceEndTag, startAttribute, endAttribute, parameter; 582 boolean getReplaceFromAttrs, replaceParamAttr; 583 CmsHtmlConverterObjectReplaceTags objectReplaceTags = new CmsHtmlConverterObjectReplaceTags(); 584 585 if (m_useBrackets) { 586 m_replaceTagsPrefix = scanBrackets(m_replaceTagsPrefix); 587 m_replaceTagsSuffix = scanBrackets(m_replaceTagsSuffix); 588 } 589 if (m_replaceTagsUseDefaults) { 590 m_replaceTagsPrefix = m_globalSuffix; 591 m_replaceTagsSuffix = m_globalPrefix; 592 m_replaceTagsAddEveryLine = m_globalAddEveryLine; 593 } 594 if (m_replaceTagsAddEveryLine) { 595 m_replaceTagsPrefix += "\n"; 596 m_replaceTagsSuffix = "\n" + m_replaceTagsSuffix; 597 } 598 599 for (int i=0; i<len; i++) { 600 objectReplaceTags = (CmsHtmlConverterObjectReplaceTags) (m_tempReplaceTags.get(i)); 601 name = objectReplaceTags.getTagName(); 602 attrib = objectReplaceTags.getTagAttrib(); 603 value = objectReplaceTags.getTagAttribValue(); 604 replaceStartTag = objectReplaceTags.getReplaceStartTag(); 605 replaceEndTag = objectReplaceTags.getReplaceEndTag(); 606 getReplaceFromAttrs = objectReplaceTags.getReplaceFromAttrs(); 607 startAttribute = objectReplaceTags.getStartAttribute(); 608 endAttribute = objectReplaceTags.getEndAttribute(); 609 parameter = objectReplaceTags.getParameter(); 610 replaceParamAttr = objectReplaceTags.getReplaceParamAttr(); 611 if (m_useBrackets) { 612 replaceStartTag = scanBrackets(replaceStartTag); 613 replaceEndTag = scanBrackets(replaceEndTag); 614 } 615 added = 616 m_replaceTags.add( 617 new CmsHtmlConverterObjectReplaceTags( 618 m_replaceTagsPrefix, 619 name, 620 attrib, 621 value, 622 replaceStartTag, 623 replaceEndTag, 624 m_replaceTagsSuffix, 625 getReplaceFromAttrs, 626 startAttribute, 627 endAttribute, 628 parameter, 629 replaceParamAttr)); 630 if (!added) { 631 System.err.println("Configuration error: failed adding object to ArrayList m_replaceTags."); 632 } 633 } 634 } 635 636 639 private void buildObjectReplaceBlocks() { 640 boolean added = true; 641 int len = m_tempReplaceBlocks.size(); 642 String name, attrib, value, replaceString, replaceAttribute, parameter; 643 boolean getReplaceFromAttrs; 644 CmsHtmlConverterObjectReplaceBlocks objectReplaceBlocks = new CmsHtmlConverterObjectReplaceBlocks(); 645 646 if (m_useBrackets) { 647 m_replaceBlocksPrefix = scanBrackets(m_replaceBlocksPrefix); 648 m_replaceBlocksSuffix = scanBrackets(m_replaceBlocksSuffix); 649 } 650 if (m_replaceBlocksUseDefaults) { 651 m_replaceBlocksPrefix = m_globalSuffix; 652 m_replaceBlocksSuffix = m_globalPrefix; 653 m_replaceBlocksAddEveryLine = m_globalAddEveryLine; 654 } 655 if (m_replaceBlocksAddEveryLine) { 656 m_replaceBlocksPrefix += "\n"; 657 m_replaceBlocksSuffix = "\n" + m_replaceBlocksSuffix; 658 } 659 660 for (int i=0; i<len; i++) { 661 objectReplaceBlocks = (CmsHtmlConverterObjectReplaceBlocks) (m_tempReplaceBlocks.get(i)); 662 name = objectReplaceBlocks.getTagName(); 663 attrib = objectReplaceBlocks.getTagAttrib(); 664 value = objectReplaceBlocks.getTagAttribValue(); 665 replaceString = objectReplaceBlocks.getReplaceString(); 666 getReplaceFromAttrs = objectReplaceBlocks.getReplaceFromAttrs(); 667 replaceAttribute = objectReplaceBlocks.getReplaceAttribute(); 668 parameter = objectReplaceBlocks.getParameter(); 669 if (m_useBrackets) { 670 replaceString = scanBrackets(replaceString); 671 } 672 added = 673 m_replaceBlocks.add( 674 new CmsHtmlConverterObjectReplaceBlocks( 675 m_replaceBlocksPrefix, 676 name, 677 attrib, 678 value, 679 replaceString, 680 m_replaceBlocksSuffix, 681 getReplaceFromAttrs, 682 replaceAttribute, 683 parameter)); 684 if (!added) { 685 System.err.println("Configuration error: failed adding object to ArrayList m_replaceBlocks."); 686 } 687 } 688 } 689 690 693 private void buildObjectReplaceStrings() { 694 boolean added = true; 695 int len = m_tempReplaceStrings.size(); 696 String content, replace; 697 CmsHtmlConverterObjectReplaceStrings objectReplaceStrings = new CmsHtmlConverterObjectReplaceStrings(); 698 699 if (m_useBrackets) { 700 m_replaceStringsPrefix = scanBrackets(m_replaceStringsPrefix); 701 m_replaceStringsSuffix = scanBrackets(m_replaceStringsSuffix); 702 } 703 if (m_replaceStringsUseDefaults) { 704 m_replaceStringsPrefix = m_globalSuffix; 705 m_replaceStringsSuffix = m_globalPrefix; 706 m_replaceStringsAddEveryLine = m_globalAddEveryLine; 707 } 708 if (m_replaceStringsAddEveryLine) { 709 m_replaceStringsPrefix += "\n"; 710 m_replaceStringsSuffix = "\n" + m_replaceStringsSuffix; 711 } 712 713 for (int i=0; i<len; i++) { 714 objectReplaceStrings = (CmsHtmlConverterObjectReplaceStrings) (m_tempReplaceStrings.get(i)); 715 content = objectReplaceStrings.getSearchString(); 716 replace = objectReplaceStrings.getReplaceItem(); 717 if (m_useBrackets) { 718 replace = scanBrackets(replace); 719 } 720 added = 721 m_replaceStrings.add( 722 new CmsHtmlConverterObjectReplaceStrings( 723 content, 724 m_replaceStringsPrefix, 725 replace, 726 m_replaceStringsSuffix)); 727 if (!added) { 728 System.err.println("Configuration error: failed adding object to ArrayList m_replaceStrings."); 729 } 730 } 731 added = 732 m_replaceStrings.add( 733 new CmsHtmlConverterObjectReplaceStrings( 734 "$nbsp$", 735 "", 736 " ", 737 "")); 738 if (!added) { 739 System.err.println("Configuration error: failed adding object to ArrayList m_replaceStrings."); 740 } 741 } 742 743 748 private void parseConfig(Node node) { 749 int type = node.getNodeType(); 750 int len = 0; 751 boolean added = true; 752 String valueString = ""; 753 switch (type) { 754 case Node.DOCUMENT_NODE: 755 parseConfig(((Document )node).getDocumentElement()); 756 break; 757 case Node.ELEMENT_NODE: 758 int nodeName = getConfigNodeName(node.getNodeName()); 759 NodeList children = node.getChildNodes(); 760 if (children != null) { 761 len = children.getLength(); 762 } 763 switch (nodeName) { 764 case C_COMMENT_NODE: 765 break; 766 case C_CONFIG_NODE: 767 for (int i = 0; i < len; i++) { 768 769 parseConfig(children.item(i)); 770 } 771 break; 772 case C_DEFAULT_NODE: 773 for (int i = 0; i < len; i++) { 774 775 parseDefaults(children.item(i)); 776 } 777 break; 778 case C_REPLACECONTENT_NODE: 779 for (int i = 0; i < len; i++) { 780 781 parseReplaceContent(children.item(i)); 782 } 783 break; 784 case C_INLINETAG_NODE: 785 for (int i = 0; i < len; i++) { 786 787 valueString = parseTag(children.item(i)); 788 if (!valueString.equals("")) { 789 added = m_inlineTags.add(new String (valueString)); 790 } 791 if (!added) { 792 System.err.println("Configuration error: failed adding object to ArrayList m_inlineTags."); 793 } 794 } 795 break; 796 case C_REMOVETAG_NODE: 797 for (int i = 0; i < len; i++) { 798 799 valueString = parseTag(children.item(i)); 800 if (!valueString.equals("")) { 801 added = m_removeTags.add(new String (valueString)); 802 } 803 if (!added) { 804 System.err.println("Configuration error: failed adding object to ArrayList m_removeTags."); 805 } 806 } 807 break; 808 case C_REMOVEBLOCK_NODE: 809 for (int i = 0; i < len; i++) { 810 811 valueString = parseTag(children.item(i)); 812 if (!valueString.equals("")) { 813 added = m_removeBlocks.add(new String (valueString)); 814 } 815 if (!added) { 816 System.err.println("Configuration error: failed adding object to ArrayList m_removeBlocks."); 817 } 818 } 819 break; 820 case C_REPLACETAG_NODE: 821 m_replaceTagsUseDefaults = checkBoolean(node, 0); 822 for (int i = 0; i < len; i++) { 823 parseReplaceTags(children.item(i)); 824 } 825 break; 826 case C_REPLACEBLOCK_NODE: 827 m_replaceBlocksUseDefaults = checkBoolean(node, 0); 828 for (int i = 0; i < len; i++) { 829 parseReplaceBlocks(children.item(i)); 830 } 831 break; 832 case C_REPLACESTRING_NODE: 833 m_replaceStringsUseDefaults = checkBoolean(node, 0); 834 for (int i = 0; i < len; i++) { 835 parseReplaceStrings(children.item(i)); 836 } 837 break; 838 default: 839 System.err.println("Configuration error: Tag <"+node.getNodeName()+"> not recognized."); 840 break; 841 } 842 default: 843 break; 844 } 845 } 846 847 852 private void parseDefaults(Node node) { 853 NamedNodeMap attrs = node.getAttributes(); 854 int nodeName = getConfigNodeName(node.getNodeName()); 855 String attrName; 856 switch (nodeName) { 857 case C_COMMENT_NODE: 858 break; 859 case C_XHTMLOUTPUT_NODE: 860 m_xhtmlOutput = checkBoolean(node, 0); 861 break; 862 case C_GLOBALPREFIX_NODE: 863 m_globalPrefix = attrs.item(0).getNodeValue(); 864 break; 865 case C_GLOBALSUFFIX_NODE: 866 m_globalSuffix = attrs.item(0).getNodeValue(); 867 break; 868 case C_GLOBALADDEVERYLINE_NODE: 869 m_globalAddEveryLine = checkBoolean(node, 0); 870 break; 871 case C_USEBRACKETS_NODE: 872 for (int i=0; i<attrs.getLength(); i++) { 873 attrName = attrs.item(i).getNodeName(); 874 if (attrName.equalsIgnoreCase("value")) { 875 m_useBrackets = checkBoolean(node, i); 876 } 877 if (attrName.equalsIgnoreCase("openbracket")) { 878 m_openBracket = attrs.item(i).getNodeValue(); 879 } 880 if (attrName.equalsIgnoreCase("closebracket")) { 881 m_closeBracket = attrs.item(i).getNodeValue(); 882 } 883 } 884 if ((m_useBrackets) && ((m_openBracket.equals("")) || (m_closeBracket.equals("")))) { 885 System.err.println("Fatal configuration error: Wrong definition of <usebrackets> in block <defaults>."); 886 System.exit(1); 887 } 888 break; 889 case C_ENCODEQUOTATIONMARKS_NODE: 890 for (int i=0; i<attrs.getLength(); i++) { 891 attrName = attrs.item(i).getNodeName(); 892 if (attrName.equalsIgnoreCase("value")) { 893 m_encodeQuotationmarks = checkBoolean(node, i); 894 } 895 if (attrName.equalsIgnoreCase("code")) { 896 m_quotationmark = attrs.item(i).getNodeValue(); 897 } 898 } 899 if ((m_encodeQuotationmarks) && (m_quotationmark.equals(""))) { 900 System.err.println("Fatal configuration error: Wrong definition of <encodequotationsmarks> in block <defaults>."); 901 System.exit(1); 902 } 903 break; 904 default: 905 System.err.println("Fatal configuration error: Tag <"+node.getNodeName()+"> not recognized in block <defaults>."); 906 System.exit(1); 907 } 908 } 909 910 915 private void parseReplaceContent(Node node) { 916 if (node.getNodeName().equals("#comment")) { 917 return; 918 } 919 NamedNodeMap attrs = node.getAttributes(); 920 String attrName; 921 String content = ""; 922 String replace = ""; 923 boolean added = true; 924 for (int i=0; i<attrs.getLength(); i++) { 925 attrName = attrs.item(i).getNodeName(); 926 if (attrName.equalsIgnoreCase("content")) { 927 content = attrs.item(i).getNodeValue(); 928 } 929 if (attrName.equalsIgnoreCase("replace")) { 930 replace = attrs.item(i).getNodeValue(); 931 } 932 } 933 if (content.equals("")) { 934 System.err.println("Fatal configuration error: Empty content in block <replacecontent>."); 935 System.exit(1); 936 } 937 added = m_tempReplaceContent.add(new CmsHtmlConverterObjectReplaceContent(content, replace)); 938 if (!added) { 939 System.err.println("Configuration error: Failed adding object to ArrayList m_tempReplaceContent."); 940 } 941 } 942 943 948 private void parseReplaceTags(Node node) { 949 boolean added = true; 950 NamedNodeMap attrs = node.getAttributes(); 951 int nodeName = getConfigNodeName(node.getNodeName()); 952 String attrName; 953 String attrNameValue = ""; 954 String attrAttribValue = ""; 955 String attrValueValue = ""; 956 String attrStartValue = ""; 957 String attrEndValue = ""; 958 boolean attrGetReplace = false; 959 String attrStartAttribute = ""; 960 String attrEndAttribute = ""; 961 String attrParameter = ""; 962 boolean attrReplaceParamAttr = false; 963 switch (nodeName) { 964 case C_COMMENT_NODE: 965 break; 966 case C_PREFIX_NODE: 967 m_replaceTagsPrefix = attrs.getNamedItem("value").getNodeValue(); 969 break; 970 case C_SUFFIX_NODE: 971 m_replaceTagsSuffix = attrs.item(0).getNodeValue(); 972 break; 973 case C_ADDEVERYLINE_NODE: 974 m_replaceTagsAddEveryLine = checkBoolean(node, 0); 975 break; 976 case C_TAG_NODE: 977 for (int i=0; i<attrs.getLength(); i++) { 978 attrName = attrs.item(i).getNodeName(); 979 if (attrName.equalsIgnoreCase("name")) { 980 attrNameValue = attrs.item(i).getNodeValue(); 981 } 982 if (attrName.equalsIgnoreCase("attrib")) { 983 attrAttribValue = attrs.item(i).getNodeValue(); 984 } 985 if (attrName.equalsIgnoreCase("value")) { 986 attrValueValue = attrs.item(i).getNodeValue(); 987 } 988 if (attrName.equalsIgnoreCase("replacestarttag")) { 989 attrStartValue = attrs.item(i).getNodeValue(); 990 } 991 if (attrName.equalsIgnoreCase("replaceendtag")) { 992 attrEndValue = attrs.item(i).getNodeValue(); 993 } 994 if (attrName.equalsIgnoreCase("getreplacefromattrs")) { 995 attrGetReplace = checkBoolean(node, i); 996 } 997 if (attrName.equalsIgnoreCase("startattribute")) { 998 attrStartAttribute = attrs.item(i).getNodeValue(); 999 } 1000 if (attrName.equalsIgnoreCase("endattribute")) { 1001 attrEndAttribute = attrs.item(i).getNodeValue(); 1002 } 1003 if (attrName.equalsIgnoreCase("parameter")) { 1004 attrParameter = attrs.item(i).getNodeValue(); 1005 } 1006 if (attrName.equalsIgnoreCase("replaceparamattr")) { 1007 attrReplaceParamAttr = checkBoolean(node, i); 1008 } 1009 } 1010 if (attrNameValue.equals("")) { 1011 System.err.println("Fatal configuration error: Tagname not specified in block <replacetags>."); 1012 System.exit(1); 1013 } 1014 added = 1015 m_tempReplaceTags.add( 1016 new CmsHtmlConverterObjectReplaceTags( 1017 "", 1018 attrNameValue, 1019 attrAttribValue, 1020 attrValueValue, 1021 attrStartValue, 1022 attrEndValue, 1023 "", 1024 attrGetReplace, 1025 attrStartAttribute, 1026 attrEndAttribute, 1027 attrParameter, 1028 attrReplaceParamAttr)); 1029 if (!added) { 1030 System.err.println("Configuration error: Failed adding object to ArrayList m_tempReplaceTags."); 1031 } 1032 break; 1033 default: 1034 System.err.println("Fatal configuration error: Tag <"+node.getNodeName()+"> not recognized in block <replacetags>."); 1035 System.exit(1); 1036 } 1037 } 1038 1039 1044 private void parseReplaceBlocks(Node node) { 1045 boolean added = false; 1046 NamedNodeMap attrs = node.getAttributes(); 1047 int nodeName = getConfigNodeName(node.getNodeName()); 1048 String attrName; 1049 String attrNameValue = ""; 1050 String attrAttribValue = ""; 1051 String attrValueValue = ""; 1052 String attrStringValue = ""; 1053 boolean attrGetReplace = false; 1054 String attrReplaceAttribute = ""; 1055 String attrParameter = ""; 1056 switch (nodeName) { 1057 case C_COMMENT_NODE: 1058 break; 1059 case C_PREFIX_NODE: 1060 m_replaceBlocksPrefix = attrs.item(0).getNodeValue(); 1061 break; 1062 case C_SUFFIX_NODE: 1063 m_replaceBlocksSuffix = attrs.item(0).getNodeValue(); 1064 break; 1065 case C_ADDEVERYLINE_NODE: 1066 m_replaceBlocksAddEveryLine = checkBoolean(node, 0); 1067 break; 1068 case C_TAG_NODE: 1069 for (int i=0; i<attrs.getLength(); i++) { 1070 attrName = attrs.item(i).getNodeName(); 1071 if (attrName.equalsIgnoreCase("name")) { 1072 attrNameValue = attrs.item(i).getNodeValue(); 1073 } 1074 if (attrName.equalsIgnoreCase("attrib")) { 1075 attrAttribValue = attrs.item(i).getNodeValue(); 1076 } 1077 if (attrName.equalsIgnoreCase("value")) { 1078 attrValueValue = attrs.item(i).getNodeValue(); 1079 } 1080 if (attrName.equalsIgnoreCase("replacestring")) { 1081 attrStringValue = attrs.item(i).getNodeValue(); 1082 } 1083 if (attrName.equalsIgnoreCase("getreplacefromattrs")) { 1084 attrGetReplace = checkBoolean(node, i); 1085 } 1086 if (attrName.equalsIgnoreCase("replaceattribute")) { 1087 attrReplaceAttribute = attrs.item(i).getNodeValue(); 1088 } 1089 if (attrName.equalsIgnoreCase("parameter")) { 1090 attrParameter = attrs.item(i).getNodeValue(); 1091 } 1092 } 1093 added = 1094 m_tempReplaceBlocks.add( 1095 new CmsHtmlConverterObjectReplaceBlocks( 1096 "", 1097 attrNameValue, 1098 attrAttribValue, 1099 attrValueValue, 1100 attrStringValue, 1101 "", 1102 attrGetReplace, 1103 attrReplaceAttribute, 1104 attrParameter)); 1105 if (!added) { 1106 System.err.println("Configuration error: Failed adding object to ArrayList m_tempReplaceBlocks."); 1107 } 1108 if (attrNameValue.equals("")) { 1109 System.err.println("Fatal configuration error: Tagname not specified in block <replaceblocks>."); 1110 System.exit(1); 1111 } 1112 1113 break; 1114 default: 1115 System.err.println("Fatal configuration error: Tag <"+node.getNodeName()+"> not recognized in block <replaceblocks>."); 1116 System.exit(1); 1117 } 1118 } 1119 1120 1125 private void parseReplaceStrings(Node node) { 1126 boolean added = false; 1127 NamedNodeMap attrs = node.getAttributes(); 1128 int nodeName = getConfigNodeName(node.getNodeName()); 1129 String attrName; 1130 String attrContentValue = ""; 1131 String attrReplaceValue = ""; 1132 switch (nodeName) { 1133 case C_COMMENT_NODE: 1134 break; 1135 case C_PREFIX_NODE: 1136 m_replaceStringsPrefix = attrs.item(0).getNodeValue(); 1137 break; 1138 case C_SUFFIX_NODE: 1139 m_replaceStringsSuffix = attrs.item(0).getNodeValue(); 1140 break; 1141 case C_ADDEVERYLINE_NODE: 1142 m_replaceStringsAddEveryLine = checkBoolean(node, 0); 1143 break; 1144 case C_STRING_NODE: 1145 for (int i=0; i<attrs.getLength(); i++) { 1146 attrName = attrs.item(i).getNodeName(); 1147 if (attrName.equalsIgnoreCase("content")) { 1148 attrContentValue = attrs.item(i).getNodeValue(); 1149 } 1150 if (attrName.equalsIgnoreCase("replace")) { 1151 attrReplaceValue = attrs.item(i).getNodeValue(); 1152 } 1153 } 1154 if (attrContentValue.equals("")) { 1155 System.err.println("Fatal configuration error: Empty content in block <replacestrings>."); 1156 System.exit(1); 1157 } 1158 added = 1159 m_tempReplaceStrings.add( 1160 new CmsHtmlConverterObjectReplaceStrings( 1161 attrContentValue, 1162 "", 1163 attrReplaceValue, 1164 "")); 1165 if (!added) { 1166 System.err.println("Configuration error: Failed adding object to ArrayList m_tempReplaceStrings."); 1167 } 1168 break; 1169 default: 1170 System.err.println("Fatal configuration error: Tag <"+node.getNodeName() 1171 +"> not recognized in block <replacestrings>."); 1172 System.exit(1); 1173 } 1174 } 1175 1176 1182 private String parseTag(Node node) { 1183 if (node.getNodeName().equals("#comment")) { 1184 return ""; 1185 } 1186 NamedNodeMap attrs = node.getAttributes(); 1187 if (attrs.getLength()<1) { 1188 System.err.println("Fatal configuration error: Tag <"+node.getNodeName() 1189 +"> has no attributes."); 1190 System.exit(1); 1191 } 1192 String name=attrs.item(0).getNodeName(); 1193 if (name.equalsIgnoreCase("name")) { 1194 return attrs.item(0).getNodeValue(); 1195 } 1196 System.err.println("Fatal configuration error: Attribute \""+name 1197 +"\" not recognized in tag <"+node.getNodeName()+">."); 1198 System.exit(1); 1199 return ""; 1200 } 1201 1202 1209 private boolean checkBoolean(Node node, int number) { 1210 NamedNodeMap attrs = node.getAttributes(); 1211 if (attrs.item(number).getNodeValue().equalsIgnoreCase(CmsStringUtil.TRUE)) { 1212 return true; 1213 } else if (attrs.item(number).getNodeValue().equalsIgnoreCase(CmsStringUtil.FALSE)) { 1214 return false; 1215 } 1216 System.err.println("Fatal configuration error: Attribute value \"" 1217 +attrs.item(number).getNodeValue()+"\" not recognized in tag <"+node.getNodeName()+">."); 1218 System.exit(1); 1219 return false; 1220 } 1221 1222 1228 private int getConfigNodeName(String name) { 1229 if (name.equalsIgnoreCase("tag")) { 1230 return C_TAG_NODE; 1231 } 1232 if (name.equalsIgnoreCase("string")) { 1233 return C_STRING_NODE; 1234 } 1235 if (name.equalsIgnoreCase("converterconfig")) { 1236 return C_CONFIG_NODE; 1237 } 1238 if (name.equalsIgnoreCase("replacecontent")) { 1239 return C_REPLACECONTENT_NODE; 1240 } 1241 if (name.equalsIgnoreCase("defaults")) { 1242 return C_DEFAULT_NODE; 1243 } 1244 if (name.equalsIgnoreCase("inlinetags")) { 1245 return C_INLINETAG_NODE; 1246 } 1247 if (name.equalsIgnoreCase("removetags")) { 1248 return C_REMOVETAG_NODE; 1249 } 1250 if (name.equalsIgnoreCase("removeblocks")) { 1251 return C_REMOVEBLOCK_NODE; 1252 } 1253 if (name.equalsIgnoreCase("replacetags")) { 1254 return C_REPLACETAG_NODE; 1255 } 1256 if (name.equalsIgnoreCase("replaceblocks")) { 1257 return C_REPLACEBLOCK_NODE; 1258 } 1259 if (name.equalsIgnoreCase("replacestrings")) { 1260 return C_REPLACESTRING_NODE; 1261 } 1262 if (name.equalsIgnoreCase("xhtmloutput")) { 1263 return C_XHTMLOUTPUT_NODE; 1264 } 1265 if (name.equalsIgnoreCase("globalprefix")) { 1266 return C_GLOBALPREFIX_NODE; 1267 } 1268 if (name.equalsIgnoreCase("globalsuffix")) { 1269 return C_GLOBALSUFFIX_NODE; 1270 } 1271 if (name.equalsIgnoreCase("globaladdeveryline")) { 1272 return C_GLOBALADDEVERYLINE_NODE; 1273 } 1274 if (name.equalsIgnoreCase("usebrackets")) { 1275 return C_USEBRACKETS_NODE; 1276 } 1277 if (name.equalsIgnoreCase("encodequotationmarks")) { 1278 return C_ENCODEQUOTATIONMARKS_NODE; 1279 } 1280 if (name.equalsIgnoreCase("addeveryline")) { 1281 return C_ADDEVERYLINE_NODE; 1282 } 1283 if (name.equalsIgnoreCase("prefix")) { 1284 return C_PREFIX_NODE; 1285 } 1286 if (name.equalsIgnoreCase("suffix")) { 1287 return C_SUFFIX_NODE; 1288 } 1289 if (name.equalsIgnoreCase("#comment")) { 1290 return C_COMMENT_NODE; 1291 } 1292 1293 return -1; 1294 } 1295 1296} | Popular Tags |