KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > intake > model > FieldFactory


1 package org.apache.fulcrum.intake.model;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.HashMap JavaDoc;
58 import java.util.Map JavaDoc;
59 import org.apache.fulcrum.intake.xmlmodel.XmlField;
60 import org.apache.fulcrum.ServiceException;
61
62 /**
63  * Creates Field objects.
64  *
65  * @author <a HREF="mailto:jmcnally@collab.net>John McNally</a>
66  * @version $Id: FieldFactory.java,v 1.1 2004/11/12 10:25:37 epugh Exp $
67  */

68 public abstract class FieldFactory
69 {
70     private static Map JavaDoc fieldCtors = initFieldCtors();
71
72     private static Map JavaDoc initFieldCtors()
73     {
74         fieldCtors = new HashMap JavaDoc();
75
76         fieldCtors.put("int", new FieldFactory.FieldCtor()
77             {
78                 public Field getInstance(XmlField f, Group g)
79                     throws Exception JavaDoc
80                 {
81                     return new IntegerField(f, g);
82                 }
83             }
84                        );
85         fieldCtors.put("boolean", new FieldFactory.FieldCtor()
86             {
87                 public Field getInstance(XmlField f, Group g)
88                     throws Exception JavaDoc
89                 {
90                     return new BooleanField(f, g);
91                 }
92             }
93                        );
94         fieldCtors.put("String", new FieldFactory.FieldCtor()
95             {
96                 public Field getInstance(XmlField f, Group g)
97                     throws Exception JavaDoc
98                 {
99                     return new StringField(f, g);
100                 }
101             }
102                        );
103         fieldCtors.put("long", new FieldFactory.FieldCtor()
104             {
105                 public Field getInstance(XmlField f, Group g)
106                     throws Exception JavaDoc
107                 {
108                     return new LongField(f, g);
109                 }
110             }
111                        );
112         fieldCtors.put("BigDecimal", new FieldFactory.FieldCtor()
113             {
114                 public Field getInstance(XmlField f, Group g)
115                     throws Exception JavaDoc
116                 {
117                     return new BigDecimalField(f, g);
118                 }
119             }
120                        );
121         fieldCtors.put("NumberKey", new FieldFactory.FieldCtor()
122             {
123                 public Field getInstance(XmlField f, Group g)
124                     throws Exception JavaDoc
125                 {
126                     return new NumberKeyField(f, g);
127                 }
128             }
129                        );
130         fieldCtors.put("ComboKey", new FieldFactory.FieldCtor()
131             {
132                 public Field getInstance(XmlField f, Group g)
133                     throws Exception JavaDoc
134                 {
135                     return new ComboKeyField(f, g);
136                 }
137             }
138                        );
139         fieldCtors.put("StringKey", new FieldFactory.FieldCtor()
140             {
141                 public Field getInstance(XmlField f, Group g)
142                     throws Exception JavaDoc
143                 {
144                     return new StringKeyField(f, g);
145                 }
146             }
147                        );
148         fieldCtors.put("FileItem", new FieldFactory.FieldCtor()
149             {
150                 public Field getInstance(XmlField f, Group g)
151                     throws Exception JavaDoc
152                 {
153                     return new FileItemField(f, g);
154                 }
155             }
156                        );
157         fieldCtors.put("DateString", new FieldFactory.FieldCtor()
158             {
159                 public Field getInstance(XmlField f, Group g)
160                     throws Exception JavaDoc
161                 {
162                     return new DateStringField(f, g);
163                 }
164             }
165                        );
166         fieldCtors.put("float", new FieldFactory.FieldCtor()
167             {
168                 public Field getInstance(XmlField f, Group g)
169                     throws Exception JavaDoc
170                 {
171                     return new FloatField(f, g);
172                 }
173             }
174                        );
175         return fieldCtors;
176     }
177
178     private static abstract class FieldCtor
179     {
180         public Field getInstance(XmlField f, Group g) throws Exception JavaDoc
181         {
182             return null;
183         }
184     }
185
186     /**
187      * Creates a Field object appropriate for the type specified
188      * in the xml file.
189      *
190      * @param f a <code>XmlField</code> value
191      * @return a <code>Field</code> value
192      */

193     public static final Field getInstance(XmlField f, Group g)
194         throws Exception JavaDoc
195     {
196         FieldCtor fieldCtor = null;
197         Field field = null;
198         String JavaDoc type = f.getType();
199
200         fieldCtor = (FieldCtor)fieldCtors.get(type);
201         if ( fieldCtor == null)
202         {
203             throw new ServiceException("Unsupported type: " + type);
204         }
205         else
206         {
207             field = fieldCtor.getInstance(f, g);
208         }
209         
210         return field;
211     }
212 }
213
Popular Tags