%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.task.JellyTask |
|
|
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 | 0 | public class JellyTask extends Task { |
48 | ||
49 | /** The Log to which logging calls will be made. */ |
|
50 | 0 | 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 | 0 | log( "Running script: " + getUrl() ); |
76 | 0 | if ( output != null ) { |
77 | 0 | log( "Sending output to: " + output ); |
78 | } |
|
79 | ||
80 | 0 | Script script = compileScript(); |
81 | 0 | JellyContext context = getJellyContext(); |
82 | 0 | context.setVariable( "project", getProject() ); |
83 | 0 | script.run( context, getXMLOutput() ); |
84 | 0 | getXMLOutput().flush(); |
85 | } |
|
86 | 0 | catch (Exception e) { |
87 | 0 | throw new BuildException(e, getLocation() ); |
88 | 0 | } |
89 | 0 | } |
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 | 0 | setUrl(resolveURL(script)); |
99 | 0 | } |
100 | ||
101 | public URL getUrl() { |
|
102 | 0 | return url; |
103 | } |
|
104 | ||
105 | /** |
|
106 | * Sets the script URL to use |
|
107 | */ |
|
108 | public void setUrl(URL url) { |
|
109 | 0 | this.url = url; |
110 | 0 | } |
111 | ||
112 | /** |
|
113 | * Sets the script file to use |
|
114 | */ |
|
115 | public void setFile(File file) throws MalformedURLException { |
|
116 | 0 | setUrl( file.toURL() ); |
117 | 0 | } |
118 | ||
119 | /** |
|
120 | * Sets the output to generate |
|
121 | */ |
|
122 | public void setOutput(File output) throws IOException { |
|
123 | 0 | this.output = output; |
124 | 0 | xmlOutput = XMLOutput.createXMLOutput( new FileWriter( output ) ); |
125 | 0 | } |
126 | ||
127 | public XMLOutput getXMLOutput() throws IOException { |
|
128 | 0 | if (xmlOutput == null) { |
129 | 0 | xmlOutput = XMLOutput.createXMLOutput( System.out ); |
130 | } |
|
131 | 0 | return xmlOutput; |
132 | } |
|
133 | ||
134 | /** |
|
135 | * Sets the XMLOutput used |
|
136 | */ |
|
137 | public void setXMLOutput(XMLOutput xmlOutput) { |
|
138 | 0 | this.xmlOutput = xmlOutput; |
139 | 0 | } |
140 | ||
141 | /** |
|
142 | * Gets the root context |
|
143 | */ |
|
144 | public URL getRootContext() throws MalformedURLException { |
|
145 | 0 | if (rootContext == null) { |
146 | 0 | rootContext = new File(System.getProperty("user.dir")).toURL(); |
147 | } |
|
148 | 0 | return rootContext; |
149 | } |
|
150 | ||
151 | /** |
|
152 | * Sets the root context |
|
153 | */ |
|
154 | public void setRootContext(URL rootContext) { |
|
155 | 0 | this.rootContext = rootContext; |
156 | 0 | } |
157 | ||
158 | /** |
|
159 | * The context to use |
|
160 | */ |
|
161 | public JellyContext getJellyContext() throws MalformedURLException { |
|
162 | 0 | if (context == null) { |
163 | // take off the name off the URL |
|
164 | 0 | String text = getUrl().toString(); |
165 | 0 | int idx = text.lastIndexOf('/'); |
166 | 0 | text = text.substring(0, idx + 1); |
167 | 0 | JellyContext parentContext = new JellyContext(getRootContext(), class="keyword">new URL(text)); |
168 | 0 | context = new AntJellyContext(getProject() , parentContext); |
169 | ||
170 | // register the Ant tag library |
|
171 | 0 | context.registerTagLibrary( "jelly:ant", new AntTagLibrary() ); |
172 | } |
|
173 | 0 | return context; |
174 | } |
|
175 | ||
176 | // Implementation methods |
|
177 | //------------------------------------------------------------------------- |
|
178 | ||
179 | /** |
|
180 | * Compiles the script |
|
181 | */ |
|
182 | protected Script compileScript() throws JellyException { |
|
183 | 0 | XMLParser parser = new XMLParser(); |
184 | ||
185 | 0 | Script script = null; |
186 | try { |
|
187 | 0 | parser.setContext(getJellyContext()); |
188 | 0 | script = parser.parse(getUrl().toString()); |
189 | } |
|
190 | 0 | catch (IOException e) { |
191 | 0 | throw new JellyException(e); |
192 | } |
|
193 | 0 | catch (SAXException e) { |
194 | 0 | throw new JellyException(e); |
195 | 0 | } |
196 | 0 | script = script.compile(); |
197 | ||
198 | 0 | if (log.isDebugEnabled()) { |
199 | 0 | log.debug("Compiled script: " + getUrl()); |
200 | } |
|
201 | 0 | 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 | 0 | File file = getProject().resolveFile(name); |
210 | 0 | if (file.exists()) { |
211 | 0 | return file.toURL(); |
212 | } |
|
213 | 0 | return new URL(name); |
214 | } |
|
215 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |