You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/04/26 14:20:12 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly run_all.jelly

jstrachan    02/04/26 05:20:12

  Modified:    jelly    build.xml
               jelly/src/java/org/apache/commons/jelly Context.java
                        Jelly.java
               jelly/src/java/org/apache/commons/jelly/tags/core
                        CoreTagLibrary.java
               jelly/src/java/org/apache/commons/jelly/tags/define
                        DynamicTag.java InvokeBodyTag.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/core
                        IncludeTag.java
               jelly/src/test/org/apache/commons/jelly run_all.jelly
  Log:
  Added <j:include> tag to allow easy calling of other jelly scripts. Also got the root and current URL contexts working so uris can be relative to the root context, relative to the current context or absolute URLs
  
  Revision  Changes    Path
  1.14      +12 -1     jakarta-commons-sandbox/jelly/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/build.xml,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- build.xml	26 Apr 2002 07:39:07 -0000	1.13
  +++ build.xml	26 Apr 2002 12:20:11 -0000	1.14
  @@ -3,7 +3,7 @@
   
   <!--
           "Jelly" component of the Jakarta Commons Subproject
  -        $Id: build.xml,v 1.13 2002/04/26 07:39:07 jstrachan Exp $
  +        $Id: build.xml,v 1.14 2002/04/26 12:20:11 jstrachan Exp $
   -->
   
   
  @@ -359,6 +359,17 @@
      </target>
   
   <!-- ========== Sample Program Targets ==================================== -->
  +
  +   <target name="demo.all" depends="compile" 
  +      description="Runs all the demo scripts inside one single script">
  +    <java classname="org.apache.commons.jelly.Jelly" fork="yes">
  +      <classpath refid="test.classpath"/>
  +      <arg value="src/test/org/apache/commons/jelly/run_all.jelly"/> 
  +      <arg value="one"/> 
  +      <arg value="two"/> 
  +      <arg value="three"/> 
  +    </java>
  +   </target>
   
      <target name="demo.hw" depends="compile" 
         description="Runs the Hello World demo">
  
  
  
  1.10      +50 -10    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Context.java
  
  Index: Context.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Context.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Context.java	26 Apr 2002 11:28:55 -0000	1.9
  +++ Context.java	26 Apr 2002 12:20:12 -0000	1.10
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Context.java,v 1.9 2002/04/26 11:28:55 jstrachan Exp $
  - * $Revision: 1.9 $
  - * $Date: 2002/04/26 11:28:55 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Context.java,v 1.10 2002/04/26 12:20:12 jstrachan Exp $
  + * $Revision: 1.10 $
  + * $Date: 2002/04/26 12:20:12 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: Context.java,v 1.9 2002/04/26 11:28:55 jstrachan Exp $
  + * $Id: Context.java,v 1.10 2002/04/26 12:20:12 jstrachan Exp $
    */
   package org.apache.commons.jelly;
   
  @@ -78,7 +78,7 @@
   /** <p><code>Context</code> represents the Jelly context.</p>
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.9 $
  +  * @version $Revision: 1.10 $
     */
   public class Context {
   
  @@ -115,6 +115,7 @@
           this.rootContext = parentContext.rootContext;
           this.currentContext = parentContext.currentContext;
           this.taglibs = parentContext.taglibs;
  +        this.variables.put( "parentScope", parentContext.variables );
       }
       
       public Context(Context parentContext, URL currentContext) {
  @@ -232,7 +233,23 @@
           XMLParser parser = new XMLParser();
           parser.setContext( this );
           
  -        Script script = parser.parse( getResourceAsStream( uri ) );
  +        InputStream in = getResourceAsStream( uri );
  +        if ( in == null ) {
  +            throw new JellyException( "Could not find Jelly script: " + uri );
  +        }
  +        Script script = parser.parse( in );
  +        return script.compile();
  +    }
  +
  +    /** 
  +     * Attempts to parse the script from the given URL using the 
  +     * {#link getResource()} method then returns the compiled script.
  +     */
  +    public Script compileScript(URL url) throws Exception {
  +        XMLParser parser = new XMLParser();
  +        parser.setContext( this );
  +        
  +        Script script = parser.parse( url.toString() );
           return script.compile();
       }
   
  @@ -241,8 +258,15 @@
        * Context.getResource() API then compiles it and runs it.
        */
       public void runScript(String uri, XMLOutput output) throws Exception {
  -        Script script = compileScript( uri );
  -        script.run( this, output );
  +        URL url = getResource(uri);
  +        if ( url == null ) {
  +            throw new JellyException( "Could not find Jelly script: " + url );
  +        }
  +        Script script = compileScript( url );
  +        
  +        URL newContextURL = getContextURL( url );
  +        Context newContext = new Context( this, newContextURL );
  +        script.run( newContext, output );
       }
   
       /**
  @@ -308,9 +332,25 @@
        * @throws MalformedURLException if the URL is invalid.
        */
       protected URL createRelativeURL(URL rootURL, String relativeURI) throws MalformedURLException {
  +        String urlText = null;
           if ( rootURL == null ) {
  -            return new URL( "file://" + relativeURI );
  +            String userDir = System.getProperty( "user.dir" );
  +            urlText = "file://" + userDir + relativeURI; 
  +        }
  +        else {
  +            urlText = rootURL.toString() + relativeURI;
           }
  -        return new URL( rootURL.toString() + relativeURI );
  +        log.info( "Attempting to open url: " + urlText );
  +        return new URL( urlText );
  +    }
  +    
  +    /** 
  +     * Strips off the name of a script to create a new context URL
  +     */
  +    protected URL getContextURL( URL url ) throws MalformedURLException {
  +        String text = url.toString();
  +        int idx = text.lastIndexOf( '/' );
  +        text = text.substring( 0, idx + 1 );
  +        return new URL( text );
       }
   }
  
  
  
  1.7       +33 -8     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java
  
  Index: Jelly.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Jelly.java	26 Apr 2002 11:28:55 -0000	1.6
  +++ Jelly.java	26 Apr 2002 12:20:12 -0000	1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java,v 1.6 2002/04/26 11:28:55 jstrachan Exp $
  - * $Revision: 1.6 $
  - * $Date: 2002/04/26 11:28:55 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java,v 1.7 2002/04/26 12:20:12 jstrachan Exp $
  + * $Revision: 1.7 $
  + * $Date: 2002/04/26 12:20:12 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: Jelly.java,v 1.6 2002/04/26 11:28:55 jstrachan Exp $
  + * $Id: Jelly.java,v 1.7 2002/04/26 12:20:12 jstrachan Exp $
    */
   package org.apache.commons.jelly;
   
  @@ -81,7 +81,7 @@
    * or can be used as the basis of an Ant task.</p>
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  - * @version $Revision: 1.6 $
  + * @version $Revision: 1.7 $
    */
   public class Jelly {
   
  @@ -90,10 +90,13 @@
   
       /** The Context to use */
       private Context context;
  -    
  +        
       /** The URL of the script to execute */
       private URL url;
       
  +    /** The URL of the root context for other scripts */
  +    private URL rootContext;
  +    
       public Jelly() {
       }
       
  @@ -172,12 +175,34 @@
       }
       
       
  +    /** 
  +     * Gets the root context
  +     */
  +    public URL getRootContext() throws MalformedURLException {
  +        if ( rootContext == null ) {
  +            rootContext = new File( System.getProperty( "user.dir" ) ).toURL();
  +        }
  +        return rootContext;
  +    }
  +    
  +    /** 
  +     * Sets the root context
  +     */
  +    public void setRootContext(URL rootContext) {
  +        this.rootContext = rootContext;
  +    }
  +    
  +    
       /**
        * The context to use
        */
  -    public Context getContext() {
  +    public Context getContext() throws MalformedURLException {
           if ( context == null ) {
  -            context = new Context( getUrl() );
  +            // take off the name off the URL
  +            String text = getUrl().toString();
  +            int idx = text.lastIndexOf( '/' );
  +            text = text.substring( 0, idx + 1 );
  +            context = new Context( getRootContext(), new URL( text ) );
           }
           return context;
       }
  
  
  
  1.5       +8 -5      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java
  
  Index: CoreTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CoreTagLibrary.java	7 Mar 2002 02:46:04 -0000	1.4
  +++ CoreTagLibrary.java	26 Apr 2002 12:20:12 -0000	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java,v 1.4 2002/03/07 02:46:04 jstrachan Exp $
  - * $Revision: 1.4 $
  - * $Date: 2002/03/07 02:46:04 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java,v 1.5 2002/04/26 12:20:12 jstrachan Exp $
  + * $Revision: 1.5 $
  + * $Date: 2002/04/26 12:20:12 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: CoreTagLibrary.java,v 1.4 2002/03/07 02:46:04 jstrachan Exp $
  + * $Id: CoreTagLibrary.java,v 1.5 2002/04/26 12:20:12 jstrachan Exp $
    */
   package org.apache.commons.jelly.tags.core;
   
  @@ -76,7 +76,7 @@
   /** Describes the Taglib. This class could be generated by XDoclet
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.4 $
  +  * @version $Revision: 1.5 $
     */
   public class CoreTagLibrary extends TagLibrary {
   
  @@ -93,6 +93,9 @@
           registerTag( "choose", ChooseTag.class );
           registerTag( "when", WhenTag.class );
           registerTag( "otherwise", OtherwiseTag.class );
  +        
  +        // other tags
  +        registerTag( "include", IncludeTag.class );
       }
       
       public Expression createExpression(ExpressionFactory factory, String tagName, String attributeName, String attributeValue) throws Exception {
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/IncludeTag.java
  
  Index: IncludeTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/IncludeTag.java,v 1.1 2002/04/26 12:20:12 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2002/04/26 12:20:12 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: IncludeTag.java,v 1.1 2002/04/26 12:20:12 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.core;
  
  import java.net.URL;
  
  import org.apache.commons.jelly.Context;
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /** A tag which conditionally evaluates its body based on some condition
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class IncludeTag extends TagSupport {
  
      private String uri;        
  
      public IncludeTag() {
      }
  
      // Tag interface
      //------------------------------------------------------------------------- 
      public void run(Context context, XMLOutput output) throws Exception {
          if ( uri == null ) {
              throw new JellyException( "<j:include> must have a 'uri' attribute defined" );
          }
          
          // we need to create a new Context of the URI
          
          // take off the script name from the URL
          context.runScript( uri, output );
      }
  
      // Properties
      //-------------------------------------------------------------------------                
      
      
      /** Sets the URI (relative URI or absolute URL) for the script to evaluate. */
      public void setUri(String uri) {
          this.uri = uri;
      }
  }
  
  
  
  1.4       +6 -6      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java
  
  Index: DynamicTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DynamicTag.java	26 Apr 2002 11:28:55 -0000	1.3
  +++ DynamicTag.java	26 Apr 2002 12:20:12 -0000	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java,v 1.3 2002/04/26 11:28:55 jstrachan Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/04/26 11:28:55 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java,v 1.4 2002/04/26 12:20:12 jstrachan Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/04/26 12:20:12 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: DynamicTag.java,v 1.3 2002/04/26 11:28:55 jstrachan Exp $
  + * $Id: DynamicTag.java,v 1.4 2002/04/26 12:20:12 jstrachan Exp $
    */
   package org.apache.commons.jelly.tags.define;
   
  @@ -81,7 +81,7 @@
    * as variables and will allow the template to invoke its instance body.</p>
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
    */
   public class DynamicTag extends TagSupport implements DynaTag {
   
  @@ -107,7 +107,7 @@
   
           log.info( "Invoking dynamic tag with attributes: " + attributes );
   
  -        attributes.put( "jelly.body", getBody() );
  +        attributes.put( "org.apache.commons.jelly.body", getBody() );
           
           // create new context based on current attributes
           Context newContext = context.newContext( attributes );
  
  
  
  1.4       +7 -7      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/InvokeBodyTag.java
  
  Index: InvokeBodyTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/InvokeBodyTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- InvokeBodyTag.java	26 Apr 2002 11:28:55 -0000	1.3
  +++ InvokeBodyTag.java	26 Apr 2002 12:20:12 -0000	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/InvokeBodyTag.java,v 1.3 2002/04/26 11:28:55 jstrachan Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/04/26 11:28:55 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/InvokeBodyTag.java,v 1.4 2002/04/26 12:20:12 jstrachan Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/04/26 12:20:12 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: InvokeBodyTag.java,v 1.3 2002/04/26 11:28:55 jstrachan Exp $
  + * $Id: InvokeBodyTag.java,v 1.4 2002/04/26 12:20:12 jstrachan Exp $
    */
   package org.apache.commons.jelly.tags.define;
   
  @@ -78,7 +78,7 @@
    * body.</p>
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
    */
   public class InvokeBodyTag extends TagSupport {
   
  @@ -93,8 +93,8 @@
       //-------------------------------------------------------------------------                    
       public void run(Context context, XMLOutput output) throws Exception {
   
  -        // Try find find the body from the reserved 'jelly.body' variable
  -        Script script = (Script) context.getVariable( "jelly.body" );
  +        // Try find find the body from the reserved 'org.apache.commons.jelly.body' variable
  +        Script script = (Script) context.getVariable( "org.apache.commons.jelly.body" );
           if ( script != null ) {
               script.run( context, output );
           }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/run_all.jelly
  
  Index: run_all.jelly
  ===================================================================
  <?xml version="1.0"?>
  <j:jelly xmlns:j="jelly:core">
  
    <!-- try an absolute path -->
    <j:include uri="/src/test/org/apache/commons/jelly/example.jelly"/>
    
    <!-- all other scripts... -->
    <j:include uri="example2.jelly"/>
    <j:include uri="example2.jelly"/>
    <j:include uri="example3.jelly"/>
    <j:include uri="hello_world.jelly"/>
    <j:include uri="show_properties.jelly"/>
    <j:include uri="testing123.jelly"/>
      
    <!-- loading all dynamic tags libraries -->
    <j:include uri="define/babelfishTaglib.jelly"/>
    
    <!-- now run a script using the new taglib -->
    <j:include uri="define/example.jelly"/>
  
  </j:jelly>
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>