View Javadoc

1   /*
2    * Created on 29.7.2004
3    */
4   package net.sf.tomp.xtcl.ant;
5   
6   import java.io.File;
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  import net.sf.tomp.xtcl.Main;
11  
12  import org.apache.tools.ant.BuildException;
13  import org.apache.tools.ant.Task;
14  
15  /***
16   * @author tomp 
17   * 
18   * Ant task for executing XTCL from within Ant build files. Simply
19   *         set the file and/or some other options such as 'quiet', 'uriResolver'
20   *         etc. <br/>Example of an build file using this new (xtcl) Ant task:
21  <pre>
22  &lt;project name=&quot;OwnTaskExample&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
23    &lt;taskdef name=&quot;xtcl&quot; classname=&quot;net.sf.tomp.xtcl.ant.XtclTask&quot;/&gt;
24    &lt;target name=&quot;main&quot;&gt;
25      &lt;xtcl quiet=&quot;false&quot; file=&quot;demo/echo.xcf&quot;  fileAfter=&quot;demo/echo.xcf&quot; resolvingReader=&quot;net.sf.tomp.xtcl.resolver.XTResolvingReader&quot; uriResolver=&quot;net.sf.tomp.xtcl.resolver.XTResolver&quot;/&gt;
26    &lt;/target&gt;
27    &lt;target name=&quot;main&quot;&gt;
28      &lt;xtcl scriptbefore=&quot;true&quot; file=&quot;demo/echo.xcf&quot; fileAfter=&quot;demo/echo.xcf&quot;&gt;
29      	ECHO Hello!
30      &lt;/xtcl&gt;
31    &lt;/target&gt;
32  &lt;/project&gt;
33  </pre>
34  It will give:<br/>
35  <pre>Buildfile: build.xml
36  
37  main:
38       [xtcl] Echo!
39       [xtcl] Echo!</pre>
40   */
41  public class XtclTask extends Task {
42  
43      private String script;
44      
45      private boolean scriptbefore = false;
46  
47      private boolean ignoreerrors = false;
48  
49      private File file;
50  
51      private File fileafter;
52  
53      private boolean quiet = false;
54  
55      private boolean verbose = false;
56  
57      private Class resolvingReader;
58  
59      private Class uriResolver;
60  
61      /***
62       * Executes the task.
63       * 
64       * @see org.apache.tools.ant.Task#execute()
65       */
66      public void execute() throws BuildException {
67          List params = new ArrayList();
68          if (quiet) {
69              params.add("-q"); 
70          }
71          if (verbose) {
72              params.add("-v");
73          }
74          if (ignoreerrors) {
75              params.add("-g");
76          }
77          if (resolvingReader != null) {
78              params.add("-r");
79              params.add(resolvingReader.getName());
80          }
81          if (uriResolver != null) {
82              params.add("-u");
83              params.add(uriResolver.getName());
84          }
85          if (file != null) {
86              params.add("-f");
87              params.add(file.toString());
88          }
89          if (fileafter != null) {
90              params.add("-F");
91              params.add(fileafter.toString());
92          }
93          if (script != null) {
94              if(scriptbefore) {
95                  params.add("-s");
96                  params.add(script);
97              } else {
98                  params.add("-S");
99                  params.add(script);
100             }
101         }
102         if(verbose) { // if extra verbose
103             System.out.println(params);
104         }
105         String[] args = (String[]) params.toArray(new String[0]);
106         try {
107             Main.main(args);
108         } catch (Exception e) {
109             throw new BuildException(e);
110         }
111     }
112 
113     /***
114      * @param sb whether the script specified in the task body is to be executed before
115      * the scripts from file
116      */
117     public void setIgnoreerrors(boolean g) {
118         this.ignoreerrors = g;
119     }
120 
121     /***
122      * @param sb whether the script specified in the task body is to be executed before
123      * the scripts from file
124      */
125     public void setScriptbefore(boolean sb) {
126         this.scriptbefore = sb;
127     }
128 
129     /***
130      * @param text The text of the script to be executed as if written 
131      *             on the command line
132      */
133     public void addText(String text) {
134         this.script = getProject().replaceProperties(text);
135     }
136 
137     /***
138      * @param file The file to set. The name of the XTCL (usually *.xcf) script
139      *            file to be executed
140      */
141     public void setFile(File file) {
142         this.file = file;
143     }
144 
145     /***
146      * @param fileAfter The file to set. The name of the XTCL (usually *.xcf) script
147      *            file to be executed
148      */
149     public void setFileafter(File fileafter) {
150         this.fileafter = fileafter;
151     }
152 
153     /***
154      * @param quiet The quiet to set. sets the non-verbose mode if set to true
155      */
156     public void setQuiet(boolean quiet) {
157         this.quiet = quiet;
158     }
159 
160     /***
161      * @param resolvingReader The resolvingReader class to set. sets
162      *            XMLResolvingReader class used to read XML resolving URIs
163      */
164     public void setResolvingReader(Class resolvingReader) {
165         this.resolvingReader = resolvingReader;
166     }
167 
168     /***
169      * @param uriResolver The uriResolver to set. sets URIResolver class to be
170      *            used by JAXP
171      */
172     public void setUriResolver(Class uriResolver) {
173         this.uriResolver = uriResolver;
174     }
175 
176 }