1 | /* |
---|
2 | * FCKeditor - The text editor for internet |
---|
3 | * Copyright (C) 2003-2005 Frederico Caldeira Knabben |
---|
4 | * |
---|
5 | * Licensed under the terms of the GNU Lesser General Public License: |
---|
6 | * http://www.opensource.org/licenses/lgpl-license.php |
---|
7 | * |
---|
8 | * For further information visit: |
---|
9 | * http://www.fckeditor.net/ |
---|
10 | * |
---|
11 | * File Name: FCKeditor.java |
---|
12 | * FCKeditor control class. |
---|
13 | * |
---|
14 | * Version: 2.3 |
---|
15 | * Modified: 2005-08-11 16:29:00 |
---|
16 | * |
---|
17 | * File Authors: |
---|
18 | * Simone Chiaretta (simo@users.sourceforge.net) |
---|
19 | * |
---|
20 | * Version: 2.5 |
---|
21 | * Modified: 2007-12-27 23:04:00 |
---|
22 | * |
---|
23 | * File Authors: |
---|
24 | * Axel Muench, Specto Design, Inc. |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | package com.fredck.FCKeditor; |
---|
29 | |
---|
30 | |
---|
31 | import javax.servlet.http.HttpServletRequest; |
---|
32 | import java.util.regex.Matcher; |
---|
33 | import java.util.regex.Pattern; |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * The main class of the class lib.<br> |
---|
38 | * <p/> |
---|
39 | * It's the container for all properties and the class that generate the output based on browser capabilities and configurations passed by the developer. |
---|
40 | * |
---|
41 | * @author Simone Chiaretta (simo@users.sourceforge.net) |
---|
42 | */ |
---|
43 | |
---|
44 | public class FCKeditor { |
---|
45 | |
---|
46 | |
---|
47 | private FCKeditorConfigurations oConfig; |
---|
48 | |
---|
49 | private String instanceName; |
---|
50 | |
---|
51 | private String value = ""; |
---|
52 | |
---|
53 | private String basePath; |
---|
54 | |
---|
55 | private String toolbarSet = "Default"; |
---|
56 | |
---|
57 | private String width = "100%"; |
---|
58 | |
---|
59 | private String height = "200"; |
---|
60 | |
---|
61 | |
---|
62 | HttpServletRequest request; |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | /** |
---|
67 | * Get the unique name of the editor |
---|
68 | * |
---|
69 | * @return name |
---|
70 | */ |
---|
71 | |
---|
72 | public String getInstanceName() { |
---|
73 | |
---|
74 | return instanceName; |
---|
75 | |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * Set the unique name of the editor |
---|
82 | * |
---|
83 | * @param value name |
---|
84 | */ |
---|
85 | |
---|
86 | public void setInstanceName(String value) { |
---|
87 | |
---|
88 | instanceName = value; |
---|
89 | |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | /** |
---|
95 | * Get the initial value to be edited.<br> |
---|
96 | * <p/> |
---|
97 | * In HTML code |
---|
98 | * |
---|
99 | * @return value |
---|
100 | */ |
---|
101 | |
---|
102 | public String getValue() { |
---|
103 | |
---|
104 | return value; |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * Set the initial value to be edited.<br> |
---|
112 | * <p/> |
---|
113 | * In HTML code |
---|
114 | * |
---|
115 | * @param value value |
---|
116 | */ |
---|
117 | |
---|
118 | public void setValue(String value) { |
---|
119 | |
---|
120 | this.value = value; |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | /** |
---|
127 | * Get the dir where the FCKeditor files reside on the server |
---|
128 | * |
---|
129 | * @return path |
---|
130 | */ |
---|
131 | |
---|
132 | public String getBasePath() { |
---|
133 | |
---|
134 | return basePath; |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | /** |
---|
141 | * Set the dir where the FCKeditor files reside on the server.<br> |
---|
142 | * <p/> |
---|
143 | * <b>Remarks</b>:<br> |
---|
144 | * <p/> |
---|
145 | * Avoid using relative paths. It is preferable to set the base path starting from the root (/).<br> |
---|
146 | * <p/> |
---|
147 | * Always finish the path with a slash (/). |
---|
148 | * |
---|
149 | * @param value path |
---|
150 | */ |
---|
151 | |
---|
152 | public void setBasePath(String value) { |
---|
153 | |
---|
154 | basePath = value; |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * Get the name of the toolbar to display |
---|
162 | * |
---|
163 | * @return toolbar name |
---|
164 | */ |
---|
165 | |
---|
166 | public String getToolbarSet() { |
---|
167 | |
---|
168 | return toolbarSet; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | /** |
---|
175 | * Set the name of the toolbar to display |
---|
176 | * |
---|
177 | * @param value toolbar name |
---|
178 | */ |
---|
179 | |
---|
180 | public void setToolbarSet(String value) { |
---|
181 | |
---|
182 | toolbarSet = value; |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | /** |
---|
189 | * Get the width of the textarea |
---|
190 | * |
---|
191 | * @return width |
---|
192 | */ |
---|
193 | |
---|
194 | public String getWidth() { |
---|
195 | |
---|
196 | return width; |
---|
197 | |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | /** |
---|
203 | * Set the width of the textarea |
---|
204 | * |
---|
205 | * @param value width |
---|
206 | */ |
---|
207 | |
---|
208 | public void setWidth(String value) { |
---|
209 | |
---|
210 | width = value; |
---|
211 | |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | /** |
---|
217 | * Get the height of the textarea |
---|
218 | * |
---|
219 | * @return height |
---|
220 | */ |
---|
221 | |
---|
222 | public String getHeight() { |
---|
223 | |
---|
224 | return height; |
---|
225 | |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | /** |
---|
231 | * Set the height of the textarea |
---|
232 | * |
---|
233 | * @param value height |
---|
234 | */ |
---|
235 | |
---|
236 | public void setHeight(String value) { |
---|
237 | |
---|
238 | height = value; |
---|
239 | |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | /** |
---|
245 | * Get the advanced configuation set.<br> |
---|
246 | * <p/> |
---|
247 | * Adding element to this collection you can override the settings specified in the config.js file. |
---|
248 | * |
---|
249 | * @return configuration collection |
---|
250 | */ |
---|
251 | |
---|
252 | public FCKeditorConfigurations getConfig() { |
---|
253 | |
---|
254 | return oConfig; |
---|
255 | |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | |
---|
260 | /** |
---|
261 | * Set the advanced configuation set. |
---|
262 | * |
---|
263 | * @param value configuration collection |
---|
264 | */ |
---|
265 | |
---|
266 | public void setConfig(FCKeditorConfigurations value) { |
---|
267 | |
---|
268 | oConfig = value; |
---|
269 | |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | |
---|
274 | /** |
---|
275 | * Initialize the object setting all value to the default ones. |
---|
276 | * <p/> |
---|
277 | * <p> |
---|
278 | * <p/> |
---|
279 | * <ul> |
---|
280 | * <p/> |
---|
281 | * <li>width: 100%</li> |
---|
282 | * <p/> |
---|
283 | * <li>height: 200</li> |
---|
284 | * <p/> |
---|
285 | * <li>toolbar name: Default</li> |
---|
286 | * <p/> |
---|
287 | * <li>basePath: context root + "/FCKeditor/"</li> |
---|
288 | * <p/> |
---|
289 | * </ul> |
---|
290 | * <p/> |
---|
291 | * </p> |
---|
292 | * |
---|
293 | * @param req request object |
---|
294 | */ |
---|
295 | |
---|
296 | public FCKeditor(HttpServletRequest req) { |
---|
297 | |
---|
298 | request = req; |
---|
299 | |
---|
300 | basePath = request.getContextPath() + "/FCKeditor/"; |
---|
301 | |
---|
302 | oConfig = new FCKeditorConfigurations(); |
---|
303 | |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | /** |
---|
309 | * Initialize the object setting the unique name and then all value to the default ones. |
---|
310 | * <p/> |
---|
311 | * <p> |
---|
312 | * <p/> |
---|
313 | * <ul> |
---|
314 | * <p/> |
---|
315 | * <li>width: 100%</li> |
---|
316 | * <p/> |
---|
317 | * <li>height: 200</li> |
---|
318 | * <p/> |
---|
319 | * <li>toolbar name: Default</li> |
---|
320 | * <p/> |
---|
321 | * <li>basePath: context root + "/FCKeditor/"</li> |
---|
322 | * <p/> |
---|
323 | * </ul> |
---|
324 | * <p/> |
---|
325 | * </p> |
---|
326 | * |
---|
327 | * @param req request object |
---|
328 | * @param parInstanceName unique name |
---|
329 | */ |
---|
330 | |
---|
331 | public FCKeditor(HttpServletRequest req, String parInstanceName) { |
---|
332 | |
---|
333 | request = req; |
---|
334 | |
---|
335 | basePath = request.getContextPath() + "/FCKeditor/"; |
---|
336 | |
---|
337 | instanceName = parInstanceName; |
---|
338 | |
---|
339 | oConfig = new FCKeditorConfigurations(); |
---|
340 | |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | |
---|
345 | /** |
---|
346 | * Initialize the object setting all basic configurations.<br> |
---|
347 | * <p/> |
---|
348 | * <p/> |
---|
349 | * <p/> |
---|
350 | * The basePath is context root + "/FCKeditor/" |
---|
351 | * |
---|
352 | * @param req request object |
---|
353 | * @param parInstanceName unique name |
---|
354 | * @param parWidth width |
---|
355 | * @param parHeight height |
---|
356 | * @param parToolbarSet toolbarSet name |
---|
357 | * @param parValue initial value |
---|
358 | */ |
---|
359 | |
---|
360 | public FCKeditor(HttpServletRequest req, String parInstanceName, String parWidth, String parHeight, String parToolbarSet, String parValue) { |
---|
361 | |
---|
362 | request = req; |
---|
363 | |
---|
364 | basePath = request.getContextPath() + "/FCKeditor/"; |
---|
365 | |
---|
366 | instanceName = parInstanceName; |
---|
367 | |
---|
368 | width = parWidth; |
---|
369 | |
---|
370 | height = parHeight; |
---|
371 | |
---|
372 | toolbarSet = parToolbarSet; |
---|
373 | |
---|
374 | value = parValue; |
---|
375 | |
---|
376 | oConfig = new FCKeditorConfigurations(); |
---|
377 | |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | |
---|
382 | private boolean isCompatible() { |
---|
383 | |
---|
384 | String userAgent = request.getHeader("user-agent"); |
---|
385 | |
---|
386 | if (userAgent == null) { |
---|
387 | return false; |
---|
388 | } else { |
---|
389 | userAgent = userAgent.toLowerCase(); |
---|
390 | } |
---|
391 | |
---|
392 | if ((userAgent.indexOf("msie") != -1) && (userAgent.indexOf("mac") == -1) && (userAgent.indexOf("opera") == -1)) { |
---|
393 | |
---|
394 | return (retrieveBrowserVersion(userAgent) >= 5.5); |
---|
395 | |
---|
396 | |
---|
397 | } else if (userAgent.indexOf("gecko") != -1 && userAgent.indexOf("applewebkit") < 0) { |
---|
398 | |
---|
399 | return (retrieveBrowserVersion(userAgent) >= 20030210); |
---|
400 | |
---|
401 | |
---|
402 | } else if (userAgent.indexOf("applewebkit") != -1) { |
---|
403 | |
---|
404 | return (retrieveBrowserVersion(userAgent) >= 522); |
---|
405 | |
---|
406 | |
---|
407 | } else if (userAgent.indexOf("opera") != -1) { |
---|
408 | |
---|
409 | return (retrieveBrowserVersion(userAgent) >= 9.5); |
---|
410 | |
---|
411 | } |
---|
412 | |
---|
413 | return false; |
---|
414 | |
---|
415 | } |
---|
416 | |
---|
417 | |
---|
418 | |
---|
419 | private double retrieveBrowserVersion(String userAgent) { |
---|
420 | |
---|
421 | if (userAgent.indexOf("msie") > -1) { |
---|
422 | |
---|
423 | String str = userAgent.substring(userAgent.indexOf("msie") + 5); |
---|
424 | |
---|
425 | try { |
---|
426 | return Double.parseDouble(str.substring(0, str.indexOf(";"))); |
---|
427 | } catch (NumberFormatException e) { |
---|
428 | return 0; |
---|
429 | } |
---|
430 | |
---|
431 | |
---|
432 | } else if (userAgent.indexOf("gecko") != -1 && userAgent.indexOf("applewebkit") < 0) { |
---|
433 | |
---|
434 | String str = userAgent.substring(userAgent.indexOf("gecko") + 6); |
---|
435 | |
---|
436 | try { |
---|
437 | return Double.parseDouble(str.substring(0, 8)); |
---|
438 | } catch (NumberFormatException e) { |
---|
439 | return 0; |
---|
440 | } |
---|
441 | |
---|
442 | |
---|
443 | } else if (userAgent.indexOf("applewebkit") != -1) { |
---|
444 | |
---|
445 | Matcher safari = Pattern.compile("applewebkit/" + "(( [\\d]* )" + "(?:" + "\\." + " [\\d]* )?)", Pattern.COMMENTS).matcher(userAgent); |
---|
446 | safari.find(); |
---|
447 | int majorVersion = 0; |
---|
448 | try { |
---|
449 | majorVersion = Integer.parseInt(safari.group(2)); |
---|
450 | } |
---|
451 | catch (NumberFormatException nfe) { |
---|
452 | // ignored |
---|
453 | } |
---|
454 | return majorVersion; |
---|
455 | |
---|
456 | |
---|
457 | } else if (userAgent.indexOf("opera") != -1) { |
---|
458 | |
---|
459 | String str = userAgent.substring(userAgent.indexOf("opera") + 6); |
---|
460 | |
---|
461 | try { |
---|
462 | return Double.parseDouble(str.substring(0, 4)); |
---|
463 | } catch (NumberFormatException e) { |
---|
464 | return 0; |
---|
465 | } |
---|
466 | |
---|
467 | |
---|
468 | } else { |
---|
469 | |
---|
470 | return 0; |
---|
471 | } |
---|
472 | |
---|
473 | } |
---|
474 | |
---|
475 | |
---|
476 | |
---|
477 | private String HTMLEncode(String txt) { |
---|
478 | |
---|
479 | txt = txt.replaceAll("&", "&"); |
---|
480 | |
---|
481 | txt = txt.replaceAll("<", "<"); |
---|
482 | |
---|
483 | txt = txt.replaceAll(">", ">"); |
---|
484 | |
---|
485 | txt = txt.replaceAll("\"", """); |
---|
486 | |
---|
487 | txt = txt.replaceAll("'", "’"); |
---|
488 | |
---|
489 | return txt; |
---|
490 | |
---|
491 | } |
---|
492 | |
---|
493 | |
---|
494 | |
---|
495 | /** |
---|
496 | * Generate the HTML Code for the editor. |
---|
497 | * <p/> |
---|
498 | * <br> |
---|
499 | * <p/> |
---|
500 | * Evalute the browser capabilities and generate the editor if IE 5.5 or Gecko 20030210 or greater, |
---|
501 | * <p/> |
---|
502 | * or a simple textarea otherwise. |
---|
503 | * |
---|
504 | * @return html code |
---|
505 | */ |
---|
506 | |
---|
507 | public String createHtml() { |
---|
508 | |
---|
509 | StringBuffer strEditor = new StringBuffer(128); |
---|
510 | |
---|
511 | |
---|
512 | strEditor.append("<div>"); |
---|
513 | |
---|
514 | String encodedValue = HTMLEncode(value); |
---|
515 | |
---|
516 | |
---|
517 | if (isCompatible()) { |
---|
518 | |
---|
519 | strEditor.append("<input type=\"hidden\" id=\"").append(instanceName).append("\" name=\"").append(instanceName).append("\" value=\"").append(encodedValue).append("\">"); |
---|
520 | |
---|
521 | |
---|
522 | strEditor.append(createConfigHTML()); |
---|
523 | |
---|
524 | strEditor.append(createIFrameHTML()); |
---|
525 | |
---|
526 | |
---|
527 | } else { |
---|
528 | |
---|
529 | strEditor.append("<TEXTAREA name=\"").append(instanceName).append("\" rows=\"4\" cols=\"40\" style=\"WIDTH: ").append(width).append("; HEIGHT: ").append(height).append("\" wrap=\"virtual\">").append(encodedValue).append("</TEXTAREA>"); |
---|
530 | |
---|
531 | } |
---|
532 | |
---|
533 | strEditor.append("</div>"); |
---|
534 | |
---|
535 | return strEditor.toString(); |
---|
536 | |
---|
537 | } |
---|
538 | |
---|
539 | |
---|
540 | |
---|
541 | /** |
---|
542 | * @deprecated Use createHtml() instead |
---|
543 | * @return html code |
---|
544 | */ |
---|
545 | |
---|
546 | public String create() { |
---|
547 | |
---|
548 | return createHtml(); |
---|
549 | |
---|
550 | } |
---|
551 | |
---|
552 | |
---|
553 | |
---|
554 | private String createConfigHTML() { |
---|
555 | |
---|
556 | String configStr = oConfig.getUrlParams(); |
---|
557 | |
---|
558 | |
---|
559 | if (!configStr.equals("")) |
---|
560 | |
---|
561 | configStr = configStr.substring(1); |
---|
562 | |
---|
563 | |
---|
564 | return new StringBuffer(128).append("<input type=\"hidden\" id=\"").append(instanceName).append("___Config\" value=\"").append(configStr).append("\">").toString(); |
---|
565 | |
---|
566 | } |
---|
567 | |
---|
568 | |
---|
569 | |
---|
570 | private String createIFrameHTML() { |
---|
571 | |
---|
572 | |
---|
573 | String sLink = basePath + "editor/fckeditor.html?InstanceName=" + instanceName; |
---|
574 | |
---|
575 | |
---|
576 | if (!toolbarSet.equals("")) |
---|
577 | |
---|
578 | sLink += "&Toolbar=" + toolbarSet; |
---|
579 | |
---|
580 | |
---|
581 | return new StringBuffer(128).append("<iframe id=\"").append(instanceName).append("___Frame\" src=\"").append(sLink).append("\" width=\"").append(width).append("\" height=\"").append(height).append("\" frameborder=\"no\" scrolling=\"no\"></iframe>").toString(); |
---|
582 | |
---|
583 | |
---|
584 | } |
---|
585 | |
---|
586 | |
---|
587 | } |
---|
588 | |
---|