1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 private String packageName = null/package-summary.html">ng> 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
67 if (!JReleaseInfoUtil.isValidClassNameString(className)) {
68 throw new IllegalArgumentException(MESSAGE_EXC_CLASSNAME + className);
69 }
70
71
72 if (!JReleaseInfoUtil/isValidPackageNameString(packageName)) {/package-summary.html">trong> (!JReleaseInfoUtil.isValidPackageNameString(packageName)) {
73 throw new IllegalArgumentException(MESSAGE_EXC_PACKAGENAME + packageName);
74 }
75
76
77 if (!JReleaseInfoUtil.isValidNameString(targetDir)) {
78 throw new IllegalArgumentException(MESSAGE_EXC_TARGETDIR + targetDir);
79 }
80
81
82 className = JReleaseInfoUtil.upperCaseFirstLetter(className);
83
84
85 File versionFile = createJReleaseInfoFile();
86
87
88 File parent = versionFile.getParentFile();
89
90 if (parent != null) {
91 parent.mkdirs();
92 }
93
94
95 if (versionFile.exists()) {
96 versionFile.delete();
97 }
98
99
100 sourceGenerator.setProperties(props);
101 sourceGenerator.setPackageName(packageName);
102 sourceGenerator.setClassName(className);
103
104
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
117 String packageDir = null;
118
119 if (packageName != null) {/package-summary.html">trong> (packageName != null) {
120 packageDir = packageName.replace('.', '/');
121 }
122
123
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 public void setPackageName(String packageName) {/package-summary.html">ng> 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 }