KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > CmsTemplateForm


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/CmsTemplateForm.java,v $
3  * Date : $Date: 2005/07/22 12:14:36 $
4  * Version: $Revision: 1.13 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.frontend.templateone;
33
34 import org.opencms.file.CmsPropertyDefinition;
35 import org.opencms.util.CmsStringUtil;
36
37 import java.util.Map JavaDoc;
38 import java.util.regex.Matcher JavaDoc;
39 import java.util.regex.Pattern JavaDoc;
40
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import javax.servlet.jsp.PageContext JavaDoc;
44
45 import org.apache.commons.httpclient.util.URIUtil;
46
47 /**
48  * Provides methods to build interactive JSP forms.<p>
49  *
50  * @author Andreas Zahner
51  *
52  * @version $Revision: 1.13 $
53  *
54  * @since 6.0.0
55  */

56 public abstract class CmsTemplateForm extends CmsTemplateBean {
57
58     /** Name of the resource bundle containing the localized form messages.<p> */
59     public static final String JavaDoc MESSAGE_BUNDLE_FORM = "templateone_form";
60     
61     /** Request parameter name for the action parameter to determine if the form has been submitted.<p> */
62     public static final String JavaDoc PARAM_ACTION = "action";
63
64     /** Holds the error messages for form validation.<p> */
65     private Map JavaDoc m_errors;
66     
67     /** Stores the URI of the JSP form.<p> */
68     private String JavaDoc m_formUri;
69     
70     /** Stores the URI of the calling page including eventual request parameter appendings.<p> */
71     private String JavaDoc m_pageUri;
72     
73     /** Stores the complete URL of the calling page including eventual request parameter appendings.<p> */
74     private String JavaDoc m_pageUrl;
75     
76     /** Stores the URI of the page containing the texts for the form.<p> */
77     private String JavaDoc m_textsUri;
78
79     /**
80      * Empty constructor, required for every JavaBean.<p>
81      */

82     public CmsTemplateForm() {
83
84         super();
85     }
86
87     /**
88      * Constructor, with parameters.<p>
89      *
90      * Use this constructor for the template.<p>
91      *
92      * @param context the JSP page context object
93      * @param req the JSP request
94      * @param res the JSP response
95      */

96     public CmsTemplateForm(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
97
98         super();
99         init(context, req, res);
100     }
101
102     /**
103      * Returns the input String with line breaks added at the specified separator character.<p>
104      *
105      * @param inputString the input String to modify
106      * @param sepChar the separator character to look up in the input String
107      * @param lineLength the line length after which a break should occur
108      * @param separator the separator String to append as line breaks, e.g. <code>&lt;br&gt;</code>
109      * @return the modified input String with line breaks added
110      */

111     public static String JavaDoc getFormattedString(String JavaDoc inputString, char sepChar, int lineLength, String JavaDoc separator) {
112
113         if (inputString.length() > lineLength) {
114             // length of input String is larger than allowed line length
115
int inputLength = inputString.length();
116             StringBuffer JavaDoc result = new StringBuffer JavaDoc(inputLength + 4);
117             while (inputLength > lineLength) {
118                 int count = lineLength;
119                 while (count < inputLength && inputString.charAt(count) != sepChar) {
120                     // look for separator position
121
count++;
122                 }
123                 if (count < inputLength) {
124                     // separator found, add line break
125
result.append(inputString.substring(0, count + 1));
126                     result.append(separator);
127                     inputString = inputString.substring(count + 1);
128                     inputLength = inputString.length();
129                 } else {
130                     // no separator found, leave look
131
break;
132                 }
133             }
134             // append end of input String to output
135
result.append(inputString);
136             inputString = result.toString();
137         }
138         return inputString;
139     }
140
141     /**
142      * Oversimplistic method to validate email addresses.<p>
143      *
144      * @param theAddress the email address to validate
145      * @return true if theAddress is at least a String of the form xx@yy.zz
146      */

147     public static boolean isValidEmailAddress(String JavaDoc theAddress) {
148
149         if (theAddress == null) {
150             return false;
151         }
152         return (theAddress.lastIndexOf(".") < (theAddress.length() - 2) && theAddress.lastIndexOf(".") > theAddress.indexOf("@"));
153     }
154
155     /**
156      * Returns the template configuration path in the OpenCms VFS.<p>
157      *
158      * Overwrites the method in the super class because the uri is set to the
159      * form uri in the module.<p>
160      *
161      * @return the template configuration path
162      */

163     public String JavaDoc getConfigPath() {
164
165         // store form uri
166
String JavaDoc uri = getRequestContext().getUri();
167         // set uri to page uri to obtain configuration path
168
getRequestContext().setUri(getPageUri(true));
169         String JavaDoc path = property(PROPERTY_CONFIGPATH, "search", "");
170         // reset uri to form uri
171
getRequestContext().setUri(uri);
172         return path;
173     }
174
175     /**
176      * Returns the error message for the specified key or an empty String if no error is present for the key.<p>
177      *
178      * @param key error key to look up
179      * @return the error message for the specified key or an empty String
180      */

181     public String JavaDoc getError(String JavaDoc key) {
182
183         if (m_errors != null) {
184             String JavaDoc message = (String JavaDoc)m_errors.get(key);
185             if (message == null) {
186                 return "";
187             } else {
188                 return message;
189             }
190         } else {
191             return "";
192         }
193     }
194
195     /**
196      * Returns the error Map holding the validation errors.<p>
197      *
198      * @return the error Map holding the validation errors
199      */

200     public Map JavaDoc getErrors() {
201
202         return m_errors;
203     }
204
205     /**
206      * Returns the content of the specified page element from the defined text page.<p>
207      *
208      * @param element the name of the element to display
209      * @param stripHtml indicates if HTML tags should be filtered from the element contents
210      * @return the content of the specified page element from the defined text page
211      */

212     public String JavaDoc getFormText(String JavaDoc element, boolean stripHtml) {
213
214         String JavaDoc content = getContent(getTextsUri(), element, getRequestContext().getLocale());
215         if (stripHtml) {
216             // remove the tags from the content
217
content = removeHtmlTags(content);
218         }
219         return content;
220     }
221
222     /**
223      * Returns the subsituted link to the JSP form.<p>
224      *
225      * This can be used for the "action" attribute value of the &lt;form&gt; tag.<p>
226      *
227      * @return the subsituted link to the JSP form
228      */

229     public String JavaDoc getFormUri() {
230
231         return link(m_formUri);
232     }
233
234     /**
235      * Returns the title of the recommended page.<p>
236      *
237      * If the title property is not found, the page URI
238      * is returned instead.<p>
239      *
240      * @return the title of the recommended page
241      */

242     public String JavaDoc getPageTitle() {
243
244         String JavaDoc title = property(CmsPropertyDefinition.PROPERTY_TITLE, getPageUri(true), "");
245         if ("".equals(title)) {
246             title = getPageUri(true);
247         }
248         return title;
249     }
250
251     /**
252      * Returns the (not substituted) URI to the page that called the form.<p>
253      *
254      * Includes all request parameters of the page, too.<p>
255      *
256      * @return the (not substituted) URI to the page that called the form
257      */

258     public String JavaDoc getPageUri() {
259
260         return getPageUri(false);
261     }
262
263     /**
264      * Returns the (not substituted) URI to the page that called the form.<p>
265      *
266      * Removes all request parameters from the page URI, if specified.<p>
267      *
268      * @param removeParams if true, all request parameters will be removed from the URI
269      * @return the (not substituted) URI to the page that called the form
270      */

271     public String JavaDoc getPageUri(boolean removeParams) {
272
273         if (removeParams) {
274             // remove eventual request parameters
275
String JavaDoc uri = m_pageUri;
276             if (CmsStringUtil.isNotEmpty(uri)) {
277                 uri = URIUtil.getPath(uri);
278             }
279             return uri;
280         } else {
281             return m_pageUri;
282         }
283     }
284
285     /**
286      * Returns the URL of the page to be displayed on the form.<p>
287      *
288      * @return the URL of the page
289      */

290     public String JavaDoc getPageUrl() {
291
292         if (CmsStringUtil.isEmpty(m_pageUrl)) {
293             StringBuffer JavaDoc result = new StringBuffer JavaDoc(64);
294             HttpServletRequest JavaDoc request = getRequest();
295             result.append(request.getScheme());
296             result.append("://");
297             result.append(request.getServerName());
298             int port = request.getServerPort();
299             String JavaDoc portString = "";
300             if (port != 80 && port != 443) {
301                 // only add port different from standard ports
302
portString = ":" + port;
303             }
304             result.append(portString);
305             result.append(link(m_pageUri));
306             m_pageUrl = result.toString();
307         }
308         return m_pageUrl;
309     }
310
311     /**
312      * Returns the URI of the page containing the texts for the form.<p>
313      *
314      * @return the URI of the page containing the texts for the form
315      */

316     public String JavaDoc getTextsUri() {
317
318         if (m_textsUri == null) {
319             m_textsUri = checkTextsUri();
320         }
321         return m_textsUri;
322     }
323
324     /**
325      * Checks if the input form has validation errors.<p>
326      *
327      * @return true if at least one validation error was found, otherwise false
328      */

329     public boolean hasValidationErrors() {
330
331         if (isSubmitted()) {
332             if (getErrors() != null && getErrors().size() > 0) {
333                 return true;
334             }
335         }
336         return false;
337     }
338
339     /**
340      * Initialize this bean with the current page context, request and response.<p>
341      *
342      * It is required to call one of the init() methods before you can use the
343      * instance of this bean.
344      *
345      * @param context the JSP page context object
346      * @param req the JSP request
347      * @param res the JSP response
348      */

349     public void init(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
350         
351         // get the page uri from request parameter
352
m_pageUri = req.getParameter(CmsTemplateBean.PARAM_URI);
353         // call initialization of super class
354
super.init(context, req, res);
355         // set site root
356
String JavaDoc siteRoot = req.getParameter(CmsTemplateBean.PARAM_SITE);
357         if (CmsStringUtil.isNotEmpty(siteRoot)) {
358             getRequestContext().setSiteRoot(siteRoot);
359         }
360         // store the form uri
361
m_formUri = getRequestContext().getUri();
362         if (m_pageUri == null) {
363             m_pageUri = m_formUri;
364         }
365         messages(MESSAGE_BUNDLE_FORM);
366     }
367
368     /**
369      * Checks if the given String is true and returns the "checked" attribute for checkboxes and radiobuttons.<p>
370      *
371      * @param fieldValue the value of the field to test
372      * @return the "checked" attribute or an empty String
373      */

374     public String JavaDoc isChecked(String JavaDoc fieldValue) {
375
376         if (Boolean.valueOf(fieldValue).booleanValue()) {
377             return " checked=\"checked\"";
378         } else {
379             return "";
380         }
381     }
382
383     /**
384      * Returns true if the two parameters are equal.<p>
385      *
386      * Use this method to determine which entry of a select box or radio buttons is selected.<p>
387      *
388      * @param fieldValue the current option or radio button value to check
389      * @param requestValue the current request value
390      * @return true if the two parameters are equal
391      */

392     public boolean isSelected(String JavaDoc fieldValue, String JavaDoc requestValue) {
393
394         if (fieldValue.equals(requestValue)) {
395             return true;
396         }
397         return false;
398     }
399
400     /**
401      * Returns if the form has been submitted or not.<p>
402      *
403      * Checks the presence of the "action" request parameter.<p>
404      *
405      * @return true if the form has been submitted, otherwise false
406      */

407     public boolean isSubmitted() {
408
409         return getRequest().getParameter(PARAM_ACTION) != null;
410     }
411
412     /**
413      * Sets the error Map holding the validation errors.<p>
414      *
415      * @param errors error Map
416      */

417     public void setErrors(Map JavaDoc errors) {
418
419         m_errors = errors;
420     }
421
422     /**
423      * Validates the values of the input fields and creates error messages, if necessary.<p>
424      *
425      * @return true if all checked input values are valid, otherwise false
426      */

427     public abstract boolean validate();
428
429     /**
430      * Returns the text URI from the configuration file and checks the presence.<p>
431      *
432      * Returns the default texts URI if the file specified in the configuration can not be found.<p>
433      *
434      * @return the text URI from the configuration file
435      */

436     protected abstract String JavaDoc checkTextsUri();
437
438     /**
439      * Removes all HTML tags from the given String by using a regular expression.<p>
440      *
441      * @param content the String to scan for HTML tags
442      * @return the content without HTML tags
443      */

444     protected String JavaDoc removeHtmlTags(String JavaDoc content) {
445
446         if (content != null && content.indexOf("<") != -1) {
447             Matcher JavaDoc matcher = Pattern.compile("<(.|\\n)+?>").matcher(content);
448             content = matcher.replaceAll("");
449         }
450         return content;
451     }
452
453 }
Popular Tags