001: package org.apache.turbine.services.intake.model;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.lang.reflect.Constructor;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.apache.turbine.services.intake.IntakeException;
027: import org.apache.turbine.services.intake.xmlmodel.XmlField;
028:
029: /**
030: * Creates Field objects.
031: *
032: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
033: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
034: * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
035: * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
036: * @version $Id: FieldFactory.java 534527 2007-05-02 16:10:59Z tv $
037: */
038: public abstract class FieldFactory {
039: private static Map fieldCtors = initFieldCtors();
040:
041: private static Map initFieldCtors() {
042: fieldCtors = new HashMap();
043:
044: fieldCtors.put("int", new FieldFactory.FieldCtor() {
045: public Field getInstance(XmlField f, Group g)
046: throws IntakeException {
047: return new IntegerField(f, g);
048: }
049: });
050: fieldCtors.put("boolean", new FieldFactory.FieldCtor() {
051: public Field getInstance(XmlField f, Group g)
052: throws IntakeException {
053: return new BooleanField(f, g);
054: }
055: });
056: fieldCtors.put("String", new FieldFactory.FieldCtor() {
057: public Field getInstance(XmlField f, Group g)
058: throws IntakeException {
059: return new StringField(f, g);
060: }
061: });
062: fieldCtors.put("BigDecimal", new FieldFactory.FieldCtor() {
063: public Field getInstance(XmlField f, Group g)
064: throws IntakeException {
065: return new BigDecimalField(f, g);
066: }
067: });
068: fieldCtors.put("NumberKey", new FieldFactory.FieldCtor() {
069: public Field getInstance(XmlField f, Group g)
070: throws IntakeException {
071: return new NumberKeyField(f, g);
072: }
073: });
074: fieldCtors.put("ComboKey", new FieldFactory.FieldCtor() {
075: public Field getInstance(XmlField f, Group g)
076: throws IntakeException {
077: return new ComboKeyField(f, g);
078: }
079: });
080: fieldCtors.put("StringKey", new FieldFactory.FieldCtor() {
081: public Field getInstance(XmlField f, Group g)
082: throws IntakeException {
083: return new StringKeyField(f, g);
084: }
085: });
086: fieldCtors.put("FileItem", new FieldFactory.FieldCtor() {
087: public Field getInstance(XmlField f, Group g)
088: throws IntakeException {
089: return new FileItemField(f, g);
090: }
091: });
092: fieldCtors.put("DateString", new FieldFactory.FieldCtor() {
093: public Field getInstance(XmlField f, Group g)
094: throws IntakeException {
095: return new DateStringField(f, g);
096: }
097: });
098: fieldCtors.put("float", new FieldFactory.FieldCtor() {
099: public Field getInstance(XmlField f, Group g)
100: throws IntakeException {
101: return new FloatField(f, g);
102: }
103: });
104: fieldCtors.put("double", new FieldFactory.FieldCtor() {
105: public Field getInstance(XmlField f, Group g)
106: throws IntakeException {
107: return new DoubleField(f, g);
108: }
109: });
110: fieldCtors.put("short", new FieldFactory.FieldCtor() {
111: public Field getInstance(XmlField f, Group g)
112: throws IntakeException {
113: return new ShortField(f, g);
114: }
115: });
116: fieldCtors.put("long", new FieldFactory.FieldCtor() {
117: public Field getInstance(XmlField f, Group g)
118: throws IntakeException {
119: return new LongField(f, g);
120: }
121: });
122: fieldCtors.put("custom", new FieldFactory.FieldCtor() {
123: public Field getInstance(XmlField f, Group g)
124: throws IntakeException {
125: String fieldClass = f.getFieldClass();
126:
127: if (fieldClass != null && fieldClass.indexOf('.') == -1) {
128: fieldClass = Field.defaultFieldPackage + fieldClass;
129: }
130:
131: if (fieldClass != null) {
132: Class field;
133:
134: try {
135: field = Class.forName(fieldClass);
136: Constructor constructor = field
137: .getConstructor(new Class[] {
138: XmlField.class, Group.class });
139:
140: return (Field) constructor
141: .newInstance(new Object[] { f, g });
142: } catch (ClassNotFoundException e) {
143: throw new IntakeException(
144: "Could not load Field class("
145: + fieldClass + ")", e);
146: } catch (Exception e) {
147: throw new IntakeException(
148: "Could not create new instance of Field("
149: + fieldClass + ")", e);
150: }
151: } else {
152: throw new IntakeException(
153: "Custom field types must define a fieldClass");
154: }
155: }
156: });
157: return fieldCtors;
158: }
159:
160: private static abstract class FieldCtor {
161: public Field getInstance(XmlField f, Group g)
162: throws IntakeException {
163: return null;
164: }
165: }
166:
167: /**
168: * Creates a Field object appropriate for the type specified
169: * in the xml file.
170: *
171: * @param xmlField a <code>XmlField</code> value
172: * @return a <code>Field</code> value
173: * @throws IntakeException indicates that an unknown type was specified for a field.
174: */
175: public static final Field getInstance(XmlField xmlField,
176: Group xmlGroup) throws IntakeException {
177: FieldCtor fieldCtor = null;
178: Field field = null;
179: String type = xmlField.getType();
180:
181: fieldCtor = (FieldCtor) fieldCtors.get(type);
182: if (fieldCtor == null) {
183: throw new IntakeException(
184: "An Unsupported type has been specified for "
185: + xmlField.getName() + " in group "
186: + xmlGroup.getIntakeGroupName()
187: + " type = " + type);
188: } else {
189: field = fieldCtor.getInstance(xmlField, xmlGroup);
190: }
191:
192: return field;
193: }
194: }
|