001: /*
002: * FCKeditor - The text editor for internet
003: * Copyright (C) 2003-2005 Frederico Caldeira Knabben
004: *
005: * Licensed under the terms of the GNU Lesser General Public License:
006: * http://www.opensource.org/licenses/lgpl-license.php
007: *
008: * For further information visit:
009: * http://www.fckeditor.net/
010: *
011: * File Name: FCKeditor.java
012: * FCKeditor control class.
013: *
014: * Version: 2.3
015: * Modified: 2005-08-11 16:29:00
016: *
017: * File Authors:
018: * Simone Chiaretta (simo@users.sourceforge.net)
019: */
020:
021: package com.fredck.FCKeditor;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: /**
026: * The main class of the class lib.<br>
027: * It's the container for all properties and the class that generate the output based on browser capabilities and configurations passed by the developer.
028: *
029: * @author Simone Chiaretta (simo@users.sourceforge.net)
030: */
031: public class FCKeditor {
032:
033: private FCKeditorConfigurations oConfig;
034: private String instanceName;
035: private String value = "";
036: private String basePath;
037: private String toolbarSet = "Default";
038: private String width = "100%";
039: private String height = "200";
040:
041: HttpServletRequest request;
042:
043: /**
044: * Get the unique name of the editor
045: *
046: * @return name
047: */
048: public String getInstanceName() {
049: return instanceName;
050: }
051:
052: /**
053: * Set the unique name of the editor
054: *
055: * @param value name
056: */
057: public void setInstanceName(String value) {
058: instanceName = value;
059: }
060:
061: /**
062: * Get the initial value to be edited.<br>
063: * In HTML code
064: *
065: * @return value
066: */
067: public String getValue() {
068: return value;
069: }
070:
071: /**
072: * Set the initial value to be edited.<br>
073: * In HTML code
074: *
075: * @param value value
076: */
077: public void setValue(String value) {
078: this .value = value;
079: }
080:
081: /**
082: * Get the dir where the FCKeditor files reside on the server
083: *
084: * @return path
085: */
086: public String getBasePath() {
087: return basePath;
088: }
089:
090: /**
091: * Set the dir where the FCKeditor files reside on the server.<br>
092: *<b>Remarks</b>:<br>
093: *Avoid using relative paths. It is preferable to set the base path starting from the root (/).<br>
094: *Always finish the path with a slash (/).
095: *
096: * @param value path
097: */
098: public void setBasePath(String value) {
099: basePath = value;
100: }
101:
102: /**
103: * Get the name of the toolbar to display
104: *
105: * @return toolbar name
106: */
107: public String getToolbarSet() {
108: return toolbarSet;
109: }
110:
111: /**
112: * Set the name of the toolbar to display
113: *
114: * @param value toolbar name
115: */
116: public void setToolbarSet(String value) {
117: toolbarSet = value;
118: }
119:
120: /**
121: * Get the width of the textarea
122: *
123: * @return width
124: */
125: public String getWidth() {
126: return width;
127: }
128:
129: /**
130: * Set the width of the textarea
131: *
132: * @param value width
133: */
134: public void setWidth(String value) {
135: width = value;
136: }
137:
138: /**
139: * Get the height of the textarea
140: *
141: * @return height
142: */
143: public String getHeight() {
144: return height;
145: }
146:
147: /**
148: * Set the height of the textarea
149: *
150: * @param value height
151: */
152: public void setHeight(String value) {
153: height = value;
154: }
155:
156: /**
157: * Get the advanced configuation set.<br>
158: * Adding element to this collection you can override the settings specified in the config.js file.
159: *
160: * @return configuration collection
161: */
162: public FCKeditorConfigurations getConfig() {
163: return oConfig;
164: }
165:
166: /**
167: * Set the advanced configuation set.
168: *
169: * @param value configuration collection
170: */
171: public void setConfig(FCKeditorConfigurations value) {
172: oConfig = value;
173: }
174:
175: /**
176: * Initialize the object setting all value to the default ones.
177: * <p>
178: * <ul>
179: * <li>width: 100%</li>
180: * <li>height: 200</li>
181: * <li>toolbar name: Default</li>
182: * <li>basePath: context root + "/FCKeditor/"</li>
183: * </ul>
184: * </p>
185: *
186: * @param req request object
187: */
188: public FCKeditor(HttpServletRequest req) {
189: request = req;
190: basePath = request.getContextPath() + "/FCKeditor/";
191: oConfig = new FCKeditorConfigurations();
192: }
193:
194: /**
195: * Initialize the object setting the unique name and then all value to the default ones.
196: * <p>
197: * <ul>
198: * <li>width: 100%</li>
199: * <li>height: 200</li>
200: * <li>toolbar name: Default</li>
201: * <li>basePath: context root + "/FCKeditor/"</li>
202: * </ul>
203: * </p>
204: *
205: * @param req request object
206: * @param parInstanceName unique name
207: */
208: public FCKeditor(HttpServletRequest req, String parInstanceName) {
209: request = req;
210: basePath = request.getContextPath() + "/FCKeditor/";
211: instanceName = parInstanceName;
212: oConfig = new FCKeditorConfigurations();
213: }
214:
215: /**
216: * Initialize the object setting all basic configurations.<br>
217: *
218: * The basePath is context root + "/FCKeditor/"
219: *
220: * @param req request object
221: * @param parInstanceName unique name
222: * @param parWidth width
223: * @param parHeight height
224: * @param parToolbarSet toolbarSet name
225: * @param parValue initial value
226: */
227: public FCKeditor(HttpServletRequest req, String parInstanceName,
228: String parWidth, String parHeight, String parToolbarSet,
229: String parValue) {
230: request = req;
231: basePath = request.getContextPath() + "/FCKeditor/";
232: instanceName = parInstanceName;
233: width = parWidth;
234: height = parHeight;
235: toolbarSet = parToolbarSet;
236: value = parValue;
237: oConfig = new FCKeditorConfigurations();
238: }
239:
240: private boolean isCompatible() {
241: String userAgent = request.getHeader("user-agent");
242: if (userAgent == null)
243: return false;
244: userAgent = userAgent.toLowerCase();
245: if ((userAgent.indexOf("msie") != -1)
246: && (userAgent.indexOf("mac") == -1)
247: && (userAgent.indexOf("opera") == -1)) {
248: if (retrieveBrowserVersion(userAgent) >= 5.5)
249: return true;
250: } else if (userAgent.indexOf("gecko") != -1) {
251: if (retrieveBrowserVersion(userAgent) >= 20030210)
252: return true;
253: }
254: return false;
255: }
256:
257: private double retrieveBrowserVersion(String userAgent) {
258: if (userAgent.indexOf("msie") > -1) {
259: String str = userAgent
260: .substring(userAgent.indexOf("msie") + 5);
261: return Double.parseDouble(str
262: .substring(0, str.indexOf(";")));
263: } else {
264: String str = userAgent
265: .substring(userAgent.indexOf("gecko") + 6);
266: return Double.parseDouble(str.substring(0, 8));
267: }
268: }
269:
270: private String HTMLEncode(String txt) {
271: txt = txt.replaceAll("&", "&");
272: txt = txt.replaceAll("<", "<");
273: txt = txt.replaceAll(">", ">");
274: txt = txt.replaceAll("\"", """);
275: txt = txt.replaceAll("'", "’");
276: return txt;
277: }
278:
279: /**
280: * Generate the HTML Code for the editor.
281: * <br>
282: * Evalute the browser capabilities and generate the editor if IE 5.5 or Gecko 20030210 or greater,
283: * or a simple textarea otherwise.
284: *
285: * @return html code
286: */
287: public String create() {
288: StringBuffer strEditor = new StringBuffer();
289:
290: strEditor.append("<div>");
291: String encodedValue = HTMLEncode(value);
292:
293: if (isCompatible()) {
294:
295: strEditor.append("<input type=\"hidden\" id=\""
296: + instanceName + "\" name=\"" + instanceName
297: + "\" value=\"" + encodedValue + "\">");
298:
299: strEditor.append(createConfigHTML());
300: strEditor.append(createIFrameHTML());
301:
302: } else {
303: strEditor.append("<TEXTAREA name=\"" + instanceName
304: + "\" rows=\"4\" cols=\"40\" style=\"WIDTH: "
305: + width + "; HEIGHT: " + height
306: + "\" wrap=\"virtual\">" + encodedValue
307: + "</TEXTAREA>");
308: }
309: strEditor.append("</div>");
310: return strEditor.toString();
311: }
312:
313: private String createConfigHTML() {
314: String configStr = oConfig.getUrlParams();
315:
316: if (!configStr.equals(""))
317: configStr = configStr.substring(1);
318:
319: return "<input type=\"hidden\" id=\"" + instanceName
320: + "___Config\" value=\"" + configStr + "\">";
321: }
322:
323: private String createIFrameHTML() {
324:
325: String sLink = basePath + "editor/fckeditor.html?InstanceName="
326: + instanceName;
327:
328: if (!toolbarSet.equals(""))
329: sLink += "&Toolbar=" + toolbarSet;
330:
331: return "<iframe id=\"" + instanceName + "___Frame\" src=\""
332: + sLink + "\" width=\"" + width + "\" height=\""
333: + height
334: + "\" frameborder=\"no\" scrolling=\"no\"></iframe>";
335:
336: }
337:
338: }
|