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  /**
26   * Default values for a model, that can be overrided with <code>defaults</code> element of the model descriptor.
27   *
28   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
29   */
30  public class ModelDefault
31  {
32      public static final String CHECK_DEPRECATION = "checkDeprecation";
33  
34      public static final String CHECK_DEPRECATION_VALUE = "false";
35  
36      public static final String PACKAGE = "package";
37  
38      public static final String PACKAGE_VALUE = "model";
39  
40      public static final String LIST = "java.util.List";
41  
42      public static final String LIST_VALUE = "new java.util.ArrayList<?>()";
43  
44      public static final String MAP = "java.util.Map";
45  
46      public static final String MAP_VALUE = "new java.util.HashMap()";
47  
48      public static final String PROPERTIES = "java.util.Properties";
49  
50      public static final String PROPERTIES_VALUE = "new java.util.Properties()";
51  
52      public static final String SET = "java.util.Set";
53  
54      public static final String SET_VALUE = "new java.util.HashSet<?>()";
55  
56      public static final String STRICT_XML_ATTRIBUTES = "strictXmlAttributes";
57  
58      public static final String STRICT_XML_ATTRIBUTES_VALUE = "true";
59  
60      private String key;
61  
62      private String value;
63  
64      public static ModelDefault getDefault( String key )
65          throws ModelValidationException
66      {
67          validateKey( key );
68  
69          ModelDefault modelDefault = new ModelDefault();
70  
71          modelDefault.setKey( key );
72  
73          if ( CHECK_DEPRECATION.equalsIgnoreCase( key ) )
74          {
75              modelDefault.setValue( CHECK_DEPRECATION_VALUE );
76          }
77          else if ( PACKAGE.equalsIgnoreCase( key ) )
78          {
79              modelDefault.setValue( PACKAGE_VALUE );
80          }
81          else if ( LIST.equalsIgnoreCase( key ) )
82          {
83              modelDefault.setValue( LIST_VALUE );
84          }
85          else if ( MAP.equalsIgnoreCase( key ) )
86          {
87              modelDefault.setValue( MAP_VALUE );
88          }
89          else if ( PROPERTIES.equalsIgnoreCase( key ) )
90          {
91              modelDefault.setValue( PROPERTIES_VALUE );
92          }
93          else if ( SET.equalsIgnoreCase( key ) )
94          {
95              modelDefault.setValue( SET_VALUE );
96          }
97          else if ( STRICT_XML_ATTRIBUTES.equalsIgnoreCase( key ) )
98          {
99              modelDefault.setValue( STRICT_XML_ATTRIBUTES_VALUE );
100         }
101 
102         return modelDefault;
103     }
104 
105     public void setKey( String key )
106     {
107         this.key = key;
108     }
109 
110     public String getKey()
111     {
112         return key;
113     }
114 
115     public void setValue( String value )
116     {
117         this.value = value;
118     }
119 
120     public String getValue()
121     {
122         return value;
123     }
124 
125     public boolean getBoolean()
126     {
127         return Boolean.valueOf( value ).booleanValue();
128     }
129 
130     public void validateElement()
131         throws ModelValidationException
132     {
133         if ( isEmpty( key ) )
134         {
135             throw new ModelValidationException( "You must define the key of default element." );
136         }
137 
138         if ( isEmpty( value ) )
139         {
140             throw new ModelValidationException( "You must define the value of default element." );
141         }
142 
143         validateKey( key );
144     }
145 
146     private static void validateKey( String key )
147         throws ModelValidationException
148     {
149         if ( ! SET.equalsIgnoreCase( key )
150              && ! LIST.equalsIgnoreCase( key )
151              && ! MAP.equalsIgnoreCase( key )
152              && ! PROPERTIES.equalsIgnoreCase( key )
153              && ! CHECK_DEPRECATION.equalsIgnoreCase( key )
154              && ! PACKAGE.equalsIgnoreCase( key )
155              && ! STRICT_XML_ATTRIBUTES.equalsIgnoreCase( key ) )
156         {
157             throw new ModelValidationException( "The key of default element must be ' " + SET + "', '" + LIST + "', '"
158                 + MAP + "', '" + PROPERTIES + "', '" + CHECK_DEPRECATION + "', '" + PACKAGE + "' or '"
159                 + STRICT_XML_ATTRIBUTES + "', was '" + key + "'." );
160         }
161     }
162 
163     protected boolean isEmpty( String string )
164     {
165         return string == null || string.trim().length() == 0;
166     }
167 }