KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > defaults > CmsXmlFormTemplateFile


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/defaults/CmsXmlFormTemplateFile.java,v $
3 * Date : $Date: 2005/06/27 23:22:23 $
4 * Version: $Revision: 1.3 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
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 OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29 package com.opencms.defaults;
30
31 import org.opencms.file.CmsFile;
32 import org.opencms.file.CmsObject;
33 import org.opencms.main.CmsException;
34
35 import com.opencms.legacy.CmsLegacyException;
36 import com.opencms.template.CmsXmlTemplateFile;
37 import com.opencms.workplace.CmsWorkplaceDefault;
38
39 import java.lang.reflect.InvocationTargetException JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 import org.w3c.dom.Element JavaDoc;
45
46 /**
47  * Template content definition for generating HTML forms.
48  * This is an extension of the default template content definition.
49  * Special tags for handling HTML form elements are added here.
50  * See the handleXxxTag Methods for more details.
51  *
52  * @author Alexander Lucas
53  * @version $Revision: 1.3 $ $Date: 2005/06/27 23:22:23 $
54  *
55  * @deprecated Will not be supported past the OpenCms 6 release.
56  */

57 public class CmsXmlFormTemplateFile extends CmsXmlTemplateFile {
58
59     /**
60      * Default constructor.
61      *
62      * @throws CmsException if something goes wrong
63      */

64     public CmsXmlFormTemplateFile() throws CmsException {
65         super();
66         registerMyTags();
67     }
68
69     /**
70      * Constructor for creating a new object containing the content
71      * of the given file.<p>
72      *
73      * @param cms CmsObject object for accessing system resources
74      * @param file the file that should be read
75      * @throws CmsException if something goes wrong
76      */

77     public CmsXmlFormTemplateFile(CmsObject cms, CmsFile file) throws CmsException {
78         super();
79         registerMyTags();
80         init(cms, file);
81     }
82
83     /**
84      * Constructor for creating a new object containing the content
85      * of the given filename.<p>
86      *
87      * @param cms CmsObject object for accessing system resources
88      * @param filename Name of the body file that shoul be read
89      * @throws CmsException if something goes wrong
90      */

91     public CmsXmlFormTemplateFile(CmsObject cms, String JavaDoc filename) throws CmsException {
92         super();
93         registerMyTags();
94         init(cms, filename);
95     }
96
97     /**
98      * Gets the expected tagname for the XML documents of this content type.<p>
99      *
100      * @return Expected XML tagname.
101      */

102     public String JavaDoc getXmlDocumentTagName() {
103         return "XMLTEMPLATE";
104     }
105
106     /**
107      * Handles any occurence of the special XML tag <code>&lt;RADIOBUTTON&gt;</code> for
108      * generating HTML form radio buttons.
109      * <P>
110      * The definition of a HTML radio button will be taken from I_CmsWpConstants.C_VFS_PATH_DEFAULT_INTERNAL + "HTMLFormDefs".
111      * If the file is missing, this method will crash. Ensure this file is created and filled
112      * with all required XML tags.
113      * <P>
114      * Radio buttons can be generated by adding the special XML tag
115      * <code>&lt;RADIOBUTTON class="myClass" method="myMethod" name="myName"/&gt;</code>
116      * to the template file. This tag will be replaced with the correspondig group of radio buttons
117      * while processing the template file. The <code>class</code> parameter is optional.
118      * The <code>method</code> parameter will be used to look up a user defined method
119      * in the template class assigned to the template file. This method should look like
120      * <br/>
121      * <code>public Integer myMethod(CmsObject cms, Vector names, Vector values, Hashtable parameters)</code><br/>
122      * and will be used to get the content of the requested radion button group. The vectors <code>names</code>
123      * and <code>values</code> should be filled with the appropriate values. The return value should be an
124      * Integer containing the pre-selected radio button or -1, if no value should be pre-selected.<p>
125      *
126      * @param n XML element containing the current special workplace tag
127      * @param callingObject reference to the calling object
128      * @param userObj hashtable containig all user parameters
129      * @return the radio button definition
130      * @throws CmsException if something goes wrong
131      */

132     public Object JavaDoc handleRadiobuttonTag(Element JavaDoc n, Object JavaDoc callingObject, Object JavaDoc userObj) throws CmsException {
133         Hashtable JavaDoc parameters = (Hashtable JavaDoc)userObj;
134
135         // StringBuffer for the generated output
136
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
137
138         // Here the different select box options will be stored
139
Vector JavaDoc values = new Vector JavaDoc();
140         Vector JavaDoc names = new Vector JavaDoc();
141         Integer JavaDoc returnObject = null;
142
143         // Read radiobutton parameters
144
String JavaDoc radioClass = n.getAttribute(CmsWorkplaceDefault.C_SELECTBOX_CLASS);
145         String JavaDoc radioName = n.getAttribute(CmsWorkplaceDefault.C_RADIO_NAME);
146         String JavaDoc radioMethod = n.getAttribute(CmsWorkplaceDefault.C_RADIO_METHOD);
147         String JavaDoc radioOrder = n.getAttribute(CmsWorkplaceDefault.C_RADIO_ORDER);
148         if (radioOrder == null || ((!"row".equals(radioOrder)) && (!"col".equals(radioOrder)))) {
149             radioOrder = "col";
150         }
151
152         // call the method for generating listbox elements
153
Method JavaDoc groupsMethod = null;
154         int selectedOption = 0;
155         try {
156             groupsMethod = callingObject.getClass().getMethod(radioMethod, new Class JavaDoc[] {
157                 CmsObject.class, Vector JavaDoc.class, Vector JavaDoc.class, Hashtable JavaDoc.class
158             });
159             returnObject = (Integer JavaDoc)groupsMethod.invoke(callingObject, new Object JavaDoc[] {
160                 m_cms, names, values, parameters
161             });
162         } catch (NoSuchMethodException JavaDoc exc) {
163
164             // The requested method was not found.
165
throwException("Could not find radio button method " + radioMethod + " in calling class " + callingObject.getClass().getName()
166                     + " for generating select box content.", CmsLegacyException.C_NOT_FOUND);
167         } catch (InvocationTargetException JavaDoc targetEx) {
168
169             // the method could be invoked, but throwed a exception
170
// itself. Get this exception and throw it again.
171
Throwable JavaDoc e = targetEx.getTargetException();
172             if (!(e instanceof CmsException)) {
173
174                 // Only print an error if this is NO CmsException
175
throwException("Radio button method " + radioMethod + " in calling class " + callingObject.getClass().getName()
176                         + " throwed an exception. " + e, CmsLegacyException.C_UNKNOWN_EXCEPTION);
177             } else {
178
179                 // This is a CmsException
180
// Error printing should be done previously.
181
throw (CmsException)e;
182             }
183         } catch (Exception JavaDoc exc2) {
184             throwException("Radio button method " + radioMethod + " in calling class " + callingObject.getClass().getName()
185                     + " was found but could not be invoked. " + exc2, CmsLegacyException.C_XML_NO_USER_METHOD);
186         }
187
188         // If the radio button method returned a value, use it for preselecting an option
189
if (returnObject != null) {
190             selectedOption = returnObject.intValue();
191         }
192
193         // process the vectors with the elelmetns of the radio buttons to be displayed.
194
int numValues = values.size();
195         CmsXmlTemplateFile radiodef = new CmsXmlTemplateFile(m_cms, CmsWorkplaceDefault.C_VFS_PATH_DEFAULT_INTERNAL + "HTMLFormDefs");
196         if (radioClass == null || "".equals(radioClass)) {
197             radiodef.setData(CmsWorkplaceDefault.C_RADIO_CLASS, "");
198         } else {
199             radiodef.setData(CmsWorkplaceDefault.C_RADIO_CLASSNAME, radioClass);
200             radiodef.setData(CmsWorkplaceDefault.C_RADIO_CLASS, radiodef.getProcessedData(CmsWorkplaceDefault.C_TAG_RADIO_CLASS));
201         }
202         for (int i = 0; i < numValues; i++) {
203
204             // Set values for this radiobutton entry
205
radiodef.setData(CmsWorkplaceDefault.C_RADIO_RADIONAME, radioName);
206             radiodef.setData(CmsWorkplaceDefault.C_RADIO_NAME, (String JavaDoc)names.elementAt(i));
207             radiodef.setData(CmsWorkplaceDefault.C_RADIO_LINK, (String JavaDoc)values.elementAt(i));
208
209             // Check, if this should be the preselected option
210
if (i == selectedOption) {
211                 radiodef.setData(CmsWorkplaceDefault.C_RADIO_SELECTEDENTRY, radiodef.getDataValue("radiobuttons." + CmsWorkplaceDefault.C_RADIO_SELECTEDOPTION));
212             } else {
213                 radiodef.setData(CmsWorkplaceDefault.C_RADIO_SELECTEDENTRY, "");
214             }
215
216             // Now get output for this option
217
if (radioOrder.equals("col")) {
218
219                 // Buttons should be displayed in one column
220
result.append(radiodef.getProcessedDataValue(CmsWorkplaceDefault.C_TAG_RADIO_COLENTRY, callingObject));
221             } else {
222
223                 // Buttons should be displayed in a row.
224
result.append(radiodef.getProcessedDataValue(CmsWorkplaceDefault.C_TAG_RADIO_ROWENTRY, callingObject));
225             }
226         }
227         return result.toString();
228     }
229
230     /**
231      * Handles any occurence of the special XML tag <code>&lt;SELECT&gt;</code> for
232      * generating HTML form select boxes.
233      * <P>
234      * The definition of a HTML selectbox will be taken from I_CmsWpConstants.C_VFS_PATH_DEFAULT_INTERNAL + "HTMLFormDefs".
235      * If the file is missing, this method will crash. Ensure this file is created and filled
236      * with all required XML tags.
237      * <P>
238      * Select boxes can be generated by adding the special XML tag
239      * <code>&lt;SELECT class="myClass" method="myMethod" name="myName" onchange="..." size="..."/&gt;</code>
240      * to the template file. This tag will be replaced with the correspondig select box
241      * while processing the template file. The <code>class</code> parameter is optional and
242      * The <code>method</code> parameter will be used to look up a user defined method
243      * in the template class assigned to the template file. This method should look like
244      * <br/>
245      * <code>public Integer myMethod(CmsObject cms, Vector names, Vector values, Hashtable parameters)</code><br/>
246      * and will be used to get the content of the requested select box group. The vectors <code>names</code>
247      * and <code>values</code> should be filled with the appropriate values. The return value should be an
248      * Integer containing the pre-selected option or -1, if no value should be pre-selected.
249      *
250      * @param n XML element containing the current special workplace tag
251      * @param callingObject reference to the calling object
252      * @param userObj hashtable containig all user parameters
253      * @return the definition of the select tag
254      * @throws CmsException if something goes wrong
255      */

256     public Object JavaDoc handleSelectTag(Element JavaDoc n, Object JavaDoc callingObject, Object JavaDoc userObj) throws CmsException {
257
258         Hashtable JavaDoc parameters = (Hashtable JavaDoc)userObj;
259
260         // Here the different select box options will be stored
261
Vector JavaDoc values = new Vector JavaDoc();
262         Vector JavaDoc names = new Vector JavaDoc();
263
264
265
266
267         // StringBuffer for the generated output *
268
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
269
270         // Read selectbox parameters
271
String JavaDoc selectClass = n.getAttribute(CmsWorkplaceDefault.C_SELECTBOX_CLASS);
272         String JavaDoc selectName = n.getAttribute(CmsWorkplaceDefault.C_SELECTBOX_NAME);
273         String JavaDoc selectMethod = n.getAttribute(CmsWorkplaceDefault.C_SELECTBOX_METHOD);
274         String JavaDoc selectWidth = n.getAttribute(CmsWorkplaceDefault.C_SELECTBOX_WIDTH);
275         String JavaDoc selectOnchange = n.getAttribute(CmsWorkplaceDefault.C_SELECTBOX_ONCHANGE);
276         String JavaDoc selectSize = n.getAttribute(CmsWorkplaceDefault.C_SELECTBOX_SIZE);
277         if ((selectSize == null) || (selectSize.length() == 0)) {
278             selectSize = "1";
279         }
280
281         // Get input definition file
282
CmsXmlTemplateFile inputdef = new CmsXmlTemplateFile(m_cms, CmsWorkplaceDefault.C_VFS_PATH_DEFAULT_INTERNAL + "HTMLFormDefs");
283
284         // Set the prefix string of the select box
285
if (selectClass == null || "".equals(selectClass)) {
286             inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_CLASS, "");
287         } else {
288             inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_CLASSNAME, selectClass);
289             inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_CLASS, inputdef.getProcessedData(CmsWorkplaceDefault.C_TAG_SELECTBOX_CLASS));
290         }
291         if (selectWidth == null || "".equals(selectWidth)) {
292             inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_WIDTH, "");
293         } else {
294             inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_WIDTHNAME, selectWidth);
295             inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_WIDTH, inputdef.getProcessedData(CmsWorkplaceDefault.C_TAG_SELECTBOX_WIDTH));
296         }
297         inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_NAME, selectName);
298         inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_ONCHANGE, selectOnchange);
299         inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_SIZE, selectSize);
300
301         // move the prefix string of the select box to the result StringBuffer
302
result.append(inputdef.getProcessedDataValue(CmsWorkplaceDefault.C_TAG_SELECTBOX_START));
303
304         // call the method for generating listbox elements
305
Method JavaDoc groupsMethod = null;
306         int selectedOption = 0;
307         try {
308             groupsMethod = callingObject.getClass().getMethod(selectMethod, new Class JavaDoc[] {
309                 CmsObject.class, Vector JavaDoc.class, Vector JavaDoc.class, Hashtable JavaDoc.class
310             });
311             selectedOption = ((Integer JavaDoc)groupsMethod.invoke(callingObject, new Object JavaDoc[] {
312                 m_cms, values, names, parameters
313             })).intValue();
314         } catch (NoSuchMethodException JavaDoc exc) {
315
316             // The requested method was not found.
317
throwException("Could not find method " + selectMethod + " in calling class " + callingObject.getClass().getName()
318                     + " for generating select box content.", CmsLegacyException.C_NOT_FOUND);
319         } catch (InvocationTargetException JavaDoc targetEx) {
320
321             // the method could be invoked, but throwed a exception
322
// itself. Get this exception and throw it again.
323
Throwable JavaDoc e = targetEx.getTargetException();
324             if (!(e instanceof CmsException)) {
325
326                 // Only print an error if this is NO CmsException
327
throwException("User method " + selectMethod + " in calling class " + callingObject.getClass().getName()
328                         + " throwed an exception. " + e, CmsLegacyException.C_UNKNOWN_EXCEPTION);
329             } else {
330
331                 // This is a CmsException
332
// Error printing should be done previously.
333
throw (CmsException)e;
334             }
335         } catch (Exception JavaDoc exc2) {
336             throwException("User method " + selectMethod + " in calling class " + callingObject.getClass().getName()
337                     + " was found but could not be invoked. " + exc2, CmsLegacyException.C_XML_NO_USER_METHOD);
338         }
339
340         // check the returned elements and put them into option tags.
341
// The element with index "selectedOption" has to get the "selected" tag.
342
int numValues = values.size();
343         for (int i = 0; i < numValues; i++) {
344             inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_OPTIONNAME, (String JavaDoc)names.elementAt(i));
345             inputdef.setData(CmsWorkplaceDefault.C_SELECTBOX_OPTIONVALUE, (String JavaDoc)values.elementAt(i));
346             if (i == selectedOption) {
347                 result.append(inputdef.getProcessedDataValue(CmsWorkplaceDefault.C_TAG_SELECTBOX_SELOPTION));
348             } else {
349                 result.append(inputdef.getProcessedDataValue(CmsWorkplaceDefault.C_TAG_SELECTBOX_OPTION));
350             }
351         }
352
353         // get the processed selectbox end sequence.
354
result.append(inputdef.getProcessedDataValue(CmsWorkplaceDefault.C_TAG_SELECTBOX_END));
355         return result.toString();
356     }
357
358     /**
359      * Registers the special tags for processing with
360      * processNode().
361      */

362     private void registerMyTags() {
363         super.registerTag("SELECT", CmsXmlFormTemplateFile.class, "handleSelectTag", C_REGISTER_MAIN_RUN);
364         super.registerTag("RADIOBUTTON", CmsXmlFormTemplateFile.class, "handleRadiobuttonTag", C_REGISTER_MAIN_RUN);
365         // just to be sure this is in the workplace view always aktiv
366
super.registerTag("ELEMENT", CmsXmlTemplateFile.class, "handleElementTag", C_REGISTER_MAIN_RUN);
367     }
368 }
369
Popular Tags