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
32      extends BaseElement
33  {
34      private String type;
35  
36      private String defaultValue;
37  
38      private String typeValidator;
39  
40      private boolean required;
41  
42      private boolean identifier;
43  
44      private String alias;
45  
46      private transient ModelClass modelClass;
47  
48      private static final String[] PRIMITIVE_TYPES =
49          { "boolean", "Boolean", "char", "Character", "byte", "Byte", "short", "Short", "int", "Integer", "long",
50              "Long", "float", "Float", "double", "Double", "String", "Date", "DOM" };
51  
52      public ModelField()
53      {
54          super( true );
55      }
56  
57      public ModelField( ModelClass modelClass, String name )
58      {
59          super( true, name );
60  
61          this.modelClass = modelClass;
62      }
63  
64      // ----------------------------------------------------------------------
65      // Property accessors
66      // ----------------------------------------------------------------------
67  
68      public String getType()
69      {
70          return type;
71      }
72  
73      public void setType( String type )
74      {
75          this.type = type;
76      }
77  
78      public String getDefaultValue()
79      {
80          return defaultValue;
81      }
82  
83      public void setDefaultValue( String defaultValue )
84      {
85          this.defaultValue = defaultValue;
86      }
87  
88      public String getTypeValidator()
89      {
90          return typeValidator;
91      }
92  
93      public void setTypeValidator( String typeValidator )
94      {
95          this.typeValidator = typeValidator;
96      }
97  
98      public boolean isRequired()
99      {
100         return required;
101     }
102 
103     public void setRequired( boolean required )
104     {
105         this.required = required;
106     }
107 
108     public boolean isIdentifier()
109     {
110         return identifier;
111     }
112 
113     public void setIdentifier( boolean identifier )
114     {
115         this.identifier = identifier;
116     }
117 
118     public String getAlias()
119     {
120         return alias;
121     }
122 
123     public void setAlias( String alias )
124     {
125         this.alias = alias;
126     }
127     // ----------------------------------------------------------------------
128     // Misc
129     // ----------------------------------------------------------------------
130 
131     public ModelClass getModelClass()
132     {
133         return modelClass;
134     }
135 
136     public FieldMetadata getMetadata( String key )
137     {
138         return getMetadata( FieldMetadata.class, key );
139     }
140 
141     public boolean isPrimitive()
142     {
143         String type = getType();
144 
145         // TODO: This should not happen
146         if ( type == null )
147         {
148             return false;
149         }
150 
151         for ( String validType : PRIMITIVE_TYPES )
152         {
153             if ( type.equals( validType ) )
154             {
155                 return true;
156             }
157         }
158 
159         return false;
160     }
161 
162     public boolean isArray()
163     {
164         return getType().endsWith( "[]" );
165     }
166 
167     public boolean isPrimitiveArray()
168     {
169         String type = getType();
170 
171         for ( String PRIMITIVE_TYPE : PRIMITIVE_TYPES )
172         {
173             String validType = PRIMITIVE_TYPE + "[]";
174 
175             if ( validType.equals( type ) )
176             {
177                 return true;
178             }
179         }
180 
181         return false;
182     }
183 
184     // ----------------------------------------------------------------------
185     // BaseElement Overrides
186     // ----------------------------------------------------------------------
187 
188     public void initialize( ModelClass modelClass )
189     {
190         this.modelClass = modelClass;
191 
192         if ( defaultValue == null )
193         {
194             if ( "boolean".equals( type ) )
195             {
196                 defaultValue = "false";
197             }
198             else if ( "float".equals( type ) || "double".equals( type ) )
199             {
200                 defaultValue = "0.0";
201             }
202             else if ( "int".equals( type ) || "long".equals( type ) || "short".equals( type ) || "byte".equals( type ) )
203             {
204                 defaultValue = "0";
205             }
206             else if ( "char".equals( type ) )
207             {
208                 defaultValue = "\0";
209             }
210         }
211     }
212 
213     public void validateElement()
214         throws ModelValidationException
215     {
216         validateFieldNotEmpty( "field", "name", getName() );
217 
218         validateFieldNotEmpty( "field '" + getName() + "'", "type", type );
219 
220         // TODO: these definitions are duplicated throughout. Defined centrally, and loop through in the various uses
221 
222         if ( !isPrimitive() && !isPrimitiveArray() )
223         {
224             throw new ModelValidationException( "Field '" + getName() + "': Illegal type: '" + type + "'." );
225         }
226     }
227 
228     // ----------------------------------------------------------------------
229     // Object Overrides
230     // ----------------------------------------------------------------------
231 
232     public String toString()
233     {
234         return "[Field: name=" + getName() + ", alias: " + alias + ", type: " + type + ", " + "version: "
235             + getVersionRange() + "]";
236     }
237 
238     public boolean isModelVersionField()
239     {
240         Model model = modelClass.getModel();
241         VersionDefinition versionDefinition = model.getVersionDefinition();
242 
243         return ( versionDefinition != null ) && versionDefinition.isFieldType()
244             && ( versionDefinition.getValue().equals( getName() ) || versionDefinition.getValue().equals( alias ) );
245     }
246 }