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 Apr 22, 2005
25 package net.sf.launch4j.config;
27 import java.io.BufferedReader;
28 import java.io.BufferedWriter;
30 import java.io.FileInputStream;
31 import java.io.FileReader;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.util.Properties;
36 import net.sf.launch4j.Util;
38 import com.thoughtworks.xstream.XStream;
39 import com.thoughtworks.xstream.io.xml.DomDriver;
42 * @author Copyright (C) 2005 Grzegorz Kowal
44 public class ConfigPersister {
46 private static final ConfigPersister _instance = new ConfigPersister();
48 private final XStream _xstream;
49 private Config _config;
50 private File _configPath;
52 private ConfigPersister() {
53 _xstream = new XStream(new DomDriver());
54 _xstream.alias("launch4jConfig", Config.class);
55 _xstream.alias("jre", Jre.class);
56 _xstream.alias("splash", Splash.class);
57 _xstream.alias("versionInfo", VersionInfo.class);
58 _xstream.alias("file", String.class);
61 public static ConfigPersister getInstance() {
65 public Config getConfig() {
69 public File getConfigPath() {
73 public File getOutputPath() throws IOException {
74 if (_config.getOutfile().isAbsolute()) {
75 return _config.getOutfile().getParentFile();
77 File parent = _config.getOutfile().getParentFile();
78 return (parent != null) ? new File(_configPath, parent.getPath()) : _configPath;
81 public File getOutputFile() throws IOException {
82 return _config.getOutfile().isAbsolute()
83 ? _config.getOutfile()
84 : new File(getOutputPath(), _config.getOutfile().getName());
87 public void createBlank() {
88 _config = new Config();
89 _config.setJre(new Jre());
93 public void setAntConfig(Config c, File basedir) {
95 _configPath = basedir;
98 public void load(File f) throws ConfigPersisterException {
100 BufferedReader r = new BufferedReader(new FileReader(f));
101 _config = (Config) _xstream.fromXML(r);
104 } catch (Exception e) {
105 throw new ConfigPersisterException(e);
110 * Imports launch4j 1.x.x config file.
112 public void loadVersion1(File f) throws ConfigPersisterException {
114 Props props = new Props(f);
115 _config = new Config();
116 String header = props.getProperty(Config.HEADER);
117 _config.setHeaderType(header == null
118 || header.toLowerCase().equals("guihead.bin") ? 0 : 1);
119 _config.setJar(props.getFile(Config.JAR));
120 _config.setOutfile(props.getFile(Config.OUTFILE));
121 _config.setJre(new Jre());
122 _config.getJre().setPath(props.getProperty(Jre.PATH));
123 _config.getJre().setMinVersion(props.getProperty(Jre.MIN_VERSION));
124 _config.getJre().setMaxVersion(props.getProperty(Jre.MAX_VERSION));
125 _config.getJre().setArgs(props.getProperty(Jre.ARGS));
126 _config.setJarArgs(props.getProperty(Config.JAR_ARGS));
127 _config.setChdir("true".equals(props.getProperty(Config.CHDIR)) ? "." : null);
128 _config.setCustomProcName("true".equals(
129 props.getProperty("setProcName"))); // 1.x
130 _config.setStayAlive("true".equals(props.getProperty(Config.STAY_ALIVE)));
131 _config.setErrTitle(props.getProperty(Config.ERR_TITLE));
132 _config.setIcon(props.getFile(Config.ICON));
133 File splashFile = props.getFile(Splash.SPLASH_FILE);
134 if (splashFile != null) {
135 _config.setSplash(new Splash());
136 _config.getSplash().setFile(splashFile);
137 String waitfor = props.getProperty("waitfor"); // 1.x
138 _config.getSplash().setWaitForWindow(waitfor != null && !waitfor.equals(""));
139 String splashTimeout = props.getProperty(Splash.TIMEOUT);
140 if (splashTimeout != null) {
141 _config.getSplash().setTimeout(Integer.parseInt(splashTimeout));
143 _config.getSplash().setTimeoutErr("true".equals(
144 props.getProperty(Splash.TIMEOUT_ERR)));
146 _config.setSplash(null);
149 } catch (IOException e) {
150 throw new ConfigPersisterException(e);
154 public void save(File f) throws ConfigPersisterException {
156 BufferedWriter w = new BufferedWriter(new FileWriter(f));
157 _xstream.toXML(_config, w);
160 } catch (Exception e) {
161 throw new ConfigPersisterException(e);
165 private void setConfigPath(File configFile) {
166 _configPath = configFile.getAbsoluteFile().getParentFile();
169 private class Props {
170 final Properties _properties = new Properties();
172 public Props(File f) throws IOException {
173 FileInputStream is = null;
175 is = new FileInputStream(f);
176 _properties.load(is);
183 * Get property and remove trailing # comments.
185 public String getProperty(String key) {
186 String p = _properties.getProperty(key);
190 int x = p.indexOf('#');
196 } while (x > 0 && (p.charAt(x) == ' ' || p.charAt(x) == '\t'));
197 return (x == 0) ? "" : p.substring(0, x + 1);
200 public File getFile(String key) {
201 String value = getProperty(key);
202 return value != null ? new File(value) : null;