View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.task;
18  
19  import java.io.File;
20  import java.io.FileWriter;
21  import java.io.IOException;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  
25  import org.apache.commons.jelly.Jelly;
26  import org.apache.commons.jelly.JellyContext;
27  import org.apache.commons.jelly.JellyException;
28  import org.apache.commons.jelly.Script;
29  import org.apache.commons.jelly.XMLOutput;
30  import org.apache.commons.jelly.parser.XMLParser;
31  import org.apache.commons.jelly.tags.ant.AntTagLibrary;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.tools.ant.BuildException;
35  import org.apache.tools.ant.Task;
36  
37  import org.xml.sax.SAXException;
38  
39  /***
40   * <p><code>JellyTask</code> is an Ant task which will
41   * run a given Jelly script.
42   *
43   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
44   * @version $Revision: 155420 $
45   */
46  
47  public class JellyTask extends Task {
48  
49      /*** The Log to which logging calls will be made. */
50      private static final Log log = LogFactory.getLog(Jelly.class);
51  
52      /*** The JellyContext to use */
53      private JellyContext context;
54  
55      /*** The URL of the script to execute */
56      private URL url;
57  
58      /*** The URL of the root context for other scripts */
59      private URL rootContext;
60  
61      /*** The XML output */
62      private XMLOutput xmlOutput;
63  
64      /*** The file where output is going */
65      private File output;
66  
67      // Task interface
68      //-------------------------------------------------------------------------
69  
70      /***
71       * Excutes the Jelly script
72       */
73      public void execute() throws BuildException {
74          try {
75              log( "Running script: " + getUrl() );
76              if ( output != null ) {
77                  log( "Sending output to: " + output );
78              }
79  
80              Script script = compileScript();
81              JellyContext context = getJellyContext();
82              context.setVariable( "project", getProject() );
83              script.run( context, getXMLOutput() );
84              getXMLOutput().flush();
85          }
86          catch (Exception e) {
87              throw new BuildException(e, getLocation() );
88          }
89      }
90  
91      // Properties
92      //-------------------------------------------------------------------------
93  
94      /***
95       * Sets the script URL to use as an absolute URL or a relative filename
96       */
97      public void setScript(String script) throws MalformedURLException {
98          setUrl(resolveURL(script));
99      }
100 
101     public URL getUrl() {
102         return url;
103     }
104 
105     /***
106      * Sets the script URL to use
107      */
108     public void setUrl(URL url) {
109         this.url = url;
110     }
111 
112     /***
113      * Sets the script file to use
114      */
115     public void setFile(File file) throws MalformedURLException {
116         setUrl( file.toURL() );
117     }
118 
119     /***
120      * Sets the output to generate
121      */
122     public void setOutput(File output) throws IOException {
123         this.output = output;
124         xmlOutput = XMLOutput.createXMLOutput( new FileWriter( output ) );
125     }
126 
127     public XMLOutput getXMLOutput() throws IOException {
128         if (xmlOutput == null) {
129             xmlOutput = XMLOutput.createXMLOutput( System.out );
130         }
131         return xmlOutput;
132     }
133 
134     /***
135      * Sets the XMLOutput used
136      */
137     public void setXMLOutput(XMLOutput xmlOutput) {
138         this.xmlOutput = xmlOutput;
139     }
140 
141     /***
142      * Gets the root context
143      */
144     public URL getRootContext() throws MalformedURLException {
145         if (rootContext == null) {
146             rootContext = new File(System.getProperty("user.dir")).toURL();
147         }
148         return rootContext;
149     }
150 
151     /***
152      * Sets the root context
153      */
154     public void setRootContext(URL rootContext) {
155         this.rootContext = rootContext;
156     }
157 
158     /***
159      * The context to use
160      */
161     public JellyContext getJellyContext() throws MalformedURLException {
162         if (context == null) {
163             // take off the name off the URL
164             String text = getUrl().toString();
165             int idx = text.lastIndexOf('/');
166             text = text.substring(0, idx + 1);
167             JellyContext parentContext =  new JellyContext(getRootContext(), new URL(text));
168             context = new AntJellyContext(getProject() , parentContext);
169 
170             // register the Ant tag library
171             context.registerTagLibrary( "jelly:ant", new AntTagLibrary() );
172         }
173         return context;
174     }
175 
176     // Implementation methods
177     //-------------------------------------------------------------------------
178 
179     /***
180      * Compiles the script
181      */
182     protected Script compileScript() throws JellyException {
183         XMLParser parser = new XMLParser();
184 
185         Script script = null;
186         try {
187             parser.setContext(getJellyContext());
188             script = parser.parse(getUrl().toString());
189         }
190         catch (IOException e) {
191             throw new JellyException(e);
192         }
193         catch (SAXException e) {
194             throw new JellyException(e);
195         }
196         script = script.compile();
197 
198         if (log.isDebugEnabled()) {
199             log.debug("Compiled script: " + getUrl());
200         }
201         return script;
202     }
203 
204 
205     /***
206      * @return the URL for the relative file name or absolute URL
207      */
208     protected URL resolveURL(String name) throws MalformedURLException {
209         File file = getProject().resolveFile(name);
210         if (file.exists()) {
211             return file.toURL();
212         }
213         return new URL(name);
214     }
215 }