View Javadoc

1   /*
2    * Copyright 2004-2005 Thomas Cotting
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * $Id: SourceGenerator.java,v 1.3 2005/08/06 14:12:36 tcotting Exp $
18   */
19  package ch.oscg.jreleaseinfo;
20  
21  import java.util.Date;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Set;
26  
27  
28  /***
29   * The SourceGenerator is a simple implementation of the
30   * SourceGeneratorIF interface. In this class the code to be
31   * generated is hard coded.<br>
32   * Other implementation may provide extensions (like SourceGeneratorApp)
33   * or other mechanism (like using a template engine, etc).
34   *
35   * @author Thomas Cotting, Tangarena Engineering AG, Luzern
36   * @version $Revision: 1.3 $ ($Date: 2005/08/06 14:12:36 $ / $Author: tcotting $)
37   */
38  public class SourceGenerator implements SourceGeneratorIF {
39     /*** Linebreak to apply to sourcefile. */
40     final static String LINE_BREAK = "\n";
41  
42     /*** Indentation. */
43     final static String INDENT = "   ";
44  
45     /*** ClassName of version file to be created. */
46     protected String className = "";
47  
48     /**/package-summary/html">PackageName of version file to be created, 'null' means default package/ *//package-summary.html">m>* PackageName of version file to be created, 'null' means default package. */
49     protectedng> String packageName = "";
50  
51     /*** Membervariables for created class. */
52     protected Map props = new HashMap();
53  
54     /*** Date for test purposes. */
55     protected Date newDate = null;
56  
57     /***
58      * Default constructor.
59      */
60     public SourceGenerator() {
61     }
62  
63     /***
64      * Helper routine  for junit test of date.
65      * @return new Date
66      */
67     private Date createDate() {
68        if (newDate == null) {
69           return new Date();
70        }
71  
72        return newDate;
73     }
74  
75     /***
76      * Set the packagename.
77      * @param packageName
78      */
79     publicng> void setPackageName(String packageName) {
80        this.packageName = packageName;
81     }
82  
83     /***
84      * Set the classname.
85      * @param className
86      */
87     public void setClassName(String className) {
88        this.className = className;
89     }
90  
91     /***
92      * Set the property map.
93      * @param property map
94      */
95     public void setProperties(Map props) {
96        this.props = props;
97     }
98  
99     /***
100     * Utility method to write the class java code to a String.
101     *
102     * @return the created java class in a string
103     */
104    public String createCode() {
105       StringBuffer buf = new StringBuffer();
106 
107       createClassInfoHeader(buf);
108 
109       createClassHeader(buf);
110 
111       createMethods(buf);
112 
113       createClassFooter(buf);
114 
115       writeln(buf);
116 
117       return buf.toString();
118    }
119 
120    /***
121     * Utility method to write the class header code to a StringBuffer.
122     *
123     * @param buf StringBuffer to write into
124     */
125    protected void createClassInfoHeader(StringBuffer buf) {
126       writeln(buf, "/* Created by JReleaseInfo AntTask from Open Source Competence Group */");
127       writeln(buf, "/* Creation date " + createDate().toString() + " */");
128       iftrong> (this.packageName != null) {
129          writeln(buf, "package " + this.packageName + ";");
130          writeln(buf, "");
131       }
132 
133       writeln(buf, "import java.util.Date;");
134    }
135 
136    /***
137     * Utility method to write the class header code to a StringBuffer.
138     *
139     * @param buf StringBuffer to write into
140     */
141    protected void createClassHeader(StringBuffer buf) {
142       writeln(buf);
143       writeln(buf, "/**");
144       writeln(buf, " * This class provides information gathered from the build environment.");
145       writeln(buf, " * ");
146       writeln(buf, " * @author JReleaseInfo AntTask");
147       writeln(buf, " */");
148       writeln(buf, "public class " + this.className + " {");
149       writeln(buf);
150    }
151 
152    /***
153     * Utility method to write the class footer code to a StringBuffer.
154     *
155     * @param buf StringBuffer to write into
156     */
157    protected void createClassFooter(StringBuffer buf) {
158       buf.append("}");
159    }
160 
161    /***
162     * Utility method to write the class method code to a StringBuffer.
163     *
164     * @param buf StringBuffer to write into
165     */
166    protected void createMethods(StringBuffer buf) {
167       // Write actual build date
168       writeDateMethod(buf, JReleaseInfoProperty.TYPE_OBJ_DATE, JReleaseInfoBean.PROPNAME_BUILDDATE,
169          createDate());
170 
171       Set keys = this.props.keySet();
172       Iterator it = keys.iterator();
173 
174       while (it.hasNext()) {
175          String key = (String)it.next();
176 
177          // Key empty continue to next
178          if (key.length() == 0) {
179             continue;
180          }
181 
182          // Uppercase first character of property
183          JReleaseInfoProperty biProp = (JReleaseInfoProperty)this.props.get(key);
184 
185          writeObjectMethod(buf, biProp.getType(), biProp.getName(), biProp.getValue().toString());
186       }
187    }
188 
189    /***
190     * Utility method to write java code for an object method to a
191     * StringBuffer.
192     *
193     * @param buf StringBuffer to write into
194     * @param type must be one of the TYPE_OBJ_xxx Strings
195     * @param name of Property
196     * @param value ReturnValue of Property
197     */
198    protected void writeObjectMethod(final StringBuffer buf, String type, String name, String value) {
199       String uName = JReleaseInfoUtil.upperCaseFirstLetter(name);
200       String lName = JReleaseInfoUtil.lowerCaseFirstLetter(name);
201       writeln(buf);
202 
203       if (type.equals(JReleaseInfoProperty.TYPE_PRI_BOOLEAN)) {
204          String strVal = value.toString();
205          writeMethodDeclaration(buf, type, "is" + uName, lName, strVal, strVal);
206       } else if (type.equals(JReleaseInfoProperty.TYPE_PRI_INT)) {
207          String strVal = value.toString();
208          writeMethodDeclaration(buf, type, "get" + uName, lName, strVal, strVal);
209       } else if (type.equals(JReleaseInfoProperty.TYPE_OBJ_BOOLEAN)) {
210          String val = value.equalsIgnoreCase("true") ? "Boolean.TRUE" : "Boolean.FALSE";
211          writeMethodDeclaration(buf, type, "is" + uName, lName, val, val);
212       } else if (type.equals(JReleaseInfoProperty.TYPE_OBJ_STRING)) {
213          String val = "\"" + value + "\"";
214          writeObjectDeclaration(buf, type, lName, val);
215          writeMethodDeclaration(buf, type, "get" + uName, lName, lName, val);
216       } else {
217          writeObjectDeclaration(buf, type, lName, value);
218          writeMethodDeclaration(buf, type, "get" + uName, lName, lName, value);
219       }
220 
221       writeln(buf);
222    }
223 
224    /***
225     * Utility method to write java code for an date method to a
226     * StringBuffer.
227     *
228     * @param buf StringBuffer to write into
229     * @param type must be TYPE_OBJ_DATE
230     * @param name of Property
231     * @param value ReturnValue of Property
232     */
233    protected void writeDateMethod(final StringBuffer buf, String type, String name, Date date) {
234       String uName = JReleaseInfoUtil.upperCaseFirstLetter(name);
235       String lName = JReleaseInfoUtil.lowerCaseFirstLetter(name);
236       writeln(buf);
237       String val = date.getTime() + "L";
238       writeObjectDeclaration(buf, type, lName, val);
239       writeMethodDeclaration(buf, type, "get" + uName, lName, lName, date.toString());
240       writeln(buf);
241    }
242 
243    /***
244     * Utility method to write a method declaration
245     * @param buf StringBuffer to write into
246     * @param type String return type
247     * @param type String property name used for generation of methodName
248     */
249    protected void writeObjectDeclaration(final StringBuffer buf, String type, String lName,
250       String value) {
251       buf.append(INDENT);
252       writeln(buf, "/** " + lName + " (set during build process to " + value + "). */");
253       buf.append(INDENT);
254       if (type.equals("String")) {
255          writeln(buf, "private static " + type + " " + lName + " = " + value + ";");
256       }
257       else {
258          writeln(buf, "private static " + type + " " + lName + " = new " + type + "(" + value + ");");
259       }
260       writeln(buf);
261    }
262 
263    /***
264     * Utility method to write a method declaration
265     * @param buf StringBuffer to write into
266     * @param type String return type
267     * @param type String property name used for generation of methodName
268     */
269    protected void writeMethodDeclaration(final StringBuffer buf, String type, String methodName,
270       String name, String value, String preset) {
271       buf.append(INDENT);
272       writeln(buf, "/**");
273       buf.append(INDENT);
274       writeln(buf, " * Get " + name + " (set during build process to " + preset + ").");
275       buf.append(INDENT);
276       writeln(buf, " * @return " + type + " " + name);
277       buf.append(INDENT);
278       writeln(buf, " */");
279       buf.append(INDENT);
280       buf.append("public static final " + type + " " + methodName + "() ");
281       buf.append("{ return " + value + "; }");
282       writeln(buf);
283    }
284 
285    /***
286     * Utility method to write a line to the StringBuffer and append a line
287     * break.
288     *
289     * @param buf StringBuffer to write into
290     * @param line String to append to buf
291     */
292    protected void writeln(final StringBuffer buf, String line) {
293       buf.append(line);
294       writeln(buf);
295    }
296 
297    /***
298     * Utility method to write an empty line to the StringBuffer.
299     *
300     * @param buf StringBuffer to write into
301     */
302    protected void writeln(final StringBuffer buf) {
303       buf.append(LINE_BREAK);
304    }
305 }