You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ge...@apache.org on 2001/08/14 02:07:40 UTC
cvs commit: jakarta-velocity/contrib/temporary/veltag/src/java/org/apache/taglibs/velocity JSPContext.java ScopeTool.java VelocityTag.java
geirm 01/08/13 17:07:39
Added: contrib/temporary/veltag README.txt build.xml index.html
contrib/temporary/veltag/examples GeirBean.java README.txt
veltag.tld veltest.jsp
contrib/temporary/veltag/src/conf MANIFEST.MF
contrib/temporary/veltag/src/java/org/apache/taglibs/velocity
JSPContext.java ScopeTool.java VelocityTag.java
Log:
Initial checking for Velocity JSP tag
Revision Changes Path
1.1 jakarta-velocity/contrib/temporary/veltag/README.txt
Index: README.txt
===================================================================
The instructions should be in index.html, but
a quick how to :
1) Install ant if not already installed.
2) Get a servlet.jar. Put in /lib
3) Get a velocity.jar. Put in /lib
4) type 'ant' and it will build.
Follow instructions in index.html for setup.
1.1 jakarta-velocity/contrib/temporary/veltag/build.xml
Index: build.xml
===================================================================
<project name="veltag" default="jar" basedir=".">
<!-- ========== Initialize Properties ===================================== -->
<property file="build/build.properties"/> <!-- Component local -->
<property file="../build.properties"/> <!-- Commons local -->
<property file="${user.home}/.build.properties"/> <!-- User local -->
<!-- ========== External Dependencies ===================================== -->
<!-- The directory containing your servlet jar -->
<property name="servlet.home" value="lib/"/>
<!-- The directory containing your servlet jar -->
<property name="velocity.home" value="lib/"/>
<!-- ========== Derived Values ============================================ -->
<!-- The pathname of the "servlet.jar" JAR file -->
<property name="servlet.jar" value="${servlet.home}/servlet.jar"/>
<!-- The pathname of the "velocity.jar" JAR file -->
<property name="velocity.jar" value="${velocity.home}/velocity.jar"/>
<!-- ========== Component Declarations ==================================== -->
<!-- The name of this component -->
<property name="project.name" value="veltag"/>
<!-- The title of this component -->
<property name="project.title" value="Velocity JSP Tag"/>
<!-- The current version number of this component -->
<property name="project.version" value="0.01-dev"/>
<!-- The base directory for compilation targets -->
<property name="build.home" value="target"/>
<!-- The base directory for component configuration files -->
<property name="conf.home" value="src/conf"/>
<!-- The base directory for distribution targets -->
<property name="dist.home" value="dist"/>
<!-- The base directory for component sources -->
<property name="source.home" value="src"/>
<!-- ========== Compiler Defaults ========================================= -->
<!-- Should Java compilations set the 'debug' compiler option? -->
<property name="compile.debug" value="true"/>
<!-- Should Java compilations set the 'deprecation' compiler option? -->
<property name="compile.deprecation" value="true"/>
<!-- Should Java compilations set the 'optimize' compiler option? -->
<property name="compile.optimize" value="true"/>
<!-- Construct compile classpath -->
<path id="compile.classpath">
<pathelement location="${build.home}/classes"/>
<pathelement location="${servlet.jar}"/>
<pathelement location="${velocity.jar}"/>
</path>
<!-- ========== Executable Targets ======================================== -->
<target name="init"
description="Initialize and evaluate conditionals">
<echo message="-------- ${project.name} ${project.version} --------"/>
<filter token="name" value="${project.name}"/>
<filter token="version" value="${project.version}"/>
</target>
<target name="prepare" depends="init"
description="Prepare build directory">
<mkdir dir="${build.home}"/>
<mkdir dir="${build.home}/classes"/>
<mkdir dir="${build.home}/conf"/>
<mkdir dir="${build.home}/javadoc"/>
</target>
<target name="static" depends="prepare"
description="Copy static files to build directory">
<tstamp/>
<copy todir="${build.home}/conf" filtering="on">
<fileset dir="${conf.home}" includes="*.MF"/>
</copy>
</target>
<target name="compile" depends="static"
description="Compile">
<javac srcdir="${source.home}/java"
destdir="${build.home}/classes"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
optimize="${compile.optimize}">
<classpath refid="compile.classpath"/>
</javac>
<copy todir="${build.home}/classes" filtering="on">
<fileset dir="${source.home}/java" excludes="**/*.java"/>
</copy>
</target>
<target name="clean"
description="Clean build and distribution directories">
<delete dir="${build.home}"/>
<delete dir="${dist.home}"/>
</target>
<target name="all" depends="clean,compile"
description="Clean and compile all components"/>
<target name="javadoc" depends="compile"
description="Create Javadoc documentation">
<javadoc sourcepath="${source.home}/java"
destdir="${build.home}/javadoc"
packagenames="org.apache.taglibs.velocity.*"
author="true"
private="true"
version="true"
doctitle="<h1>${project.title}</h1>"
windowtitle="${project.title} (Version ${project.version})"
bottom="Copyright (c) 2001 Apache Software Foundation" >
<classpath refid="compile.classpath"/>
</javadoc>
</target>
<target name="jar" depends="compile">
<jar jarfile="${project.name}-${project.version}.jar"
basedir="${build.home}/classes"
manifest="${conf.home}/MANIFEST.MF"/>
</target>
</project>
1.1 jakarta-velocity/contrib/temporary/veltag/index.html
Index: index.html
===================================================================
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Geir Magnusson Jr.">
<title>Jakarta Project: Velocity Template Engine Tag Library</title>
<link type="text/css" href="taglib.css" rel="stylesheet">
</head>
<body>
<h1>Jakarta Project: Velocity Template Engine Tag Library</h1>
<h3 class="middle">Version: 1.0</h3>
<h3>Table of Contents</h3>
<a href="#overview">Overview</a>
<br>
<a href="#requirements">Requirements</a>
<br>
<a href="#build">Building the Library</a>
<br>
<a href="#config">Configuration</a>
<br>
<a href="#summary">Tag Summary</a>
<br>
<a href="#reference">Tag Reference</a>
<br>
<a href="#examples">Examples</a>
<br>
<a href="#javadocs">Javadocs</a>
<br>
<a href="#history">Revision History</a>
<br>
<a name="overview"></a>
<h3>Overview</h3>
<p>
The Velocity Template Engine allows the Velocity Template Language to be
used within a JSP using JSP custom tags.
<a href="http://jakarta.apache.org/velocity/">Velocity</a> is a general
templating tool that provides a simple template language that allows easy
access to java objects, methods, and data.
</p>
<p>
By allowing the use of Velocity from within a JSP, it allows a JSP author
easy access to all public methods exposed by a Java class, as well as
a simple set of control and logical directives.
</p>
<a name="requirements"></a>
<h3>Requirements</h3>
<p>
This custom tag library requires no software other than a servlet container
that supports the JavaServer Pages Specification, version 1.1 or higher, and
the Jakarta Velocity Template Engine, version 1.1 or higher.
</p>
<a name="build"></a>
<h3>Building the Library</h3>
<p>
Building the library is fairly straightforward. What to do :
<ul>
<li> This taglib includes an <a href="http://jakarta.apache.org/ant/">ant</a>
build script. (Ant rocks!) Install ant on your computer (it's easy...)
<li> You will need a servlet API jar. We recommend you get it from
<a href="http://jakarta.apache.org/builds/jakarta-tomcat/release/v3.2.3/bin/">here</a>
for servlet 2.2 and
<a href="http://jakarta.apache.org/builds/jakarta-servletapi-4/nightly/">here</a>
for the unreleased servlet 2.3 API. Note - if you are a JSP user, you will have
one included with your servlet engine.
<li> A velocity.jar. This can be found
<a href="http://jakarta.apache.org/velocity/">here</a>.
<li> The build script expects to find the jars in the lib directory. Modification
of the build script is easy if they are in another location. Look for
'servlet.home' and 'velocity.home' in the build.xml.
<li> Once all of the pieces are in place, cd to your distribution directory (where
you unpacked this taglib, and where build.xml is, and simply type 'ant'.
</ul>
<a name="config"></a>
<h3>Configuration</h3>
<p>Follow these steps to configure your web application with this tag library:</p>
<ul>
<li>Copy the tag library descriptor file to the /WEB-INF subdirectory
of your web application. An example of this descriptor file
can be found in the /examples directory.</li>
<li>Copy the tag library JAR file to the /WEB-INF/lib subdirectory
of your web application. This will be created by the
build process.</li>
<li>Copy the Velocity JAR file to the /WEB-INF/lib subdirectory
of your web application. Please get from the
<a href="http://jakarta.apache.org/velocity/">Jakarta Velocity site.</a></li>
<li>Add a <taglib> element to your web application deployment
descriptor in /WEB-INF/web.xml like this:
<pre>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/veltag-0.01</taglib-uri>
<taglib-location>/WEB-INF/veltag.tld</taglib-location>
</taglib>
</pre>
</li>
</ul>
<p>To use the tags from this library in your JSP pages, add the following
directive at the top of each page: </p>
<pre>
<%@ taglib uri="/WEB-INF/veltag.tld" prefix="vel" %>
</pre>
<p>
where "<i>vel</i>" is the tag name prefix you wish to use for tags
from this library. You can change this value to any prefix you like.
</p>
<a name="summary"></a>
<h3>Tag Summary</h3>
<table summary="summary list of tags with short description of each.">
<tr>
<td><a href="#velocity">velocity</a></td><td>
Processes the body as VTL. The body of the tag consists of normal Velocity template
content. This content is a free mixture of VTL (Velocity Template Language)
references and directives, as well as non-VTL content which is called
'schmoo'.
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
<a name="reference"></a>
<h3>Tag Reference</h3>
<table summary="This table is for visual formatting of tag reference information." cellspacing="0" cellpadding="3" width="90%" border="0">
<tr>
<td colspan="2">
<table summary="name and tag library version availability of tag." cellspacing="0" width="100%" bgcolor="#cccccc" border="0">
<tr>
<td><b><font size="+1"><a name="velocity">velocity</a></font></b></td><td align="right">Availability: version 0.01</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
Processes the body as VTL. The body of the tag consists of normal Velocity template
content. This content is a free mixture of VTL (Velocity Template Language)
references and directives, as well as non-VTL content which is called
'schmoo'.
</td>
</tr>
<tr>
<td width="2%"> </td><td width="98%">
<table summary="Tag class, tag name, script variable (if any), and restrictions (if any)." width="100%" border="0">
<tr>
<td width="20%"><span class="tclass">Tag Class</span></td><td width="80%">org.apache.taglibs.velocity.VelocityTag</td>
</tr>
<tr>
<td width="20%"><span class="tbody">Tag Body</span></td><td width="80%">Velocity Template Language</td>
</tr>
<tr>
<td width="20%"><span class="scriptvar">Script Variable</span></td><td width="80%">No</td>
</tr>
<tr>
<td width="20%"><span class="restrictions">Restrictions</span></td><td width="80%"></td>
</tr>
<tr>
<td width="20%"><span class="attributes">Attributes</span></td><td width="80%"> </td>
</tr>
<tr>
<td width="20%"> </td><td width="80%">
<table summary="Tag attribute name, whether required, and whether rtexprvalue allowed." width="100%" border="0">
<tr>
<td width="40%">Name</td><td width="20%">Required</td><td width="40%">Runtime Expression Evaluation</td>
</tr>
<tr bgcolor="#cccccc">
<td width="40%"><b>strictaccess</b></td><td width="20%">false</td><td width="40%">true</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top" width="20%"><span class="example">Example</span></td><td width="80%">
<ol>
<li>
<br>
<p>
<pre>
<code>
<vel:velocity strictaccess="true">
#set( $name = "Geir")
Hello $name
</vel:velocity>
</code>
</pre>
</p>
</li>
</ol>
</td>
</tr>
</table>
</td>
</tr>
</table>
<a name="examples">
<h3>Examples</h3>
</a>
<p>See the example template in the distrubution /examples directory</p>
<a name="javadocs">
<h3>Java Docs</h3>
</a>
<p>
To create the javadocs, use the 'javadoc' build target. The docs will
be <a href="target/javadoc/index.html">here</a>.</p>
<a name="history">
<h3>Revision History</h3>
</a>
<p>v0.01</p>
</body>
</html>
1.1 jakarta-velocity/contrib/temporary/veltag/examples/GeirBean.java
Index: GeirBean.java
===================================================================
public class GeirBean
{
private static String[] arr = { "a","b","c","d"};
public void setString( String foo )
{
}
public String getString()
{
return "Hello from GeirBean";
}
public String[] getArray()
{
return arr;
}
}
1.1 jakarta-velocity/contrib/temporary/veltag/examples/README.txt
Index: README.txt
===================================================================
The example jsp page accesses the 'SimpleBean'.
Simply compile SimpleBean.java and drop the
compiled SimpleBean.class into WEB-INF/classes of
your web app.
1.1 jakarta-velocity/contrib/temporary/veltag/examples/veltag.tld
Index: veltag.tld
===================================================================
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglib_1_1.dtd">
<taglib>
<tlibversion>2.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Velocity JSP Support</shortname>
<tag>
<name>velocity</name>
<tagclass>org.apache.taglibs.velocity.VelocityTag</tagclass>
<bodycontent>tagdependent</bodycontent>
<info>Velocity and JSP, together at last</info>
<attribute>
<name>strictaccess</name>
<required>false</required>
</attribute>
</tag>
</taglib>
1.1 jakarta-velocity/contrib/temporary/veltag/examples/veltest.jsp
Index: veltest.jsp
===================================================================
<%@ taglib uri="/WEB-INF/veltag.tld" prefix="vel" %>
<html>
<head>
<title> Velocity! </title>
</head>
<jsp:useBean id="mybean" class="SimpleBean" />
<jsp:useBean id="mybeanreq" class="SimpleBean" scope="request" />
<body>
<vel:velocity strictaccess="true">
#set($mybean = $scopetool.getPageScope("mybean"))
#set($mybeanreq = $scopetool.getRequestScope("mybeanreq"))
#if(true)
this is true!
#end
<br>
$mybean.string
<br>
#foreach($item in $mybean.array)
$item <br>
#end
request : $mybeanreq.string
</vel:velocity>
</body>
</html>
1.1 jakarta-velocity/contrib/temporary/veltag/src/conf/MANIFEST.MF
Index: MANIFEST.MF
===================================================================
Extension-Name: @name@
Specification-Vendor: Apache Software Foundation
Specification-Version: 1.0
Implementation-Vendor: Apache Software Foundation
Implementation-Version: @version@
1.1 jakarta-velocity/contrib/temporary/veltag/src/java/org/apache/taglibs/velocity/JSPContext.java
Index: JSPContext.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 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 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/>.
*/
package org.apache.taglibs.velocity;
import java.util.HashMap;
import javax.servlet.jsp.PageContext;
import org.apache.velocity.context.AbstractContext;
/**
* <p>
* Velocity Context implementationfor use in JSP's,
* where the servlet API 'scope' is used directly.
* </p>
* <p>
* This context will 'search' the scopes looking for an
* item, working outwards
* page->request->session->application
* </p>
*
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
* @version $Id: JSPContext.java,v 1.1 2001/08/14 00:07:39 geirm Exp $
*/
public class JSPContext extends AbstractContext
{
private HashMap context = new HashMap();
private PageContext pageContext = null;
public JSPContext( PageContext pageContext )
{
this.pageContext = pageContext;
}
public Object internalGet( String key )
{
Object o = context.get( key );
if ( o == null)
{
o = pageContext.getAttribute( key, PageContext.PAGE_SCOPE);
if (o == null)
{
o = pageContext.getAttribute( key, PageContext.REQUEST_SCOPE);
if ( o == null)
{
o = pageContext.getAttribute( key, PageContext.SESSION_SCOPE);
if ( o == null )
{
o = pageContext.getAttribute( key, PageContext.APPLICATION_SCOPE);
}
}
}
if ( o != null)
context.put( key, o );
}
return o;
}
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 );
}
}
1.1 jakarta-velocity/contrib/temporary/veltag/src/java/org/apache/taglibs/velocity/ScopeTool.java
Index: ScopeTool.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 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 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/>.
*/
package org.apache.taglibs.velocity;
import javax.servlet.jsp.PageContext;
/**
* <p>
* Simple context tool to allow a template-in-JSP to access
* the scopes directly to retrieve objects/beans.
* </p>
*
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
* @version $Id: ScopeTool.java,v 1.1 2001/08/14 00:07:39 geirm Exp $
*/
public class ScopeTool
{
protected PageContext pageContext = null;
public ScopeTool( PageContext pageContext )
{
this.pageContext = pageContext;
}
/**
* retrieves an object from the page scope
*
* @param name Name of object in scope
* @return object if found, null otherwise
*/
public Object getPageScope( String name )
{
return pageContext.getAttribute( name, PageContext.PAGE_SCOPE);
}
/**
* retrieves an object from the request scope
*
* @param name Name of object in scope
* @return object if found, null otherwise
*/
public Object getRequestScope( String name )
{
return pageContext.getAttribute( name, PageContext.REQUEST_SCOPE);
}
/**
* retrieves an object from the session scope
*
* @param name Name of object in scope
* @return object if found, null otherwise
*/
public Object getSessionScope( String name )
{
return pageContext.getAttribute( name, PageContext.SESSION_SCOPE);
}
/**
* retrieves an object from the application scope
*
* @param name Name of object in scope
* @return object if found, null otherwise
*/
public Object getApplicationScope( String name )
{
return pageContext.getAttribute( name, PageContext.APPLICATION_SCOPE);
}
/**
* retrieves a named object from anyscope,
* working 'upwards':
* page - > request - > session - > application
*
* @param name Name of object in scope
* @return object if found, null otherwise
*/
public Object getAnyScope( String name )
{
Object o = getPageScope( name );
if (o == null)
{
o = getRequestScope( name );
if ( o == null)
{
o = getSessionScope( name );
if ( o == null )
{
o = getApplicationScope( name );
}
}
}
return o;
}
}
1.1 jakarta-velocity/contrib/temporary/veltag/src/java/org/apache/taglibs/velocity/VelocityTag.java
Index: VelocityTag.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 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 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/>.
*/
package org.apache.taglibs.velocity;
import java.io.Reader;
import java.util.Enumeration;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
/**
* <p>
* Simple implementation of JSP tag to allow
* use of VTL in JSP's.
* </p>
*
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
* @version $Id: VelocityTag.java,v 1.1 2001/08/14 00:07:39 geirm Exp $
*/
public class VelocityTag implements BodyTag
{
protected Tag parent = null;
protected BodyContent bodyContent = null;
protected PageContext pageContext = null;
/*
* strictaccess : determines if the JSPContext is used
* to autofetch information from the 'scopes'
* or if the scopetool() is used
*/
protected boolean strictAccess = false;
/**
* CTOR : current implementation uses the Singleton
* model for velocity.
*/
public VelocityTag()
{
try
{
Velocity.init();
}
catch(Exception e)
{
System.out.println("VelocityTag() : " + e );
}
}
/**
* switch for strictaccess
*
* @param sa if true, then normal VelocityContext is used
* and template must directly get beans from scopes
* Otherwise, the JSPContext is used which searches for
* objects/beans in the scopes automatically.
*/
public void setStrictaccess( boolean sa )
{
this.strictAccess = sa;
}
public Tag getParent()
{
return parent;
}
public void setParent( Tag parent)
{
this.parent = parent;
return;
}
public int doStartTag()
throws JspException
{
return EVAL_BODY_TAG;
}
public void setBodyContent( BodyContent bc )
{
this.bodyContent = bc;
return;
}
public void setPageContext( PageContext pc )
{
this.pageContext = pc;
return;
}
public void doInitBody()
throws JspException
{
return;
}
public int doAfterBody()
throws JspException
{
return 0;
}
public void release()
{
return;
}
/**
* This is the real worker for this taglib.
* There are efficiencies to be added - the plan
* is to cache the AST to avoid reparsing every
* time.
*/
public int doEndTag()
throws JspException
{
/*
* if there is no body, we are done
*/
if ( bodyContent == null)
return EVAL_PAGE;
try
{
JspWriter writer = pageContext.getOut();
/*
* get our body
*/
Reader bodyreader = bodyContent.getReader();
/*
* now make a JSPContext
*/
Context vc = null;
/*
* if strictAccess == true, then we want to use a regular
* VelocityContext, as we assume that the template will
* bring into the context any beans using the scope tool
* or the like
*/
if (strictAccess)
{
vc = (Context) new VelocityContext();
}
else
{
vc = (Context) new JSPContext( pageContext );
}
/*
* add the scope tool
*/
vc.put( "scopetool", new ScopeTool( pageContext ) );
Velocity.evaluate( vc , writer, "JSP for me!", bodyreader );
}
catch( Exception e )
{
System.out.println( e.toString() );
}
return EVAL_PAGE;
}
}