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: JReleaseInfoBean.java,v 1.2 2005/08/06 14:12:36 tcotting Exp $
18   */
19  package ch.oscg.jreleaseinfo;
20  
21  import java.io.File;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  
25  import java.util.Map;
26  
27  
28  /***
29   * Central class creates the java source file for
30   * the desired properties.
31   *
32   * @author Thomas Cotting, Tangarena Engineering AG, Luzern
33   * @version $Revision: 1.2 $ ($Date: 2005/08/06 14:12:36 $ / $Author: tcotting $)
34   */
35  public class JReleaseInfoBean {
36     /*** Property name for Build Date. */
37     public static final String PROPNAME_BUILDDATE = "buildDate";
38  
39     /*** Exception message on invalid class name. */
40     public static final String MESSAGE_EXC_CLASSNAME = "Invalid Java class name :";
41  
42     /**/package-summary/html">Exception message on invalid package name/ *//package-summary.html">m>* Exception message on invalid package name. */
43     public static final String MESSAGE_EXC_PACKAGENAME = "Invalid package name :";
44  
45     /*** Exception message on invalid filename. */
46     public static final String MESSAGE_EXC_TARGETDIR = "Invalid target directory :";
47  
48     /*** Target directory of version file to be created. */
49     private String targetDir = null;
50  
51     /*** ClassName of version file to be created .*/
52     private String className = null;
53  
54     /**/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. */
55     privateng> String packageName = null;
56  
57     /***
58      * Execute() method.
59      * @param props Map containing the JReleaseInfoProperties
60      *
61      * @throws IllegalArgumentException on invalid arguments
62      * @throws IOException on file writing problems
63      */
64     public void execute(Map props, SourceGeneratorIF sourceGenerator)
65        throws IOException {
66        // check classname
67        if (!JReleaseInfoUtil.isValidClassNameString(className)) {
68           throw new IllegalArgumentException(MESSAGE_EXC_CLASSNAME + className);
69        }
70  
71        // check packagename
72        iftrong> (!JReleaseInfoUtil.isValidPackageNameString(packageName)) {
73           throw new IllegalArgumentException(MESSAGE_EXC_PACKAGENAME + packageName);
74        }
75  
76        // check targetDir
77        if (!JReleaseInfoUtil.isValidNameString(targetDir)) {
78           throw new IllegalArgumentException(MESSAGE_EXC_TARGETDIR + targetDir);
79        }
80  
81        // Prepare className
82        className = JReleaseInfoUtil.upperCaseFirstLetter(className);
83  
84        // create version file
85        File versionFile = createJReleaseInfoFile();
86  
87        //create and check directories
88        File parent = versionFile.getParentFile();
89  
90        if (parent != null) {
91           parent.mkdirs();
92        }
93  
94        // delete existent file
95        if (versionFile.exists()) {
96           versionFile.delete();
97        }
98  
99        // Update sourceGenerator
100       sourceGenerator.setProperties(props);
101       sourceGenerator.setPackageName(packageName);
102       sourceGenerator.setClassName(className);
103 
104       // Now write the file
105       FileWriter fw = new FileWriter(versionFile);
106       fw.write(sourceGenerator.createCode());
107       fw.close();
108    }
109 
110    /***
111     * Utility method to write the class java code to a String.
112     *
113     * @return the created java class in a string
114     */
115    public File createJReleaseInfoFile() {
116       // set up package directory
117       String packageDir = null;
118 
119       iftrong> (packageName != null) {
120          packageDir = packageName.replace('.', '/');
121       }
122 
123       // Now create the full filename
124       return new File(JReleaseInfoUtil.getPathElement(targetDir) +
125          JReleaseInfoUtil.getPathElement(packageDir) + className + ".java");
126    }
127 
128    /***
129     * Utility method to delete the JReleaseInfo file (for test purposes).
130     */
131    public void deleteJReleaseInfoFile() {
132       File file = createJReleaseInfoFile();
133 
134       if (file.exists()) {
135          file.delete();
136       }
137    }
138 
139    /***
140     * Set the class name of the JReleaseInfoAntTask class.
141     *
142     * @param className String
143     */
144    public void setClassName(String className) {
145       if (className != null) {
146          this.className = className.trim();
147       }
148    }
149 
150    /***
151     * Set the package name of the JReleaseInfoAntTask class.
152     *
153     * @param packageName String
154     */
155    publicng> void setPackageName(String packageName) {
156       if ((packageName != null) && (packageName.trim() != "")) {
157          this.packageName = packageName.trim().toLowerCase();
158       }
159    }
160 
161    /***
162     * Set the target directory where the file should be created.
163     *
164     * @param targetDir Name of directory
165     */
166    public void setTargetDir(String targetDir) {
167       this.targetDir = targetDir.trim();
168    }
169 }