View Javadoc

1   package org.apache.turbine.services.intake.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Constructor;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.turbine.services.intake.IntakeException;
27  import org.apache.turbine.services.intake.xmlmodel.XmlField;
28  
29  /***
30   * Creates Field objects.
31   *
32   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
33   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
34   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
35   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
36   * @version $Id: FieldFactory.java 534527 2007-05-02 16:10:59Z tv $
37   */
38  public abstract class FieldFactory
39  {
40      private static Map fieldCtors = initFieldCtors();
41  
42      private static Map initFieldCtors()
43      {
44          fieldCtors = new HashMap();
45  
46          fieldCtors.put("int", new FieldFactory.FieldCtor()
47          {
48              public Field getInstance(XmlField f, Group g)
49                      throws IntakeException
50              {
51                  return new IntegerField(f, g);
52              }
53          }
54          );
55          fieldCtors.put("boolean", new FieldFactory.FieldCtor()
56          {
57              public Field getInstance(XmlField f, Group g)
58                      throws IntakeException
59              {
60                  return new BooleanField(f, g);
61              }
62          }
63          );
64          fieldCtors.put("String", new FieldFactory.FieldCtor()
65          {
66              public Field getInstance(XmlField f, Group g)
67                      throws IntakeException
68              {
69                  return new StringField(f, g);
70              }
71          }
72          );
73          fieldCtors.put("BigDecimal", new FieldFactory.FieldCtor()
74          {
75              public Field getInstance(XmlField f, Group g)
76                      throws IntakeException
77              {
78                  return new BigDecimalField(f, g);
79              }
80          }
81          );
82          fieldCtors.put("NumberKey", new FieldFactory.FieldCtor()
83          {
84              public Field getInstance(XmlField f, Group g)
85                      throws IntakeException
86              {
87                  return new NumberKeyField(f, g);
88              }
89          }
90          );
91          fieldCtors.put("ComboKey", new FieldFactory.FieldCtor()
92          {
93              public Field getInstance(XmlField f, Group g)
94                      throws IntakeException
95              {
96                  return new ComboKeyField(f, g);
97              }
98          }
99          );
100         fieldCtors.put("StringKey", new FieldFactory.FieldCtor()
101         {
102             public Field getInstance(XmlField f, Group g)
103                     throws IntakeException
104             {
105                 return new StringKeyField(f, g);
106             }
107         }
108         );
109         fieldCtors.put("FileItem", new FieldFactory.FieldCtor()
110         {
111             public Field getInstance(XmlField f, Group g)
112                     throws IntakeException
113             {
114                 return new FileItemField(f, g);
115             }
116         }
117         );
118         fieldCtors.put("DateString", new FieldFactory.FieldCtor()
119         {
120             public Field getInstance(XmlField f, Group g)
121                     throws IntakeException
122             {
123                 return new DateStringField(f, g);
124             }
125         }
126         );
127         fieldCtors.put("float", new FieldFactory.FieldCtor()
128         {
129             public Field getInstance(XmlField f, Group g)
130                     throws IntakeException
131             {
132                 return new FloatField(f, g);
133             }
134         }
135         );
136         fieldCtors.put("double", new FieldFactory.FieldCtor()
137         {
138             public Field getInstance(XmlField f, Group g)
139                     throws IntakeException
140             {
141                 return new DoubleField(f, g);
142             }
143         }
144         );
145         fieldCtors.put("short", new FieldFactory.FieldCtor()
146         {
147             public Field getInstance(XmlField f, Group g)
148                     throws IntakeException
149             {
150                 return new ShortField(f, g);
151             }
152         }
153         );
154         fieldCtors.put("long", new FieldFactory.FieldCtor()
155         {
156             public Field getInstance(XmlField f, Group g)
157                     throws IntakeException
158             {
159                 return new LongField(f, g);
160             }
161         }
162         );
163         fieldCtors.put("custom", new FieldFactory.FieldCtor()
164         {
165             public Field getInstance(XmlField f, Group g)
166                     throws IntakeException
167             {
168                 String fieldClass = f.getFieldClass();
169 
170                 if (fieldClass != null
171                         && fieldClass.indexOf('.') == -1)
172                 {
173                     fieldClass = Field.defaultFieldPackage + fieldClass;
174                 }
175 
176                 if (fieldClass != null)
177                 {
178                     Class field;
179 
180                     try
181                     {
182                         field = Class.forName(fieldClass);
183                         Constructor constructor =
184                             field.getConstructor(new Class[] { XmlField.class, Group.class });
185 
186                         return (Field)constructor.newInstance(new Object[] { f, g });
187                     }
188                     catch (ClassNotFoundException e)
189                     {
190                         throw new IntakeException(
191                                 "Could not load Field class("
192                                 + fieldClass + ")", e);
193                     }
194                     catch (Exception e)
195                     {
196                         throw new IntakeException(
197                                 "Could not create new instance of Field("
198                                 + fieldClass + ")", e);
199                     }
200                 }
201                 else
202                 {
203                     throw new IntakeException(
204                             "Custom field types must define a fieldClass");
205                 }
206             }
207         }
208         );
209         return fieldCtors;
210     }
211 
212     private static abstract class FieldCtor
213     {
214         public Field getInstance(XmlField f, Group g) throws IntakeException
215         {
216             return null;
217         }
218     }
219 
220     /***
221      * Creates a Field object appropriate for the type specified
222      * in the xml file.
223      *
224      * @param xmlField a <code>XmlField</code> value
225      * @return a <code>Field</code> value
226      * @throws IntakeException indicates that an unknown type was specified for a field.
227      */
228     public static final Field getInstance(XmlField xmlField, Group xmlGroup)
229             throws IntakeException
230     {
231         FieldCtor fieldCtor = null;
232         Field field = null;
233         String type = xmlField.getType();
234 
235         fieldCtor = (FieldCtor) fieldCtors.get(type);
236         if (fieldCtor == null)
237         {
238             throw new IntakeException("An Unsupported type has been specified for " +
239                     xmlField.getName() + " in group " + xmlGroup.getIntakeGroupName() + " type = " + type);
240         }
241         else
242         {
243             field = fieldCtor.getInstance(xmlField, xmlGroup);
244         }
245 
246         return field;
247     }
248 }