1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.fckeditor;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import net.fckeditor.handlers.PropertiesLoader;
27 import net.fckeditor.tool.Compatibility;
28 import net.fckeditor.tool.Utils;
29 import net.fckeditor.tool.XHtmlTagTool;
30
31
32
33
34
35
36
37
38
39
40
41
42 public class FCKeditor {
43
44 private FCKeditorConfig config;
45 private String instanceName;
46 private String value;
47 private HttpServletRequest request;
48
49
50 private String toolbarSet = PropertiesLoader.getProperty("fckeditor.toolbarSet");
51 private String width = PropertiesLoader.getProperty("fckeditor.width");
52 private String height = PropertiesLoader.getProperty("fckeditor.height");
53 private String basePath = PropertiesLoader.getProperty("fckeditor.basePath");
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public FCKeditor(final HttpServletRequest request, final String instanceName,
75 final String width, final String height, final String toolbarSet, final String value,
76 final String basePath) {
77 this.request = request;
78 if (Utils.isBlank(instanceName))
79 throw new IllegalArgumentException(
80 "instanceName must be a valid HTML id");
81 else
82 this.instanceName = instanceName;
83 if (Utils.isNotBlank(width))
84 this.width = width;
85 if (Utils.isNotBlank(height))
86 this.height = height;
87 if (Utils.isNotBlank(toolbarSet))
88 this.toolbarSet = toolbarSet;
89
90 if (Utils.isNotEmpty(value))
91 this.value = value;
92 else
93 this.value = new String();
94 if (Utils.isNotBlank(basePath))
95 this.basePath = request.getContextPath().concat(basePath);
96 else
97 this.basePath = request.getContextPath().concat(this.basePath);
98
99 config = new FCKeditorConfig();
100 }
101
102
103
104
105
106
107
108
109
110
111 public FCKeditor(final HttpServletRequest request, final String instanceName) {
112 this(request, instanceName, null, null, null, null, null);
113 }
114
115
116
117
118
119
120
121 public void setInstanceName(final String instanceName) {
122 this.instanceName = instanceName;
123 }
124
125
126
127
128
129
130
131 public void setValue(final String value) {
132 this.value = value;
133 }
134
135
136
137
138
139
140
141
142
143 public void setBasePath(final String basePath) {
144 this.basePath = basePath;
145 }
146
147
148
149
150
151
152
153 public void setToolbarSet(final String toolbarSet) {
154 this.toolbarSet = toolbarSet;
155 }
156
157
158
159
160
161
162
163 public void setWidth(final String width) {
164 this.width = width;
165 }
166
167
168
169
170
171
172
173 public void setHeight(final String height) {
174 this.height = height;
175 }
176
177
178
179
180
181
182
183
184 public FCKeditorConfig getConfig() {
185 return config;
186 }
187
188
189
190
191
192
193 public void setConfig(FCKeditorConfig config) {
194 this.config = config;
195 }
196
197
198
199
200
201
202
203
204
205 private String escapeXml(String str) {
206 if (Utils.isEmpty(str))
207 return str;
208
209
210
211
212
213 str = str.replaceAll("&", "&");
214 str = str.replaceAll("<", "<");
215 str = str.replaceAll(">", ">");
216 str = str.replaceAll("\"", """);
217 str = str.replaceAll("'", "'");
218 return str;
219 }
220
221
222
223
224
225
226
227
228
229
230 @Override
231 public String toString() {
232 return createHtml();
233 }
234
235
236
237
238
239
240
241
242
243 public String createHtml() {
244 StringBuffer strEditor = new StringBuffer();
245
246 strEditor.append("<div>");
247 String encodedValue = escapeXml(value);
248
249 if (Compatibility.check(request.getHeader("user-agent"))) {
250 strEditor.append(createInputForVariable(instanceName, instanceName, encodedValue));
251
252
253 String configStr = config.getUrlParams();
254 if (Utils.isNotEmpty(configStr))
255 strEditor.append(createInputForVariable(null, instanceName.concat("___Config"),
256 configStr));
257
258
259 String sLink = basePath.concat("/editor/fckeditor.html?InstanceName=").concat(
260 instanceName);
261 if (Utils.isNotEmpty(toolbarSet))
262 sLink += "&Toolbar=".concat(toolbarSet);
263 XHtmlTagTool iframeTag = new XHtmlTagTool("iframe", XHtmlTagTool.SPACE);
264 iframeTag.addAttribute("id", instanceName.concat("___Frame"));
265 iframeTag.addAttribute("src", sLink);
266 iframeTag.addAttribute("width", width);
267 iframeTag.addAttribute("height", height);
268 iframeTag.addAttribute("frameborder", "0");
269 iframeTag.addAttribute("scrolling", "no");
270 strEditor.append(iframeTag);
271
272 } else {
273 XHtmlTagTool textareaTag = new XHtmlTagTool("textarea", encodedValue);
274 textareaTag.addAttribute("name", instanceName);
275 textareaTag.addAttribute("rows", "4");
276 textareaTag.addAttribute("cols", "40");
277 textareaTag.addAttribute("wrap", "virtual");
278 textareaTag.addAttribute("style", "width: ".concat(width).concat("; height: ").concat(
279 height));
280 }
281 strEditor.append("</div>");
282 return strEditor.toString();
283 }
284
285 private String createInputForVariable(final String name, final String id, final String value) {
286 XHtmlTagTool tag = new XHtmlTagTool("input");
287 if (Utils.isNotEmpty(id))
288 tag.addAttribute("id", id);
289 if (Utils.isNotEmpty(name))
290 tag.addAttribute("name", name);
291 tag.addAttribute("value", value);
292 tag.addAttribute("type", "hidden");
293 return tag.toString();
294 }
295 }