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