2 Launch4j (http://launch4j.sourceforge.net/)
3 Cross-platform Java application wrapper for creating Windows native executables.
5 Copyright (C) 2004, 2006 Grzegorz Kowal
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.
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.
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
23 * Created on 2005-04-24
25 package net.sf.launch4j;
27 import java.io.BufferedWriter;
29 import java.io.FileWriter;
30 import java.io.IOException;
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;
39 * @author Copyright (C) 2005 Grzegorz Kowal
41 public class RcBuilder {
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;
50 public static final int APP_ICON = 1;
53 public static final int SPLASH_BITMAP = 1;
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;
71 private final StringBuffer _sb = new StringBuffer();
73 public String getContent() {
74 return _sb.toString();
77 public File build(Config c) throws IOException {
78 _sb.append("LANGUAGE ");
79 _sb.append(LANG_NEUTRAL);
81 _sb.append(SUBLANG_DEFAULT);
83 addVersionInfo(c.getVersionInfo());
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());
95 File f = Util.createTempFile("rc");
96 BufferedWriter w = new BufferedWriter(new FileWriter(f));
97 w.write(_sb.toString());
102 private void addVersionInfo(VersionInfo v) {
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" +
115 " BLOCK \"StringFileInfo\"\n" +
117 " BLOCK \"040904E4\"\n" + // English
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");
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());
140 if (jre.getMaxHeapSize() > 0) {
142 jvmArgs.append("-Xmx");
143 jvmArgs.append(jre.getMaxHeapSize());
146 if (jre.getArgs() != null && jre.getArgs().length() > 0) {
148 jvmArgs.append(jre.getArgs().replaceAll("\n", " "));
150 addText(JVM_ARGS, jvmArgs.toString());
153 private void addSplash(Splash splash) {
154 if (splash == null) {
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());
164 private void addText(int id, String text) {
165 if (text == null || text.length() == 0) {
169 _sb.append(" RCDATA BEGIN \"");
170 _sb.append(text.replaceAll("\"", "\"\""));
171 _sb.append("\\0\" END\n");
175 * Stores path in Windows format with '\' separators.
177 private void addWindowsPath(int id, String path) {
178 if (path == null || path.equals("")) {
182 _sb.append(" RCDATA BEGIN \"");
183 _sb.append(path.replaceAll("\\\\", "\\\\\\\\")
184 .replaceAll("/", "\\\\\\\\"));
185 _sb.append("\\0\" END\n");
188 private void addTrue(int id, boolean value) {
194 private void addIcon(int id, File icon) {
195 if (icon == null || icon.getPath().equals("")) {
199 _sb.append(" ICON DISCARDABLE \"");
200 _sb.append(getPath(Util.getAbsoluteFile(
201 ConfigPersister.getInstance().getConfigPath(), icon)));
205 private void addBitmap(int id, File bitmap) {
206 if (bitmap == null) {
210 _sb.append(" BITMAP \"");
211 _sb.append(getPath(Util.getAbsoluteFile(
212 ConfigPersister.getInstance().getConfigPath(), bitmap)));
216 private String getPath(File f) {
217 return f.getPath().replaceAll("\\\\", "\\\\\\\\");
220 private void addSpace(StringBuffer sb) {
221 int len = sb.length();
222 if (len-- > 0 && sb.charAt(len) != ' ') {
227 private void addVerBlockValue(String key, String value) {
228 _sb.append(" VALUE \"");
230 _sb.append("\", \"");