KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > html > EditorSession


1 /*
2  * Created on Nov 27, 2003
3  *
4 /*
5 Copyright (c) 2003 eInnovation Inc. All rights reserved
6
7 This library is free software; you can redistribute it and/or modify it under the terms
8 of the GNU Lesser General Public License as published by the Free Software Foundation;
9 either version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU Lesser General Public License for more details.
14 */

15 package com.openedit.modules.html;
16
17 import com.openedit.modules.edit.EditSession;
18 import com.openedit.modules.edit.SpecialCharacter;
19
20 /**
21  * @author Matt Avery, mavery@einnovation.com
22  */

23 public class EditorSession extends EditSession
24 {
25     public static final String JavaDoc BODYSTART = "<BODY>";
26     public static final String JavaDoc BODYSTART_ALTERNATE = "<body>";
27     public static final String JavaDoc BODYEND = "</BODY>";
28     public static final String JavaDoc BODYEND_ALTERNATE = "</body>";
29 // protected HtmlWysiwygConverter fieldWysiwygConverter;
30
// protected HtmlSourceConverter fieldSourceConverter;
31
protected String JavaDoc fieldBasePath;
32     protected String JavaDoc fieldFontsCss;
33     protected String JavaDoc fieldHighlightCss;
34     protected String JavaDoc fieldOriginalSource;
35     protected String JavaDoc fieldWorkingSource;
36     protected String JavaDoc fieldDefaultCopy = "<p>Your copy here.</p>";
37     protected boolean fieldDocumentModified;
38
39
40     public String JavaDoc createVariable(String JavaDoc inCode)
41     {
42         final int MAX_LINE_LENGTH = 128;
43         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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) //only if it's a long enought line
62
{
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                     // "//" is interpreted as start of comment even if within string (JavaScript interpreter bug)
81
// Therefore, "//" must be split across two lines
82
if ( lastC == '/' )
83                     {
84                         sb.append( "\" + \n\t\"" );
85                         linecount = 0;
86                     }
87                     sb.append( '/' );
88                 break;
89             case 't':
90             case 'T':
91                 // Don't allow the word "script" to appear without splitting it across lines, because of another
92
// bug in the JavaScript interpreter.
93
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 /* public HtmlWysiwygConverter getWysiwygConverter( WebPageRequest inContext )
112     {
113         if (fieldWysiwygConverter == null)
114         {
115             String userAgent = inContext.getRequest().getHeader( "User-Agent" );
116             if ( userAgent.indexOf("Gecko") > -1 )
117             {
118                 fieldWysiwygConverter = new MozillaHtmlWysiwygConverter();
119             }
120             else
121             {
122                 if ( userAgent.indexOf( "MSIE 6.0") > -1 )
123                 {
124                     fieldWysiwygConverter = new IE60HtmlWysiwygConverter();
125                 }
126                 else
127                 {
128                     fieldWysiwygConverter = new IE55HtmlWysiwygConverter();
129                 }
130             }
131         }
132         return fieldWysiwygConverter;
133     }
134 */

135 /* public String escapeSource(String inContent, WebPageRequest inContext) throws Exception
136     {
137         String sourceContent = getSourceConverter().toDisplayCode(inContent);
138         String finalHtml = "<html><head><base HREF='" + getBasePath() + "'>";
139         finalHtml += "<style type='text/css'>";
140         finalHtml += getExternalCss();
141         finalHtml += "</style>";
142         finalHtml += "</head>";
143         finalHtml += BODYSTART;
144         finalHtml += URLUtilities.xmlEscape( sourceContent);
145         finalHtml += BODYEND;
146         finalHtml += "</html>";
147
148         return finalHtml;
149     }
150 */

151     public String JavaDoc wrapForWysiwyg(String JavaDoc inHtml)
152     {
153         if (maintainHeader())
154         {
155             // Need to insert a "base" tag for images to work.
156
inHtml = inHtml.replaceFirst("<HEAD>", "<HEAD><base HREF=\"" + getBasePath() + "\">");
157             //alert( inHtml );
158
return inHtml;
159         }
160         else
161         {
162             //using a link tag breaks gecko
163
String JavaDoc 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 JavaDoc getWysiwygSource()
178     {
179         //return wrapForWysiwyg( getWorkingSource() );
180
return getWorkingSource();
181     }
182     
183     public String JavaDoc 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 JavaDoc getBasePath()
199     {
200         return fieldBasePath;
201     }
202
203     public String JavaDoc getExternalCss()
204     {
205         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
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 JavaDoc string)
219     {
220         fieldBasePath = string;
221     }
222
223     public String JavaDoc getOriginalSource()
224     {
225         if ( fieldOriginalSource == null )
226         {
227             fieldOriginalSource = getDefaultCopy();
228         }
229         return fieldOriginalSource;
230     }
231
232     public void setOriginalSource(String JavaDoc string)
233     {
234         fieldOriginalSource = string;
235     }
236     
237 /* public HtmlSourceConverter getSourceConverter()
238     {
239         if (fieldSourceConverter == null)
240         {
241             fieldSourceConverter = new HtmlSourceConverter();
242         }
243         return fieldSourceConverter;
244     }
245 */
public String JavaDoc getFontsCss()
246     {
247         return fieldFontsCss;
248     }
249
250     public String JavaDoc getHighlightCss()
251     {
252         return fieldHighlightCss;
253     }
254
255     public void setFontsCss(String JavaDoc string)
256     {
257         fieldFontsCss = string;
258     }
259
260     public void setHighlightCss(String JavaDoc string)
261     {
262         fieldHighlightCss = string;
263     }
264     
265     public String JavaDoc getWorkingSource()
266     {
267         if ( fieldWorkingSource == null )
268         {
269             fieldWorkingSource = getDefaultCopy();
270         }
271         return fieldWorkingSource;
272     }
273     
274     public String JavaDoc getEscapedSource()
275     {
276         return SpecialCharacter.escapeSpecialCharacters( getWorkingSource() );
277     }
278
279     public void setWorkingSource(String JavaDoc inWorkingSource)
280     {
281         fieldWorkingSource = inWorkingSource;
282     }
283
284     /**
285      * @param inContent
286      * @param inInContext
287      * @return
288      */

289 /* public String tidySource(String inContent, WebPageRequest inInContext)
290     {
291         String content = stripBody(inContent);
292         String inTidySource = getSourceConverter().getTidy().tidySource( content, false );
293         return inTidySource;
294     }
295 */

296     /**
297      * @param inContent
298      * @return
299      */

300     public String JavaDoc stripBody(String JavaDoc inContent)
301     {
302         String JavaDoc 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 JavaDoc getDefaultCopy()
334     {
335         return fieldDefaultCopy;
336     }
337     public void setDefaultCopy( String JavaDoc defaultCopy )
338     {
339         fieldDefaultCopy = defaultCopy;
340     }
341     public String JavaDoc removeBaseHrefAndFixQuotes(String JavaDoc inContent)
342     {
343         //(?s)fun*
344
//String content = inContent.replaceAll("(?s)_base_href=\".*\" ","");
345
if ( inContent == null)
346         {
347             return null;
348         }
349         String JavaDoc content = inContent.replaceAll("_base_href=\"([^\"]+)\"","");
350
351         content = content.replaceAll("&quot;","\"");
352         content = content.replaceAll("&amp;","&");
353         return content;
354     }
355 }
356
Popular Tags