1 15 package com.openedit.modules.html; 16 17 import com.openedit.modules.edit.EditSession; 18 import com.openedit.modules.edit.SpecialCharacter; 19 20 23 public class EditorSession extends EditSession 24 { 25 public static final String BODYSTART = "<BODY>"; 26 public static final String BODYSTART_ALTERNATE = "<body>"; 27 public static final String BODYEND = "</BODY>"; 28 public static final String BODYEND_ALTERNATE = "</body>"; 29 protected String fieldBasePath; 32 protected String fieldFontsCss; 33 protected String fieldHighlightCss; 34 protected String fieldOriginalSource; 35 protected String fieldWorkingSource; 36 protected String fieldDefaultCopy = "<p>Your copy here.</p>"; 37 protected boolean fieldDocumentModified; 38 39 40 public String createVariable(String inCode) 41 { 42 final int MAX_LINE_LENGTH = 128; 43 StringBuffer sb = new StringBuffer (); 44 int linecount = 0; 45 46 char lastC = 0; 47 for ( int n = 0; n < inCode.length(); n++ ) 48 { 49 char c = inCode.charAt( n ); 50 linecount++; 51 if ( linecount > MAX_LINE_LENGTH) 52 { 53 sb.append( "\" + \n\t\"" ); 54 linecount = 0; 55 } 56 57 switch ( c ) 58 { 59 case '\n': 60 { 61 if ( linecount < 64) { 63 sb.append("\\n"); 64 } 65 else 66 { 67 sb.append( "\" + \n\t\"" ); 68 linecount = 0; 69 } 70 break; 71 } 72 case '\r': 73 { 74 break; 75 } 76 case '\"': 77 sb.append( "\\\"" ); 78 break; 79 case '/': 80 if ( lastC == '/' ) 83 { 84 sb.append( "\" + \n\t\"" ); 85 linecount = 0; 86 } 87 sb.append( '/' ); 88 break; 89 case 't': 90 case 'T': 91 if ( sb.length() > 5 && sb.substring(sb.length() - 5).equalsIgnoreCase("SCRIP") ) 94 { 95 sb.append( "\" + \n\t\"" ); 96 linecount = 0; 97 } 98 sb.append( c ); 99 break; 100 case '\\': 101 sb.append( "\\\\"); 102 break; 103 default: 104 sb.append( c ); 105 } 106 lastC = c; 107 } 108 return sb.toString(); 109 } 110 111 135 151 public String wrapForWysiwyg(String inHtml) 152 { 153 if (maintainHeader()) 154 { 155 inHtml = inHtml.replaceFirst("<HEAD>", "<HEAD><base HREF=\"" + getBasePath() + "\">"); 157 return inHtml; 159 } 160 else 161 { 162 String html = "<html><head><base HREF='" + getBasePath() + "'>\n"; 164 html += "<style type='text/css'>\n"; 165 html += getExternalCss(); 166 html += "</style>"; 167 168 html += "</head>"; 169 html += BODYSTART; 170 html += inHtml; 171 html += BODYEND; 172 html += "</html>"; 173 return html; 174 } 175 } 176 177 public String getWysiwygSource() 178 { 179 return getWorkingSource(); 181 } 182 183 public String getWysiwygSourceVariable() 184 { 185 return createVariable( getWysiwygSource() ); 186 } 187 188 protected boolean maintainHeader() 189 { 190 if (getOriginalSource().indexOf("</html>") > -1 191 || getOriginalSource().indexOf("</HTML>") > -1) 192 { 193 return true; 194 } 195 return false; 196 } 197 198 public String getBasePath() 199 { 200 return fieldBasePath; 201 } 202 203 public String getExternalCss() 204 { 205 StringBuffer out = new StringBuffer (); 206 if ( getFontsCss() != null) 207 { 208 out.append( getFontsCss() ); 209 } 210 if ( getHighlightCss() != null) 211 { 212 out.append( "\n"); 213 out.append( getHighlightCss() ); 214 } 215 return out.toString(); 216 } 217 218 public void setBasePath(String string) 219 { 220 fieldBasePath = string; 221 } 222 223 public String getOriginalSource() 224 { 225 if ( fieldOriginalSource == null ) 226 { 227 fieldOriginalSource = getDefaultCopy(); 228 } 229 return fieldOriginalSource; 230 } 231 232 public void setOriginalSource(String string) 233 { 234 fieldOriginalSource = string; 235 } 236 237 public String getFontsCss() 246 { 247 return fieldFontsCss; 248 } 249 250 public String getHighlightCss() 251 { 252 return fieldHighlightCss; 253 } 254 255 public void setFontsCss(String string) 256 { 257 fieldFontsCss = string; 258 } 259 260 public void setHighlightCss(String string) 261 { 262 fieldHighlightCss = string; 263 } 264 265 public String getWorkingSource() 266 { 267 if ( fieldWorkingSource == null ) 268 { 269 fieldWorkingSource = getDefaultCopy(); 270 } 271 return fieldWorkingSource; 272 } 273 274 public String getEscapedSource() 275 { 276 return SpecialCharacter.escapeSpecialCharacters( getWorkingSource() ); 277 } 278 279 public void setWorkingSource(String inWorkingSource) 280 { 281 fieldWorkingSource = inWorkingSource; 282 } 283 284 289 296 300 public String stripBody(String inContent) 301 { 302 String content = inContent; 303 int bodyStartIndex = content.indexOf( EditorSession.BODYSTART ); 304 if ( bodyStartIndex < 0 ) 305 { 306 bodyStartIndex = content.indexOf( EditorSession.BODYSTART_ALTERNATE ); 307 } 308 if ( bodyStartIndex >= 0 ) 309 { 310 content = content.substring( bodyStartIndex + EditorSession.BODYSTART.length() ); 311 } 312 int bodyEndIndex = content.lastIndexOf( EditorSession.BODYEND ); 313 if ( bodyEndIndex < 0 ) 314 { 315 bodyEndIndex = content.indexOf( EditorSession.BODYEND_ALTERNATE ); 316 } 317 if ( bodyEndIndex >= 0 ) 318 { 319 content = content.substring( 0, bodyEndIndex ); 320 } 321 322 return content; 323 } 324 325 public boolean isDocumentModified() { 326 return fieldDocumentModified; 327 } 328 329 public void setDocumentModified(boolean inB) { 330 fieldDocumentModified = inB; 331 } 332 333 public String getDefaultCopy() 334 { 335 return fieldDefaultCopy; 336 } 337 public void setDefaultCopy( String defaultCopy ) 338 { 339 fieldDefaultCopy = defaultCopy; 340 } 341 public String removeBaseHrefAndFixQuotes(String inContent) 342 { 343 if ( inContent == null) 346 { 347 return null; 348 } 349 String content = inContent.replaceAll("_base_href=\"([^\"]+)\"",""); 350 351 content = content.replaceAll(""","\""); 352 content = content.replaceAll("&","&"); 353 return content; 354 } 355 } 356 | Popular Tags |