OSDN Git Service

git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/filelock/trunk@23 c6214a2a-ec3a...
[filelock/repo.git] / filelock / src / main / lib / launch4j-2.1.5-win32 / src / net / sf / launch4j / RcBuilder.java
1 /*
2         Launch4j (http://launch4j.sourceforge.net/)
3         Cross-platform Java application wrapper for creating Windows native executables.
4
5         Copyright (C) 2004, 2006 Grzegorz Kowal
6
7         This program is free software; you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation; either version 2 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program; if not, write to the Free Software
19         Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 /*
23  * Created on 2005-04-24
24  */
25 package net.sf.launch4j;
26
27 import java.io.BufferedWriter;
28 import java.io.File;
29 import java.io.FileWriter;
30 import java.io.IOException;
31
32 import net.sf.launch4j.config.Config;
33 import net.sf.launch4j.config.ConfigPersister;
34 import net.sf.launch4j.config.Jre;
35 import net.sf.launch4j.config.Splash;
36 import net.sf.launch4j.config.VersionInfo;
37
38 /**
39  * @author Copyright (C) 2005 Grzegorz Kowal
40  */
41 public class RcBuilder {
42
43         // winnt.h
44         public static final int LANG_NEUTRAL = 0;
45         public static final int SUBLANG_NEUTRAL = 0;
46         public static final int SUBLANG_DEFAULT = 1;
47         public static final int SUBLANG_SYS_DEFAULT     = 2;
48
49         // ICON
50         public static final int APP_ICON = 1;
51
52         // BITMAP
53         public static final int SPLASH_BITMAP = 1;
54
55         // RCDATA
56         public static final int JRE_PATH = 1;
57         public static final int JAVA_MIN_VER = 2;
58         public static final int JAVA_MAX_VER = 3;
59         public static final int SHOW_SPLASH = 4;
60         public static final int SPLASH_WAITS_FOR_WINDOW = 5;
61         public static final int SPLASH_TIMEOUT = 6;
62         public static final int SPLASH_TIMEOUT_ERR = 7;
63         public static final int CHDIR = 8;
64         public static final int SET_PROC_NAME = 9;
65         public static final int ERR_TITLE = 10;
66         public static final int GUI_HEADER_STAYS_ALIVE = 11;
67         public static final int JVM_ARGS = 12;
68         public static final int JAR_ARGS = 13;
69         public static final int JAR = 14;
70
71         private final StringBuffer _sb = new StringBuffer();
72
73         public String getContent() {
74                 return _sb.toString();
75         }
76
77         public File build(Config c) throws IOException {
78                 _sb.append("LANGUAGE ");
79                 _sb.append(LANG_NEUTRAL);
80                 _sb.append(", ");
81                 _sb.append(SUBLANG_DEFAULT);
82                 _sb.append('\n');
83                 addVersionInfo(c.getVersionInfo());
84                 addJre(c.getJre());
85                 addIcon(APP_ICON, c.getIcon());
86                 addText(ERR_TITLE, c.getErrTitle());
87                 addText(JAR_ARGS, c.getJarArgs());
88                 addWindowsPath(CHDIR, c.getChdir());
89                 addTrue(SET_PROC_NAME, c.isCustomProcName());
90                 addTrue(GUI_HEADER_STAYS_ALIVE, c.isStayAlive());
91                 addSplash(c.getSplash());
92                 if (c.isDontWrapJar()) {
93                         addWindowsPath(JAR, c.getJar().getPath());
94                 }
95                 File f = Util.createTempFile("rc");
96                 BufferedWriter w = new BufferedWriter(new FileWriter(f));
97                 w.write(_sb.toString());
98                 w.close();
99                 return f;
100         }
101
102         private void addVersionInfo(VersionInfo v) {
103                 if (v == null) {
104                         return;
105                 }
106                 _sb.append("1 VERSIONINFO\n");
107                 _sb.append("FILEVERSION ");
108                 _sb.append(v.getFileVersion().replaceAll("\\.", ", "));
109                 _sb.append("\nPRODUCTVERSION ");
110                 _sb.append(v.getProductVersion().replaceAll("\\.", ", "));
111                 _sb.append("\nFILEFLAGSMASK 0\n" +
112                                 "FILEOS 0x40000\n" +
113                                 "FILETYPE 1\n" +
114                                 "{\n" + 
115                                 " BLOCK \"StringFileInfo\"\n" +
116                                 " {\n" +
117                                 "  BLOCK \"040904E4\"\n" +      // English
118                                 "  {\n");
119                 addVerBlockValue("CompanyName", v.getCompanyName());
120                 addVerBlockValue("FileDescription", v.getFileDescription());
121                 addVerBlockValue("FileVersion", v.getTxtFileVersion());
122                 addVerBlockValue("InternalName", v.getInternalName());
123                 addVerBlockValue("LegalCopyright", v.getCopyright());
124                 addVerBlockValue("OriginalFilename", v.getOriginalFilename());
125                 addVerBlockValue("ProductName", v.getProductName());
126                 addVerBlockValue("ProductVersion", v.getTxtProductVersion());
127                 _sb.append("  }\n }\n}\n");
128         }
129         
130         private void addJre(Jre jre) {
131                 addWindowsPath(JRE_PATH, jre.getPath());
132                 addText(JAVA_MIN_VER, jre.getMinVersion());
133                 addText(JAVA_MAX_VER, jre.getMaxVersion());
134                 StringBuffer jvmArgs = new StringBuffer();
135                 if (jre.getInitialHeapSize() > 0) {
136                         jvmArgs.append("-Xms");
137                         jvmArgs.append(jre.getInitialHeapSize());
138                         jvmArgs.append('m');
139                 }
140                 if (jre.getMaxHeapSize() > 0) {
141                         addSpace(jvmArgs);
142                         jvmArgs.append("-Xmx");
143                         jvmArgs.append(jre.getMaxHeapSize());
144                         jvmArgs.append('m');
145                 }
146                 if (jre.getArgs() != null && jre.getArgs().length() > 0) {
147                         addSpace(jvmArgs);
148                         jvmArgs.append(jre.getArgs().replaceAll("\n", " "));
149                 }
150                 addText(JVM_ARGS, jvmArgs.toString());
151         }
152         
153         private void addSplash(Splash splash) {
154                 if (splash == null) {
155                         return;
156                 }
157                 addTrue(SHOW_SPLASH, true);
158                 addTrue(SPLASH_WAITS_FOR_WINDOW, splash.getWaitForWindow());
159                 addText(SPLASH_TIMEOUT, String.valueOf(splash.getTimeout()));
160                 addTrue(SPLASH_TIMEOUT_ERR, splash.isTimeoutErr());
161                 addBitmap(SPLASH_BITMAP, splash.getFile());
162         }
163
164         private void addText(int id, String text) {
165                 if (text == null || text.length() == 0) {
166                         return;
167                 }
168                 _sb.append(id);
169                 _sb.append(" RCDATA BEGIN \"");
170                 _sb.append(text.replaceAll("\"", "\"\""));
171                 _sb.append("\\0\" END\n");
172         }
173
174         /**
175          * Stores path in Windows format with '\' separators. 
176          */
177         private void addWindowsPath(int id, String path) {
178                 if (path == null || path.equals("")) {
179                         return;
180                 }
181                 _sb.append(id);
182                 _sb.append(" RCDATA BEGIN \"");
183                 _sb.append(path.replaceAll("\\\\", "\\\\\\\\")
184                                 .replaceAll("/", "\\\\\\\\"));
185                 _sb.append("\\0\" END\n");
186         }
187
188         private void addTrue(int id, boolean value) {
189                 if (value) {
190                         addText(id, "true");
191                 }
192         }
193
194         private void addIcon(int id, File icon) {
195                 if (icon == null || icon.getPath().equals("")) {
196                         return;
197                 }
198                 _sb.append(id);
199                 _sb.append(" ICON DISCARDABLE \"");
200                 _sb.append(getPath(Util.getAbsoluteFile(
201                                 ConfigPersister.getInstance().getConfigPath(), icon)));
202                 _sb.append("\"\n");
203         }
204
205         private void addBitmap(int id, File bitmap) {
206                 if (bitmap == null) {
207                         return;
208                 }
209                 _sb.append(id);
210                 _sb.append(" BITMAP \"");
211                 _sb.append(getPath(Util.getAbsoluteFile(
212                                 ConfigPersister.getInstance().getConfigPath(), bitmap)));
213                 _sb.append("\"\n");
214         }
215         
216         private String getPath(File f) {
217                 return f.getPath().replaceAll("\\\\", "\\\\\\\\");
218         }
219         
220         private void addSpace(StringBuffer sb) {
221                 int len = sb.length();
222                 if (len-- > 0 && sb.charAt(len) != ' ') {
223                         sb.append(' ');
224                 }
225         }
226         
227         private void addVerBlockValue(String key, String value) {
228                 _sb.append("   VALUE \"");
229                 _sb.append(key);
230                 _sb.append("\", \"");
231                 if (value != null) {
232                         _sb.append(value);
233                 }
234                 _sb.append("\"\n");
235         }
236 }