001: /*
002: * Created on Nov 27, 2003
003: *
004: /*
005: Copyright (c) 2003 eInnovation Inc. All rights reserved
006:
007: This library is free software; you can redistribute it and/or modify it under the terms
008: of the GNU Lesser General Public License as published by the Free Software Foundation;
009: either version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
012: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013: See the GNU Lesser General Public License for more details.
014: */
015: package com.openedit.modules.html;
016:
017: import com.openedit.modules.edit.EditSession;
018: import com.openedit.modules.edit.SpecialCharacter;
019:
020: /**
021: * @author Matt Avery, mavery@einnovation.com
022: */
023: public class EditorSession extends EditSession {
024: public static final String BODYSTART = "<BODY>";
025: public static final String BODYSTART_ALTERNATE = "<body>";
026: public static final String BODYEND = "</BODY>";
027: public static final String BODYEND_ALTERNATE = "</body>";
028: // protected HtmlWysiwygConverter fieldWysiwygConverter;
029: // protected HtmlSourceConverter fieldSourceConverter;
030: protected String fieldBasePath;
031: protected String fieldCssPath;
032: //protected String fieldHighlightCss;
033: protected String fieldOriginalSource;
034: protected String fieldWorkingSource;
035: protected String fieldDefaultCopy = "<p>Your copy here.</p>";
036: protected boolean fieldDocumentModified;
037:
038: public String createVariable(String inCode) {
039: final int MAX_LINE_LENGTH = 300;
040: StringBuffer sb = new StringBuffer();
041: int linecount = 0;
042:
043: char lastC = 0;
044: for (int n = 0; n < inCode.length(); n++) {
045: char c = inCode.charAt(n);
046: linecount++;
047: if (linecount > MAX_LINE_LENGTH) {
048: sb.append("\" +\n\t\"");
049: linecount = 0;
050: }
051:
052: switch (c) {
053: case '\r': //This may not be needed
054: if (linecount < MAX_LINE_LENGTH / 2) //only if it's a short line
055: {
056: sb.append("\\n");
057: } else {
058: sb.append("\\n\" +\n\t\"");
059: linecount = 0;
060: }
061: break;
062: case '\n': {
063: if (lastC == '\r') {
064: break;
065: }
066: if (linecount < MAX_LINE_LENGTH / 2) //only if it's a short line
067: {
068: sb.append("\\n");
069: } else {
070: sb.append("\\n\" +\n\t\"");
071: linecount = 0;
072: }
073: break;
074: }
075: case '\"':
076: sb.append("\\\"");
077: break;
078: case '/':
079: // "//" is interpreted as start of comment even if within string (JavaScript interpreter bug)
080: // Therefore, "//" must be split across two lines
081: if (lastC == '/') {
082: sb.append("\" +\n\t\"");
083: linecount = 0;
084: }
085: sb.append('/');
086: break;
087: case 't':
088: case 'T':
089: // Don't allow the word "script" to appear without splitting it across lines, because of another
090: // bug in the JavaScript interpreter.
091: if (sb.length() > 5
092: && sb.substring(sb.length() - 5)
093: .equalsIgnoreCase("SCRIP")) {
094: sb.append("\" +\n\t\"");
095: linecount = 0;
096: }
097: sb.append(c);
098: break;
099: case '\\':
100: sb.append("\\\\");
101: break;
102: default:
103: sb.append(c);
104: }
105: lastC = c;
106: }
107: return sb.toString();
108: }
109:
110: /* public HtmlWysiwygConverter getWysiwygConverter( WebPageRequest inContext )
111: {
112: if (fieldWysiwygConverter == null)
113: {
114: String userAgent = inContext.getRequest().getHeader( "User-Agent" );
115: if ( userAgent.indexOf("Gecko") > -1 )
116: {
117: fieldWysiwygConverter = new MozillaHtmlWysiwygConverter();
118: }
119: else
120: {
121: if ( userAgent.indexOf( "MSIE 6.0") > -1 )
122: {
123: fieldWysiwygConverter = new IE60HtmlWysiwygConverter();
124: }
125: else
126: {
127: fieldWysiwygConverter = new IE55HtmlWysiwygConverter();
128: }
129: }
130: }
131: return fieldWysiwygConverter;
132: }
133: */
134: /* public String escapeSource(String inContent, WebPageRequest inContext) throws Exception
135: {
136: String sourceContent = getSourceConverter().toDisplayCode(inContent);
137: String finalHtml = "<html><head><base href='" + getBasePath() + "'>";
138: finalHtml += "<style type='text/css'>";
139: finalHtml += getExternalCss();
140: finalHtml += "</style>";
141: finalHtml += "</head>";
142: finalHtml += BODYSTART;
143: finalHtml += URLUtilities.xmlEscape( sourceContent);
144: finalHtml += BODYEND;
145: finalHtml += "</html>";
146:
147: return finalHtml;
148: }
149: */
150: // public String wrapForWysiwyg(String inHtml)
151: // {
152: // if (hasHeader())
153: // {
154: // // Need to insert a "base" tag for images to work.
155: // inHtml = inHtml.replaceFirst("<HEAD>", "<HEAD><base href=\"" + getBasePath() + "\">");
156: // //alert( inHtml );
157: // return inHtml;
158: // }
159: // else
160: // {
161: // //using a link tag breaks gecko
162: // String html = "<html><head><base href='" + getBasePath() + "'>\n";
163: //// html += "<style type='text/css'>\n";
164: //// html += getExternalCss();
165: //// html += "</style>";
166: //
167: // html += "</head>";
168: // html += BODYSTART;
169: // html += inHtml;
170: // html += BODYEND;
171: // html += "</html>";
172: // return html;
173: // }
174: // }
175:
176: public String getWysiwygSource() {
177: //return wrapForWysiwyg( getWorkingSource() );
178: return getWorkingSource();
179: }
180:
181: public String getWysiwygSourceVariable() {
182: return createVariable(getWysiwygSource());
183: }
184:
185: public boolean hasHeader() {
186: String lower = getOriginalSource().toLowerCase();
187: if (lower.indexOf("<html") > -1) {
188: if (lower.indexOf("<body") > -1) {
189: return true;
190: }
191: }
192: return false;
193: }
194:
195: public String getBasePath() {
196: return fieldBasePath;
197: }
198:
199: // public String getExternalCss()
200: // {
201: // StringBuffer out = new StringBuffer();
202: // if ( getFontsCss() != null)
203: // {
204: // out.append( getFontsCss() );
205: // }
206: // if ( getHighlightCss() != null)
207: // {
208: // out.append( "\n");
209: // out.append( getHighlightCss() );
210: // }
211: // return out.toString();
212: // }
213:
214: public void setBasePath(String string) {
215: fieldBasePath = string;
216: }
217:
218: public String getOriginalSource() {
219: if (fieldOriginalSource == null) {
220: fieldOriginalSource = getDefaultCopy();
221: }
222: return fieldOriginalSource;
223: }
224:
225: public void setOriginalSource(String string) {
226: fieldOriginalSource = string;
227: }
228:
229: /* public HtmlSourceConverter getSourceConverter()
230: {
231: if (fieldSourceConverter == null)
232: {
233: fieldSourceConverter = new HtmlSourceConverter();
234: }
235: return fieldSourceConverter;
236: }
237: */
238: public String getWorkingSource() {
239: if (fieldWorkingSource == null) {
240: fieldWorkingSource = getDefaultCopy();
241: }
242: return fieldWorkingSource;
243: }
244:
245: public String getEscapedSource() {
246: return SpecialCharacter
247: .escapeSpecialCharacters(getWorkingSource());
248: }
249:
250: public void setWorkingSource(String inWorkingSource) {
251: fieldWorkingSource = inWorkingSource;
252: }
253:
254: /**
255: * @param inContent
256: * @param inInContext
257: * @return
258: */
259: /* public String tidySource(String inContent, WebPageRequest inInContext)
260: {
261: String content = stripBody(inContent);
262: String inTidySource = getSourceConverter().getTidy().tidySource( content, false );
263: return inTidySource;
264: }
265: */
266: /**
267: * @param inContent
268: * @return
269: */
270: public String stripBody(String inContent) {
271: String content = inContent;
272: int bodyStartIndex = content.indexOf(EditorSession.BODYSTART);
273: if (bodyStartIndex < 0) {
274: bodyStartIndex = content
275: .indexOf(EditorSession.BODYSTART_ALTERNATE);
276: }
277: if (bodyStartIndex >= 0) {
278: content = content.substring(bodyStartIndex
279: + EditorSession.BODYSTART.length());
280: }
281: int bodyEndIndex = content.lastIndexOf(EditorSession.BODYEND);
282: if (bodyEndIndex < 0) {
283: bodyEndIndex = content
284: .indexOf(EditorSession.BODYEND_ALTERNATE);
285: }
286: if (bodyEndIndex >= 0) {
287: content = content.substring(0, bodyEndIndex);
288: }
289:
290: return content;
291: }
292:
293: public boolean isDocumentModified() {
294: return fieldDocumentModified;
295: }
296:
297: public void setDocumentModified(boolean inB) {
298: fieldDocumentModified = inB;
299: }
300:
301: public String getDefaultCopy() {
302: return fieldDefaultCopy;
303: }
304:
305: public void setDefaultCopy(String defaultCopy) {
306: fieldDefaultCopy = defaultCopy;
307: }
308:
309: public String removeBaseHrefAndFixQuotes(String inContent) {
310: //(?s)fun*
311: //String content = inContent.replaceAll("(?s)_base_href=\".*\" ","");
312: if (inContent == null) {
313: return null;
314: }
315: String content = inContent.replaceAll(
316: "_base_href=\"([^\"]+)\"", "");
317:
318: content = content.replaceAll(""", "\"");
319:
320: //removed since most times & is what we want to have
321: //content = content.replaceAll("&","&");
322:
323: //Comments are loosing the \n at the end in IE look for space
324: content = content.replaceAll("--> ", "-->\n");
325: content = content.replace("spellcheck=\"true\"", ""); //FCK editor is leaving this in
326:
327: return content;
328: }
329:
330: public String getCssPath() {
331: return fieldCssPath;
332: }
333:
334: public void setCssPath(String inCssPath) {
335: fieldCssPath = inCssPath;
336: }
337:
338: }
|