View Javadoc
1   package org.codehaus.modello.model;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import org.codehaus.modello.metadata.FieldMetadata;
26  
27  /**
28   * @author <a href="mailto:jason@modello.org">Jason van Zyl </a>
29   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse </a>
30   */
31  public class ModelField extends BaseElement {
32      private String type;
33  
34      private String defaultValue;
35  
36      private String typeValidator;
37  
38      private boolean required;
39  
40      private boolean identifier;
41  
42      private String alias;
43  
44      private transient ModelClass modelClass;
45  
46      private static final String[] PRIMITIVE_TYPES = {
47          "boolean",
48          "Boolean",
49          "char",
50          "Character",
51          "byte",
52          "Byte",
53          "short",
54          "Short",
55          "int",
56          "Integer",
57          "long",
58          "Long",
59          "float",
60          "Float",
61          "double",
62          "Double",
63          "String",
64          "Date",
65          "DOM"
66      };
67  
68      public ModelField() {
69          super(true);
70      }
71  
72      public ModelField(ModelClass modelClass, String name) {
73          super(true, name);
74  
75          this.modelClass = modelClass;
76      }
77  
78      // ----------------------------------------------------------------------
79      // Property accessors
80      // ----------------------------------------------------------------------
81  
82      public String getType() {
83          return type;
84      }
85  
86      public void setType(String type) {
87          this.type = type;
88      }
89  
90      public String getDefaultValue() {
91          return defaultValue;
92      }
93  
94      public void setDefaultValue(String defaultValue) {
95          this.defaultValue = defaultValue;
96      }
97  
98      public String getTypeValidator() {
99          return typeValidator;
100     }
101 
102     public void setTypeValidator(String typeValidator) {
103         this.typeValidator = typeValidator;
104     }
105 
106     public boolean isRequired() {
107         return required;
108     }
109 
110     public void setRequired(boolean required) {
111         this.required = required;
112     }
113 
114     public boolean isIdentifier() {
115         return identifier;
116     }
117 
118     public void setIdentifier(boolean identifier) {
119         this.identifier = identifier;
120     }
121 
122     public String getAlias() {
123         return alias;
124     }
125 
126     public void setAlias(String alias) {
127         this.alias = alias;
128     }
129     // ----------------------------------------------------------------------
130     // Misc
131     // ----------------------------------------------------------------------
132 
133     public ModelClass getModelClass() {
134         return modelClass;
135     }
136 
137     public FieldMetadata getMetadata(String key) {
138         return getMetadata(FieldMetadata.class, key);
139     }
140 
141     public boolean isPrimitive() {
142         String type = getType();
143 
144         // TODO: This should not happen
145         if (type == null) {
146             return false;
147         }
148 
149         for (String validType : PRIMITIVE_TYPES) {
150             if (type.equals(validType)) {
151                 return true;
152             }
153         }
154 
155         return false;
156     }
157 
158     public boolean isArray() {
159         return getType().endsWith("[]");
160     }
161 
162     public boolean isPrimitiveArray() {
163         String type = getType();
164 
165         for (String PRIMITIVE_TYPE : PRIMITIVE_TYPES) {
166             String validType = PRIMITIVE_TYPE + "[]";
167 
168             if (validType.equals(type)) {
169                 return true;
170             }
171         }
172 
173         return false;
174     }
175 
176     // ----------------------------------------------------------------------
177     // BaseElement Overrides
178     // ----------------------------------------------------------------------
179 
180     public void initialize(ModelClass modelClass) {
181         this.modelClass = modelClass;
182 
183         if (defaultValue == null) {
184             if ("boolean".equals(type)) {
185                 defaultValue = "false";
186             } else if ("float".equals(type) || "double".equals(type)) {
187                 defaultValue = "0.0";
188             } else if ("int".equals(type) || "long".equals(type) || "short".equals(type) || "byte".equals(type)) {
189                 defaultValue = "0";
190             } else if ("char".equals(type)) {
191                 defaultValue = "\0";
192             }
193         }
194     }
195 
196     public void validateElement() throws ModelValidationException {
197         validateFieldNotEmpty("field", "name", getName());
198 
199         validateFieldNotEmpty("field '" + getName() + "'", "type", type);
200 
201         // TODO: these definitions are duplicated throughout. Defined centrally, and loop through in the various uses
202 
203         if (!isPrimitive() && !isPrimitiveArray()) {
204             throw new ModelValidationException("Field '" + getName() + "': Illegal type: '" + type + "'.");
205         }
206     }
207 
208     // ----------------------------------------------------------------------
209     // Object Overrides
210     // ----------------------------------------------------------------------
211 
212     public String toString() {
213         return "[Field: name=" + getName() + ", alias: " + alias + ", type: " + type + ", " + "version: "
214                 + getVersionRange() + "]";
215     }
216 
217     public boolean isModelVersionField() {
218         Model model = modelClass.getModel();
219         VersionDefinition versionDefinition = model.getVersionDefinition();
220 
221         return (versionDefinition != null)
222                 && versionDefinition.isFieldType()
223                 && (versionDefinition.getValue().equals(getName())
224                         || versionDefinition.getValue().equals(alias));
225     }
226 }