1 22 23 package com.izforge.izpack.ant; 24 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 import java.util.Properties ; 28 import java.util.ResourceBundle ; 29 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.Task; 33 import org.apache.tools.ant.types.EnumeratedAttribute; 34 import org.apache.tools.ant.types.PropertySet; 35 36 import com.izforge.izpack.compiler.CompilerConfig; 37 import com.izforge.izpack.compiler.CompilerException; 38 import com.izforge.izpack.compiler.PackagerListener; 39 40 45 public class IzPackTask extends Task implements PackagerListener 46 { 47 48 private ConfigHolder config; 49 50 51 private String input; 52 53 54 private String basedir; 55 56 57 private String output; 58 59 60 private String compression; 61 62 63 private int compressionLevel; 64 65 66 private InstallerType installerType; 67 68 71 private String izPackDir; 72 73 74 private Properties properties; 75 76 77 private boolean inheritAll = false; 78 79 80 public IzPackTask() 81 { 82 basedir = null; 83 config = null; 84 input = null; 85 output = null; 86 installerType = null; 87 izPackDir = null; 88 compression = "default"; 89 compressionLevel = -1; 90 } 91 92 96 public ConfigHolder createConfig() 97 { 98 config = new ConfigHolder(getProject()); 99 return config; 100 } 101 102 107 public void packagerMsg(String str) 108 { 109 packagerMsg(str, MSG_INFO); 110 } 111 112 118 public void packagerMsg(String str, int priority) 119 { 120 final int antPriority; 121 switch (priority) 122 { 124 case MSG_DEBUG: 125 antPriority = Project.MSG_DEBUG; 126 break; 127 case MSG_ERR: 128 antPriority = Project.MSG_ERR; 129 break; 130 case MSG_INFO: 131 antPriority = Project.MSG_INFO; 132 break; 133 case MSG_VERBOSE: 134 antPriority = Project.MSG_VERBOSE; 135 break; 136 case MSG_WARN: 137 antPriority = Project.MSG_WARN; 138 break; 139 default: antPriority = Project.MSG_INFO; 141 } 142 log(str, antPriority); 143 } 144 145 146 public void packagerStart() 147 { 148 log(ResourceBundle.getBundle("com/izforge/izpack/ant/langpacks/messages").getString( 149 "Packager_starting"), Project.MSG_DEBUG); 150 } 151 152 153 public void packagerStop() 154 { 155 log(ResourceBundle.getBundle("com/izforge/izpack/ant/langpacks/messages").getString( 156 "Packager_ended"), Project.MSG_DEBUG); 157 } 158 159 164 public void execute() throws org.apache.tools.ant.BuildException 165 { 166 if (input == null && config == null) 168 throw new BuildException(ResourceBundle.getBundle( 169 "com/izforge/izpack/ant/langpacks/messages").getString( 170 "input_must_be_specified")); 171 172 if (output == null) 173 throw new BuildException(ResourceBundle.getBundle( 174 "com/izforge/izpack/ant/langpacks/messages").getString( 175 "output_must_be_specified")); 176 177 179 if (basedir == null) 180 throw new BuildException(ResourceBundle.getBundle( 181 "com/izforge/izpack/ant/langpacks/messages").getString( 182 "basedir_must_be_specified")); 183 184 188 String kind = (installerType == null ? null : installerType.getValue()); 189 190 CompilerConfig c = null; 191 String configText = null; 192 if(config != null ) 193 { configText = config.getText(); 195 input = null; 196 } 197 try 198 { 199 c = new CompilerConfig(input, basedir, kind, output, 201 compression, compressionLevel, this, configText); 202 } 203 catch (CompilerException e1) 204 { 205 throw new BuildException(e1); 206 } 207 CompilerConfig.setIzpackHome(izPackDir); 208 209 if (properties != null) 210 { 211 Enumeration e = properties.keys(); 212 while (e.hasMoreElements()) 213 { 214 String name = (String ) e.nextElement(); 215 String value = properties.getProperty(name); 216 value = fixPathString(value); 217 c.addProperty(name, value); 218 } 219 } 220 221 if (inheritAll) 222 { 223 Hashtable projectProps = getProject().getProperties(); 224 Enumeration e = projectProps.keys(); 225 while (e.hasMoreElements()) 226 { 227 String name = (String ) e.nextElement(); 228 String value = (String ) projectProps.get(name); 229 value = fixPathString(value); 230 c.addProperty(name, value); 231 } 232 } 233 234 try 235 { 236 c.executeCompiler(); 237 } 238 catch (Exception e) 239 { 240 throw new BuildException(e); } 243 } 244 245 private static String fixPathString(String path) 246 { 247 252 StringBuffer fixpath = new StringBuffer (path); 253 for(int q=0; q<fixpath.length(); q++) 254 if(fixpath.charAt(q) == '\\') 255 fixpath.setCharAt(q, '/'); 256 return fixpath.toString(); 257 } 258 259 264 public void setInput(String input) 265 { 266 this.input = input; 267 } 268 269 274 public void setBasedir(String basedir) 275 { 276 this.basedir = basedir; 277 } 278 279 284 public void setOutput(String output) 285 { 286 this.output = output; 287 } 288 289 294 public void setInstallerType(InstallerType installerType) 295 { 296 this.installerType = installerType; 297 } 298 299 304 public void setIzPackDir(String izPackDir) 305 { 306 if (!(izPackDir.endsWith("/"))) izPackDir += "/"; 307 this.izPackDir = izPackDir; 308 } 309 310 313 public void setInheritAll(boolean value) 314 { 315 inheritAll = value; 316 } 317 318 322 public void setCompression(String compression) 323 { 324 this.compression = compression; 325 } 326 327 330 public void setCompressionLevel(int compressionLevel) 331 { 332 this.compressionLevel = compressionLevel; 333 } 334 335 338 public void addConfiguredProperty(Property property) 339 { 340 if (properties == null) properties = new Properties (); 341 342 property.execute(); 344 Properties props = property.getProperties(); 345 Enumeration e = props.keys(); 346 while (e.hasMoreElements()) 347 { 348 String name = (String ) e.nextElement(); 349 String value = props.getProperty(name); 350 log("Adding property: " + property.getClass() + name+"=" + value, 351 Project.MSG_VERBOSE); 352 353 properties.setProperty(name, value); 354 } 355 } 356 357 362 public void addConfiguredPropertyset(PropertySet ps) 363 { 364 if (properties == null) properties = new Properties (); 365 366 properties.putAll(ps.getProperties()); 367 } 368 369 374 public static class InstallerType extends EnumeratedAttribute 375 { 376 377 public String [] getValues() 378 { 379 return new String [] { CompilerConfig.STANDARD, CompilerConfig.WEB}; 380 } 381 } 382 } 383 | Popular Tags |