1 21 22 package com.izforge.izpack.compiler; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 import java.io.StringWriter ; 29 import java.util.Enumeration ; 30 import java.util.Properties ; 31 import java.util.Vector ; 32 33 import net.n3.nanoxml.XMLElement; 34 35 import org.apache.tools.ant.taskdefs.Execute; 36 37 import com.izforge.izpack.util.VariableSubstitutor; 38 39 89 public class Property 90 { 91 92 protected String name; 93 94 protected String value; 95 96 protected File file; 97 98 protected String env; 101 102 protected String prefix; 104 105 protected XMLElement xmlProp; 106 107 protected CompilerConfig config; 108 protected Compiler compiler; 109 110 public Property(XMLElement xmlProp, CompilerConfig config) 111 { 112 this.xmlProp = xmlProp; 113 this.config = config; 114 this.compiler = config.getCompiler(); 115 name = xmlProp.getAttribute("name"); 116 value = xmlProp.getAttribute("value"); 117 env = xmlProp.getAttribute("environment"); 118 if (env != null && !env.endsWith(".")) env += "."; 119 120 prefix = xmlProp.getAttribute("prefix"); 121 if (prefix != null && !prefix.endsWith(".")) prefix += "."; 122 123 String filename = xmlProp.getAttribute("file"); 124 if (filename != null) file = new File (filename); 125 } 126 127 132 public String getValue() 133 { 134 return toString(); 135 } 136 137 142 public String toString() 143 { 144 return value == null ? "" : value; 145 } 146 147 151 public void execute() throws CompilerException 152 { 153 if (name != null) 154 { 155 if (value == null) 156 config.parseError(xmlProp, "You must specify a value with the name attribute"); 157 } 158 else 159 { 160 if (file == null && env == null) 161 config.parseError(xmlProp, 162 "You must specify file, or environment when not using the name attribute"); 163 } 164 165 if (file == null && prefix != null) 166 config.parseError(xmlProp, "Prefix is only valid when loading from a file "); 167 168 if ((name != null) && (value != null)) 169 addProperty(name, value); 170 171 else if (file != null) 172 loadFile(file); 173 174 else if (env != null) loadEnvironment(env); 175 } 176 177 182 protected void loadFile(File file) throws CompilerException 183 { 184 Properties props = new Properties (); 185 config.getPackagerListener().packagerMsg("Loading " + file.getAbsolutePath(), 186 PackagerListener.MSG_VERBOSE); 187 try 188 { 189 if (file.exists()) 190 { 191 FileInputStream fis = new FileInputStream (file); 192 try 193 { 194 props.load(fis); 195 } 196 finally 197 { 198 if (fis != null) fis.close(); 199 } 200 addProperties(props); 201 } 202 else 203 { 204 config.getPackagerListener().packagerMsg( 205 "Unable to find property file: " + file.getAbsolutePath(), 206 PackagerListener.MSG_VERBOSE); 207 } 208 } 209 catch (IOException ex) 210 { 211 config.parseError(xmlProp, "Faild to load file: " + file.getAbsolutePath(), ex); 212 } 213 } 214 215 220 protected void loadEnvironment(String prefix) throws CompilerException 221 { 222 Properties props = new Properties (); 223 config.getPackagerListener().packagerMsg("Loading Environment " + prefix, 224 PackagerListener.MSG_VERBOSE); 225 Vector osEnv = Execute.getProcEnvironment(); 226 for (Enumeration e = osEnv.elements(); e.hasMoreElements();) 227 { 228 String entry = (String ) e.nextElement(); 229 int pos = entry.indexOf('='); 230 if (pos == -1) 231 { 232 config.getPackagerListener().packagerMsg("Ignoring " + prefix, 233 PackagerListener.MSG_WARN); 234 } 235 else 236 { 237 props.put(prefix + entry.substring(0, pos), entry.substring(pos + 1)); 238 } 239 } 240 addProperties(props); 241 } 242 243 249 protected void addProperty(String name, String value) throws CompilerException 250 { 251 value = compiler.replaceProperties(value); 252 253 compiler.addProperty(name, value); 254 } 255 256 259 protected void addProperties(Properties props) throws CompilerException 260 { 261 resolveAllProperties(props); 262 Enumeration e = props.keys(); 263 while (e.hasMoreElements()) 264 { 265 String name = (String ) e.nextElement(); 266 String value = props.getProperty(name); 267 268 if (prefix != null) 269 { 270 name = prefix + name; 271 } 272 273 addProperty(name, value); 274 } 275 } 276 277 282 private void resolveAllProperties(Properties props) throws CompilerException 283 { 284 VariableSubstitutor subs = new VariableSubstitutor(props); 285 subs.setBracesRequired(true); 286 287 for (Enumeration e = props.keys(); e.hasMoreElements();) 288 { 289 String name = (String ) e.nextElement(); 290 String value = props.getProperty(name); 291 292 int mods = -1; 293 do 294 { 295 StringReader read = new StringReader (value); 296 StringWriter write = new StringWriter (); 297 298 try 299 { 300 mods = subs.substitute(read, write, "at"); 301 props.put(name, value); 305 } 306 catch (IOException ex) 307 { 308 config.parseError(xmlProp, "Faild to load file: " + file.getAbsolutePath(), 309 ex); 310 } 311 } 312 while (mods != 0); 313 } 314 } 315 } 316 | Popular Tags |