KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > form > CmsFieldFactory


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/form/CmsFieldFactory.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.5 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2004 Alkacon Software (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, 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.form;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.main.OpenCms;
36
37 import java.io.File JavaDoc;
38 import java.io.FileInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import org.apache.commons.collections.ExtendedProperties;
45 import org.apache.commons.logging.Log;
46
47 /**
48  * A factory to create form field instances of a specified type.<p>
49  *
50  * @author Thomas Weckert (t.weckert@alkacon.com)
51  * @version $Revision: 1.5 $
52  */

53 public final class CmsFieldFactory {
54
55     /** Filename of the optional custom form field properties. */
56     public static final String JavaDoc CUSTOM_FORM_FIELD_PROPERTIES = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
57         "classes" + File.separatorChar + "custom_form_field.properties");
58
59     /** The log object for this class. */
60     private static final Log LOG = CmsLog.getLog(CmsFieldFactory.class);
61
62     /** The shared instance of the field factory. */
63     private static CmsFieldFactory sharedInstance = null;
64
65     /** The registered field types keyed by their type name. */
66     private Map JavaDoc m_registeredFieldTypes;
67
68     /**
69      * Default constructor.<p>
70      */

71     private CmsFieldFactory() {
72
73         super();
74
75         m_registeredFieldTypes = new HashMap JavaDoc();
76
77         // register all the standard OpenCms field types
78
registerFieldType(CmsCheckboxField.getStaticType(), CmsCheckboxField.class.getName());
79         registerFieldType(CmsEmailField.getStaticType(), CmsEmailField.class.getName());
80         registerFieldType(CmsHiddenField.getStaticType(), CmsHiddenField.class.getName());
81         registerFieldType(CmsRadioButtonField.getStaticType(), CmsRadioButtonField.class.getName());
82         registerFieldType(CmsSelectionField.getStaticType(), CmsSelectionField.class.getName());
83         registerFieldType(CmsTextField.getStaticType(), CmsTextField.class.getName());
84         registerFieldType(CmsTextareaField.getStaticType(), CmsTextareaField.class.getName());
85
86         File JavaDoc propertyFile = null;
87         try {
88
89             // register all custom field types declared in a property file.
90
// since custom fields are optional, the property file doesn't have to exist necessarily in the file system.
91
// this file should contain a mapping of field type names to a Java classes separated by a colo ":", e.g.:
92
// FIELDS=<fieldtype>:<java class>,...,<fieldtype>:<java class>
93

94             propertyFile = new File JavaDoc(CUSTOM_FORM_FIELD_PROPERTIES);
95             if (propertyFile.exists()) {
96
97                 ExtendedProperties fieldProperties = new ExtendedProperties();
98                 fieldProperties.load(new FileInputStream JavaDoc(propertyFile));
99
100                 Iterator JavaDoc i = fieldProperties.keySet().iterator();
101                 while (i.hasNext()) {
102
103                     String JavaDoc key = (String JavaDoc)i.next();
104                     if (!"FIELDS".equalsIgnoreCase(key)) {
105                         continue;
106                     }
107
108                     String JavaDoc[] values = fieldProperties.getStringArray(key);
109                     if (values == null || values.length == 0) {
110                         continue;
111                     }
112
113                     for (int j = 0, n = values.length; j < n; j++) {
114
115                         String JavaDoc field = values[j];
116                         int index = field.indexOf(":");
117                         if (index == -1) {
118                             continue;
119                         }
120
121                         String JavaDoc fieldType = field.substring(0, index);
122                         String JavaDoc fieldClass = field.substring(index + 1, field.length());
123                         registerFieldType(fieldType, fieldClass);
124                     }
125                 }
126             }
127         } catch (IOException JavaDoc e) {
128
129             if (LOG.isErrorEnabled()) {
130                 LOG.error(Messages.get().getBundle().key(
131                     Messages.LOG_ERR_READING_CUSTOM_FORM_FIELD_PROPERTIES_1,
132                     propertyFile.getAbsolutePath()), e);
133             }
134         }
135     }
136
137     /**
138      * @see java.lang.Object#finalize()
139      */

140     protected void finalize() throws Throwable JavaDoc {
141
142         try {
143
144             if (m_registeredFieldTypes != null) {
145                 m_registeredFieldTypes.clear();
146             }
147
148             m_registeredFieldTypes = null;
149         } catch (Throwable JavaDoc t) {
150             // ignore
151
}
152
153         super.finalize();
154     }
155
156     /**
157      * Returns the shared instance of the field factory.<p>
158      *
159      * @return the shared instance of the field factory
160      */

161     public static synchronized CmsFieldFactory getSharedInstance() {
162
163         if (sharedInstance == null) {
164             sharedInstance = new CmsFieldFactory();
165         }
166
167         return sharedInstance;
168     }
169
170     /**
171      * Registers a class as a field type in the factory.<p>
172      *
173      * @param type the type of the field
174      * @param className the name of the field class
175      * @return the previous class associated with this type, or null if there was no mapping before
176      */

177     private Object JavaDoc registerFieldType(String JavaDoc type, String JavaDoc className) {
178
179         return m_registeredFieldTypes.put(type, className);
180     }
181
182     /**
183      * Returns an instance of a form field of the specified type.<p>
184      *
185      * @param type the desired type of the form field.
186      * @return the instance of a form field, or null if creating an instance of the class failed
187      */

188     protected A_CmsField getField(String JavaDoc type) {
189
190         A_CmsField field = null;
191
192         try {
193
194             String JavaDoc className = (String JavaDoc)m_registeredFieldTypes.get(type);
195             field = (A_CmsField)Class.forName(className).newInstance();
196         } catch (Throwable JavaDoc t) {
197
198             if (LOG.isErrorEnabled()) {
199                 LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_FIELD_INSTANTIATION_1, type), t);
200             }
201         }
202
203         return field;
204     }
205 }
206
Popular Tags