1 31 32 package org.opencms.workplace.editors; 33 34 import org.opencms.i18n.CmsEncoder; 35 import org.opencms.jsp.CmsJspActionElement; 36 import org.opencms.main.OpenCms; 37 import org.opencms.util.CmsStringUtil; 38 39 import java.util.ArrayList ; 40 import java.util.List ; 41 import java.util.Vector ; 42 import java.util.regex.Matcher ; 43 import java.util.regex.Pattern ; 44 45 60 public class CmsMSDHtmlEditor extends CmsSimplePageEditor { 61 62 63 public static final String EDITOR_TYPE = "msdhtml"; 64 65 66 public static final String [] SELECTBOX_EDITORVIEWKEYS = { 67 Messages.GUI_EDITOR_MODE_WYSIWYG_0, 68 Messages.GUI_EDITOR_MODE_SOURCE_0}; 69 70 71 public static final String [] SELECTBOX_EDITORVIEWS = {"edithtml", "edit"}; 72 73 74 public static final int[] SELECTBOX_EDITORVIEWS_ALLOWED = {3, 2}; 75 76 77 private static final Pattern REGEX_LINKS = Pattern.compile( 78 "<(img|a)(\\s+)(.*?)(src|href)=(\"|\')(.*?)(\"|\')(.*?)>", 79 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); 80 81 86 public CmsMSDHtmlEditor(CmsJspActionElement jsp) { 87 88 super(jsp); 89 } 90 91 97 public final String buildSelectViews(String attributes) { 98 99 Vector names = new Vector (); 100 Vector values = new Vector (); 101 String [] contents = SELECTBOX_EDITORVIEWS; 103 for (int i = 0; i < contents.length; i++) { 104 String value = contents[i]; 105 values.addElement(value); 106 String s = keyDefault(SELECTBOX_EDITORVIEWKEYS[i], value); 107 names.addElement(s); 108 } 109 int browserId = 0; 110 int loop = 1; 111 int allowedEditors = SELECTBOX_EDITORVIEWS_ALLOWED[browserId]; 112 if ("script".equals(getParamElementname())) { 113 allowedEditors = allowedEditors & 510; 114 } 115 List namesFinal = new ArrayList (names.size()); 116 List valuesFinal = new ArrayList (values.size()); 117 for (int i = 0; i < names.size(); i++) { 118 if ((allowedEditors & loop) > 0) { 119 valuesFinal.add(values.elementAt(i)); 120 namesFinal.add(names.elementAt(i)); 121 } 122 loop <<= 1; 123 } 124 int currentIndex = valuesFinal.indexOf(getParamEditormode()); 125 return buildSelect(attributes, namesFinal, valuesFinal, currentIndex, false); 126 } 127 128 131 public void escapeParams() { 132 133 String result = CmsStringUtil.substitute(getParamContent(), "\r\n", "\n"); 141 result = CmsStringUtil.substitute(result, "=\n\"", "=\""); 142 result = CmsEncoder.escapeWBlanks(result, CmsEncoder.ENCODING_UTF_8); 144 setParamContent(result); 145 } 146 147 150 public final String getEditorResourceUri() { 151 152 return getSkinUri() + "editors/" + EDITOR_TYPE + "/"; 153 } 154 155 161 protected String prepareContent(boolean save) { 162 163 String content = getParamContent(); 164 content = CmsStringUtil.extractHtmlBody(content); 166 content = filterAnchors(content); 168 169 content = CmsEncoder.adjustHtmlEncoding(content, getFileEncoding()); 171 172 if (!("edit".equals(getParamEditormode()) || save)) { 173 String stylesheet = getUriStyleSheet(); 175 176 String server = getJsp().getRequest().getScheme() 178 + "://" 179 + getJsp().getRequest().getServerName() 180 + ":" 181 + getJsp().getRequest().getServerPort(); 182 StringBuffer result = new StringBuffer (content.length() + 1024); 183 result.append("<html><head>"); 184 if (!"".equals(stylesheet)) { 185 stylesheet = getJsp().link(stylesheet); 186 result.append("<link HREF=\""); 187 result.append(server); 188 result.append(stylesheet); 189 result.append("\" rel=\"stylesheet\" type=\"text/css\">"); 190 } 191 result.append("<base HREF=\""); 192 result.append(server); 193 result.append(OpenCms.getSystemInfo().getOpenCmsContext()); 194 result.append("\"></base></head><body>"); 195 result.append(content); 196 result.append("</body></html>"); 197 content = result.toString(); 198 } 199 if (!save) { 200 setParamContent(content); 202 } 203 return content.trim(); 204 } 205 206 214 private String filterAnchors(String content) { 215 216 String anchor = null; 217 String newAnchor = null; 218 219 Matcher matcher = REGEX_LINKS.matcher(content); 221 while (matcher.find()) { 222 anchor = matcher.group(6); 223 newAnchor = CmsStringUtil.substitute(anchor, "&", "&"); 224 if (anchor.length() != newAnchor.length()) { 225 content = CmsStringUtil.substitute(content, anchor, newAnchor); 227 } 228 } 229 return content; 230 } 231 232 } 233 | Popular Tags |