View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java,v 1.14 2002/08/09 19:11:56 jstrachan Exp $ 3 * $Revision: 1.14 $ 4 * $Date: 2002/08/09 19:11:56 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: Jelly.java,v 1.14 2002/08/09 19:11:56 jstrachan Exp $ 61 */ 62 63 package org.apache.commons.jelly; 64 import java.io.BufferedWriter; 65 import java.io.File; 66 import java.io.FileWriter; 67 import java.io.InputStream; 68 import java.io.OutputStreamWriter; 69 import java.io.Writer; 70 import java.net.MalformedURLException; 71 import java.net.URL; 72 73 import org.apache.commons.jelly.parser.XMLParser; 74 import org.apache.commons.logging.Log; 75 import org.apache.commons.logging.LogFactory; 76 77 /*** 78 * <p><code>Jelly</code> is a helper class which is capable of 79 * running a Jelly script. This class can be used from the command line 80 * or can be used as the basis of an Ant task.</p> 81 * 82 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 83 * @version $Revision: 1.14 $ 84 */ 85 public class Jelly { 86 87 /*** The Log to which logging calls will be made. */ 88 private static final Log log = LogFactory.getLog(Jelly.class); 89 90 /*** The JellyContext to use */ 91 private JellyContext context; 92 93 /*** The URL of the script to execute */ 94 private URL url; 95 96 /*** The URL of the root context for other scripts */ 97 private URL rootContext; 98 99 100 public Jelly() { 101 } 102 103 public static void main(String[] args) throws Exception { 104 105 try 106 { 107 if (args.length <= 0) { 108 System.out.println("Usage: Jelly scriptFile [outputFile]"); 109 return; 110 } 111 112 Jelly jelly = new Jelly(); 113 jelly.setScript(args[0]); 114 115 // later we might wanna add some command line arguments 116 // checking stuff using commons-cli to specify the output file 117 // and input file via command line arguments 118 final Writer writer = ( args.length > 1 ) 119 ? new FileWriter( args[1] ) 120 : new OutputStreamWriter( System.out ); 121 122 Script script = jelly.compileScript(); 123 XMLOutput output = XMLOutput.createXMLOutput(writer); 124 125 // add the system properties and the command line arguments 126 JellyContext context = jelly.getJellyContext(); 127 context.setVariable("args", args); 128 script.run(context, output); 129 130 // now lets wait for all threads to close 131 Runtime.getRuntime().addShutdownHook( 132 new Thread() { 133 public void run() { 134 try { 135 writer.close(); 136 } 137 catch (Exception e) { 138 // ignore errors 139 } 140 } 141 } 142 ); 143 } 144 catch (JellyException e) 145 { 146 Throwable cause = e.getCause(); 147 148 if ( cause != null ) 149 { 150 cause.printStackTrace(); 151 } 152 else 153 { 154 e.printStackTrace(); 155 } 156 } 157 } 158 159 /*** 160 * Compiles the script 161 */ 162 public Script compileScript() throws Exception { 163 XMLParser parser = new XMLParser(); 164 parser.setContext(getJellyContext()); 165 Script script = parser.parse(getUrl().openStream()); 166 script = script.compile(); 167 if (log.isDebugEnabled()) { 168 log.debug("Compiled script: " + getUrl()); 169 } 170 return script; 171 } 172 173 174 // Properties 175 //------------------------------------------------------------------------- 176 177 /*** 178 * Sets the script URL to use as an absolute URL or a relative filename 179 */ 180 public void setScript(String script) throws MalformedURLException { 181 setUrl(resolveURL(script)); 182 } 183 184 public URL getUrl() { 185 return url; 186 } 187 188 /*** 189 * Sets the script URL to use 190 */ 191 public void setUrl(URL url) { 192 this.url = url; 193 } 194 195 /*** 196 * Gets the root context 197 */ 198 public URL getRootContext() throws MalformedURLException { 199 if (rootContext == null) { 200 rootContext = new File(System.getProperty("user.dir")).toURL(); 201 } 202 return rootContext; 203 } 204 205 /*** 206 * Sets the root context 207 */ 208 public void setRootContext(URL rootContext) { 209 this.rootContext = rootContext; 210 } 211 212 /*** 213 * The context to use 214 */ 215 public JellyContext getJellyContext() throws MalformedURLException { 216 if (context == null) { 217 // take off the name off the URL 218 String text = getUrl().toString(); 219 int idx = text.lastIndexOf('/'); 220 text = text.substring(0, idx + 1); 221 context = new JellyContext(getRootContext(), new URL(text)); 222 } 223 return context; 224 } 225 226 // Implementation methods 227 //------------------------------------------------------------------------- 228 /*** 229 * @return the URL for the relative file name or absolute URL 230 */ 231 protected URL resolveURL(String name) throws MalformedURLException { 232 File file = new File(name); 233 if (file.exists()) { 234 return file.toURL(); 235 } 236 return new URL(name); 237 } 238 }

This page was automatically generated by Maven