1 12 package org.eclipse.pde.internal.build.ant; 13 14 import java.io.*; 15 import java.util.*; 16 import org.eclipse.pde.build.IAntScript; 17 18 23 public class AntScript implements IAntScript { 24 25 protected OutputStream out; 26 protected PrintWriter output; 27 protected final String XML_PROLOG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; protected int indent = 0; 29 30 36 public AntScript(OutputStream out) throws IOException { 37 this.out = out; 38 output = new PrintWriter(new OutputStreamWriter(out, "UTF8")); output.println(XML_PROLOG); 40 } 41 42 45 public void close() { 46 output.flush(); 47 output.close(); 48 49 try { 51 out.close(); 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } 55 } 56 57 66 public void printAntCallTask(String target, boolean inheritAll, Map params) { 67 printTab(); 68 output.print("<antcall"); printAttribute("target", target, true); if (inheritAll == false) 71 printAttribute("inheritAll", "false", false); if (params == null) 73 output.println("/>"); else { 75 output.println(">"); indent++; 77 Set entries = params.entrySet(); 78 for (Iterator iter = entries.iterator(); iter.hasNext();) { 79 Map.Entry entry = (Map.Entry) iter.next(); 80 printParam((String ) entry.getKey(), (String ) entry.getValue()); 81 } 82 indent--; 83 printTab(); 84 output.println("</antcall>"); } 86 } 87 88 96 public void printJarTask(String jarFile, String basedir, String manifestAttribute) { 97 printJarTask(jarFile, basedir, manifestAttribute, null); 98 } 99 100 114 public void printJarTask(String jarFile, String basedir, String manifestAttribute, String filesetManifest) { 115 printTab(); 116 output.print("<jar"); printAttribute("destfile", jarFile, true); printAttribute("basedir", basedir, false); printAttribute("manifest", manifestAttribute, false); printAttribute("filesetmanifest", filesetManifest, false); output.println("/>"); } 123 124 131 public void printAvailableTask(String property, String file) { 132 printTab(); 133 output.print("<available"); printAttribute("property", property, true); printAttribute("file", file, false); output.println("/>"); } 138 139 146 public void printAvailableTask(String property, String file, String value){ 147 printTab(); 148 output.print("<available"); printAttribute("property", property, true); printAttribute("file", file, false); printAttribute("value", value, false); output.println("/>"); } 154 155 167 public void printAntTask(String antfile, String dir, String target, String outputParam, String inheritAll, Map properties ) { 168 printAntTask(antfile, dir, target, outputParam, inheritAll, properties, null ); 169 } 170 171 184 public void printAntTask(String antfile, String dir, String target, String outputParam, String inheritAll, Map properties, Map references) { 185 printTab(); 186 output.print("<ant"); printAttribute("antfile", antfile, false); printAttribute("dir", dir, false); printAttribute("target", target, false); printAttribute("output", outputParam, false); printAttribute("inheritAll", inheritAll, false); if (properties == null && references == null) 193 output.println("/>"); else { 195 output.println(">"); indent++; 197 if( properties != null ) { 198 Set entries = properties.entrySet(); 199 for (Iterator iter = entries.iterator(); iter.hasNext();) { 200 Map.Entry entry = (Map.Entry) iter.next(); 201 printProperty((String ) entry.getKey(), (String ) entry.getValue()); 202 } 203 } 204 if( references != null ){ 205 Set entries = references.entrySet(); 206 for (Iterator iter = entries.iterator(); iter.hasNext();) { 207 Map.Entry entry = (Map.Entry) iter.next(); 208 printTab(); 209 print("<reference refid=\"" + (String )entry.getKey() + "\""); if( entry.getValue() != null ){ 211 print(" torefid=\"" + (String ) entry.getValue() + "\""); } 213 print("/>"); println(); 215 } 216 } 217 indent--; 218 printTab(); 219 output.println("</ant>"); } 221 } 222 223 public void printSubantTask(String antfile, String target, String buildpath, String failOnError, String inheritAll, Map properties, Map references) { 224 printTab(); 225 output.print("<subant"); printAttribute("antfile", antfile, false); printAttribute("target", target, false); printAttribute("failonerror", failOnError, false); printAttribute("buildpath", buildpath, false); printAttribute("inheritall", inheritAll, false); if (properties == null && references == null) 232 output.println("/>"); else { 234 output.println(">"); indent++; 236 if( properties != null ) { 237 Set entries = properties.entrySet(); 238 for (Iterator iter = entries.iterator(); iter.hasNext();) { 239 Map.Entry entry = (Map.Entry) iter.next(); 240 printProperty((String ) entry.getKey(), (String ) entry.getValue()); 241 } 242 } 243 if( references != null ){ 244 Set entries = references.entrySet(); 245 for (Iterator iter = entries.iterator(); iter.hasNext();) { 246 Map.Entry entry = (Map.Entry) iter.next(); 247 printTab(); 248 print("<reference refid=\"" + (String )entry.getKey() + "\""); if( entry.getValue() != null ){ 250 print(" torefid=\"" + (String ) entry.getValue() + "\""); } 252 print("/>"); println(); 254 } 255 } 256 indent--; 257 printTab(); 258 output.println("</subant>"); } 260 } 261 270 public void printZipTask(String zipfile, String basedir, boolean filesOnly, boolean update, FileSet[] fileSets) { 271 printTab(); 272 output.print("<zip"); printAttribute("destfile", zipfile, true); printAttribute("basedir", basedir, false); printAttribute("filesonly", filesOnly ? "true" : "false", true); printAttribute("whenempty", "skip", true); printAttribute("update", update ? "true" : "false", true); if (fileSets == null) 279 output.println("/>"); else { 281 output.println(">"); indent++; 283 for (int i = 0; i < fileSets.length; i++) 284 if (fileSets[i] != null) 285 fileSets[i].print(this); 286 indent--; 287 printTab(); 288 output.println("</zip>"); } 290 } 291 292 public void printTarTask(String zipfile, String basedir, boolean filesOnly, boolean update, FileSet[] fileSets) { 293 printTab(); 294 output.print("<tar"); printAttribute("destfile", zipfile, true); printAttribute("basedir", basedir, false); printAttribute("compression", "gzip", true); if (fileSets == null) 299 output.println("/>"); else { 301 output.println(">"); indent++; 303 for (int i = 0; i < fileSets.length; i++) 304 if (fileSets[i] != null) 305 fileSets[i].print(this); 306 indent--; 307 printTab(); 308 output.println("</tar>"); } 310 } 311 312 317 protected void printArg(String line) { 318 printTab(); 319 output.print("<arg"); printAttribute("line", line, false); output.println("/>"); } 323 324 329 public void printString(String string) { 330 printTab(); 331 output.println(getEscaped(string)); 332 } 333 334 339 public void printComment(String comment) { 340 printTab(); 341 output.print("<!-- "); output.print(getEscaped(comment)); 343 output.println(" -->"); } 345 346 356 public void printAttribute(String name, String value, boolean mandatory) { 357 if (mandatory && value == null) 358 value = ""; if (value != null) { 360 output.print(" "); output.print(getEscaped(name)); 362 output.print("="); printQuotes(value); 364 } 365 } 366 367 376 public void printCopyTask(String file, String todir, FileSet[] fileSets, boolean failOnError, boolean overwrite) { 377 printTab(); 378 output.print("<copy"); printAttribute("file", file, false); printAttribute("todir", todir, false); printAttribute("failonerror", failOnError ? "true" : "false", true); printAttribute("overwrite", overwrite ? "true" : "false", true); if (fileSets == null) 384 output.println("/>"); else { 386 output.println(">"); indent++; 388 for (int i = 0; i < fileSets.length; i++) 389 fileSets[i].print(this); 390 indent--; 391 printTab(); 392 output.println("</copy>"); } 394 } 395 396 public void printMoveTask(String todir, FileSet[] fileSets, boolean failOnError) { 397 printTab(); 398 output.print("<move"); printAttribute("todir", todir, false); printAttribute("failonerror", failOnError ? "true" : "false", true); output.println(">"); indent++; 403 for (int i = 0; i < fileSets.length; i++) 404 fileSets[i].print(this); 405 indent--; 406 printTab(); 407 output.println("</move>"); } 409 410 417 public void printCopyFileTask(String file, String toFile, boolean overwrite) { 418 printTab(); 419 output.print("<copy"); printAttribute("file", file, false); printAttribute("tofile", toFile, false); printAttribute("overwrite", overwrite ? "true" : null, false); output.println("/>"); } 425 426 435 public void printDeleteTask(String dir, String file, FileSet[] fileSets) { 436 printTab(); 437 output.print("<delete"); printAttribute("dir", dir, false); printAttribute("file", file, false); if (fileSets == null) 441 output.println("/>"); else { 443 output.println(">"); indent++; 445 for (int i = 0; i < fileSets.length; i++) 446 fileSets[i].print(this); 447 indent--; 448 printTab(); 449 output.println("</delete>"); } 451 } 452 453 460 public void printExecTask(String executable, String dir, List lineArgs, String os) { 461 printTab(); 462 output.print("<exec"); printAttribute("executable", executable, true); printAttribute("dir", dir, false); printAttribute("os", os, false); if (lineArgs == null || lineArgs.size() == 0) 467 output.println("/>"); else { 469 output.println(">"); indent++; 471 for (int i = 0; i < lineArgs.size(); i++) 472 printArg((String ) lineArgs.get(i)); 473 indent--; 474 printTab(); 475 output.println("</exec>"); } 477 } 478 479 484 public void printMkdirTask(String dir) { 485 printTab(); 486 output.print("<mkdir"); printAttribute("dir", dir, false); output.println("/>"); } 490 491 498 public void printBrandTask(String root, String icons, String name, String os) { 499 printTab(); 500 print("<eclipse.brand"); printAttribute("root", root, true); if (icons != null) 503 printAttribute("icons", icons, true); printAttribute("name", name, true); printAttribute("os", os, true); println("/>"); } 508 509 514 public void printEchoTask(String message) { 515 printTab(); 516 output.print("<echo"); printAttribute("message", message, true); output.println("/>"); } 520 521 529 public void printPathStructure(String tag, String id, List paths) { 530 printTab(); 531 print("<" + getEscaped(tag)); if (id != null) 533 print(" id=\"" + getEscaped(id) + "\""); print(">"); println(); 536 537 if (paths != null) { 538 indent++; 539 for (Iterator iter = paths.iterator(); iter.hasNext();) { 540 Object path = iter.next(); 541 printTab(); 542 print("<pathelement"); printAttribute("path", path.toString(), false); print("/>"); println(); 546 } 547 indent--; 548 } 549 printEndTag(tag); 550 } 551 552 558 559 protected void printParam(String name, String value) { 560 printTab(); 561 output.print("<param"); printAttribute("name", name, true); printAttribute("value", value, true); output.println("/>"); } 566 567 574 public void printProjectDeclaration(String name, String target, String basedir) { 575 output.print("<project"); printAttribute("name", name, false); printAttribute("default", target, true); printAttribute("basedir", basedir, false); output.println(">"); indent++; 581 } 582 583 586 public void printProjectEnd() { 587 indent--; 588 printEndTag("project"); } 590 591 597 public void printProperty(String name, String value) { 598 printTab(); 599 output.print("<property"); printAttribute("name", name, true); printAttribute("value", value, true); output.println("/>"); } 604 605 public void printPropertyRefid(String name, String ref) { 606 printTab(); 607 output.print("<property"); printAttribute("name", name, true); printAttribute("refid", ref, true); output.println("/>"); } 612 613 618 protected void printQuotes(String message) { 619 output.print("\""); output.print(getEscaped(message)); 621 output.print("\""); } 623 624 629 public void printStartTag(String tag) { 630 printTab(); 631 output.print("<"); output.print(tag); 633 output.println(">"); } 635 636 641 public void printEndTag(String tag) { 642 printTab(); 643 output.print("</"); output.print(tag); 645 output.println(">"); } 647 648 651 public void printTab() { 652 for (int i = 0; i < indent; i++) 653 output.print("\t"); } 655 656 661 public void println(String message) { 662 printTab(); 663 output.println(message); 664 } 665 666 671 public void print(String message) { 672 output.print(message); 673 } 674 675 678 public void println() { 679 output.println(); 680 } 681 682 687 public void print(ITask task) { 688 task.print(this); 689 } 690 691 700 public void printTargetDeclaration(String name, String depends, String ifClause, String unlessClause, String description) { 701 printTab(); 702 output.print("<target"); printAttribute("name", name, true); printAttribute("depends", depends, false); printAttribute("if", ifClause, false); printAttribute("unless", unlessClause, false); printAttribute("description", description, false); output.println(">"); indent++; 710 } 711 712 716 public void printTargetEnd() { 717 indent--; 718 printEndTag("target"); } 720 721 729 public void printRefreshLocalTask(String resource, String depth) { 730 printTab(); 731 output.print("<eclipse.refreshLocal"); printAttribute("resource", resource, true); printAttribute("depth", depth, false); output.println("/>"); } 736 737 public void printChmod(String dir, String rights, String files) { 738 printTab(); 739 output.print("<chmod perm=\"" + rights + "\" "); output.print("dir=\"" + getEscaped(dir) + "\" "); output.print("includes=\"" + getEscaped(files) + "\" /> "); output.println(); 743 } 744 745 public void printGet(String source, String destination, String login, String password, boolean usetimestamp) { 746 printTab(); 747 output.print("<get "); printAttribute("username", login, false); printAttribute("password", password, false); printAttribute("src", source, true); printAttribute("dest", destination, true); printAttribute("usetimestamp", usetimestamp ? "true" : null, false); output.println("/>"); } 755 756 public void printGZip(String source, String destination) { 757 printTab(); 758 output.println("<gzip SRC=\"" + getEscaped(source) + "\" zipfile=\"" + getEscaped(destination) + "\"/>"); } 760 761 769 public void printConvertPathTask(String toConvert, String propertyName, boolean isEclipseResource) { 770 printTab(); 771 output.print("<eclipse.convertPath"); if (isEclipseResource == false) 773 printAttribute("fileSystemPath", toConvert, true); else 775 printAttribute("resourcePath", toConvert, true); printAttribute("property", propertyName, true); output.println("/>"); } 779 780 785 public void printDirName(String property, String file) { 786 printTab(); 787 output.print("<dirname"); printAttribute("property", property, true); printAttribute("file", file, true); output.println("/>"); } 792 793 799 public void printConditionIsSet(String property, String value, String testProperty) { 800 println("<condition property=\"" + property + "\" value=\"" + value + "\">"); indent++; 802 println("<isset property=\"" + testProperty + "\"/>"); indent--; 804 printEndTag("condition"); } 806 807 public void printTabs() { 808 printTab(); 809 } 810 811 public void printTaskDef(String name, String classname) { 812 printTabs(); 813 output.println("<taskdef name=\"" + name+ "\" classname=\"" + classname + "\" />"); } 815 816 public static String getEscaped(String s) { 817 StringBuffer result = new StringBuffer (s.length() + 10); 818 for (int i = 0; i < s.length(); ++i) 819 appendEscapedChar(result, s.charAt(i)); 820 return result.toString(); 821 } 822 823 private static void appendEscapedChar(StringBuffer buffer, char c) { 824 buffer.append(getReplacement(c)); 825 } 826 827 private static String getReplacement(char c) { 828 switch (c) { 831 case '<' : 832 return "<"; case '>' : 834 return ">"; case '"' : 836 return """; case '\'' : 838 return "'"; case '&' : 840 return "&"; default : 842 return String.valueOf(c); 843 } 844 } 845 } 846 | Popular Tags |