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      public static final String CHECK_DEPRECATION = "checkDeprecation";
32  
33      public static final String CHECK_DEPRECATION_VALUE = "false";
34  
35      public static final String PACKAGE = "package";
36  
37      public static final String PACKAGE_VALUE = "model";
38  
39      public static final String LIST = "java.util.List";
40  
41      public static final String LIST_VALUE = "new java.util.ArrayList<?>()";
42  
43      public static final String MAP = "java.util.Map";
44  
45      public static final String MAP_VALUE = "new java.util.HashMap()";
46  
47      public static final String PROPERTIES = "java.util.Properties";
48  
49      public static final String PROPERTIES_VALUE = "new java.util.Properties()";
50  
51      public static final String SET = "java.util.Set";
52  
53      public static final String SET_VALUE = "new java.util.HashSet<?>()";
54  
55      public static final String STRICT_XML_ATTRIBUTES = "strictXmlAttributes";
56  
57      public static final String STRICT_XML_ATTRIBUTES_VALUE = "true";
58  
59      private String key;
60  
61      private String value;
62  
63      public static ModelDefault getDefault(String key) throws ModelValidationException {
64          validateKey(key);
65  
66          ModelDefault modelDefault = new ModelDefault();
67  
68          modelDefault.setKey(key);
69  
70          if (CHECK_DEPRECATION.equalsIgnoreCase(key)) {
71              modelDefault.setValue(CHECK_DEPRECATION_VALUE);
72          } else if (PACKAGE.equalsIgnoreCase(key)) {
73              modelDefault.setValue(PACKAGE_VALUE);
74          } else if (LIST.equalsIgnoreCase(key)) {
75              modelDefault.setValue(LIST_VALUE);
76          } else if (MAP.equalsIgnoreCase(key)) {
77              modelDefault.setValue(MAP_VALUE);
78          } else if (PROPERTIES.equalsIgnoreCase(key)) {
79              modelDefault.setValue(PROPERTIES_VALUE);
80          } else if (SET.equalsIgnoreCase(key)) {
81              modelDefault.setValue(SET_VALUE);
82          } else if (STRICT_XML_ATTRIBUTES.equalsIgnoreCase(key)) {
83              modelDefault.setValue(STRICT_XML_ATTRIBUTES_VALUE);
84          }
85  
86          return modelDefault;
87      }
88  
89      public void setKey(String key) {
90          this.key = key;
91      }
92  
93      public String getKey() {
94          return key;
95      }
96  
97      public void setValue(String value) {
98          this.value = value;
99      }
100 
101     public String getValue() {
102         return value;
103     }
104 
105     public boolean getBoolean() {
106         return Boolean.valueOf(value).booleanValue();
107     }
108 
109     public void validateElement() throws ModelValidationException {
110         if (isEmpty(key)) {
111             throw new ModelValidationException("You must define the key of default element.");
112         }
113 
114         if (isEmpty(value)) {
115             throw new ModelValidationException("You must define the value of default element.");
116         }
117 
118         validateKey(key);
119     }
120 
121     private static void validateKey(String key) throws ModelValidationException {
122         if (!SET.equalsIgnoreCase(key)
123                 && !LIST.equalsIgnoreCase(key)
124                 && !MAP.equalsIgnoreCase(key)
125                 && !PROPERTIES.equalsIgnoreCase(key)
126                 && !CHECK_DEPRECATION.equalsIgnoreCase(key)
127                 && !PACKAGE.equalsIgnoreCase(key)
128                 && !STRICT_XML_ATTRIBUTES.equalsIgnoreCase(key)) {
129             throw new ModelValidationException("The key of default element must be ' " + SET + "', '" + LIST + "', '"
130                     + MAP + "', '" + PROPERTIES + "', '" + CHECK_DEPRECATION + "', '" + PACKAGE + "' or '"
131                     + STRICT_XML_ATTRIBUTES + "', was '" + key + "'.");
132         }
133     }
134 
135     protected boolean isEmpty(String string) {
136         return string == null || string.trim().length() == 0;
137     }
138 }