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