1 18 package org.apache.tools.ant.taskdefs.optional.dotnet; 19 20 import java.io.File ; 21 import java.util.Vector ; 22 import java.util.Iterator ; 23 import java.net.MalformedURLException ; 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.Task; 28 import org.apache.tools.ant.types.EnumeratedAttribute; 29 import org.apache.tools.ant.taskdefs.condition.Os; 30 import org.apache.tools.ant.util.FileUtils; 31 32 53 54 public class WsdlToDotnet extends Task { 55 56 59 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 60 61 64 private File destFile = null; 65 66 69 private String language = "CS"; 70 71 74 private boolean server = false; 75 76 79 private String namespace = null; 80 81 84 private boolean failOnError = true; 85 86 90 protected String extraOptions = null; 91 92 94 95 99 private String protocol = null; 100 101 106 private boolean ideErrors = false; 107 108 112 private Vector schemas = new Vector (); 113 114 118 private Schema wsdl = new Schema(); 119 120 124 private Compiler compiler = null; 125 126 129 public static final String ERROR_DEST_FILE_IS_DIR = "destination file is a directory"; 130 131 134 public static final String ERROR_NO_DEST_FILE = "destination file must be specified"; 135 136 140 public void setDestFile(File destFile) { 141 this.destFile = destFile; 142 } 143 144 149 150 public void setUrl(String url) { 151 wsdl.setUrl(url); 152 } 153 154 158 public void setSrcFile(File srcFile) { 159 wsdl.setFile(srcFile); 160 } 161 162 167 public void setLanguage(String language) { 168 this.language = language; 169 } 170 171 176 177 public void setServer(boolean server) { 178 this.server = server; 179 } 180 181 186 public void setNamespace(String namespace) { 187 this.namespace = namespace; 188 } 189 190 195 public void setFailOnError(boolean failOnError) { 196 this.failOnError = failOnError; 197 } 198 199 205 public void setExtraOptions(String extraOptions) { 206 this.extraOptions = extraOptions; 207 } 208 209 216 public void setIdeErrors(boolean ideErrors) { 217 this.ideErrors = ideErrors; 218 } 219 220 228 public void setProtocol(String protocol) { 229 this.protocol = protocol; 230 } 231 232 238 public void addSchema(Schema source) { 239 schemas.add(source); 240 } 241 242 247 public void setMakeURL(boolean b) { 248 wsdl.setMakeURL(b); 249 } 250 251 256 public void setCompiler(Compiler compiler) { 257 this.compiler = compiler; 258 } 259 260 264 protected void validate() 265 throws BuildException { 266 if (destFile == null) { 267 throw new BuildException(ERROR_NO_DEST_FILE); 268 } 269 if (destFile.isDirectory()) { 270 throw new BuildException( 271 ERROR_DEST_FILE_IS_DIR); 272 } 273 wsdl.validate(); 274 } 275 276 281 public void execute() 282 throws BuildException { 283 log("This task is deprecated and will be removed in a future version\n" 284 + "of Ant. It is now part of the .NET Antlib:\n" 285 + "http://ant.apache.org/antlibs/dotnet/index.html", 286 Project.MSG_WARN); 287 288 if (compiler == null) { 289 compiler = Compiler.createDefaultCompiler(); 290 } 291 validate(); 292 NetCommand command = new NetCommand(this, 293 "WSDL", 294 compiler.getCommand()); 295 command.setFailOnError(failOnError); 296 compiler.applyExtraArgs(command); 298 command.addArgument("/nologo"); 299 command.addArgument("/out:" + destFile); 300 command.addArgument("/language:", language); 301 if (server) { 302 command.addArgument("/server"); 303 } 304 command.addArgument("/namespace:", namespace); 305 if (protocol != null) { 306 command.addArgument("/protocol:" + protocol); 307 } 308 if (ideErrors) { 309 command.addArgument("/parsableErrors"); 310 } 311 command.addArgument(extraOptions); 312 313 boolean rebuild = true; 315 long destLastModified = -1; 316 317 if (destFile.exists()) { 319 destLastModified = destFile.lastModified(); 320 rebuild = isRebuildNeeded(wsdl, destLastModified); 321 } 322 String path; 323 path = wsdl.evaluate(); 325 if (!compiler.supportsAbsoluteFiles() && wsdl.getFile() != null) { 326 File f = wsdl.getFile(); 328 command.setDirectory(f.getParentFile()); 329 path = f.getName(); 330 } 331 command.addArgument(path); 332 Iterator it = schemas.iterator(); 336 while (it.hasNext()) { 337 Schema schema = (Schema) it.next(); 338 rebuild |= isRebuildNeeded(schema, destLastModified); 340 command.addArgument(schema.evaluate()); 341 } 342 if (rebuild) { 344 command.runCommand(); 345 } 346 } 347 348 354 private boolean isRebuildNeeded(Schema schema, long destLastModified) { 355 if (destLastModified == -1) { 356 return true; 357 } 358 return !FILE_UTILS.isUpToDate(schema.getTimestamp(), destLastModified); 359 } 360 361 362 366 public static class Schema { 367 private File file; 368 private String url; 369 private boolean makeURL = false; 370 371 373 public static final String ERROR_NONE_DECLARED = "One of file and url must be set"; 374 375 public static final String ERROR_BOTH_DECLARED = "Only one of file or url can be set"; 376 377 public static final String ERROR_FILE_NOT_FOUND = "Not found: "; 378 379 public static final String ERROR_FILE_IS_DIR = "File is a directory: "; 380 381 public static final String ERROR_NO_URL_CONVERT = "Could not URL convert "; 382 383 386 public void validate() { 387 388 if (file != null) { 389 if (!file.exists()) { 390 throw new BuildException(ERROR_FILE_NOT_FOUND + file.toString()); 391 } 392 if (file.isDirectory()) { 393 throw new BuildException(ERROR_FILE_IS_DIR + file.toString()); 394 } 395 } 396 if (file != null && url != null) { 397 throw new BuildException(ERROR_BOTH_DECLARED); 398 } 399 if (file == null && url == null) { 400 throw new BuildException(ERROR_NONE_DECLARED); 401 } 402 } 403 404 408 public String evaluate() { 409 validate(); 410 if (url != null) { 411 return getUrl(); 412 } 413 if (makeURL) { 414 try { 415 return file.toURL().toExternalForm(); 416 } catch (MalformedURLException e) { 417 throw new BuildException(ERROR_NO_URL_CONVERT + file); 418 } 419 } 420 return file.toString(); 421 } 422 423 427 public File getFile() { 428 return file; 429 } 430 431 435 public void setFile(File file) { 436 this.file = file; 437 } 438 439 443 public String getUrl() { 444 return url; 445 } 446 447 452 public void setUrl(String url) { 453 this.url = url; 454 } 455 456 460 public boolean isMakeURL() { 461 return makeURL; 462 } 463 464 469 public void setMakeURL(boolean makeURL) { 470 this.makeURL = makeURL; 471 } 472 473 477 public long getTimestamp() { 478 if (file != null) { 479 return file.lastModified(); 480 } else { 481 return -1; 482 } 483 } 484 } 485 486 489 public static class Compiler extends EnumeratedAttribute { 490 491 492 public static final String COMPILER_MS = "microsoft"; 493 494 public static final String COMPILER_MONO = "mono"; 495 496 public static final String COMPILER_MS_ON_MONO = "microsoft-on-mono"; 497 499 String [] compilers = { 500 COMPILER_MS, 501 COMPILER_MONO, 502 COMPILER_MS_ON_MONO 503 }; 504 505 506 public static final String EXE_WSDL = "wsdl"; 507 508 public static final String EXE_MONO = "mono"; 509 512 String [] compilerExecutables = { 513 EXE_WSDL, 514 EXE_WSDL, 515 EXE_MONO 516 }; 517 518 519 522 String [][] extraCompilerArgs = { 523 {}, 524 {}, 525 {EXE_WSDL + ".exe"} 526 }; 527 528 boolean[] absoluteFiles = { 529 true, 530 false, 531 true 532 }; 533 534 542 public String [] getValues() { 543 return compilers; 544 } 545 546 550 public static Compiler createDefaultCompiler() { 551 Compiler c = new Compiler (); 552 String compilerName; 553 compilerName = Os.isFamily("windows") ? COMPILER_MS : COMPILER_MONO; 554 c.setValue(compilerName); 555 return c; 556 } 557 558 562 public String getCommand() { 563 return compilerExecutables[getIndex()]; 564 } 565 566 570 public String [] getExtraArgs() { 571 return extraCompilerArgs[getIndex()]; 572 } 573 574 578 public boolean supportsAbsoluteFiles() { 579 return absoluteFiles[getIndex()]; 580 } 581 582 586 public void applyExtraArgs(NetCommand command) { 587 String [] args = getExtraArgs(); 588 for (int i = 0; i < args.length; i++) { 589 command.addArgument(args[i]); 590 } 591 } 592 593 } 594 595 } 596 | Popular Tags |