Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FCKeditor |
|
| 0.0;0 |
1 | /* | |
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net | |
3 | * Copyright (C) 2003-2008 Frederico Caldeira Knabben | |
4 | * | |
5 | * == BEGIN LICENSE == | |
6 | * | |
7 | * Licensed under the terms of any of the following licenses at your | |
8 | * choice: | |
9 | * | |
10 | * - GNU General Public License Version 2 or later (the "GPL") | |
11 | * http://www.gnu.org/licenses/gpl.html | |
12 | * | |
13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") | |
14 | * http://www.gnu.org/licenses/lgpl.html | |
15 | * | |
16 | * - Mozilla Public License Version 1.1 or later (the "MPL") | |
17 | * http://www.mozilla.org/MPL/MPL-1.1.html | |
18 | * | |
19 | * == END LICENSE == | |
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 | * FCKeditor control class.<br> | |
33 | * | |
34 | * It creates the html code for the FCKeditor based on the following things: | |
35 | * <ul> | |
36 | * <li>browser's capabilities</li> | |
37 | * <li>different properties settings managed by the {@link PropertiesLoader}</li> | |
38 | * <li>settings from the 'caller', eg. jsp-pages</li> | |
39 | * </ul> | |
40 | * | |
41 | * @version $Id: FCKeditor.java 1682 2008-03-05 17:27:06Z th-schwarz $ | |
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 | // defaults | |
52 | 0 | private String toolbarSet = PropertiesLoader.getProperty("fckeditor.toolbarSet"); |
53 | 0 | private String width = PropertiesLoader.getProperty("fckeditor.width"); |
54 | 0 | private String height = PropertiesLoader.getProperty("fckeditor.height"); |
55 | 0 | private String defaultBasePath = PropertiesLoader.getProperty("fckeditor.basePath"); |
56 | ||
57 | /** | |
58 | * Main constructor.<br> | |
59 | * All important settings are done here and will be preset by there defaults taken from | |
60 | * {@link PropertiesLoader}. | |
61 | * | |
62 | * @param request | |
63 | * request object | |
64 | * @param instanceName | |
65 | * unique name | |
66 | * @param width | |
67 | * width | |
68 | * @param height | |
69 | * height | |
70 | * @param toolbarSet | |
71 | * toolbarSet name | |
72 | */ | |
73 | public FCKeditor(final HttpServletRequest request, final String instanceName, | |
74 | final String width, final String height, final String toolbarSet, final String value, | |
75 | 0 | final String basePath) { |
76 | 0 | this.request = request; |
77 | 0 | this.instanceName = instanceName; |
78 | 0 | if (Utils.isNotEmpty(width)) |
79 | 0 | this.width = width; |
80 | 0 | if (Utils.isNotEmpty(height)) |
81 | 0 | this.height = height; |
82 | 0 | if (Utils.isNotEmpty(toolbarSet)) |
83 | 0 | this.toolbarSet = toolbarSet; |
84 | 0 | if (Utils.isNotEmpty(value)) |
85 | 0 | this.value = value; |
86 | 0 | if (Utils.isNotEmpty(basePath)) |
87 | 0 | this.basePath = request.getContextPath().concat(basePath); |
88 | else | |
89 | 0 | this.basePath = request.getContextPath().concat(defaultBasePath); |
90 | ||
91 | 0 | config = new FCKeditorConfig(); |
92 | 0 | } |
93 | ||
94 | /** | |
95 | * Just a wrapper to {@link FCKeditor}. | |
96 | * | |
97 | * @param request | |
98 | * request object | |
99 | * @param instanceName | |
100 | * unique name | |
101 | */ | |
102 | ||
103 | public FCKeditor(final HttpServletRequest request, final String instanceName) { | |
104 | 0 | this(request, instanceName, null, null, null, null, null); |
105 | 0 | } |
106 | ||
107 | /** | |
108 | * Set the unique name of the editor | |
109 | * | |
110 | * @param value | |
111 | * name | |
112 | */ | |
113 | public void setInstanceName(final String value) { | |
114 | 0 | instanceName = value; |
115 | 0 | } |
116 | ||
117 | /** | |
118 | * Set the initial value to be edited.<br> | |
119 | * In HTML code | |
120 | * | |
121 | * @param value | |
122 | * value | |
123 | */ | |
124 | public void setValue(final String value) { | |
125 | 0 | this.value = value; |
126 | 0 | } |
127 | ||
128 | /** | |
129 | * Set the dir where the FCKeditor files reside on the server.<br> | |
130 | * <b>Remarks</b>:<br> | |
131 | * Avoid using relative paths. It is preferable to set the base path starting from the root (/).<br> | |
132 | * Always finish the path with a slash (/). | |
133 | * | |
134 | * @param value | |
135 | * path | |
136 | */ | |
137 | public void setBasePath(final String value) { | |
138 | 0 | basePath = value; |
139 | 0 | } |
140 | ||
141 | /** | |
142 | * Set the name of the toolbar to display | |
143 | * | |
144 | * @param value | |
145 | * toolbar name | |
146 | */ | |
147 | public void setToolbarSet(final String value) { | |
148 | 0 | toolbarSet = value; |
149 | 0 | } |
150 | ||
151 | /** | |
152 | * Set the width of the textarea | |
153 | * | |
154 | * @param value | |
155 | * width | |
156 | */ | |
157 | public void setWidth(final String value) { | |
158 | 0 | width = value; |
159 | 0 | } |
160 | ||
161 | /** | |
162 | * Set the height of the textarea | |
163 | * | |
164 | * @param value | |
165 | * height | |
166 | */ | |
167 | public void setHeight(final String value) { | |
168 | 0 | height = value; |
169 | 0 | } |
170 | ||
171 | /** | |
172 | * Get the advanced configuation set.<br> | |
173 | * Adding element to this collection you can override the settings specified in the config.js | |
174 | * file. | |
175 | * | |
176 | * @return configuration collection | |
177 | */ | |
178 | public FCKeditorConfig getConfig() { | |
179 | 0 | return config; |
180 | } | |
181 | ||
182 | /** | |
183 | * Set the advanced configuation set. | |
184 | * | |
185 | * @param value | |
186 | * configuration collection | |
187 | */ | |
188 | public void setConfig(FCKeditorConfig value) { | |
189 | 0 | config = value; |
190 | 0 | } |
191 | ||
192 | private String escapeXml(String txt) { | |
193 | 0 | if (Utils.isEmpty(txt)) |
194 | 0 | return txt; |
195 | 0 | txt = txt.replaceAll("&", "&"); |
196 | 0 | txt = txt.replaceAll("<", "<"); |
197 | 0 | txt = txt.replaceAll(">", ">"); |
198 | 0 | txt = txt.replaceAll("\"", """); |
199 | 0 | txt = txt.replaceAll("'", "'"); |
200 | 0 | return txt; |
201 | } | |
202 | ||
203 | /** | |
204 | * Minimum implementation, see ticket #27 for detailed information. | |
205 | */ | |
206 | public String create() { | |
207 | 0 | return createHtml(); |
208 | } | |
209 | ||
210 | @Override | |
211 | public String toString() { | |
212 | 0 | return createHtml(); |
213 | } | |
214 | ||
215 | /** | |
216 | * Generate the HTML Code for the editor. <br> | |
217 | * Evalute the browser capabilities and generate the editor if compatible, or a simple textarea | |
218 | * otherwise. | |
219 | * | |
220 | * @return html code | |
221 | */ | |
222 | public String createHtml() { | |
223 | 0 | StringBuffer strEditor = new StringBuffer(); |
224 | ||
225 | 0 | strEditor.append("<div>"); |
226 | 0 | String encodedValue = escapeXml(value.replaceAll("((\r?\n)+|\t*)", "")); |
227 | ||
228 | 0 | if (Compatibility.check(request.getHeader("user-agent"))) { |
229 | 0 | strEditor.append(createInputForVariable(instanceName, instanceName, encodedValue)); |
230 | ||
231 | // create config html | |
232 | 0 | String configStr = config.getUrlParams(); |
233 | 0 | if (Utils.isNotEmpty(configStr)) |
234 | // configStr = configStr.substring(1); | |
235 | 0 | strEditor.append(createInputForVariable(null, instanceName.concat("___Config"), |
236 | configStr)); | |
237 | ||
238 | // create IFrame | |
239 | 0 | String sLink = basePath.concat("/editor/fckeditor.html?InstanceName=").concat( |
240 | instanceName); | |
241 | 0 | if (Utils.isNotEmpty(toolbarSet)) |
242 | 0 | sLink += "&Toolbar=".concat(toolbarSet); |
243 | 0 | XHtmlTagTool iframeTag = new XHtmlTagTool("iframe", XHtmlTagTool.SPACE); |
244 | 0 | iframeTag.addAttribute("id", instanceName.concat("___Frame")); |
245 | 0 | iframeTag.addAttribute("src", sLink); |
246 | 0 | iframeTag.addAttribute("width", width); |
247 | 0 | iframeTag.addAttribute("height", height); |
248 | 0 | iframeTag.addAttribute("frameborder", "no"); |
249 | 0 | iframeTag.addAttribute("scrolling", "no"); |
250 | 0 | strEditor.append(iframeTag); |
251 | ||
252 | 0 | } else { |
253 | 0 | XHtmlTagTool textareaTag = new XHtmlTagTool("textarea", encodedValue); |
254 | 0 | textareaTag.addAttribute("name", instanceName); |
255 | 0 | textareaTag.addAttribute("rows", "4"); |
256 | 0 | textareaTag.addAttribute("cols", "40"); |
257 | 0 | textareaTag.addAttribute("wrap", "virtual"); |
258 | 0 | textareaTag.addAttribute("style", "width: ".concat(width).concat("; height: ").concat( |
259 | height)); | |
260 | } | |
261 | 0 | strEditor.append("</div>"); |
262 | 0 | return strEditor.toString(); |
263 | } | |
264 | ||
265 | private String createInputForVariable(final String name, final String id, final String value) { | |
266 | 0 | XHtmlTagTool tag = new XHtmlTagTool("input"); |
267 | 0 | if (Utils.isNotEmpty(id)) |
268 | 0 | tag.addAttribute("id", id); |
269 | 0 | if (Utils.isNotEmpty(name)) |
270 | 0 | tag.addAttribute("name", name); |
271 | 0 | tag.addAttribute("value", value); |
272 | 0 | tag.addAttribute("type", "hidden"); |
273 | 0 | return tag.toString(); |
274 | } | |
275 | } |