KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > ant > AntScript


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  * Prosyst - create proper OSGi bundles (bug 174157)
11  *******************************************************************************/

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 /**
19  * Class for producing Ant scripts. Contains convenience methods for creating the
20  * XML elements required for Ant scripts. See the <a HREF="http://jakarta.apache.org/ant">Ant</a>
21  * website for more details on Ant scripts and the particular Ant tasks.
22  */

23 public class AntScript implements IAntScript {
24
25     protected OutputStream out;
26     protected PrintWriter output;
27     protected final String JavaDoc XML_PROLOG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
28
protected int indent = 0;
29
30     /**
31      * Constructor for the class.
32      *
33      * @param out the output stream to write the script to
34      * @throws IOException
35      */

36     public AntScript(OutputStream out) throws IOException {
37         this.out = out;
38         output = new PrintWriter(new OutputStreamWriter(out, "UTF8")); //$NON-NLS-1$
39
output.println(XML_PROLOG);
40     }
41
42     /**
43      * Close the output stream.
44      */

45     public void close() {
46         output.flush();
47         output.close();
48
49         // introduced because sometimes the file was not closed.
50
try {
51             out.close();
52         } catch (IOException e) {
53             e.printStackTrace();
54         }
55     }
56
57     /**
58      * Print an <code>antcall</code> task to the script. This calls Ant on the given
59      * target which is located within the same build file.
60      *
61      * @param target the target of the ant call
62      * @param inheritAll <code>true</code> if the parameters should be pass to the
63      * called target
64      * @param params table of parameters for the call
65      */

66     public void printAntCallTask(String JavaDoc target, boolean inheritAll, Map params) {
67         printTab();
68         output.print("<antcall"); //$NON-NLS-1$
69
printAttribute("target", target, true); //$NON-NLS-1$
70
if (inheritAll == false)
71             printAttribute("inheritAll", "false", false); //$NON-NLS-1$ //$NON-NLS-2$
72
if (params == null)
73             output.println("/>"); //$NON-NLS-1$
74
else {
75             output.println(">"); //$NON-NLS-1$
76
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 JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
81             }
82             indent--;
83             printTab();
84             output.println("</antcall>"); //$NON-NLS-1$
85
}
86     }
87
88     /**
89      * Print a <code>jar</code> Ant task to this script. This jars together a group of
90      * files into a single file.
91      *
92      * @param jarFile the destination file name
93      * @param basedir the base directory
94      * @param manifestAttribute the manifest file to use
95      */

96     public void printJarTask(String JavaDoc jarFile, String JavaDoc basedir, String JavaDoc manifestAttribute) {
97         printJarTask(jarFile, basedir, manifestAttribute, null);
98     }
99
100     /**
101      * Print a <code>jar</code> Ant task to this script. This jars together a group of
102      * files into a single file.
103      *
104      * @param jarFile the destination file name
105      * @param basedir the base directory
106      * @param manifestAttribute the manifest file to use
107      * @param filesetManifest behavior when a Manifest is found in a zipfileset or
108      * zipgroupfileset file is found. Valid values are "skip", "merge", and
109      * "mergewithoutmain". "merge" will merge all of the manifests together,
110      * and merge this into any other specified manifests. "mergewithoutmain"
111      * merges everything but the Main section of the manifests. Default value
112      * is "skip".
113      */

114     public void printJarTask(String JavaDoc jarFile, String JavaDoc basedir, String JavaDoc manifestAttribute, String JavaDoc filesetManifest) {
115         printTab();
116         output.print("<jar"); //$NON-NLS-1$
117
printAttribute("destfile", jarFile, true); //$NON-NLS-1$
118
printAttribute("basedir", basedir, false); //$NON-NLS-1$
119
printAttribute("manifest", manifestAttribute, false); //$NON-NLS-1$
120
printAttribute("filesetmanifest", filesetManifest, false); //$NON-NLS-1$
121
output.println("/>"); //$NON-NLS-1$
122
}
123
124     /**
125      * Print the <code>available</code> Ant task to this script. This task sets a property
126      * value if the given file exists at runtime.
127      *
128      * @param property the property to set
129      * @param file the file to look for
130      */

131     public void printAvailableTask(String JavaDoc property, String JavaDoc file) {
132         printTab();
133         output.print("<available"); //$NON-NLS-1$
134
printAttribute("property", property, true); //$NON-NLS-1$
135
printAttribute("file", file, false); //$NON-NLS-1$
136
output.println("/>"); //$NON-NLS-1$
137
}
138
139     /**
140      * Print the <code>available</code> Ant task to this script. This task sets a property
141      * to the given value if the given file exists at runtime.
142      *
143      * @param property the property to set
144      * @param file the file to look for
145      */

146     public void printAvailableTask(String JavaDoc property, String JavaDoc file, String JavaDoc value){
147         printTab();
148         output.print("<available"); //$NON-NLS-1$
149
printAttribute("property", property, true); //$NON-NLS-1$
150
printAttribute("file", file, false); //$NON-NLS-1$
151
printAttribute("value", value, false); //$NON-NLS-1$
152
output.println("/>"); //$NON-NLS-1$
153
}
154     
155     /**
156      * Print an <code>ant</code> task to this script. This calls Ant on the specified
157      * target contained in the specified Ant file with the given parameters.
158      *
159      * @param antfile the name of the Ant file which contains the target to run
160      * @param dir the basedir for the target
161      * @param target the name of the target
162      * @param outputParam filename to write the output to
163      * @param inheritAll <code>true</code> if the parameters should be passed on
164      * to the ant target
165      * @param properties the table of properties
166      */

167     public void printAntTask(String JavaDoc antfile, String JavaDoc dir, String JavaDoc target, String JavaDoc outputParam, String JavaDoc inheritAll, Map properties ) {
168         printAntTask(antfile, dir, target, outputParam, inheritAll, properties, null );
169     }
170     
171     /**
172      * Print an <code>ant</code> task to this script. This calls Ant on the specified
173      * target contained in the specified Ant file with the given parameters.
174      *
175      * @param antfile the name of the Ant file which contains the target to run
176      * @param dir the basedir for the target
177      * @param target the name of the target
178      * @param outputParam filename to write the output to
179      * @param inheritAll <code>true</code> if the parameters should be passed on
180      * to the ant target
181      * @param properties the table of properties
182      * @param references the table of references
183      */

184     public void printAntTask(String JavaDoc antfile, String JavaDoc dir, String JavaDoc target, String JavaDoc outputParam, String JavaDoc inheritAll, Map properties, Map references) {
185         printTab();
186         output.print("<ant"); //$NON-NLS-1$
187
printAttribute("antfile", antfile, false); //$NON-NLS-1$
188
printAttribute("dir", dir, false); //$NON-NLS-1$
189
printAttribute("target", target, false); //$NON-NLS-1$
190
printAttribute("output", outputParam, false); //$NON-NLS-1$
191
printAttribute("inheritAll", inheritAll, false); //$NON-NLS-1$
192
if (properties == null && references == null)
193             output.println("/>"); //$NON-NLS-1$
194
else {
195             output.println(">"); //$NON-NLS-1$
196
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 JavaDoc) entry.getKey(), (String JavaDoc) 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 JavaDoc)entry.getKey() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
210
if( entry.getValue() != null ){
211                         print(" torefid=\"" + (String JavaDoc) entry.getValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
212
}
213                     print("/>"); //$NON-NLS-1$
214
println();
215                 }
216             }
217             indent--;
218             printTab();
219             output.println("</ant>"); //$NON-NLS-1$
220
}
221     }
222
223     public void printSubantTask(String JavaDoc antfile, String JavaDoc target, String JavaDoc buildpath, String JavaDoc failOnError, String JavaDoc inheritAll, Map properties, Map references) {
224         printTab();
225         output.print("<subant"); //$NON-NLS-1$
226
printAttribute("antfile", antfile, false); //$NON-NLS-1$
227
printAttribute("target", target, false); //$NON-NLS-1$
228
printAttribute("failonerror", failOnError, false); //$NON-NLS-1$
229
printAttribute("buildpath", buildpath, false); //$NON-NLS-1$
230
printAttribute("inheritall", inheritAll, false); //$NON-NLS-1$
231
if (properties == null && references == null)
232             output.println("/>"); //$NON-NLS-1$
233
else {
234             output.println(">"); //$NON-NLS-1$
235
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 JavaDoc) entry.getKey(), (String JavaDoc) 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 JavaDoc)entry.getKey() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
249
if( entry.getValue() != null ){
250                         print(" torefid=\"" + (String JavaDoc) entry.getValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
251
}
252                     print("/>"); //$NON-NLS-1$
253
println();
254                 }
255             }
256             indent--;
257             printTab();
258             output.println("</subant>"); //$NON-NLS-1$
259
}
260     }
261     /**
262      * Print a <code>zip</code> task to this script.
263      *
264      * @param zipfile the destination file name
265      * @param basedir the source directory to start the zip
266      * @param filesOnly <code>true</code> if the resulting zip file should contain only files and not directories
267      * @param update ndicates whether to update or overwrite the destination file if it already exists
268      * @param fileSets the inclusion/exclusion rules to use when zipping
269      */

270     public void printZipTask(String JavaDoc zipfile, String JavaDoc basedir, boolean filesOnly, boolean update, FileSet[] fileSets) {
271         printTab();
272         output.print("<zip"); //$NON-NLS-1$
273
printAttribute("destfile", zipfile, true); //$NON-NLS-1$
274
printAttribute("basedir", basedir, false); //$NON-NLS-1$
275
printAttribute("filesonly", filesOnly ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
276
printAttribute("whenempty", "skip", true); //$NON-NLS-1$//$NON-NLS-2$
277
printAttribute("update", update ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
278
if (fileSets == null)
279             output.println("/>"); //$NON-NLS-1$
280
else {
281             output.println(">"); //$NON-NLS-1$
282
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>"); //$NON-NLS-1$
289
}
290     }
291
292     public void printTarTask(String JavaDoc zipfile, String JavaDoc basedir, boolean filesOnly, boolean update, FileSet[] fileSets) {
293         printTab();
294         output.print("<tar"); //$NON-NLS-1$
295
printAttribute("destfile", zipfile, true); //$NON-NLS-1$
296
printAttribute("basedir", basedir, false); //$NON-NLS-1$
297
printAttribute("compression", "gzip", true); //$NON-NLS-1$//$NON-NLS-2$
298
if (fileSets == null)
299             output.println("/>"); //$NON-NLS-1$
300
else {
301             output.println(">"); //$NON-NLS-1$
302
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>"); //$NON-NLS-1$
309
}
310     }
311
312     /**
313      * Print an <code>arg</code> element to the Ant file.
314      *
315      * @param line
316      */

317     protected void printArg(String JavaDoc line) {
318         printTab();
319         output.print("<arg"); //$NON-NLS-1$
320
printAttribute("line", line, false); //$NON-NLS-1$
321
output.println("/>"); //$NON-NLS-1$
322
}
323
324     /**
325      * Print the given string to the Ant script.
326      *
327      * @param string the string to write to the file
328      */

329     public void printString(String JavaDoc string) {
330         printTab();
331         output.println(getEscaped(string));
332     }
333
334     /**
335      * Print the given comment to the Ant script.
336      *
337      * @param comment the comment to write out
338      */

339     public void printComment(String JavaDoc comment) {
340         printTab();
341         output.print("<!-- "); //$NON-NLS-1$
342
output.print(getEscaped(comment));
343         output.println(" -->"); //$NON-NLS-1$
344
}
345
346     /**
347      * Add the given name/value attribute pair to the script. Do not write the attribute
348      * if the value is <code>null</code> unless a <code>true</code> is specified
349      * indicating that it is mandatory.
350      *
351      * @param name the name of the attribute
352      * @param value the value of the attribute or <code>null</code>
353      * @param mandatory <code>true</code> if the attribute should be printed even
354      * if it is <code>null</code>
355      */

356     public void printAttribute(String JavaDoc name, String JavaDoc value, boolean mandatory) {
357         if (mandatory && value == null)
358             value = ""; //$NON-NLS-1$
359
if (value != null) {
360             output.print(" "); //$NON-NLS-1$
361
output.print(getEscaped(name));
362             output.print("="); //$NON-NLS-1$
363
printQuotes(value);
364         }
365     }
366
367     /**
368      * Print a <code>copy</code> task to the script. The source file is specified
369      * by the <code>file</code> parameter. The destination directory is specified by
370      * the <code>todir</code> parameter.
371      * @param file the source file
372      * @param todir the destination directory
373      * @param fileSets the inclusion/exclusion rules to use when copying
374      * @param overwrite TODO
375      */

376     public void printCopyTask(String JavaDoc file, String JavaDoc todir, FileSet[] fileSets, boolean failOnError, boolean overwrite) {
377         printTab();
378         output.print("<copy"); //$NON-NLS-1$
379
printAttribute("file", file, false); //$NON-NLS-1$
380
printAttribute("todir", todir, false); //$NON-NLS-1$
381
printAttribute("failonerror", failOnError ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
382
printAttribute("overwrite", overwrite ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
383
if (fileSets == null)
384             output.println("/>"); //$NON-NLS-1$
385
else {
386             output.println(">"); //$NON-NLS-1$
387
indent++;
388             for (int i = 0; i < fileSets.length; i++)
389                 fileSets[i].print(this);
390             indent--;
391             printTab();
392             output.println("</copy>"); //$NON-NLS-1$
393
}
394     }
395
396     public void printMoveTask(String JavaDoc todir, FileSet[] fileSets, boolean failOnError) {
397         printTab();
398         output.print("<move"); //$NON-NLS-1$
399
printAttribute("todir", todir, false); //$NON-NLS-1$
400
printAttribute("failonerror", failOnError ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
401
output.println(">"); //$NON-NLS-1$
402
indent++;
403         for (int i = 0; i < fileSets.length; i++)
404             fileSets[i].print(this);
405         indent--;
406         printTab();
407         output.println("</move>"); //$NON-NLS-1$
408
}
409
410     /**
411      * Print a <code>copy</code> tak to the script. The source file is specified by
412      * the <code>file</code> parameter. The destination file is specified by the
413      * <code>toFile</code> parameter.
414      * @param file the source file
415      * @param toFile the destination file
416      */

417     public void printCopyFileTask(String JavaDoc file, String JavaDoc toFile, boolean overwrite) {
418         printTab();
419         output.print("<copy"); //$NON-NLS-1$
420
printAttribute("file", file, false); //$NON-NLS-1$
421
printAttribute("tofile", toFile, false); //$NON-NLS-1$
422
printAttribute("overwrite", overwrite ? "true" : null, false); //$NON-NLS-1$ //$NON-NLS-2$
423
output.println("/>"); //$NON-NLS-1$
424
}
425
426     /**
427      * Print a <code>delete</code> task to the Ant script. At least one of <code>dir</code>
428      * or <code>file</code> is required unless some <code>fileSets</code> are
429      * present.
430      *
431      * @param dir the name of the directory to delete
432      * @param file the name of the file to delete
433      * @param fileSets the specification for the files to delete
434      */

435     public void printDeleteTask(String JavaDoc dir, String JavaDoc file, FileSet[] fileSets) {
436         printTab();
437         output.print("<delete"); //$NON-NLS-1$
438
printAttribute("dir", dir, false); //$NON-NLS-1$
439
printAttribute("file", file, false); //$NON-NLS-1$
440
if (fileSets == null)
441             output.println("/>"); //$NON-NLS-1$
442
else {
443             output.println(">"); //$NON-NLS-1$
444
indent++;
445             for (int i = 0; i < fileSets.length; i++)
446                 fileSets[i].print(this);
447             indent--;
448             printTab();
449             output.println("</delete>"); //$NON-NLS-1$
450
}
451     }
452
453     /**
454      * Print an <code>exec</code> task to the Ant script.
455      *
456      * @param executable the program to execute
457      * @param dir the working directory for the executable
458      * @param lineArgs the arguments for the executable
459      */

460     public void printExecTask(String JavaDoc executable, String JavaDoc dir, List lineArgs, String JavaDoc os) {
461         printTab();
462         output.print("<exec"); //$NON-NLS-1$
463
printAttribute("executable", executable, true); //$NON-NLS-1$
464
printAttribute("dir", dir, false); //$NON-NLS-1$
465
printAttribute("os", os, false); //$NON-NLS-1$
466
if (lineArgs == null || lineArgs.size() == 0)
467             output.println("/>"); //$NON-NLS-1$
468
else {
469             output.println(">"); //$NON-NLS-1$
470
indent++;
471             for (int i = 0; i < lineArgs.size(); i++)
472                 printArg((String JavaDoc) lineArgs.get(i));
473             indent--;
474             printTab();
475             output.println("</exec>"); //$NON-NLS-1$
476
}
477     }
478
479     /**
480      * Print a <code>mkdir</code> task to the Ant script.
481      *
482      * @param dir the name of the directory to create.
483      */

484     public void printMkdirTask(String JavaDoc dir) {
485         printTab();
486         output.print("<mkdir"); //$NON-NLS-1$
487
printAttribute("dir", dir, false); //$NON-NLS-1$
488
output.println("/>"); //$NON-NLS-1$
489
}
490
491     /**
492      * Print a <code>brand</code> task to the Ant script.
493      *
494      * @param root the location of the launcher to brand.
495      * @param icons the list of icons to use in the branding
496      * @param name the name of the resultant launcher
497      */

498     public void printBrandTask(String JavaDoc root, String JavaDoc icons, String JavaDoc name, String JavaDoc os) {
499         printTab();
500         print("<eclipse.brand"); //$NON-NLS-1$
501
printAttribute("root", root, true); //$NON-NLS-1$
502
if (icons != null)
503             printAttribute("icons", icons, true); //$NON-NLS-1$
504
printAttribute("name", name, true); //$NON-NLS-1$
505
printAttribute("os", os, true); //$NON-NLS-1$
506
println("/>"); //$NON-NLS-1$
507
}
508
509     /**
510      * Print an <code>echo</code> task to the Ant script.
511      *
512      * @param message the message to echo to the output
513      */

514     public void printEchoTask(String JavaDoc message) {
515         printTab();
516         output.print("<echo"); //$NON-NLS-1$
517
printAttribute("message", message, true); //$NON-NLS-1$
518
output.println("/>"); //$NON-NLS-1$
519
}
520
521     /**
522      * Print a <code>path</code> structure to the Ant Script.
523      * The list of paths are printed using path.toString(), so paths
524      * can be any Object. Commonly String or ClasspathComputer3_0.ClasspathElement
525      * @param tag - tag for the structure, normally path or classpath
526      * @param id - id for this structure
527      * @param paths - list of paths. Paths are printed using path.toString()
528      */

529     public void printPathStructure(String JavaDoc tag, String JavaDoc id, List paths) {
530         printTab();
531         print("<" + getEscaped(tag)); //$NON-NLS-1$
532
if (id != null)
533             print(" id=\"" + getEscaped(id) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
534
print(">"); //$NON-NLS-1$
535
println();
536
537         if (paths != null) {
538             indent++;
539             for (Iterator iter = paths.iterator(); iter.hasNext();) {
540                 Object JavaDoc path = iter.next();
541                 printTab();
542                 print("<pathelement"); //$NON-NLS-1$
543
printAttribute("path", path.toString(), false); //$NON-NLS-1$
544
print("/>"); //$NON-NLS-1$
545
println();
546             }
547             indent--;
548         }
549         printEndTag(tag);
550     }
551     
552     /**
553      * Print a <code>param</code> tag to the Ant script.
554      *
555      * @param name the parameter name
556      * @param value the parameter value
557      */

558     
559     protected void printParam(String JavaDoc name, String JavaDoc value) {
560         printTab();
561         output.print("<param"); //$NON-NLS-1$
562
printAttribute("name", name, true); //$NON-NLS-1$
563
printAttribute("value", value, true); //$NON-NLS-1$
564
output.println("/>"); //$NON-NLS-1$
565
}
566
567     /**
568      * Print a <code>project</code> tag to the Ant script.
569      *
570      * @param name the name of the project
571      * @param target the name of default target
572      * @param basedir the base directory for all the project's path calculations
573      */

574     public void printProjectDeclaration(String JavaDoc name, String JavaDoc target, String JavaDoc basedir) {
575         output.print("<project"); //$NON-NLS-1$
576
printAttribute("name", name, false); //$NON-NLS-1$
577
printAttribute("default", target, true); //$NON-NLS-1$
578
printAttribute("basedir", basedir, false); //$NON-NLS-1$
579
output.println(">"); //$NON-NLS-1$
580
indent++;
581     }
582
583     /**
584      * Print a <code>project</code> end tag to the Ant script.
585      */

586     public void printProjectEnd() {
587         indent--;
588         printEndTag("project"); //$NON-NLS-1$
589
}
590
591     /**
592      * Print a <code>property</code> tag to the Ant script.
593      *
594      * @param name the property name
595      * @param value the property value
596      */

597     public void printProperty(String JavaDoc name, String JavaDoc value) {
598         printTab();
599         output.print("<property"); //$NON-NLS-1$
600
printAttribute("name", name, true); //$NON-NLS-1$
601
printAttribute("value", value, true); //$NON-NLS-1$
602
output.println("/>"); //$NON-NLS-1$
603
}
604
605     public void printPropertyRefid(String JavaDoc name, String JavaDoc ref) {
606         printTab();
607         output.print("<property"); //$NON-NLS-1$
608
printAttribute("name", name, true); //$NON-NLS-1$
609
printAttribute("refid", ref, true); //$NON-NLS-1$
610
output.println("/>"); //$NON-NLS-1$
611
}
612
613     /**
614      * Print the given string to the Ant script within quotes.
615      *
616      * @param message the string to print
617      */

618     protected void printQuotes(String JavaDoc message) {
619         output.print("\""); //$NON-NLS-1$
620
output.print(getEscaped(message));
621         output.print("\""); //$NON-NLS-1$
622
}
623
624     /**
625      * Print a start tag in the Ant script for the given element name.
626      *
627      * @param tag the name of the element
628      */

629     public void printStartTag(String JavaDoc tag) {
630         printTab();
631         output.print("<"); //$NON-NLS-1$
632
output.print(tag);
633         output.println(">"); //$NON-NLS-1$
634
}
635
636     /**
637      * Print an end tag in the Ant script for the given element name.
638      *
639      * @param tag the name of the element
640      */

641     public void printEndTag(String JavaDoc tag) {
642         printTab();
643         output.print("</"); //$NON-NLS-1$
644
output.print(tag);
645         output.println(">"); //$NON-NLS-1$
646
}
647
648     /**
649      * Print the given number of tabs to the Ant script.
650      */

651     public void printTab() {
652         for (int i = 0; i < indent; i++)
653             output.print("\t"); //$NON-NLS-1$
654
}
655
656     /**
657      * Print the given string to the Ant script followed by a carriage-return.
658      *
659      * @param message the string to print
660      */

661     public void println(String JavaDoc message) {
662         printTab();
663         output.println(message);
664     }
665
666     /**
667      * Print the given string to the Ant script.
668      *
669      * @param message
670      */

671     public void print(String JavaDoc message) {
672         output.print(message);
673     }
674
675     /**
676      * Print a carriage-return to the Ant script.
677      */

678     public void println() {
679         output.println();
680     }
681
682     /**
683      * Print the given task to the Ant script.
684      *
685      * @param task the task to print
686      */

687     public void print(ITask task) {
688         task.print(this);
689     }
690
691     /**
692      * Print a <code>target</code> tag to the Ant script.
693      *
694      * @param name the name of the target
695      * @param depends a comma separated list of required targets
696      * @param ifClause the name of the property that this target depends on
697      * @param unlessClause the name of the property that this target cannot have
698      * @param description a user-readable description of this target
699      */

700     public void printTargetDeclaration(String JavaDoc name, String JavaDoc depends, String JavaDoc ifClause, String JavaDoc unlessClause, String JavaDoc description) {
701         printTab();
702         output.print("<target"); //$NON-NLS-1$
703
printAttribute("name", name, true); //$NON-NLS-1$
704
printAttribute("depends", depends, false); //$NON-NLS-1$
705
printAttribute("if", ifClause, false); //$NON-NLS-1$
706
printAttribute("unless", unlessClause, false); //$NON-NLS-1$
707
printAttribute("description", description, false); //$NON-NLS-1$
708
output.println(">"); //$NON-NLS-1$
709
indent++;
710     }
711
712     /**
713      * Print a closing <code>target</code> tag to the script. Indent the specified
714      * number of tabs.
715      */

716     public void printTargetEnd() {
717         indent--;
718         printEndTag("target"); //$NON-NLS-1$
719
}
720
721     /**
722      * Print a <code>eclipse.refreshLocal</code> task to the script. This task refreshes
723      * the specified resource in the workspace, to the specified depth.
724      *
725      * @param resource the resource to refresh
726      * @param depth one of <code>IResource.DEPTH_ZERO</code>,
727      * <code>IResource.DEPTH_ONE</code>, or <code>IResource.DEPTH_INFINITY</code>
728      */

729     public void printRefreshLocalTask(String JavaDoc resource, String JavaDoc depth) {
730         printTab();
731         output.print("<eclipse.refreshLocal"); //$NON-NLS-1$
732
printAttribute("resource", resource, true); //$NON-NLS-1$
733
printAttribute("depth", depth, false); //$NON-NLS-1$
734
output.println("/>"); //$NON-NLS-1$
735
}
736
737     public void printChmod(String JavaDoc dir, String JavaDoc rights, String JavaDoc files) {
738         printTab();
739         output.print("<chmod perm=\"" + rights + "\" "); //$NON-NLS-1$//$NON-NLS-2$
740
output.print("dir=\"" + getEscaped(dir) + "\" "); //$NON-NLS-1$//$NON-NLS-2$
741
output.print("includes=\"" + getEscaped(files) + "\" /> "); //$NON-NLS-1$ //$NON-NLS-2$
742
output.println();
743     }
744
745     public void printGet(String JavaDoc source, String JavaDoc destination, String JavaDoc login, String JavaDoc password, boolean usetimestamp) {
746         printTab();
747         output.print("<get "); //$NON-NLS-1$
748
printAttribute("username", login, false); //$NON-NLS-1$
749
printAttribute("password", password, false); //$NON-NLS-1$
750
printAttribute("src", source, true); //$NON-NLS-1$
751
printAttribute("dest", destination, true); //$NON-NLS-1$
752
printAttribute("usetimestamp", usetimestamp ? "true" : null, false); //$NON-NLS-1$ //$NON-NLS-2$
753
output.println("/>"); //$NON-NLS-1$
754
}
755
756     public void printGZip(String JavaDoc source, String JavaDoc destination) {
757         printTab();
758         output.println("<gzip SRC=\"" + getEscaped(source) + "\" zipfile=\"" + getEscaped(destination) + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
759
}
760
761     /**
762      * Print a <code> eclipse.convertTask</code> task to the script. This task convert a file path to
763      * an Eclipse resource or vice-versa.
764      *
765      * @param toConvert the entry to convert
766      * @param propertyName the property where to store the result of the convertion
767      * @param isEclipseResource true if toConvert refers to an eclipse resource.
768      */

769     public void printConvertPathTask(String JavaDoc toConvert, String JavaDoc propertyName, boolean isEclipseResource) {
770         printTab();
771         output.print("<eclipse.convertPath"); //$NON-NLS-1$
772
if (isEclipseResource == false)
773             printAttribute("fileSystemPath", toConvert, true); //$NON-NLS-1$
774
else
775             printAttribute("resourcePath", toConvert, true); //$NON-NLS-1$
776
printAttribute("property", propertyName, true); //$NON-NLS-1$
777
output.println("/>"); //$NON-NLS-1$
778
}
779
780     /**
781      * Print a <code> dirname </code> task to the script.
782      * @param property
783      * @param file
784      */

785     public void printDirName(String JavaDoc property, String JavaDoc file) {
786         printTab();
787         output.print("<dirname"); //$NON-NLS-1$
788
printAttribute("property", property, true); //$NON-NLS-1$
789
printAttribute("file", file, true); //$NON-NLS-1$
790
output.println("/>"); //$NON-NLS-1$
791
}
792     
793     /**
794      * Print a <code>Condition</code> task with isset test to the script
795      * @param property name of the property to set
796      * @param value value to set the property to
797      * @param testProperty name of the property for the isset test
798      */

799     public void printConditionIsSet(String JavaDoc property, String JavaDoc value, String JavaDoc testProperty) {
800         println("<condition property=\"" + property + "\" value=\"" + value + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
801
indent++;
802         println("<isset property=\"" + testProperty + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
803
indent--;
804         printEndTag("condition"); //$NON-NLS-1$
805
}
806     
807     public void printTabs() {
808         printTab();
809     }
810     
811     public void printTaskDef(String JavaDoc name, String JavaDoc classname) {
812         printTabs();
813         output.println("<taskdef name=\"" + name+ "\" classname=\"" + classname + "\" />"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
814
}
815     
816     public static String JavaDoc getEscaped(String JavaDoc s) {
817         StringBuffer JavaDoc result = new StringBuffer JavaDoc(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 JavaDoc buffer, char c) {
824         buffer.append(getReplacement(c));
825     }
826     
827     private static String JavaDoc getReplacement(char c) {
828         // Encode special XML characters into the equivalent character references.
829
// These five are defined by default for all XML documents.
830
switch (c) {
831             case '<' :
832                 return "&lt;"; //$NON-NLS-1$
833
case '>' :
834                 return "&gt;"; //$NON-NLS-1$
835
case '"' :
836                 return "&quot;"; //$NON-NLS-1$
837
case '\'' :
838                 return "&apos;"; //$NON-NLS-1$
839
case '&' :
840                 return "&amp;"; //$NON-NLS-1$
841
default :
842                 return String.valueOf(c);
843         }
844     }
845 }
846
Popular Tags