daveb       01/01/04 20:24:31

  Added:       whiteboard/daveb build.sh ContextTool.java
                        ContextWithTools.java myprops.properties README.txt
                        testtool.sh testtool.vm ToolOne.java ToolTest.java
                        ToolTwo.java
  Log:
  Experimental ContextTool stuff.
  Nothing fancy. Just gives us something to
  discuss.
  
  Revision  Changes    Path
  1.1                  jakarta-velocity/whiteboard/daveb/build.sh
  
  Index: build.sh
  ===================================================================
  echo "Building TestTool"
  
  javac -classpath .:../../bin/classes *.java
  
  
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/ContextTool.java
  
  Index: ContextTool.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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", "Velocity", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   */
  
  import java.io.Serializable;
  import org.apache.velocity.context.Context;
  
  /**
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Dave Bryson</a>
   * @version $Id: ContextTool.java,v 1.1 2001/01/05 04:24:31 daveb Exp $
   */
  public abstract class ContextTool implements Serializable
  {
      /** Owner */
      protected Context context;
      
      /** 
       * keep a pointer to my context
       * in case there's other stuff in there
       * that I might want to use in the Tool
       */
      public void setContext( Context c )
      {
          this.context = c;
      }
      
      /**
       * Provides the name for the context
       */
      public abstract String getName();
  }
  
  
  
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/ContextWithTools.java
  
  Index: ContextWithTools.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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", "Velocity", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   */
  
  import java.util.*;
  
  import org.apache.velocity.context.AbstractContext;
  import org.apache.velocity.context.Context;
  import org.apache.velocity.runtime.Runtime;
  import org.apache.velocity.runtime.configuration.*;
  
  /**
   * Just a demo context that loads it's own tools.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Dave Bryson</a>
   * @version $Id: ContextWithTools.java,v 1.1 2001/01/05 04:24:31 daveb Exp $
   */
  public class ContextWithTools extends AbstractContext
  {
      
      public ContextWithTools()
      {
          super();
          loadTools();
      }
  
      /**
       * Load the tools specified in the Properties file
       * This can use a lot of work, but it works for testing.
       */
      private void loadTools()
      {
          String toolPackages = VelocityResources.getString("ContextTools");
          Enumeration tools = new StringTokenizer( toolPackages ); 
          try
          {
              while ( tools.hasMoreElements() )
              {
                  String toolName = (String)tools.nextElement();
                  Object o = Class.forName( toolName ).newInstance();
                  ContextTool ct = (ContextTool)o;
                  ct.setContext( this );
                  put( ct.getName(), ct);
              }
          }
          catch ( Exception cnf )
          {
              Runtime.error( "Not loaded: " + cnf );
          }
      }
  
      // Below: Implements the AbstractContext stuff
      
       /** storage for key/value pairs */
      private HashMap context = new HashMap();
  
      
      /** chaining CTOR */
      public ContextWithTools( Context context )
      {
          super( context );
          loadTools();
      }
   
      public Object internalGet( String key )
      {
          return context.get( key );
      }        
  
      public Object internalPut( String key, Object value )
      {
          return context.put( key, value );
      }
  
      public  boolean internalContainsKey(Object key)
      {
          return context.containsKey( key );
      }
  
      public  Object[] internalGetKeys()
      {
          return context.keySet().toArray();
      }
  
      public  Object internalRemove(Object key)
      {
          return context.remove( key );
      }
  
      /**
       * Clones this context object
       * @return Object an instance of this Context
       */
      public Object clone()
      {
          ContextWithTools clone = null;
  
          try
          {
              clone = (ContextWithTools) super.clone();
              clone.context = (HashMap) context.clone();
          }
          catch (CloneNotSupportedException cnse)
          {
          }
          return clone;
      }
  }
  
  
  
  
  
  
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/myprops.properties
  
  Index: myprops.properties
  ===================================================================
  ##
  # Properties for the template loader
  ##
  
  runtime.log = velocity.log
  
  template.loader=org.apache.velocity.runtime.loader.FileTemplateLoader
  
  template.modificationCheckInterval = 2
  
  # #
  # Path to templates.
  # Default=current directory 
  ## 
  template.path=.
  
  # #
  # Cache the templates?
  # #
  template.cache=false
  
  # Options for the Output
  template.encoding=8859_1
  
  
  # This is for #foreach loops. You can
  # retrieve the current number of the item
  # being operated on by using the special
  # $velocityCount variable. Here you can specify
  # what to start the counter at 0 or 1.
  counter.initial.value = 1
  
  # You can name what the counter
  # variable can be accessed as.
  counter.name = velocityCount
  
  # The value for the default content type to return
  # in VelocityServlet
  default.contentType=text/html
  
  
  #
  # Place to add context tools
  # 
  ContextTools=ToolOne ToolTwo
  
  
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/README.txt
  
  Index: README.txt
  ===================================================================
  Nothing fancy here. 
  
  Just Experimental - playing with the use of a ContextTool.
  
  To build the classes run - build.sh ( of course you need to build Velocity first )
  
  To run the small test - testtool.sh
  
  
  Where do we want to go with this stuff??
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/testtool.sh
  
  Index: testtool.sh
  ===================================================================
  echo "Running TestTool"
  
  java -cp .:../../bin/classes ToolTest testtool.vm
  
  
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/testtool.vm
  
  Index: testtool.vm
  ===================================================================
  This is the tool test
  
  $test
  
  Tool 1: $toolone.sayHello()
  
  Tool 2: $tooltwo.sayHello()
  
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/ToolOne.java
  
  Index: ToolOne.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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", "Velocity", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   */
  
  import org.apache.velocity.context.*;
  
  /** 
   * Test Tool 
   */
  public class ToolOne extends ContextTool
  {
      /**
       * This is the name put in the context
       */
      public String getName()
      {
          return "toolone";
      }
      
      public String sayHello()
      {
          return "Hello from Tool one";
      }
  }
  
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/ToolTest.java
  
  Index: ToolTest.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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", "Velocity", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   */
  
  import java.io.BufferedWriter;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  
  import java.util.Properties;
  
  import org.apache.velocity.*;
  import org.apache.velocity.Template;
  
  import org.apache.velocity.runtime.Runtime;
  
  /**
   * Just for testing
   */
  public class ToolTest
  {
      public ToolTest(String templateFile)
      {
          try
          {
              Runtime.init( "myprops.properties" );
   
              Template template = Runtime.getTemplate(templateFile);
  
              ContextWithTools context = new ContextWithTools();
              context.put("test", "HEY!");
              
              Writer writer = new BufferedWriter(new OutputStreamWriter(System.out));
         
              template.merge(context, writer);
  
              writer.flush();
              writer.close();
          }
          catch( Exception e )
          {
              Runtime.error(e);
          }
      }
  
      public static void main(String[] args)
      {
          ToolTest t;
          t = new ToolTest(args[0]);
      }
  }
  
  
  
  
  
  
  
  
  1.1                  jakarta-velocity/whiteboard/daveb/ToolTwo.java
  
  Index: ToolTwo.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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", "Velocity", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   */
  
  import org.apache.velocity.context.*;
  
  /**
   * another sample tool
   */
  public class ToolTwo extends ContextTool
  {
      /**
       * the name put in the context
       */
      public String getName()
      {
          return "tooltwo";
      }
  
      public String sayHello()
      {
          return "Hello from Tool Two";
      }
  }
  
  
  

Reply via email to