1 21 22 25 package net.sf.launch4j.config; 26 27 import java.io.BufferedWriter ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileReader ; 31 import java.io.FileWriter ; 32 import java.io.IOException ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 import java.util.Properties ; 36 37 import net.sf.launch4j.Util; 38 39 import com.thoughtworks.xstream.XStream; 40 import com.thoughtworks.xstream.io.xml.DomDriver; 41 42 45 public class ConfigPersister { 46 47 private static final ConfigPersister _instance = new ConfigPersister(); 48 49 private final XStream _xstream; 50 private Config _config; 51 private File _configPath; 52 53 private ConfigPersister() { 54 _xstream = new XStream(new DomDriver()); 55 _xstream.alias("launch4jConfig", Config.class); 56 _xstream.alias("classPath", ClassPath.class); 57 _xstream.alias("jre", Jre.class); 58 _xstream.alias("splash", Splash.class); 59 _xstream.alias("versionInfo", VersionInfo.class); 60 61 _xstream.addImplicitCollection(Config.class, "headerObjects", "obj", 62 String .class); 63 _xstream.addImplicitCollection(Config.class, "libs", "lib", String .class); 64 _xstream.addImplicitCollection(Config.class, "variables", "var", String .class); 65 _xstream.addImplicitCollection(ClassPath.class, "paths", "cp", String .class); 66 _xstream.addImplicitCollection(Jre.class, "options", "opt", String .class); 67 } 68 69 public static ConfigPersister getInstance() { 70 return _instance; 71 } 72 73 public Config getConfig() { 74 return _config; 75 } 76 77 public File getConfigPath() { 78 return _configPath; 79 } 80 81 public File getOutputPath() throws IOException { 82 if (_config.getOutfile().isAbsolute()) { 83 return _config.getOutfile().getParentFile(); 84 } 85 File parent = _config.getOutfile().getParentFile(); 86 return (parent != null) ? new File (_configPath, parent.getPath()) : _configPath; 87 } 88 89 public File getOutputFile() throws IOException { 90 return _config.getOutfile().isAbsolute() 91 ? _config.getOutfile() 92 : new File (getOutputPath(), _config.getOutfile().getName()); 93 } 94 95 public void createBlank() { 96 _config = new Config(); 97 _config.setJre(new Jre()); 98 _configPath = null; 99 } 100 101 public void setAntConfig(Config c, File basedir) { 102 _config = c; 103 _configPath = basedir; 104 } 105 106 public void load(File f) throws ConfigPersisterException { 107 try { 108 FileReader r = new FileReader (f); 109 char[] buf = new char[(int) f.length()]; 110 r.read(buf); 111 r.close(); 112 String s = String.valueOf(buf) 114 .replaceAll("<headerType>0<", "<headerType>gui<") 115 .replaceAll("<headerType>1<", "<headerType>console<") 116 .replaceAll("jarArgs>", "cmdLine>") 117 .replaceAll("<jarArgs[ ]*/>", "<cmdLine/>") 118 .replaceAll("args>", "opt>") 119 .replaceAll("<args[ ]*/>", "<opt/>"); 120 _config = (Config) _xstream.fromXML(s); 121 setConfigPath(f); 122 } catch (Exception e) { 123 throw new ConfigPersisterException(e); 124 } 125 } 126 127 130 public void loadVersion1(File f) throws ConfigPersisterException { 131 try { 132 Props props = new Props(f); 133 _config = new Config(); 134 String header = props.getProperty(Config.HEADER); 135 _config.setHeaderType(header == null 136 || header.toLowerCase().equals("guihead.bin") ? Config.GUI_HEADER 137 : Config.CONSOLE_HEADER); 138 _config.setJar(props.getFile(Config.JAR)); 139 _config.setOutfile(props.getFile(Config.OUTFILE)); 140 _config.setJre(new Jre()); 141 _config.getJre().setPath(props.getProperty(Jre.PATH)); 142 _config.getJre().setMinVersion(props.getProperty(Jre.MIN_VERSION)); 143 _config.getJre().setMaxVersion(props.getProperty(Jre.MAX_VERSION)); 144 List args = new ArrayList (); 145 args.add(props.getProperty(Jre.ARGS)); 146 _config.getJre().setOptions(args); 147 _config.setCmdLine(props.getProperty(Config.JAR_ARGS)); 148 _config.setChdir("true".equals(props.getProperty(Config.CHDIR)) 149 ? "." : null); 150 _config.setCustomProcName("true".equals( 151 props.getProperty("setProcName"))); _config.setStayAlive("true".equals(props.getProperty(Config.STAY_ALIVE))); 153 _config.setErrTitle(props.getProperty(Config.ERR_TITLE)); 154 _config.setIcon(props.getFile(Config.ICON)); 155 File splashFile = props.getFile(Splash.SPLASH_FILE); 156 if (splashFile != null) { 157 _config.setSplash(new Splash()); 158 _config.getSplash().setFile(splashFile); 159 String waitfor = props.getProperty("waitfor"); _config.getSplash().setWaitForWindow(waitfor != null 161 && !waitfor.equals("")); 162 String splashTimeout = props.getProperty(Splash.TIMEOUT); 163 if (splashTimeout != null) { 164 _config.getSplash().setTimeout(Integer.parseInt(splashTimeout)); 165 } 166 _config.getSplash().setTimeoutErr("true".equals( 167 props.getProperty(Splash.TIMEOUT_ERR))); 168 } else { 169 _config.setSplash(null); 170 } 171 setConfigPath(f); 172 } catch (IOException e) { 173 throw new ConfigPersisterException(e); 174 } 175 } 176 177 public void save(File f) throws ConfigPersisterException { 178 try { 179 BufferedWriter w = new BufferedWriter (new FileWriter (f)); 180 _xstream.toXML(_config, w); 181 w.close(); 182 setConfigPath(f); 183 } catch (Exception e) { 184 throw new ConfigPersisterException(e); 185 } 186 } 187 188 private void setConfigPath(File configFile) { 189 _configPath = configFile.getAbsoluteFile().getParentFile(); 190 } 191 192 private class Props { 193 final Properties _properties = new Properties (); 194 195 public Props(File f) throws IOException { 196 FileInputStream is = null; 197 try { 198 is = new FileInputStream (f); 199 _properties.load(is); 200 } finally { 201 Util.close(is); 202 } 203 } 204 205 208 public String getProperty(String key) { 209 String p = _properties.getProperty(key); 210 if (p == null) { 211 return null; 212 } 213 int x = p.indexOf('#'); 214 if (x == -1) { 215 return p; 216 } 217 do { 218 x--; 219 } while (x > 0 && (p.charAt(x) == ' ' || p.charAt(x) == '\t')); 220 return (x == 0) ? "" : p.substring(0, x + 1); 221 } 222 223 public File getFile(String key) { 224 String value = getProperty(key); 225 return value != null ? new File (value) : null; 226 } 227 } 228 } 229 | Popular Tags |