View Javadoc

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  	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  	 * 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  	        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  	 * 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 		this(request, instanceName, null, null, null, null, null);
105 	}
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 		instanceName = value;
115 	}
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 		this.value = value;
126 	}
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 		basePath = value;
139 	}
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 		toolbarSet = value;
149 	}
150 
151 	/**
152 	 * Set the width of the textarea
153 	 * 
154 	 * @param value
155 	 *            width
156 	 */
157 	public void setWidth(final String value) {
158 		width = value;
159 	}
160 
161 	/**
162 	 * Set the height of the textarea
163 	 * 
164 	 * @param value
165 	 *            height
166 	 */
167 	public void setHeight(final String value) {
168 		height = value;
169 	}
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 		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 		config = value;
190 	}
191 
192 	private String escapeXml(String txt) {
193 		if (Utils.isEmpty(txt))
194 			return txt;
195 		txt = txt.replaceAll("&", "&#38;");
196 		txt = txt.replaceAll("<", "&#60;");
197 		txt = txt.replaceAll(">", "&#62;");
198 		txt = txt.replaceAll("\"", "&#34;");
199 		txt = txt.replaceAll("'", "&#39;");
200 		return txt;
201 	}
202 
203 	/**
204 	 * Minimum implementation, see ticket #27 for detailed information.
205 	 */
206 	public String create() {
207 		return createHtml();
208 	}
209 
210 	@Override
211 	public String toString() {
212 		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 		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 			// create config html
232 			String configStr = config.getUrlParams();
233 			if (Utils.isNotEmpty(configStr))
234 				// configStr = configStr.substring(1);
235 				strEditor.append(createInputForVariable(null, instanceName.concat("___Config"),
236 				        configStr));
237 
238 			// create IFrame
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 }