1 31 32 package com.opencms.template; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.main.CmsException; 36 import org.opencms.main.OpenCms; 37 38 import com.opencms.htmlconverter.CmsHtmlConverter; 39 import com.opencms.legacy.CmsLegacyException; 40 import com.opencms.legacy.CmsXmlTemplateLoader; 41 42 import java.net.URL ; 43 44 import javax.servlet.http.HttpServletRequest ; 45 46 57 public final class CmsXmlTemplateLinkConverter { 58 59 60 private static final String m_converterConfiguration = 61 "<?xml version=\"1.0\"?>" 62 + "<converterconfig>" 63 + " <defaults>" 64 + " <xhtmloutput value=\"false\"/>" 65 + " <globalprefix value=\"\"/>" 66 + " <globalsuffix value=\"\"/>" 67 + " <globaladdeveryline value=\"false\"/>" 68 + " <usebrackets value=\"true\" openbracket=\"#(\" closebracket=\")#\"/>" 69 + " <encodequotationmarks value=\"false\"/>" 70 + " </defaults>" 71 + " <replacecontent>" 72 + " <string content=\"&mdash;\" replace=\"$mdash$\"/>" 73 + " <string content=\"&ndash;\" replace=\"$ndash$\"/>" 74 + " <string content=\"&bull;\" replace=\"$bull$\"/>" 75 + " <string content=\"&#\" replace=\"$Sonder$\"/>" 76 + " </replacecontent>" 77 + " <replacestrings usedefaults=\"true\">" 78 + " <string content=\"$Sonder$\" replace=\"&#\"/>" 79 + " <string content=\"$mdash$\" replace=\"&mdash;\"/>" 80 + " <string content=\"$ndash$\" replace=\"&ndash;\"/>" 81 + " <string content=\"$bull$\" replace=\"&bull;\"/>" 82 + " </replacestrings>" 83 + " <inlinetags>" 84 + " <tag name=\"img\"/>" 85 + " <tag name=\"br\"/>" 86 + " <tag name=\"hr\"/>" 87 + " <tag name=\"input\"/>" 88 + " <tag name=\"frame\"/>" 89 + " <tag name=\"meta\"/>" 90 + " </inlinetags>" 91 + " <replacetags usedefaults=\"true\">" 92 + " <tag name=\"img\" attrib=\"src\" replacestarttag=\"]]><LINK><![CDATA[$parameter$]]></LINK><![CDATA[\" parameter=\"src\" replaceparamattr=\"true\"/>" 93 + " <tag name=\"a\" attrib=\"href\" replacestarttag=\"]]><LINK><![CDATA[$parameter$]]></LINK><![CDATA[\" replaceendtag=\"</a>\" parameter=\"href\" replaceparamattr=\"true\"/>" 94 + " </replacetags>" 95 + "</converterconfig>"; 96 97 100 private CmsXmlTemplateLinkConverter() { 101 } 103 104 119 public static String convertFromImport(String body, String webappUrl, String fileName) throws CmsException { 120 body = "<html><head></head><body>" + body + "</body></html>"; 122 CmsHtmlConverter converter = new CmsHtmlConverter(); 123 try { 124 if (converter.hasErrors(body)) { 126 String errors = converter.showErrors(body); 127 throw new CmsLegacyException(errors); 128 } 129 converter.setConverterConfString(m_converterConfiguration); 131 URL url = new URL (webappUrl + fileName); 132 converter.setServletPrefix(OpenCms.getSystemInfo().getOpenCmsContext(), null); 133 converter.setOriginalUrl(url); 134 body = converter.convertHTML(body); 136 } catch (Exception e) { 137 e.printStackTrace(System.err); 138 throw new CmsLegacyException("[LinkSubstitution] can't convert the editor content:" + e.toString()); 139 } 140 int startIndex = body.indexOf("<body"); 142 startIndex = body.indexOf(">", startIndex + 1) + 1; 143 int endIndex = body.lastIndexOf("</body>"); 144 if (startIndex > 0) { 145 body = body.substring(startIndex, endIndex); 146 } 147 return body; 148 } 149 150 166 public static String convertFromEditor(CmsObject cms, String content, String path, String relativeRoot) throws CmsException { 167 CmsHtmlConverter converter = new CmsHtmlConverter(); 168 String retValue = null; 169 if (path == null || "".equals(path)) { 170 path = "/"; 171 } 172 try { 173 if (converter.hasErrors(content)) { 174 String errors = converter.showErrors(content); 175 throw new CmsLegacyException(errors); 176 } 177 converter.setConverterConfString(m_converterConfiguration); 178 String servletPrefix = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getServletUrl(); 180 String prot = ((HttpServletRequest )CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()).getScheme(); 181 String host = ((HttpServletRequest )CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()).getServerName(); 182 int port = ((HttpServletRequest )CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()).getServerPort(); 183 URL urltool = new URL (prot, host, port, servletPrefix + path); 184 converter.setServletPrefix(servletPrefix, relativeRoot); 185 converter.setOriginalUrl(urltool); 186 retValue = converter.convertHTML(content); 187 } catch (Exception e) { 188 throw new CmsLegacyException("[LinkSubstitution] can't convert the editor content:" + e.toString()); 189 } 190 return retValue; 191 } 192 }
| Popular Tags
|