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 HttpServletRequest request;
46
47
48 private String value = Utils.EMPTY_STRING;
49 private String toolbarSet = PropertiesLoader.getEditorToolbarSet();
50 private String width = PropertiesLoader.getEditorWidth();
51 private String height = PropertiesLoader.getEditorHeight();
52 private String basePath = PropertiesLoader.getEditorBasePath();
53
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(final HttpServletRequest request,
80 final String instanceName, final String width, final String height,
81 final String toolbarSet, final String value, final String basePath) {
82
83 this(request, instanceName);
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 public FCKeditor(final HttpServletRequest request, final String instanceName) {
105
106 if (request == null)
107 throw new NullPointerException("the request cannot be null");
108 this.request = request;
109
110 setInstanceName(instanceName);
111
112 }
113
114
115
116
117
118
119
120
121
122 public void setInstanceName(final String instanceName) {
123 if (Utils.isEmpty(instanceName))
124 throw new IllegalArgumentException("instanceName cannot be empty");
125 if (!instanceName.matches("\\p{Alpha}[\\p{Alnum}:_.-]*"))
126 throw new IllegalArgumentException(
127 "instanceName must be a valid XHTML id containing only \"\\p{Alpha}[\\p{Alnum}:_.-]*\"");
128 this.instanceName = instanceName;
129 }
130
131
132
133
134
135
136
137
138 public void setValue(final String value) {
139 this.value = value;
140 }
141
142
143
144
145
146
147
148
149 public void setBasePath(final String basePath) {
150 this.basePath = basePath;
151 }
152
153
154
155
156
157
158
159 public void setToolbarSet(final String toolbarSet) {
160 this.toolbarSet = toolbarSet;
161 }
162
163
164
165
166
167
168
169
170 public void setWidth(final String width) {
171 this.width = width;
172 }
173
174
175
176
177
178
179
180
181 public void setHeight(final String height) {
182 this.height = height;
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197 @Deprecated
198 public FCKeditorConfig getConfig() {
199 return fckConfig;
200 }
201
202
203
204
205
206
207
208
209
210 public String getConfig(String name) {
211 return fckConfig.get(name);
212 }
213
214
215
216
217
218
219
220
221
222
223 public void setConfig(String name, String value) {
224 if (value != null)
225 fckConfig.put(name, value);
226 }
227
228
229
230
231
232
233
234
235
236
237 @Deprecated
238 public void setConfig(FCKeditorConfig config) {
239 this.fckConfig = config;
240 }
241
242
243
244
245
246
247
248
249
250 private String escapeXml(String str) {
251
252 if (Utils.isEmpty(str))
253 return str;
254
255 StringBuffer sb = new StringBuffer();
256
257 int len = str.length();
258 char c;
259
260 for (int i = 0; i < len; i++) {
261
262 c = str.charAt(i);
263 switch (c) {
264 case '&':
265 sb.append("&");
266 break;
267 case '<':
268 sb.append("<");
269 break;
270 case '>':
271 sb.append(">");
272 break;
273 case '"':
274 sb.append(""");
275 break;
276
277
278
279 case '\'':
280 sb.append("'");
281 break;
282 default:
283 sb.append(c);
284 break;
285 }
286 }
287
288 return sb.toString();
289 }
290
291
292
293
294
295
296
297
298
299 @Override
300 public String toString() {
301 StringBuffer strEditor = new StringBuffer();
302
303 strEditor.append("<div>");
304 String encodedValue = escapeXml(value);
305
306 if (Compatibility.isCompatibleBrowser(request)) {
307 strEditor.append(createInputForVariable(instanceName, instanceName,
308 encodedValue));
309
310
311 String configStr = fckConfig.getUrlParams();
312 if (Utils.isNotEmpty(configStr))
313 strEditor.append(createInputForVariable(null, instanceName
314 .concat("___Config"), configStr));
315
316
317 StringBuffer editorLink = new StringBuffer(request.getContextPath());
318 editorLink.append(basePath);
319 editorLink.append("/editor/fckeditor.html?InstanceName=").append(
320 instanceName);
321 if (Utils.isNotEmpty(toolbarSet))
322 editorLink.append("&Toolbar=").append(toolbarSet);
323
324 XHtmlTagTool iframeTag = new XHtmlTagTool("iframe",
325 XHtmlTagTool.SPACE);
326 iframeTag.addAttribute("id", instanceName.concat("___Frame"));
327 iframeTag.addAttribute("src", editorLink.toString());
328 iframeTag.addAttribute("width", width);
329 iframeTag.addAttribute("height", height);
330 iframeTag.addAttribute("frameborder", "0");
331 iframeTag.addAttribute("scrolling", "no");
332 strEditor.append(iframeTag);
333
334 } else {
335 XHtmlTagTool textareaTag = new XHtmlTagTool("textarea",
336 encodedValue);
337 textareaTag.addAttribute("name", instanceName);
338 textareaTag.addAttribute("rows", "4");
339 textareaTag.addAttribute("cols", "40");
340 textareaTag.addAttribute("wrap", "virtual");
341 textareaTag.addAttribute("style", "width: ".concat(width).concat(
342 "; height: ").concat(height));
343 }
344 strEditor.append("</div>");
345 return strEditor.toString();
346 }
347
348
349
350
351
352
353
354 public String createHtml() {
355 return toString();
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369 private String createInputForVariable(final String name, final String id,
370 final String value) {
371 XHtmlTagTool tag = new XHtmlTagTool("input");
372 if (Utils.isNotEmpty(id))
373 tag.addAttribute("id", id);
374 if (Utils.isNotEmpty(name))
375 tag.addAttribute("name", name);
376 tag.addAttribute("value", value);
377 tag.addAttribute("type", "hidden");
378 return tag.toString();
379 }
380 }