| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 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 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
0 |
public class JellyTask extends Task { |
| 48 |
|
|
| 49 |
|
|
| 50 |
0 |
private static final Log log = LogFactory.getLog(Jelly.class); |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
private JellyContext context; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
private URL url; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
private URL rootContext; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
private XMLOutput xmlOutput; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
private File output; |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 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 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 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 |
|
|
| 107 |
|
|
| 108 |
|
public void setUrl(URL url) { |
| 109 |
0 |
this.url = url; |
| 110 |
0 |
} |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
public void setFile(File file) throws MalformedURLException { |
| 116 |
0 |
setUrl( file.toURL() ); |
| 117 |
0 |
} |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 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 |
|
|
| 136 |
|
|
| 137 |
|
public void setXMLOutput(XMLOutput xmlOutput) { |
| 138 |
0 |
this.xmlOutput = xmlOutput; |
| 139 |
0 |
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 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 |
|
|
| 153 |
|
|
| 154 |
|
public void setRootContext(URL rootContext) { |
| 155 |
0 |
this.rootContext = rootContext; |
| 156 |
0 |
} |
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
public JellyContext getJellyContext() throws MalformedURLException { |
| 162 |
0 |
if (context == null) { |
| 163 |
|
|
| 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 |
|
|
| 171 |
0 |
context.registerTagLibrary( "jelly:ant", new AntTagLibrary() ); |
| 172 |
|
} |
| 173 |
0 |
return context; |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 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 |
|
|
| 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 |
|
} |