1 28 29 30 package com.opencms.template; 31 32 import org.opencms.file.CmsFile; 33 import org.opencms.file.CmsObject; 34 import org.opencms.main.CmsException; 35 import org.opencms.main.CmsLog; 36 import org.opencms.main.OpenCms; 37 import org.opencms.util.CmsStringUtil; 38 39 import com.opencms.core.I_CmsRequest; 40 import com.opencms.legacy.CmsLegacyException; 41 import com.opencms.legacy.CmsXmlTemplateLoader; 42 import com.opencms.template.cache.CmsElementLink; 43 import com.opencms.template.cache.CmsElementVariant; 44 import com.opencms.template.cache.CmsMethodLink; 45 46 import java.io.OutputStream ; 47 import java.io.StringReader ; 48 import java.io.StringWriter ; 49 import java.util.Hashtable ; 50 import java.util.Vector ; 51 52 import org.w3c.dom.Document ; 53 import org.w3c.dom.Element ; 54 import org.w3c.dom.Node ; 55 import org.w3c.dom.NodeList ; 56 57 65 public class CmsXmlTemplateFile extends A_CmsXmlContent { 66 67 68 public static final String C_EDIT_TEMPLATE = "edittemplate"; 69 70 71 public static final String C_TEMPLATE = "template"; 72 73 76 public CmsXmlTemplateFile() throws CmsException { 77 if(CmsXmlTemplateLoader.getOnlineElementCache() == null){ 78 registerTag("ELEMENT", CmsXmlTemplateFile.class, "handleElementTag", C_REGISTER_MAIN_RUN); 79 } 80 } 81 82 89 public CmsXmlTemplateFile(CmsObject cms, CmsFile file) throws CmsException { 90 super(); 91 if(!CmsXmlTemplateLoader.isElementCacheEnabled()) { 92 registerMyTags(); 93 } 94 init(cms, file); 95 } 96 97 104 public CmsXmlTemplateFile(CmsObject cms, String filename) throws CmsException { 105 super(); 106 if(!CmsXmlTemplateLoader.isElementCacheEnabled()) { 107 registerMyTags(); 108 } 109 init(cms, filename); 110 } 111 112 public CmsXmlTemplateFile(CmsObject cms, String filename, String content) throws CmsException { 113 super(); 114 if(!CmsXmlTemplateLoader.isElementCacheEnabled()) { 115 registerMyTags(); 116 } 117 init(cms, filename, content); 118 } 119 120 121 public int createNewSection(String sectionName) { 122 int loop = 2; 123 String tempName = sectionName + loop; 124 while(hasData("template." + tempName)) { 125 tempName = sectionName + (++loop); 126 } 127 Element newData = getXmlDocument().createElement("template"); 128 newData.setAttribute("name", tempName); 129 setData("template." + tempName, newData); 130 Element newEditData = getXmlDocument().createElement(C_EDIT_TEMPLATE); 132 newEditData.setAttribute("name", tempName); 133 setData(C_EDIT_TEMPLATE + "."+ tempName, newEditData); 134 return loop; 135 } 136 public Vector getAllSections() throws CmsException { 137 NodeList nl = getXmlDocument().getDocumentElement().getChildNodes(); 138 return getNamesFromNodeList(nl, "TEMPLATE", true); 139 } 140 141 145 public Vector getAllLinkTagValues()throws CmsException{ 146 Vector retValue = new Vector (); 147 NodeList list = getXmlDocument().getDocumentElement().getChildNodes(); 148 int numElements = list.getLength(); 149 for(int i=0; i < numElements; i++){ 150 Node n = list.item(i); 151 if(n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().toLowerCase().equals(C_TEMPLATE)){ 153 NodeList subList = n.getChildNodes(); 154 for(int j=0; j<subList.getLength(); j++){ 155 Node subNode = subList.item(j); 156 if(subNode.getNodeType()==Node.ELEMENT_NODE && subNode.getNodeName().equalsIgnoreCase("link")){ 157 String value = subNode.getFirstChild().getNodeValue(); 159 if(!retValue.contains(value)){ 160 retValue.add(value); 161 } 162 } 163 } 164 } 165 } 166 167 return retValue; 168 } 169 170 176 public Vector getAllSubElements() throws CmsException { 177 NodeList nl = getXmlDocument().getDocumentElement().getElementsByTagName("*"); 178 return getNamesFromNodeList(nl, "ELEMENT", false); 179 } 180 181 187 public Vector getAllSubElementDefinitions() throws CmsException { 188 NodeList nl = getXmlDocument().getDocumentElement().getElementsByTagName("*"); 189 return getNamesFromNodeList(nl, "ELEMENTDEF", false); 190 } 191 192 199 public Vector getAllSubElements(String selector) throws CmsException { 200 String templateDatablockName = getTemplateDatablockName(selector); 201 Element templateElement = getData(templateDatablockName); 202 NodeList nl = templateElement.getChildNodes(); 203 return getNamesFromNodeList(nl, "ELEMENT", false); 204 } 205 public Element getBodyTag() throws CmsException { 206 Element result = null; 207 if(hasData("bodyTag")) { 208 result = getData("bodytag"); 209 } 210 else { 211 if(CmsLog.getLog(this).isDebugEnabled() ) { 212 CmsLog.getLog(this).debug("Cannot find \"bodytag\" tag in XML template file " + getFilename()); 213 } 214 } 215 return result; 216 } 217 218 222 public String getContentDescription() { 223 return "OpenCms XML template file"; 224 } 225 226 233 public Element getData(String tag) throws CmsException { 234 return super.getData(tag); 235 } 236 237 245 public String getDataValue(String tag) throws CmsException { 246 return super.getDataValue(tag); 247 } 248 249 public String getEditableTemplateContent(Object callingObject, Hashtable parameters, String templateSelector, boolean html, String style) throws CmsException { 250 Vector cdatas = new Vector (); 251 String editDatablockName = this.getEditTemplateDatablockName(templateSelector); 252 String datablockName = null; 253 String testValue = getDataValue(editDatablockName); 254 if(testValue == null || "".equals(testValue)){ 257 datablockName = this.getTemplateDatablockName(templateSelector); 258 }else{ 259 datablockName = editDatablockName; 260 } 261 Element data = getData(datablockName); 262 StringBuffer result = new StringBuffer (); 263 if(style == null) { 264 style = ""; 265 } 266 Document tempDoc = (Document )getXmlDocument().cloneNode(true); 267 Element rootElem = tempDoc.getDocumentElement(); 268 while(rootElem.hasChildNodes()) { 269 rootElem.removeChild(rootElem.getFirstChild()); 270 } 271 data = (Element )getXmlParser().importNode(tempDoc, data); 272 rootElem.appendChild(data); 273 if(html) { 274 Node n = data; 276 while(n != null) { 277 if(n.getNodeType() == Node.CDATA_SECTION_NODE) { 278 cdatas.addElement(n.getNodeValue()); 279 n.setNodeValue(""); 280 } 281 n = treeWalker(rootElem, n); 282 } 283 } 284 StringWriter out = new StringWriter (); 285 getXmlParser().getXmlText(tempDoc, out); 286 String xmlString = out.toString(); 287 int endOpeningXmlTag = xmlString.indexOf(">"); 288 int endOpeningDocTag = xmlString.indexOf(">", endOpeningXmlTag + 1); 289 int endOpeningBodyTag = xmlString.indexOf(">", endOpeningDocTag + 1) + 1; 290 int startClosingDocTag = xmlString.lastIndexOf("<"); 291 int startClosingBodyTag = xmlString.lastIndexOf("<", startClosingDocTag - 1); 292 if(startClosingBodyTag <= endOpeningBodyTag) { 293 xmlString = ""; 294 }else { 295 xmlString = xmlString.substring(endOpeningBodyTag, startClosingBodyTag); 296 xmlString = xmlString.trim(); 297 } 298 if(html) { 299 int cdataStart = xmlString.indexOf("<![CDATA["); 300 int currentPos = 0; 301 int loop = 0; 302 result.append("<HTML>\n<HEAD>\n"); 303 result.append("<link rel=stylesheet type=\"text/css\" HREF=\"" + style + "\">\n"); 304 result.append("</HEAD>\n"); 305 result.append("<BASE HREF=\""); 306 I_CmsRequest req = CmsXmlTemplateLoader.getRequest(m_cms.getRequestContext()); 307 result.append(req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getServletUrl() + (String )parameters.get("file")); 308 result.append("\"></BASE>"); 309 result.append("<BODY " + getProcessedDataValue("bodytag", callingObject, parameters) + ">\n"); 310 while(cdataStart != -1) { 311 String tempString = xmlString.substring(currentPos, cdataStart); 312 tempString = replaceBack(tempString); 313 result.append(tempString); 315 result.append((String )cdatas.elementAt(loop++)); 316 cdataStart = xmlString.indexOf("<![CDATA[", cdataStart + 1); 317 currentPos = xmlString.indexOf("]]>", currentPos + 1) + 3; 318 } 319 String tempString = xmlString.substring(currentPos); 320 tempString = replaceBack(tempString); 321 result.append(tempString); 323 result.append("\n</BODY>\n</HTML>"); 324 xmlString = result.toString(); 325 }else { 326 if(xmlString.trim().equals("")) { 330 xmlString = "<![CDATA[\n]]>"; 331 } 332 } 333 return xmlString; 334 } 335 336 346 private Vector getNamesFromNodeList(NodeList nl, String tag, boolean unnamedAllowed) throws CmsException { 347 int numElements = nl.getLength(); 348 Vector collectNames = new Vector (); 349 for(int i = 0;i < numElements;i++) { 350 Node n = nl.item(i); 351 if(n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().toLowerCase().equals(tag.toLowerCase())) { 352 String name = ((Element )n).getAttribute("name"); 353 if(name == null || "".equals(name)) { 354 if(unnamedAllowed) { 356 name = "(default)"; 357 }else { 358 if(CmsLog.getLog(this).isErrorEnabled() ) { 359 CmsLog.getLog(this).error("Unnamed <" + n.getNodeName() + "> found in OpenCms control file " + getAbsoluteFilename()); 360 } 361 throw new CmsLegacyException("Unnamed \"" + n.getNodeName() + "\" found in OpenCms control file " + getAbsoluteFilename(), CmsLegacyException.C_XML_TAG_MISSING); 362 } 363 } 364 collectNames.addElement(name); 365 } 366 } 367 return collectNames; 368 } 369 370 375 public String getParameter(String elementName, String parameterName) throws CmsException { 376 return getDataValue("ELEMENTDEF." + elementName + ".PARAMETER." + parameterName); 377 } 378 379 385 public Vector getParameterNames(String elementName) throws CmsException { 386 if(hasData("elementdef." + elementName)) { 387 Element elementDefinition = getData("elementdef." + elementName); 388 NodeList parameterTags = elementDefinition.getChildNodes(); 389 return getNamesFromNodeList(parameterTags, "PARAMETER", false); 390 } 391 else { 392 return null; 393 } 394 } 395 396 402 public Hashtable getParameters(String elementName) throws CmsException { 403 Hashtable result = new Hashtable (); 404 if(hasData("elementdef." + elementName)) { 405 Element elementDefinition = getData("elementdef." + elementName); 406 NodeList parameterTags = elementDefinition.getChildNodes(); 407 408 int numElements = parameterTags.getLength(); 409 for(int i = 0;i < numElements;i++) { 410 Node n = parameterTags.item(i); 411 if(n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().toLowerCase().equals("parameter")) { 412 String name = ((Element )n).getAttribute("name"); 413 if(name != null && !"".equals(name)) { 414 result.put(name, getTagValue((Element )n)); 415 } 416 } 417 } 418 } 419 return result; 420 } 421 422 429 public Element getProcessedData(String tag) throws CmsException { 430 return super.getProcessedData(tag); 431 } 432 433 441 public Element getProcessedData(String tag, Object callingObject) throws CmsException { 442 return super.getProcessedData(tag, callingObject); 443 } 444 445 457 public Element getProcessedData(String tag, Object callingObject, Object userObj) throws CmsException { 458 return super.getProcessedData(tag, callingObject, userObj); 459 } 460 461 469 public String getProcessedDataValue(String tag) throws CmsException { 470 return super.getProcessedDataValue(tag); 471 } 472 473 482 public String getProcessedDataValue(String tag, Object callingObject) throws CmsException { 483 return super.getProcessedDataValue(tag, callingObject); 484 } 485 486 499 public String getProcessedDataValue(String tag, Object callingObject, Object userObj) throws CmsException { 500 return super.getProcessedDataValue(tag, callingObject, userObj); 501 } 502 503 518 public String getProcessedDataValue(String tag, Object callingObject, Object userObj, OutputStream stream) throws CmsException { 519 return super.getProcessedDataValue(tag, callingObject, userObj, stream); 520 } 521 522 534 public String getProcessedTemplateContent(Object callingObject, Hashtable parameters) throws CmsException { 535 return getProcessedTemplateContent(callingObject, parameters, null); 536 } 537 538 551 public String getProcessedTemplateContent(Object callingObject, Hashtable parameters, String templateSelector) throws CmsException { 552 OutputStream os = null; 553 554 String datablockName = this.getTemplateDatablockName(templateSelector); 555 if(datablockName == null && (templateSelector.toLowerCase().equals("script"))) { 556 return ""; 557 } 558 559 if (CmsLog.getLog(this).isDebugEnabled()) { 560 CmsLog.getLog(this).debug("TemplateSelector is " + templateSelector); 561 } 562 return getProcessedDataValue(datablockName, callingObject, parameters, os); 563 } 564 565 580 public CmsElementVariant generateElementCacheVariant(Object callingObject, Hashtable parameters, String elementName, String templateSelector) throws CmsException { 581 CmsElementVariant result = new CmsElementVariant(); 582 583 String datablockName = this.getTemplateDatablockName(templateSelector); 584 if(datablockName == null && (templateSelector.toLowerCase().equals("script"))) { 585 return result; 586 } 587 588 Element domEl = getProcessedData(datablockName, callingObject, parameters, null); 589 StringBuffer buf = new StringBuffer (); 590 for(Node n = domEl.getFirstChild(); n != null; n = treeWalker(domEl, n)) { 591 if(n.getNodeType() == Node.ELEMENT_NODE && "element".equalsIgnoreCase(n.getNodeName())) { 592 String elName = ((Element )n).getAttribute("name"); 594 595 if(elName != null && !"".equalsIgnoreCase(elName)) { 596 if(buf.length() > 0) { 598 result.add(buf.toString()); 599 buf = new StringBuffer (); 600 601 } 602 603 CmsElementLink link = new CmsElementLink(elName); 605 result.add(link); 606 } 607 } else if (n.getNodeType() == Node.ELEMENT_NODE && "method".equalsIgnoreCase(n.getNodeName())) { 608 String methodName = ((Element )n).getAttribute("name"); 610 String tagcontent = getTagValue((Element )n); 611 if(methodName != null && !"".equals(methodName)){ 612 if(buf.length() > 0) { 614 result.add(buf.toString()); 615 buf = new StringBuffer (); 616 } 617 CmsMethodLink methodLink = new CmsMethodLink(methodName, tagcontent); 619 result.add(methodLink); 620 if(tagcontent != null && !"".equals(tagcontent)){ 622 n = treeWalker(domEl, n); 623 } 624 } 625 } else if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) { 626 buf.append(n.getNodeValue()); 627 } 628 } 629 630 if(buf.length() > 0) { 632 result.add(buf.toString()); 633 } 634 return result; 635 } 636 637 public String getSectionTitle(String sectionName) throws CmsException { 638 String datablockName = getTemplateDatablockName(sectionName); 639 String result = null; 640 try { 641 Element data = getData(datablockName); 642 result = data.getAttribute("title"); 643 } 644 catch(Exception e) { 645 646 result = ""; 648 } 649 return result; 650 } 651 652 657 public String getSubtemplateClass(String name) throws CmsException { 658 String className = getDataValue("ELEMENTDEF." + name + ".CLASS"); 659 return className; 660 } 661 662 667 public String getSubtemplateFilename(String name) throws CmsException { 668 String className = getDataValue("ELEMENTDEF." + name + ".TEMPLATE"); 669 return className; 670 } 671 672 677 public String getSubtemplateSelector(String name) throws CmsException { 678 String templateSelector = getDataValue("ELEMENTDEF." + name + ".TEMPLATESELECTOR"); 679 return templateSelector; 680 } 681 682 695 public String getTemplateContent(Object callingObject, Hashtable parameters, String templateSelector) throws CmsException { 696 String datablockName = this.getTemplateDatablockName(templateSelector); 697 return getDataValue(datablockName); 698 } 699 700 710 private String getTemplateDatablockName(String templateSelector) throws CmsException { 711 String templateDatablockName = null; 712 if(templateSelector != null && !"".equals(templateSelector)) { 713 if(hasData("template." + templateSelector)) { 714 templateDatablockName = "template." + templateSelector; 715 }else { 716 if(CmsLog.getLog(this).isDebugEnabled() && (!"script".equals(templateSelector))) { 717 CmsLog.getLog(this).debug("Cannot load selected template file section " + templateSelector + " in template file " + getFilename() + ", fallback to default section"); 718 } 719 } 720 } 721 if(templateDatablockName == null && (!"script".equals(templateSelector))) { 722 if(hasData("TEMPLATE")) { 723 templateDatablockName = "TEMPLATE"; 724 }else { 725 if(hasData("TEMPLATE.default")) { 726 templateDatablockName = "TEMPLATE.default"; 727 }else { 728 if(CmsLog.getLog(this).isErrorEnabled() ) { 729 CmsLog.getLog(this).error("Template definition file " + getAbsoluteFilename() + " is corrupt, cannot find default section"); 730 } 731 throw new CmsLegacyException("Corrupt template file " + getAbsoluteFilename() + ", cannot find default section", CmsLegacyException.C_XML_TAG_MISSING); 732 } 733 } 734 } 735 return templateDatablockName; 736 } 737 738 748 private String getEditTemplateDatablockName(String templateSelector) throws CmsException { 749 String templateDatablockName = null; 750 if(templateSelector != null && !"".equals(templateSelector)) { 751 if(hasData(C_EDIT_TEMPLATE + "." + templateSelector)) { 752 templateDatablockName = C_EDIT_TEMPLATE + "." + templateSelector; 753 }else { 754 if(CmsLog.getLog(this).isDebugEnabled() && (!"script".equals(templateSelector))) { 755 CmsLog.getLog(this).debug("Cannot load selected template file section " + templateSelector + " in template file " + getFilename() + ", fallback to default section"); 756 } 757 } 758 } 759 if(templateDatablockName == null && (!"script".equals(templateSelector))) { 760 if(hasData(C_EDIT_TEMPLATE)) { 761 templateDatablockName = C_EDIT_TEMPLATE; 762 }else { 763 if(hasData(C_EDIT_TEMPLATE + ".default")) { 764 templateDatablockName = C_EDIT_TEMPLATE + ".default"; 765 }else{ 766 setData(C_EDIT_TEMPLATE , (String )null); 769 templateDatablockName = C_EDIT_TEMPLATE; 770 } 771 } 772 } 773 return templateDatablockName; 774 } 775 776 780 public String getXmlDocumentTagName() { 781 return "XMLTEMPLATE"; 782 } 783 784 795 public Object handleElementTag(Element n, Object callingObject, Object userObj) throws CmsException { 796 String tagcontent = n.getAttribute("name"); 797 return callUserMethod("templateElement", tagcontent, callingObject, userObj, false); 798 } 799 800 805 public boolean hasData(String key) { 806 return super.hasData(key); 807 } 808 809 815 public boolean hasSection(String name) { 816 return hasData("template." + name); 817 } 818 819 824 public boolean hasSubtemplateClass(String name) throws CmsException { 825 return hasData("ELEMENTDEF." + name + ".CLASS"); 826 } 827 828 833 public boolean hasSubtemplateFilename(String name) throws CmsException { 834 return hasData("ELEMENTDEF." + name + ".TEMPLATE"); 835 } 836 837 843 public boolean hasSubtemplateSelector(String name) throws CmsException { 844 return hasData("ELEMENTDEF." + name + ".TEMPLATESELECTOR"); 845 } 846 private int min(int a, int b) { 847 if(a == -1) { 848 return b; 849 } 850 if(b == -1) { 851 return a; 852 } 853 return a < b ? a : b; 854 } 855 private String replaceBack(String s) { 856 StringBuffer tempContent = new StringBuffer (); 857 858 int index = min(s.indexOf("<"), s.indexOf(">")); 860 index = min(index, s.indexOf("\"")); 861 int lastindex = 0; 862 while(index != -1) { 863 String sub = s.substring(lastindex, index); 864 tempContent.append(sub); 865 if(s.charAt(index) == '>') { 866 867 tempContent.append("]]"); 869 lastindex = index + 1; 870 } 871 else { 872 if(s.charAt(index) == '<') { 873 874 tempContent.append("[["); 876 lastindex = index + 1; 877 } 878 else { 879 tempContent.append("""); 880 lastindex = index + 1; 881 } 882 } 883 884 886 index = min(s.indexOf("<", index + 1), min(s.indexOf(">", index + 1), s.indexOf("\"", index + 1))); 888 } 889 tempContent.append(s.substring(lastindex)); 890 return new String (tempContent); 891 } 892 893 897 private void registerMyTags() { 898 registerTag("ELEMENT", CmsXmlTemplateFile.class, "handleElementTag", C_REGISTER_MAIN_RUN); 899 } 900 901 906 public void removeData(String tag) { 907 super.removeData(tag); 908 } 909 public void renameSection(String oldName, String newName) throws CmsException { 910 if(!hasData("template." + newName)) { 911 if(CmsLog.getLog(this).isInfoEnabled() ) { 912 CmsLog.getLog(this).info("Datablock TEMPLATE." + newName + " not found, creating it"); 913 } 914 Element newData = (Element )getData("template." + oldName).cloneNode(true); 915 newData.setAttribute("name", newName); 916 setData("template." + newName, newData); 917 removeData("template." + oldName); 918 if(hasData(C_EDIT_TEMPLATE +"."+oldName)){ 920 Element newEditData = (Element )getData(C_EDIT_TEMPLATE +"."+oldName).cloneNode(true); 921 newEditData.setAttribute("name", newName); 922 setData(C_EDIT_TEMPLATE +"."+newName, newEditData); 923 removeData(C_EDIT_TEMPLATE +"."+oldName); 924 } 925 }else { 926 throw new CmsLegacyException("Section already exists: " + newName, CmsLegacyException.C_BAD_NAME); 927 } 928 } 929 930 private String replace(String s, String search, String replace) { 931 StringBuffer tempContent = new StringBuffer (); 932 933 int index = min(s.indexOf("[["), s.indexOf("]]")); 935 index = min(index, s.indexOf(""")); 936 int lastindex = 0; 937 while(index != -1) { 938 String sub = s.substring(lastindex, index); 939 tempContent.append(sub); 940 if(s.charAt(index) == ']') { 941 tempContent.append("><![CDATA["); 942 lastindex = index + 2; 943 } 944 else { 945 if(s.charAt(index) == '[') { 946 tempContent.append("]]><"); 947 lastindex = index + 2; 948 } 949 else { 950 tempContent.append("\""); 951 lastindex = index + 6; 952 } 953 } 954 955 index = min(s.indexOf("[[", index + 1), min(s.indexOf("]]", index + 1), s.indexOf(""", index + 1))); 957 } 958 tempContent.append(s.substring(lastindex)); 959 return new String (tempContent); 960 } 961 public void setBodyTag(Element data) throws CmsException { 962 setData("bodytag", data); 963 } 964 965 972 public void setData(String tag, String data) { 973 super.setData(tag, data); 974 } 975 976 982 public void setData(String tag, Element data) { 983 super.setData(tag, data); 984 } 985 986 993 public void setParsedData(String tag, String data) throws CmsException { 994 super.setParsedData(tag, data); 995 } 996 997 public void setEditedTemplateContent(CmsObject cms, String content, String templateSelector, boolean html, String filePath, String relativeRoot) throws CmsException { 998 String editDatablockName = getEditTemplateDatablockName(templateSelector); 1000 String copyOfContent = content; 1001 if(html) { 1002 1003 int startIndex = content.indexOf("<body"); 1004 if (startIndex < 0) startIndex = content.indexOf("<BODY"); 1005 startIndex = content.indexOf(">", startIndex + 1) + 1; 1006 int endIndex = content.lastIndexOf("</body>"); 1007 if (endIndex < 0) endIndex = content.lastIndexOf("</BODY>"); 1008 if(startIndex > 0) { 1009 content = content.substring(startIndex, endIndex); 1010 } 1011 } 1012 1013 content = CmsStringUtil.substituteContextPath(content, OpenCms.getSystemInfo().getOpenCmsContext() + "/"); 1015 1016 StringBuffer tempXmlString = new StringBuffer (); 1017 tempXmlString.append("<?xml version=\"1.0\"?>\n"); 1018 tempXmlString.append("<" + getXmlDocumentTagName() + ">"); 1019 tempXmlString.append("<"+C_EDIT_TEMPLATE +">\n"); 1020 if(html) { 1021 tempXmlString.append("<![CDATA["); 1022 content = replace(content, "[", "]]><"); 1023 tempXmlString.append(content.trim()); 1024 tempXmlString.append("]]>"); 1025 }else { 1026 tempXmlString.append(content); 1027 } 1028 tempXmlString.append("</"+C_EDIT_TEMPLATE +">\n"); 1029 tempXmlString.append("</" + getXmlDocumentTagName() + ">\n"); 1030 I_CmsXmlParser parser = getXmlParser(); 1031 StringReader parserReader = new StringReader (tempXmlString.toString()); 1032 Document tempDoc = null; 1033 try { 1034 tempDoc = parser.parse(parserReader); 1035 }catch(Exception e) { 1036 throwException("PARSING ERROR!", CmsLegacyException.C_XML_PARSING_ERROR); 1037 } 1038 Element templateNode = (Element )tempDoc.getDocumentElement().getFirstChild(); 1039 setData(editDatablockName, templateNode); 1040 1041 String datablockName = this.getTemplateDatablockName(templateSelector); 1043 if(!html){ 1044 copyOfContent = "<HTML><HEAD></HEAD><body>" + copyOfContent.substring(9, copyOfContent.lastIndexOf("]]>")) + "</body></HTML>"; 1046 }else{ 1047 copyOfContent = replace(copyOfContent, "", ""); 1048 } 1049 try{ 1051 copyOfContent = CmsXmlTemplateLinkConverter.convertFromEditor(cms, copyOfContent, filePath, relativeRoot); 1052 }catch(CmsException e){ 1053 throw new CmsLegacyException("["+this.getClass().getName()+"] cant parse the content:", e); 1054 } 1055 int startIndex = copyOfContent.indexOf("<body"); 1056 startIndex = copyOfContent.indexOf(">", startIndex + 1) + 1; 1057 int endIndex = copyOfContent.lastIndexOf("</body>"); 1058 if(startIndex > 0) { 1059 copyOfContent = copyOfContent.substring(startIndex, endIndex); 1060 } 1061 tempXmlString = new StringBuffer (); 1062 tempXmlString.append("<?xml version=\"1.0\"?>\n"); 1063 tempXmlString.append("<" + getXmlDocumentTagName() + ">"); 1064 tempXmlString.append("<template>\n"); 1065 tempXmlString.append("<![CDATA["); 1066 tempXmlString.append(copyOfContent.trim()); 1067 tempXmlString.append("]]>"); 1068 tempXmlString.append("</template>\n"); 1069 tempXmlString.append("</" + getXmlDocumentTagName() + ">\n"); 1070 I_CmsXmlParser parser2 = getXmlParser(); 1071 StringReader parserReader2 = new StringReader (tempXmlString.toString()); 1072 Document tempDoc2 = null; 1073 try { 1074 tempDoc2 = parser2.parse(parserReader2); 1075 } 1076 catch(Exception e) { 1077 throwException("PARSING ERROR!", CmsLegacyException.C_XML_PARSING_ERROR); 1078 } 1079 Element templateNode2 = (Element )tempDoc2.getDocumentElement().getFirstChild(); 1080 setData(datablockName, templateNode2); 1081 1082 } 1083 1084 public void setSectionTitle(String sectionName, String title) throws CmsException { 1085 String datablockName = getTemplateDatablockName(sectionName); 1086 Element data = null; 1087 try { 1088 data = getData(datablockName); 1089 } 1090 catch(Exception e) { 1091 1092 if(CmsLog.getLog(this).isWarnEnabled() ) { 1094 CmsLog.getLog(this).warn("Cannot set title for template section \"" + sectionName + "\" in file " + getAbsoluteFilename() + ", section doesn't exist"); 1095 } 1096 return ; 1097 } 1098 data.setAttribute("title", title); 1099 } 1100} 1101
| Popular Tags
|