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

This page was automatically generated by Maven