View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.apt.generate;
21  
22  import org.apache.commons.lang3.StringUtils;
23  
24  public class PropertyInfo {
25    private String name;
26    private String type;
27    private String[] allowedValues;
28    private String[] methodSignature;
29    private String defaultValue;
30    private String defaultCode;
31    private boolean valueExpressionRequired;
32    private boolean methodExpressionRequired;
33    private boolean literalOnly;
34    private boolean deprecated;
35    private boolean bodyContent;
36    private boolean tagAttribute;
37    private String description;
38    private boolean transientValue;
39    private boolean generate;
40  
41    public PropertyInfo() {
42    }
43  
44    public PropertyInfo(final String name) {
45      this.name = name;
46    }
47  
48    public boolean isBodyContent() {
49      return bodyContent;
50    }
51  
52    public void setBodyContent(final boolean bodyContent) {
53      this.bodyContent = bodyContent;
54    }
55  
56    public boolean isLiteralOnly() {
57      return literalOnly;
58    }
59  
60    public void setLiteralOnly(final boolean literalOnly) {
61      this.literalOnly = literalOnly;
62    }
63  
64    public String getTemplate() {
65      if (valueExpressionRequired) {
66        return "ValueExpression";
67      }
68      if (isMethodExpression()) {
69        return getUpperCamelCaseName();
70      }
71      return getShortTypeProperty();
72    }
73  
74    public String getName() {
75      return name;
76    }
77  
78    public void setName(final String name) {
79      this.name = name;
80    }
81  
82    public String getType() {
83      return type.replace("$", ".");
84    }
85  
86    public String getUnmodifiedType() {
87      return type;
88    }
89  
90    public String getInternalType() {
91      return "boolean".equals(type) ? Boolean.class.getName() : getType();
92    }
93  
94    public boolean isWidthOrHeight() {
95      return "width".equals(name) || "height".equals(name);
96    }
97  
98    public void setType(final String type) {
99      this.type = type;
100   }
101 
102   public boolean isMethodExpression() {
103     return "javax.el.MethodExpression".equals(type);
104   }
105 
106   public String getShortType() {
107     final String shortType = type.substring(type.lastIndexOf('.') + 1, type.length());
108     return shortType.replace("[]", "Array").replace("$", ".");
109   }
110 
111   public String getShortTypeProperty() {
112     final String shortType = getShortType();
113     final int index = shortType.lastIndexOf('.');
114     if (index != -1) {
115       return shortType.substring(shortType.lastIndexOf('.') + 1, shortType.length());
116     }
117     return shortType;
118   }
119 
120   public String getUpperCamelCaseName() {
121     return StringUtils.capitalize(name);
122   }
123 
124   public String getPropertyName() {
125     if (name.equals("for")) {
126       return "forComponent";
127     }
128     return name;
129   }
130 
131   public String[] getAllowedValues() {
132     return allowedValues;
133   }
134 
135   public void setAllowedValues(final String[] allowdValues) {
136     this.allowedValues = allowdValues;
137   }
138 
139   public String[] getMethodSignature() {
140     return methodSignature;
141   }
142 
143   public void setMethodSignature(final String[] methodSignature) {
144     this.methodSignature = methodSignature;
145   }
146 
147   public String getDefaultValue() {
148     return defaultValue;
149   }
150 
151   public void setDefaultValue(final String defaultValue) {
152     this.defaultValue = defaultValue;
153   }
154 
155   public void setDeprecated(final boolean deprecated) {
156     this.deprecated = deprecated;
157   }
158 
159   public boolean isDeprecated() {
160     return deprecated;
161   }
162 
163   public String getDefaultCode() {
164     if (defaultCode == null && defaultValue != null) {
165       if (String.class.getName().equals(type)) {
166         return "\"" + defaultValue + "\"";
167       } else if (Character.class.getName().equals(type)) {
168         return "'" + defaultValue + "'";
169       } else {
170         return defaultValue;
171       }
172     }
173     return defaultCode;
174   }
175 
176   public void setDefaultCode(final String defaultCode) {
177     this.defaultCode = defaultCode;
178   }
179 
180   public boolean isValueExpressionRequired() {
181     return valueExpressionRequired;
182   }
183 
184   public void setValueExpressionRequired(final boolean valueExpressionRequired) {
185     this.valueExpressionRequired = valueExpressionRequired;
186   }
187 
188   public boolean isMethodExpressionRequired() {
189     return methodExpressionRequired;
190   }
191 
192   public void setMethodExpressionRequired(final boolean methodExpressionRequired) {
193     this.methodExpressionRequired = methodExpressionRequired;
194   }
195 
196   public boolean isTagAttribute() {
197     return tagAttribute;
198   }
199 
200   public void setTagAttribute(final boolean tagAttribute) {
201     this.tagAttribute = tagAttribute;
202   }
203 
204   public PropertyInfo fill(final PropertyInfo info) {
205     info.setName(name);
206     info.setType(type);
207     info.setAllowedValues(allowedValues);
208     info.setDefaultValue(defaultValue);
209     info.setDeprecated(deprecated);
210     info.setMethodSignature(methodSignature);
211     info.setDefaultCode(defaultCode);
212     info.setValueExpressionRequired(valueExpressionRequired);
213     info.setLiteralOnly(literalOnly);
214     info.setMethodExpressionRequired(methodExpressionRequired);
215     info.setTagAttribute(tagAttribute);
216     info.setDescription(description);
217     info.setTransient(transientValue);
218     return info;
219   }
220 
221   public boolean equals(final Object o) {
222     if (this == o) {
223       return true;
224     }
225     if (!(o instanceof PropertyInfo)) {
226       return false;
227     }
228 
229     final PropertyInfo that = (PropertyInfo) o;
230 
231     return name.equals(that.name);
232 
233   }
234 
235   public int hashCode() {
236     return name.hashCode();
237   }
238 
239   public void setDescription(final String description) {
240     this.description = description;
241   }
242 
243   public String getDescription() {
244     return description;
245   }
246 
247   public boolean isTransient() {
248     return transientValue;
249   }
250 
251   public void setTransient(final boolean transientValueParameter) {
252     this.transientValue = transientValueParameter;
253   }
254 
255   public boolean isGenerate() {
256     return generate;
257   }
258 
259   public void setGenerate(final boolean generate) {
260     this.generate = generate;
261   }
262 }