1 18 19 package org.apache.tools.ant.taskdefs.optional; 20 21 import java.io.File ; 22 import java.util.ArrayList ; 23 import java.util.Enumeration ; 24 import java.util.StringTokenizer ; 25 import java.util.Vector ; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.Task; 29 import org.apache.tools.ant.taskdefs.optional.javah.JavahAdapter; 30 import org.apache.tools.ant.taskdefs.optional.javah.JavahAdapterFactory; 31 import org.apache.tools.ant.types.Commandline; 32 import org.apache.tools.ant.types.Path; 33 import org.apache.tools.ant.types.Reference; 34 import org.apache.tools.ant.util.facade.FacadeTaskHelper; 35 import org.apache.tools.ant.util.facade.ImplementationSpecificArgument; 36 37 67 68 public class Javah extends Task { 69 70 private Vector classes = new Vector (2); 71 private String cls; 72 private File destDir; 73 private Path classpath = null; 74 private File outputFile = null; 75 private boolean verbose = false; 76 private boolean force = false; 77 private boolean old = false; 78 private boolean stubs = false; 79 private Path bootclasspath; 80 private static String lSep = System.getProperty("line.separator"); 82 private FacadeTaskHelper facade = null; 83 84 87 public Javah() { 88 facade = new FacadeTaskHelper(JavahAdapterFactory.getDefault()); 89 } 90 91 95 public void setClass(String cls) { 96 this.cls = cls; 97 } 98 99 103 public ClassArgument createClass() { 104 ClassArgument ga = new ClassArgument(); 105 classes.addElement(ga); 106 return ga; 107 } 108 109 113 public class ClassArgument { 114 private String name; 115 116 117 public ClassArgument() { 118 } 119 120 124 public void setName(String name) { 125 this.name = name; 126 } 127 128 132 public String getName() { 133 return name; 134 } 135 } 136 137 142 public String [] getClasses() { 143 ArrayList al = new ArrayList (); 144 if (cls != null) { 145 StringTokenizer tok = new StringTokenizer (cls, ",", false); 146 while (tok.hasMoreTokens()) { 147 al.add(tok.nextToken().trim()); 148 } 149 } 150 151 Enumeration e = classes.elements(); 152 while (e.hasMoreElements()) { 153 ClassArgument arg = (ClassArgument) e.nextElement(); 154 al.add(arg.getName()); 155 } 156 return (String []) al.toArray(new String [al.size()]); 157 } 158 159 164 public void setDestdir(File destDir) { 165 this.destDir = destDir; 166 } 167 168 173 public File getDestdir() { 174 return destDir; 175 } 176 177 181 public void setClasspath(Path src) { 182 if (classpath == null) { 183 classpath = src; 184 } else { 185 classpath.append(src); 186 } 187 } 188 189 193 public Path createClasspath() { 194 if (classpath == null) { 195 classpath = new Path(getProject()); 196 } 197 return classpath.createPath(); 198 } 199 200 205 public void setClasspathRef(Reference r) { 206 createClasspath().setRefid(r); 207 } 208 209 214 public Path getClasspath() { 215 return classpath; 216 } 217 218 222 public void setBootclasspath(Path src) { 223 if (bootclasspath == null) { 224 bootclasspath = src; 225 } else { 226 bootclasspath.append(src); 227 } 228 } 229 230 234 public Path createBootclasspath() { 235 if (bootclasspath == null) { 236 bootclasspath = new Path(getProject()); 237 } 238 return bootclasspath.createPath(); 239 } 240 241 246 public void setBootClasspathRef(Reference r) { 247 createBootclasspath().setRefid(r); 248 } 249 250 255 public Path getBootclasspath() { 256 return bootclasspath; 257 } 258 259 264 public void setOutputFile(File outputFile) { 265 this.outputFile = outputFile; 266 } 267 268 273 public File getOutputfile() { 274 return outputFile; 275 } 276 277 281 public void setForce(boolean force) { 282 this.force = force; 283 } 284 285 290 public boolean getForce() { 291 return force; 292 } 293 294 301 public void setOld(boolean old) { 302 this.old = old; 303 } 304 305 310 public boolean getOld() { 311 return old; 312 } 313 314 318 public void setStubs(boolean stubs) { 319 this.stubs = stubs; 320 } 321 322 327 public boolean getStubs() { 328 return stubs; 329 } 330 331 336 public void setVerbose(boolean verbose) { 337 this.verbose = verbose; 338 } 339 340 345 public boolean getVerbose() { 346 return verbose; 347 } 348 349 354 public void setImplementation(String impl) { 355 if ("default".equals(impl)) { 356 facade.setImplementation(JavahAdapterFactory.getDefault()); 357 } else { 358 facade.setImplementation(impl); 359 } 360 } 361 362 368 public ImplementationSpecificArgument createArg() { 369 ImplementationSpecificArgument arg = 370 new ImplementationSpecificArgument(); 371 facade.addImplementationArgument(arg); 372 return arg; 373 } 374 375 381 public String [] getCurrentArgs() { 382 return facade.getArgs(); 383 } 384 385 390 public void execute() throws BuildException { 391 393 if ((cls == null) && (classes.size() == 0)) { 394 throw new BuildException("class attribute must be set!", 395 getLocation()); 396 } 397 398 if ((cls != null) && (classes.size() > 0)) { 399 throw new BuildException("set class attribute or class element, " 400 + "not both.", getLocation()); 401 } 402 403 if (destDir != null) { 404 if (!destDir.isDirectory()) { 405 throw new BuildException("destination directory \"" + destDir 406 + "\" does not exist or is not a directory", getLocation()); 407 } 408 if (outputFile != null) { 409 throw new BuildException("destdir and outputFile are mutually " 410 + "exclusive", getLocation()); 411 } 412 } 413 414 if (classpath == null) { 415 classpath = (new Path(getProject())).concatSystemClasspath("last"); 416 } else { 417 classpath = classpath.concatSystemClasspath("ignore"); 418 } 419 420 JavahAdapter ad = 421 JavahAdapterFactory.getAdapter(facade.getImplementation(), 422 this); 423 if (!ad.compile(this)) { 424 throw new BuildException("compilation failed"); 425 } 426 } 427 428 433 public void logAndAddFiles(Commandline cmd) { 434 logAndAddFilesToCompile(cmd); 435 } 436 437 442 protected void logAndAddFilesToCompile(Commandline cmd) { 443 log("Compilation " + cmd.describeArguments(), 444 Project.MSG_VERBOSE); 445 446 StringBuffer niceClassList = new StringBuffer (); 447 String [] c = getClasses(); 448 for (int i = 0; i < c.length; i++) { 449 cmd.createArgument().setValue(c[i]); 450 niceClassList.append(" "); 451 niceClassList.append(c[i]); 452 niceClassList.append(lSep); 453 } 454 455 StringBuffer prefix = new StringBuffer ("Class"); 456 if (c.length > 1) { 457 prefix.append("es"); 458 } 459 prefix.append(" to be compiled:"); 460 prefix.append(lSep); 461 462 log(prefix.toString() + niceClassList.toString(), Project.MSG_VERBOSE); 463 } 464 } 465 466 | Popular Tags |