001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM - Initial API and implementation
010: * Prosyst - create proper OSGi bundles (bug 174157)
011: *******************************************************************************/package org.eclipse.pde.internal.build.ant;
012:
013: import java.io.*;
014: import java.util.*;
015: import org.eclipse.pde.build.IAntScript;
016:
017: /**
018: * Class for producing Ant scripts. Contains convenience methods for creating the
019: * XML elements required for Ant scripts. See the <a href="http://jakarta.apache.org/ant">Ant</a>
020: * website for more details on Ant scripts and the particular Ant tasks.
021: */
022: public class AntScript implements IAntScript {
023:
024: protected OutputStream out;
025: protected PrintWriter output;
026: protected final String XML_PROLOG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
027: protected int indent = 0;
028:
029: /**
030: * Constructor for the class.
031: *
032: * @param out the output stream to write the script to
033: * @throws IOException
034: */
035: public AntScript(OutputStream out) throws IOException {
036: this .out = out;
037: output = new PrintWriter(new OutputStreamWriter(out, "UTF8")); //$NON-NLS-1$
038: output.println(XML_PROLOG);
039: }
040:
041: /**
042: * Close the output stream.
043: */
044: public void close() {
045: output.flush();
046: output.close();
047:
048: // introduced because sometimes the file was not closed.
049: try {
050: out.close();
051: } catch (IOException e) {
052: e.printStackTrace();
053: }
054: }
055:
056: /**
057: * Print an <code>antcall</code> task to the script. This calls Ant on the given
058: * target which is located within the same build file.
059: *
060: * @param target the target of the ant call
061: * @param inheritAll <code>true</code> if the parameters should be pass to the
062: * called target
063: * @param params table of parameters for the call
064: */
065: public void printAntCallTask(String target, boolean inheritAll,
066: Map params) {
067: printTab();
068: output.print("<antcall"); //$NON-NLS-1$
069: printAttribute("target", target, true); //$NON-NLS-1$
070: if (inheritAll == false)
071: printAttribute("inheritAll", "false", false); //$NON-NLS-1$ //$NON-NLS-2$
072: if (params == null)
073: output.println("/>"); //$NON-NLS-1$
074: else {
075: output.println(">"); //$NON-NLS-1$
076: indent++;
077: Set entries = params.entrySet();
078: for (Iterator iter = entries.iterator(); iter.hasNext();) {
079: Map.Entry entry = (Map.Entry) iter.next();
080: printParam((String) entry.getKey(), (String) entry
081: .getValue());
082: }
083: indent--;
084: printTab();
085: output.println("</antcall>"); //$NON-NLS-1$
086: }
087: }
088:
089: /**
090: * Print a <code>jar</code> Ant task to this script. This jars together a group of
091: * files into a single file.
092: *
093: * @param jarFile the destination file name
094: * @param basedir the base directory
095: * @param manifestAttribute the manifest file to use
096: */
097: public void printJarTask(String jarFile, String basedir,
098: String manifestAttribute) {
099: printJarTask(jarFile, basedir, manifestAttribute, null);
100: }
101:
102: /**
103: * Print a <code>jar</code> Ant task to this script. This jars together a group of
104: * files into a single file.
105: *
106: * @param jarFile the destination file name
107: * @param basedir the base directory
108: * @param manifestAttribute the manifest file to use
109: * @param filesetManifest behavior when a Manifest is found in a zipfileset or
110: * zipgroupfileset file is found. Valid values are "skip", "merge", and
111: * "mergewithoutmain". "merge" will merge all of the manifests together,
112: * and merge this into any other specified manifests. "mergewithoutmain"
113: * merges everything but the Main section of the manifests. Default value
114: * is "skip".
115: */
116: public void printJarTask(String jarFile, String basedir,
117: String manifestAttribute, String filesetManifest) {
118: printTab();
119: output.print("<jar"); //$NON-NLS-1$
120: printAttribute("destfile", jarFile, true); //$NON-NLS-1$
121: printAttribute("basedir", basedir, false); //$NON-NLS-1$
122: printAttribute("manifest", manifestAttribute, false); //$NON-NLS-1$
123: printAttribute("filesetmanifest", filesetManifest, false); //$NON-NLS-1$
124: output.println("/>"); //$NON-NLS-1$
125: }
126:
127: /**
128: * Print the <code>available</code> Ant task to this script. This task sets a property
129: * value if the given file exists at runtime.
130: *
131: * @param property the property to set
132: * @param file the file to look for
133: */
134: public void printAvailableTask(String property, String file) {
135: printTab();
136: output.print("<available"); //$NON-NLS-1$
137: printAttribute("property", property, true); //$NON-NLS-1$
138: printAttribute("file", file, false); //$NON-NLS-1$
139: output.println("/>"); //$NON-NLS-1$
140: }
141:
142: /**
143: * Print the <code>available</code> Ant task to this script. This task sets a property
144: * to the given value if the given file exists at runtime.
145: *
146: * @param property the property to set
147: * @param file the file to look for
148: */
149: public void printAvailableTask(String property, String file,
150: String value) {
151: printTab();
152: output.print("<available"); //$NON-NLS-1$
153: printAttribute("property", property, true); //$NON-NLS-1$
154: printAttribute("file", file, false); //$NON-NLS-1$
155: printAttribute("value", value, false); //$NON-NLS-1$
156: output.println("/>"); //$NON-NLS-1$
157: }
158:
159: /**
160: * Print an <code>ant</code> task to this script. This calls Ant on the specified
161: * target contained in the specified Ant file with the given parameters.
162: *
163: * @param antfile the name of the Ant file which contains the target to run
164: * @param dir the basedir for the target
165: * @param target the name of the target
166: * @param outputParam filename to write the output to
167: * @param inheritAll <code>true</code> if the parameters should be passed on
168: * to the ant target
169: * @param properties the table of properties
170: */
171: public void printAntTask(String antfile, String dir, String target,
172: String outputParam, String inheritAll, Map properties) {
173: printAntTask(antfile, dir, target, outputParam, inheritAll,
174: properties, null);
175: }
176:
177: /**
178: * Print an <code>ant</code> task to this script. This calls Ant on the specified
179: * target contained in the specified Ant file with the given parameters.
180: *
181: * @param antfile the name of the Ant file which contains the target to run
182: * @param dir the basedir for the target
183: * @param target the name of the target
184: * @param outputParam filename to write the output to
185: * @param inheritAll <code>true</code> if the parameters should be passed on
186: * to the ant target
187: * @param properties the table of properties
188: * @param references the table of references
189: */
190: public void printAntTask(String antfile, String dir, String target,
191: String outputParam, String inheritAll, Map properties,
192: Map references) {
193: printTab();
194: output.print("<ant"); //$NON-NLS-1$
195: printAttribute("antfile", antfile, false); //$NON-NLS-1$
196: printAttribute("dir", dir, false); //$NON-NLS-1$
197: printAttribute("target", target, false); //$NON-NLS-1$
198: printAttribute("output", outputParam, false); //$NON-NLS-1$
199: printAttribute("inheritAll", inheritAll, false); //$NON-NLS-1$
200: if (properties == null && references == null)
201: output.println("/>"); //$NON-NLS-1$
202: else {
203: output.println(">"); //$NON-NLS-1$
204: indent++;
205: if (properties != null) {
206: Set entries = properties.entrySet();
207: for (Iterator iter = entries.iterator(); iter.hasNext();) {
208: Map.Entry entry = (Map.Entry) iter.next();
209: printProperty((String) entry.getKey(),
210: (String) entry.getValue());
211: }
212: }
213: if (references != null) {
214: Set entries = references.entrySet();
215: for (Iterator iter = entries.iterator(); iter.hasNext();) {
216: Map.Entry entry = (Map.Entry) iter.next();
217: printTab();
218: print("<reference refid=\"" + (String) entry.getKey() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
219: if (entry.getValue() != null) {
220: print(" torefid=\"" + (String) entry.getValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
221: }
222: print("/>"); //$NON-NLS-1$
223: println();
224: }
225: }
226: indent--;
227: printTab();
228: output.println("</ant>"); //$NON-NLS-1$
229: }
230: }
231:
232: public void printSubantTask(String antfile, String target,
233: String buildpath, String failOnError, String inheritAll,
234: Map properties, Map references) {
235: printTab();
236: output.print("<subant"); //$NON-NLS-1$
237: printAttribute("antfile", antfile, false); //$NON-NLS-1$
238: printAttribute("target", target, false); //$NON-NLS-1$
239: printAttribute("failonerror", failOnError, false); //$NON-NLS-1$
240: printAttribute("buildpath", buildpath, false); //$NON-NLS-1$
241: printAttribute("inheritall", inheritAll, false); //$NON-NLS-1$
242: if (properties == null && references == null)
243: output.println("/>"); //$NON-NLS-1$
244: else {
245: output.println(">"); //$NON-NLS-1$
246: indent++;
247: if (properties != null) {
248: Set entries = properties.entrySet();
249: for (Iterator iter = entries.iterator(); iter.hasNext();) {
250: Map.Entry entry = (Map.Entry) iter.next();
251: printProperty((String) entry.getKey(),
252: (String) entry.getValue());
253: }
254: }
255: if (references != null) {
256: Set entries = references.entrySet();
257: for (Iterator iter = entries.iterator(); iter.hasNext();) {
258: Map.Entry entry = (Map.Entry) iter.next();
259: printTab();
260: print("<reference refid=\"" + (String) entry.getKey() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
261: if (entry.getValue() != null) {
262: print(" torefid=\"" + (String) entry.getValue() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
263: }
264: print("/>"); //$NON-NLS-1$
265: println();
266: }
267: }
268: indent--;
269: printTab();
270: output.println("</subant>"); //$NON-NLS-1$
271: }
272: }
273:
274: /**
275: * Print a <code>zip</code> task to this script.
276: *
277: * @param zipfile the destination file name
278: * @param basedir the source directory to start the zip
279: * @param filesOnly <code>true</code> if the resulting zip file should contain only files and not directories
280: * @param update ndicates whether to update or overwrite the destination file if it already exists
281: * @param fileSets the inclusion/exclusion rules to use when zipping
282: */
283: public void printZipTask(String zipfile, String basedir,
284: boolean filesOnly, boolean update, FileSet[] fileSets) {
285: printTab();
286: output.print("<zip"); //$NON-NLS-1$
287: printAttribute("destfile", zipfile, true); //$NON-NLS-1$
288: printAttribute("basedir", basedir, false); //$NON-NLS-1$
289: printAttribute("filesonly", filesOnly ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
290: printAttribute("whenempty", "skip", true); //$NON-NLS-1$//$NON-NLS-2$
291: printAttribute("update", update ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
292: if (fileSets == null)
293: output.println("/>"); //$NON-NLS-1$
294: else {
295: output.println(">"); //$NON-NLS-1$
296: indent++;
297: for (int i = 0; i < fileSets.length; i++)
298: if (fileSets[i] != null)
299: fileSets[i].print(this );
300: indent--;
301: printTab();
302: output.println("</zip>"); //$NON-NLS-1$
303: }
304: }
305:
306: public void printTarTask(String zipfile, String basedir,
307: boolean filesOnly, boolean update, FileSet[] fileSets) {
308: printTab();
309: output.print("<tar"); //$NON-NLS-1$
310: printAttribute("destfile", zipfile, true); //$NON-NLS-1$
311: printAttribute("basedir", basedir, false); //$NON-NLS-1$
312: printAttribute("compression", "gzip", true); //$NON-NLS-1$//$NON-NLS-2$
313: if (fileSets == null)
314: output.println("/>"); //$NON-NLS-1$
315: else {
316: output.println(">"); //$NON-NLS-1$
317: indent++;
318: for (int i = 0; i < fileSets.length; i++)
319: if (fileSets[i] != null)
320: fileSets[i].print(this );
321: indent--;
322: printTab();
323: output.println("</tar>"); //$NON-NLS-1$
324: }
325: }
326:
327: /**
328: * Print an <code>arg</code> element to the Ant file.
329: *
330: * @param line
331: */
332: protected void printArg(String line) {
333: printTab();
334: output.print("<arg"); //$NON-NLS-1$
335: printAttribute("line", line, false); //$NON-NLS-1$
336: output.println("/>"); //$NON-NLS-1$
337: }
338:
339: /**
340: * Print the given string to the Ant script.
341: *
342: * @param string the string to write to the file
343: */
344: public void printString(String string) {
345: printTab();
346: output.println(getEscaped(string));
347: }
348:
349: /**
350: * Print the given comment to the Ant script.
351: *
352: * @param comment the comment to write out
353: */
354: public void printComment(String comment) {
355: printTab();
356: output.print("<!-- "); //$NON-NLS-1$
357: output.print(getEscaped(comment));
358: output.println(" -->"); //$NON-NLS-1$
359: }
360:
361: /**
362: * Add the given name/value attribute pair to the script. Do not write the attribute
363: * if the value is <code>null</code> unless a <code>true</code> is specified
364: * indicating that it is mandatory.
365: *
366: * @param name the name of the attribute
367: * @param value the value of the attribute or <code>null</code>
368: * @param mandatory <code>true</code> if the attribute should be printed even
369: * if it is <code>null</code>
370: */
371: public void printAttribute(String name, String value,
372: boolean mandatory) {
373: if (mandatory && value == null)
374: value = ""; //$NON-NLS-1$
375: if (value != null) {
376: output.print(" "); //$NON-NLS-1$
377: output.print(getEscaped(name));
378: output.print("="); //$NON-NLS-1$
379: printQuotes(value);
380: }
381: }
382:
383: /**
384: * Print a <code>copy</code> task to the script. The source file is specified
385: * by the <code>file</code> parameter. The destination directory is specified by
386: * the <code>todir</code> parameter.
387: * @param file the source file
388: * @param todir the destination directory
389: * @param fileSets the inclusion/exclusion rules to use when copying
390: * @param overwrite TODO
391: */
392: public void printCopyTask(String file, String todir,
393: FileSet[] fileSets, boolean failOnError, boolean overwrite) {
394: printTab();
395: output.print("<copy"); //$NON-NLS-1$
396: printAttribute("file", file, false); //$NON-NLS-1$
397: printAttribute("todir", todir, false); //$NON-NLS-1$
398: printAttribute(
399: "failonerror", failOnError ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
400: printAttribute("overwrite", overwrite ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
401: if (fileSets == null)
402: output.println("/>"); //$NON-NLS-1$
403: else {
404: output.println(">"); //$NON-NLS-1$
405: indent++;
406: for (int i = 0; i < fileSets.length; i++)
407: fileSets[i].print(this );
408: indent--;
409: printTab();
410: output.println("</copy>"); //$NON-NLS-1$
411: }
412: }
413:
414: public void printMoveTask(String todir, FileSet[] fileSets,
415: boolean failOnError) {
416: printTab();
417: output.print("<move"); //$NON-NLS-1$
418: printAttribute("todir", todir, false); //$NON-NLS-1$
419: printAttribute(
420: "failonerror", failOnError ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
421: output.println(">"); //$NON-NLS-1$
422: indent++;
423: for (int i = 0; i < fileSets.length; i++)
424: fileSets[i].print(this );
425: indent--;
426: printTab();
427: output.println("</move>"); //$NON-NLS-1$
428: }
429:
430: /**
431: * Print a <code>copy</code> tak to the script. The source file is specified by
432: * the <code>file</code> parameter. The destination file is specified by the
433: * <code>toFile</code> parameter.
434: * @param file the source file
435: * @param toFile the destination file
436: */
437: public void printCopyFileTask(String file, String toFile,
438: boolean overwrite) {
439: printTab();
440: output.print("<copy"); //$NON-NLS-1$
441: printAttribute("file", file, false); //$NON-NLS-1$
442: printAttribute("tofile", toFile, false); //$NON-NLS-1$
443: printAttribute("overwrite", overwrite ? "true" : null, false); //$NON-NLS-1$ //$NON-NLS-2$
444: output.println("/>"); //$NON-NLS-1$
445: }
446:
447: /**
448: * Print a <code>delete</code> task to the Ant script. At least one of <code>dir</code>
449: * or <code>file</code> is required unless some <code>fileSets</code> are
450: * present.
451: *
452: * @param dir the name of the directory to delete
453: * @param file the name of the file to delete
454: * @param fileSets the specification for the files to delete
455: */
456: public void printDeleteTask(String dir, String file,
457: FileSet[] fileSets) {
458: printTab();
459: output.print("<delete"); //$NON-NLS-1$
460: printAttribute("dir", dir, false); //$NON-NLS-1$
461: printAttribute("file", file, false); //$NON-NLS-1$
462: if (fileSets == null)
463: output.println("/>"); //$NON-NLS-1$
464: else {
465: output.println(">"); //$NON-NLS-1$
466: indent++;
467: for (int i = 0; i < fileSets.length; i++)
468: fileSets[i].print(this );
469: indent--;
470: printTab();
471: output.println("</delete>"); //$NON-NLS-1$
472: }
473: }
474:
475: /**
476: * Print an <code>exec</code> task to the Ant script.
477: *
478: * @param executable the program to execute
479: * @param dir the working directory for the executable
480: * @param lineArgs the arguments for the executable
481: */
482: public void printExecTask(String executable, String dir,
483: List lineArgs, String os) {
484: printTab();
485: output.print("<exec"); //$NON-NLS-1$
486: printAttribute("executable", executable, true); //$NON-NLS-1$
487: printAttribute("dir", dir, false); //$NON-NLS-1$
488: printAttribute("os", os, false); //$NON-NLS-1$
489: if (lineArgs == null || lineArgs.size() == 0)
490: output.println("/>"); //$NON-NLS-1$
491: else {
492: output.println(">"); //$NON-NLS-1$
493: indent++;
494: for (int i = 0; i < lineArgs.size(); i++)
495: printArg((String) lineArgs.get(i));
496: indent--;
497: printTab();
498: output.println("</exec>"); //$NON-NLS-1$
499: }
500: }
501:
502: /**
503: * Print a <code>mkdir</code> task to the Ant script.
504: *
505: * @param dir the name of the directory to create.
506: */
507: public void printMkdirTask(String dir) {
508: printTab();
509: output.print("<mkdir"); //$NON-NLS-1$
510: printAttribute("dir", dir, false); //$NON-NLS-1$
511: output.println("/>"); //$NON-NLS-1$
512: }
513:
514: /**
515: * Print a <code>brand</code> task to the Ant script.
516: *
517: * @param root the location of the launcher to brand.
518: * @param icons the list of icons to use in the branding
519: * @param name the name of the resultant launcher
520: */
521: public void printBrandTask(String root, String icons, String name,
522: String os) {
523: printTab();
524: print("<eclipse.brand"); //$NON-NLS-1$
525: printAttribute("root", root, true); //$NON-NLS-1$
526: if (icons != null)
527: printAttribute("icons", icons, true); //$NON-NLS-1$
528: printAttribute("name", name, true); //$NON-NLS-1$
529: printAttribute("os", os, true); //$NON-NLS-1$
530: println("/>"); //$NON-NLS-1$
531: }
532:
533: /**
534: * Print an <code>echo</code> task to the Ant script.
535: *
536: * @param message the message to echo to the output
537: */
538: public void printEchoTask(String message) {
539: printTab();
540: output.print("<echo"); //$NON-NLS-1$
541: printAttribute("message", message, true); //$NON-NLS-1$
542: output.println("/>"); //$NON-NLS-1$
543: }
544:
545: /**
546: * Print a <code>path</code> structure to the Ant Script.
547: * The list of paths are printed using path.toString(), so paths
548: * can be any Object. Commonly String or ClasspathComputer3_0.ClasspathElement
549: * @param tag - tag for the structure, normally path or classpath
550: * @param id - id for this structure
551: * @param paths - list of paths. Paths are printed using path.toString()
552: */
553: public void printPathStructure(String tag, String id, List paths) {
554: printTab();
555: print("<" + getEscaped(tag)); //$NON-NLS-1$
556: if (id != null)
557: print(" id=\"" + getEscaped(id) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
558: print(">"); //$NON-NLS-1$
559: println();
560:
561: if (paths != null) {
562: indent++;
563: for (Iterator iter = paths.iterator(); iter.hasNext();) {
564: Object path = iter.next();
565: printTab();
566: print("<pathelement"); //$NON-NLS-1$
567: printAttribute("path", path.toString(), false); //$NON-NLS-1$
568: print("/>"); //$NON-NLS-1$
569: println();
570: }
571: indent--;
572: }
573: printEndTag(tag);
574: }
575:
576: /**
577: * Print a <code>param</code> tag to the Ant script.
578: *
579: * @param name the parameter name
580: * @param value the parameter value
581: */
582:
583: protected void printParam(String name, String value) {
584: printTab();
585: output.print("<param"); //$NON-NLS-1$
586: printAttribute("name", name, true); //$NON-NLS-1$
587: printAttribute("value", value, true); //$NON-NLS-1$
588: output.println("/>"); //$NON-NLS-1$
589: }
590:
591: /**
592: * Print a <code>project</code> tag to the Ant script.
593: *
594: * @param name the name of the project
595: * @param target the name of default target
596: * @param basedir the base directory for all the project's path calculations
597: */
598: public void printProjectDeclaration(String name, String target,
599: String basedir) {
600: output.print("<project"); //$NON-NLS-1$
601: printAttribute("name", name, false); //$NON-NLS-1$
602: printAttribute("default", target, true); //$NON-NLS-1$
603: printAttribute("basedir", basedir, false); //$NON-NLS-1$
604: output.println(">"); //$NON-NLS-1$
605: indent++;
606: }
607:
608: /**
609: * Print a <code>project</code> end tag to the Ant script.
610: */
611: public void printProjectEnd() {
612: indent--;
613: printEndTag("project"); //$NON-NLS-1$
614: }
615:
616: /**
617: * Print a <code>property</code> tag to the Ant script.
618: *
619: * @param name the property name
620: * @param value the property value
621: */
622: public void printProperty(String name, String value) {
623: printTab();
624: output.print("<property"); //$NON-NLS-1$
625: printAttribute("name", name, true); //$NON-NLS-1$
626: printAttribute("value", value, true); //$NON-NLS-1$
627: output.println("/>"); //$NON-NLS-1$
628: }
629:
630: public void printPropertyRefid(String name, String ref) {
631: printTab();
632: output.print("<property"); //$NON-NLS-1$
633: printAttribute("name", name, true); //$NON-NLS-1$
634: printAttribute("refid", ref, true); //$NON-NLS-1$
635: output.println("/>"); //$NON-NLS-1$
636: }
637:
638: /**
639: * Print the given string to the Ant script within quotes.
640: *
641: * @param message the string to print
642: */
643: protected void printQuotes(String message) {
644: output.print("\""); //$NON-NLS-1$
645: output.print(getEscaped(message));
646: output.print("\""); //$NON-NLS-1$
647: }
648:
649: /**
650: * Print a start tag in the Ant script for the given element name.
651: *
652: * @param tag the name of the element
653: */
654: public void printStartTag(String tag) {
655: printTab();
656: output.print("<"); //$NON-NLS-1$
657: output.print(tag);
658: output.println(">"); //$NON-NLS-1$
659: }
660:
661: /**
662: * Print an end tag in the Ant script for the given element name.
663: *
664: * @param tag the name of the element
665: */
666: public void printEndTag(String tag) {
667: printTab();
668: output.print("</"); //$NON-NLS-1$
669: output.print(tag);
670: output.println(">"); //$NON-NLS-1$
671: }
672:
673: /**
674: * Print the given number of tabs to the Ant script.
675: */
676: public void printTab() {
677: for (int i = 0; i < indent; i++)
678: output.print("\t"); //$NON-NLS-1$
679: }
680:
681: /**
682: * Print the given string to the Ant script followed by a carriage-return.
683: *
684: * @param message the string to print
685: */
686: public void println(String message) {
687: printTab();
688: output.println(message);
689: }
690:
691: /**
692: * Print the given string to the Ant script.
693: *
694: * @param message
695: */
696: public void print(String message) {
697: output.print(message);
698: }
699:
700: /**
701: * Print a carriage-return to the Ant script.
702: */
703: public void println() {
704: output.println();
705: }
706:
707: /**
708: * Print the given task to the Ant script.
709: *
710: * @param task the task to print
711: */
712: public void print(ITask task) {
713: task.print(this );
714: }
715:
716: /**
717: * Print a <code>target</code> tag to the Ant script.
718: *
719: * @param name the name of the target
720: * @param depends a comma separated list of required targets
721: * @param ifClause the name of the property that this target depends on
722: * @param unlessClause the name of the property that this target cannot have
723: * @param description a user-readable description of this target
724: */
725: public void printTargetDeclaration(String name, String depends,
726: String ifClause, String unlessClause, String description) {
727: printTab();
728: output.print("<target"); //$NON-NLS-1$
729: printAttribute("name", name, true); //$NON-NLS-1$
730: printAttribute("depends", depends, false); //$NON-NLS-1$
731: printAttribute("if", ifClause, false); //$NON-NLS-1$
732: printAttribute("unless", unlessClause, false); //$NON-NLS-1$
733: printAttribute("description", description, false); //$NON-NLS-1$
734: output.println(">"); //$NON-NLS-1$
735: indent++;
736: }
737:
738: /**
739: * Print a closing <code>target</code> tag to the script. Indent the specified
740: * number of tabs.
741: */
742: public void printTargetEnd() {
743: indent--;
744: printEndTag("target"); //$NON-NLS-1$
745: }
746:
747: /**
748: * Print a <code>eclipse.refreshLocal</code> task to the script. This task refreshes
749: * the specified resource in the workspace, to the specified depth.
750: *
751: * @param resource the resource to refresh
752: * @param depth one of <code>IResource.DEPTH_ZERO</code>,
753: * <code>IResource.DEPTH_ONE</code>, or <code>IResource.DEPTH_INFINITY</code>
754: */
755: public void printRefreshLocalTask(String resource, String depth) {
756: printTab();
757: output.print("<eclipse.refreshLocal"); //$NON-NLS-1$
758: printAttribute("resource", resource, true); //$NON-NLS-1$
759: printAttribute("depth", depth, false); //$NON-NLS-1$
760: output.println("/>"); //$NON-NLS-1$
761: }
762:
763: public void printChmod(String dir, String rights, String files) {
764: printTab();
765: output.print("<chmod perm=\"" + rights + "\" "); //$NON-NLS-1$//$NON-NLS-2$
766: output.print("dir=\"" + getEscaped(dir) + "\" "); //$NON-NLS-1$//$NON-NLS-2$
767: output.print("includes=\"" + getEscaped(files) + "\" /> "); //$NON-NLS-1$ //$NON-NLS-2$
768: output.println();
769: }
770:
771: public void printGet(String source, String destination,
772: String login, String password, boolean usetimestamp) {
773: printTab();
774: output.print("<get "); //$NON-NLS-1$
775: printAttribute("username", login, false); //$NON-NLS-1$
776: printAttribute("password", password, false); //$NON-NLS-1$
777: printAttribute("src", source, true); //$NON-NLS-1$
778: printAttribute("dest", destination, true); //$NON-NLS-1$
779: printAttribute(
780: "usetimestamp", usetimestamp ? "true" : null, false); //$NON-NLS-1$ //$NON-NLS-2$
781: output.println("/>"); //$NON-NLS-1$
782: }
783:
784: public void printGZip(String source, String destination) {
785: printTab();
786: output
787: .println("<gzip src=\"" + getEscaped(source) + "\" zipfile=\"" + getEscaped(destination) + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
788: }
789:
790: /**
791: * Print a <code> eclipse.convertTask</code> task to the script. This task convert a file path to
792: * an Eclipse resource or vice-versa.
793: *
794: * @param toConvert the entry to convert
795: * @param propertyName the property where to store the result of the convertion
796: * @param isEclipseResource true if toConvert refers to an eclipse resource.
797: */
798: public void printConvertPathTask(String toConvert,
799: String propertyName, boolean isEclipseResource) {
800: printTab();
801: output.print("<eclipse.convertPath"); //$NON-NLS-1$
802: if (isEclipseResource == false)
803: printAttribute("fileSystemPath", toConvert, true); //$NON-NLS-1$
804: else
805: printAttribute("resourcePath", toConvert, true); //$NON-NLS-1$
806: printAttribute("property", propertyName, true); //$NON-NLS-1$
807: output.println("/>"); //$NON-NLS-1$
808: }
809:
810: /**
811: * Print a <code> dirname </code> task to the script.
812: * @param property
813: * @param file
814: */
815: public void printDirName(String property, String file) {
816: printTab();
817: output.print("<dirname"); //$NON-NLS-1$
818: printAttribute("property", property, true); //$NON-NLS-1$
819: printAttribute("file", file, true); //$NON-NLS-1$
820: output.println("/>"); //$NON-NLS-1$
821: }
822:
823: /**
824: * Print a <code>Condition</code> task with isset test to the script
825: * @param property name of the property to set
826: * @param value value to set the property to
827: * @param testProperty name of the property for the isset test
828: */
829: public void printConditionIsSet(String property, String value,
830: String testProperty) {
831: println("<condition property=\"" + property + "\" value=\"" + value + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
832: indent++;
833: println("<isset property=\"" + testProperty + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
834: indent--;
835: printEndTag("condition"); //$NON-NLS-1$
836: }
837:
838: public void printTabs() {
839: printTab();
840: }
841:
842: public void printTaskDef(String name, String classname) {
843: printTabs();
844: output
845: .println("<taskdef name=\"" + name + "\" classname=\"" + classname + "\" />"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
846: }
847:
848: public static String getEscaped(String s) {
849: StringBuffer result = new StringBuffer(s.length() + 10);
850: for (int i = 0; i < s.length(); ++i)
851: appendEscapedChar(result, s.charAt(i));
852: return result.toString();
853: }
854:
855: private static void appendEscapedChar(StringBuffer buffer, char c) {
856: buffer.append(getReplacement(c));
857: }
858:
859: private static String getReplacement(char c) {
860: // Encode special XML characters into the equivalent character references.
861: // These five are defined by default for all XML documents.
862: switch (c) {
863: case '<':
864: return "<"; //$NON-NLS-1$
865: case '>':
866: return ">"; //$NON-NLS-1$
867: case '"':
868: return """; //$NON-NLS-1$
869: case '\'':
870: return "'"; //$NON-NLS-1$
871: case '&':
872: return "&"; //$NON-NLS-1$
873: default:
874: return String.valueOf(c);
875: }
876: }
877: }
|