1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor;
22
23 import javax.servlet.http.HttpServletRequest;
24
25 import net.fckeditor.handlers.PropertiesLoader;
26 import net.fckeditor.tool.Compatibility;
27 import net.fckeditor.tool.Utils;
28 import net.fckeditor.tool.XHtmlTagTool;
29
30
31
32
33
34
35
36
37
38
39
40
41 public class FCKeditor {
42
43 private FCKeditorConfig fckConfig = new FCKeditorConfig();
44 private String instanceName;
45 private String inputName;
46 private HttpServletRequest request;
47
48
49 private String value = Utils.EMPTY_STRING;
50 private String toolbarSet = PropertiesLoader.getEditorToolbarSet();
51 private String width = PropertiesLoader.getEditorWidth();
52 private String height = PropertiesLoader.getEditorHeight();
53 private String basePath = PropertiesLoader.getEditorBasePath();
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public FCKeditor(HttpServletRequest request, String instanceName,
80 String inputName, String width, String height, String toolbarSet,
81 String value, String basePath) {
82
83 this(request, instanceName, inputName);
84 this.width = width;
85 this.height = height;
86 this.toolbarSet = toolbarSet;
87 this.value = value;
88 this.basePath = basePath;
89
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public FCKeditor(final HttpServletRequest request,
114 final String instanceName, final String width, final String height,
115 final String toolbarSet, final String value, final String basePath) {
116
117 this(request, instanceName, null, width, height, toolbarSet, value,
118 basePath);
119
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134 public FCKeditor(final HttpServletRequest request, final String instanceName) {
135
136 if (request == null)
137 throw new NullPointerException("the request cannot be null");
138 this.request = request;
139
140 setInstanceName(instanceName);
141
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public FCKeditor(HttpServletRequest request, String instanceName,
160 String inputName) {
161 this(request, instanceName);
162 setInputName(inputName);
163 }
164
165
166
167
168
169
170
171
172
173 public void setInstanceName(final String instanceName) {
174 if (Utils.isEmpty(instanceName))
175 throw new IllegalArgumentException("instanceName cannot be empty");
176 if (!instanceName.matches("\\p{Alpha}[\\p{Alnum}:_.-]*"))
177 throw new IllegalArgumentException(
178 "instanceName must be a valid XHTML id containing only \"\\p{Alpha}[\\p{Alnum}:_.-]*\"");
179 this.instanceName = instanceName;
180 }
181
182
183
184
185
186
187
188
189 public void setInputName(final String inputName) {
190 if (Utils.isEmpty(inputName))
191 this.inputName = instanceName;
192 else
193 this.inputName = inputName;
194 }
195
196
197
198
199
200
201
202
203 public void setValue(final String value) {
204 this.value = value;
205 }
206
207
208
209
210
211
212
213
214 public void setBasePath(final String basePath) {
215 this.basePath = basePath;
216 }
217
218
219
220
221
222
223
224 public void setToolbarSet(final String toolbarSet) {
225 this.toolbarSet = toolbarSet;
226 }
227
228
229
230
231
232
233
234
235 public void setWidth(final String width) {
236 this.width = width;
237 }
238
239
240
241
242
243
244
245
246 public void setHeight(final String height) {
247 this.height = height;
248 }
249
250
251
252
253
254
255
256
257
258 public String getConfig(String name) {
259 return fckConfig.get(name);
260 }
261
262
263
264
265
266
267
268
269
270
271 public void setConfig(String name, String value) {
272 if (value != null)
273 fckConfig.put(name, value);
274 }
275
276
277
278
279
280
281
282
283
284 private String escapeXml(String str) {
285
286 if (Utils.isEmpty(str))
287 return str;
288
289 StringBuffer sb = new StringBuffer();
290
291 int len = str.length();
292 char c;
293
294 for (int i = 0; i < len; i++) {
295
296 c = str.charAt(i);
297 switch (c) {
298 case '&':
299 sb.append("&");
300 break;
301 case '<':
302 sb.append("<");
303 break;
304 case '>':
305 sb.append(">");
306 break;
307 case '"':
308 sb.append(""");
309 break;
310
311
312
313 case '\'':
314 sb.append("'");
315 break;
316 default:
317 sb.append(c);
318 break;
319 }
320 }
321
322 return sb.toString();
323 }
324
325
326
327
328
329
330
331
332
333 @Override
334 public String toString() {
335 StringBuffer strEditor = new StringBuffer();
336
337 strEditor.append("<div>");
338 String encodedValue = escapeXml(value);
339
340
341
342
343
344 setInputName(inputName);
345
346 if (Compatibility.isCompatibleBrowser(request)) {
347 strEditor.append(createInputForVariable(instanceName, inputName,
348 encodedValue));
349
350
351 String configStr = fckConfig.getUrlParams();
352 if (Utils.isNotEmpty(configStr))
353 strEditor.append(createInputForVariable(instanceName
354 .concat("___Config"), null, configStr));
355
356
357 StringBuffer editorLink = new StringBuffer(request.getContextPath());
358 editorLink.append(basePath);
359 editorLink.append("/editor/fckeditor.html?InstanceName=").append(
360 instanceName);
361 if (Utils.isNotEmpty(toolbarSet))
362 editorLink.append("&Toolbar=").append(toolbarSet);
363
364 XHtmlTagTool iframeTag = new XHtmlTagTool("iframe",
365 XHtmlTagTool.SPACE);
366 iframeTag.addAttribute("id", instanceName.concat("___Frame"));
367 iframeTag.addAttribute("src", editorLink.toString());
368 iframeTag.addAttribute("width", width);
369 iframeTag.addAttribute("height", height);
370 iframeTag.addAttribute("frameborder", "0");
371 iframeTag.addAttribute("scrolling", "no");
372 strEditor.append(iframeTag);
373
374 } else {
375 XHtmlTagTool textareaTag = new XHtmlTagTool("textarea",
376 encodedValue);
377 textareaTag.addAttribute("name", inputName);
378 textareaTag.addAttribute("rows", "4");
379 textareaTag.addAttribute("cols", "40");
380 textareaTag.addAttribute("wrap", "virtual");
381 textareaTag.addAttribute("style", "width: ".concat(width).concat(
382 "; height: ").concat(height));
383 }
384 strEditor.append("</div>");
385 return strEditor.toString();
386 }
387
388
389
390
391
392
393
394 public String createHtml() {
395 return toString();
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409 private String createInputForVariable(final String id, final String name,
410 final String value) {
411 XHtmlTagTool tag = new XHtmlTagTool("input");
412 tag.addAttribute("id", id);
413 tag.addAttribute("name", name);
414 tag.addAttribute("value", value);
415 tag.addAttribute("type", "hidden");
416 return tag.toString();
417 }
418 }