You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by jv...@locus.apache.org on 2000/12/30 15:46:25 UTC
cvs commit: jakarta-velocity/whiteboard/jvz AbstractContext.java Context.java ContextContainer.java DefaultContext.java
jvanzyl 00/12/30 06:46:25
Added: whiteboard/jvz AbstractContext.java Context.java
ContextContainer.java DefaultContext.java
Log:
- essentially the same context work except rearranged to make
the contexts cleaner and hide anything behind the runtime.
Revision Changes Path
1.1 jakarta-velocity/whiteboard/jvz/AbstractContext.java
Index: AbstractContext.java
===================================================================
package org.apache.velocity.context;
/*
* 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 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/>.
*/
import java.io.Serializable;
/**
* This class is the abstract base class for all Velocity Context
* implementations. Simply extend this class and implement the
* abstract routines that access your preferred storage method.
*
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
* @version $Id: AbstractContext.java,v 1.1 2000/12/30 14:46:24 jvanzyl Exp $
*/
public abstract class AbstractContext implements Context, Serializable
{
/**
* we handle the context wrapping
*/
private Context innerContext = null;
/**
* CTOR's
*/
public AbstractContext()
{
}
public AbstractContext( Context innerContext )
{
this.innerContext = innerContext;
}
/** implement to return a value from the context storage */
public abstract Object internalGet( String key );
/** implement to put a value into the context storage */
public abstract Object internalPut( String key, Object value );
/** implement to remove an item from your storage */
public abstract Object internalRemove(Object key);
/** implement to determine if a key is in the storage */
public abstract boolean internalContainsKey(String key);
/**
* Adds a name/value pair to the context.
*
* @param key The name to key the provided value with.
* @param value The corresponding value.
*/
public Object put(String key, Object value)
{
try
{
return internalPut(key, value);
}
catch (NullPointerException npe)
{
if (key == null)
{
org.apache.velocity.runtime.Runtime.error (
"Context key was null! Value was: " + value);
}
else if (value == null)
{
org.apache.velocity.runtime.Runtime.error (
"Context value was null! Key was: " + key);
}
return null;
}
}
/**
* Gets the value corresponding to the provided key from the context.
*
* @param key The name of the desired value.
* @return The value corresponding to the provided key.
*/
public Object get(String key)
{
if (key == null)
{
org.apache.velocity.runtime.Runtime.debug ("Context key was null!");
}
Object o = internalGet( key );
if (o == null && innerContext != null)
{
o = innerContext.get( key );
}
return o;
}
/**
* Removes the value associated with the specified key from the context.
*
* @param key The name of the value to remove.
* @return The value that the key was mapped to, or <code>null</code>
* if unmapped.
*/
public Object remove(Object key)
{
if (key == null)
{
org.apache.velocity.runtime.Runtime.debug ("Context key was null!");
}
return internalRemove(key);
}
public boolean containsKey(String key)
{
return internalContainsKey(key);
}
}
1.1 jakarta-velocity/whiteboard/jvz/Context.java
Index: Context.java
===================================================================
package org.apache.velocity.context;
/*
* 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 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/>.
*/
/**
*
* @author <a href="mailto:fedor.karpelevitch@home.com">Fedor</a>
* @version $Id: Context.java,v 1.1 2000/12/30 14:46:24 jvanzyl Exp $
*/
public interface Context
{
/**
* Gets the value corresponding to the provided key from the context.
*
* @param name The name of the desired value.
* @return The value corresponding to the provided name.
*/
public Object get(String name);
/**
* Adds a name/value pair to the context.
*
* @param name The name to key the provided value with.
* @param value The corresponding value.
*/
public Object put(String name, Object value);
/**
* Removes the value associated with the specified key from the context.
*
* @param name The name of the value to remove.
* @return The value that the key was mapped to, or <code>null</code>
* if unmapped.
*/
public Object remove(Object name);
public boolean containsKey(String key);
}
1.1 jakarta-velocity/whiteboard/jvz/ContextContainer.java
Index: ContextContainer.java
===================================================================
package org.apache.velocity.context;
/*
* 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 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/>.
*/
import java.util.Hashtable;
import org.apache.velocity.util.introspection.IntrospectionCacheData;
/**
* class to encapsulate the 'stuff' for internal operation of velocity.
* We use the context as a thread-safe storage : we take advantage of the
* fact that it's a visitor of sorts to all nodes (that matter) of the
* AST during init() and render().
* Currently, it carries the template name for namespace
* support, as well as node-local context data introspection caching.
*
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
* @version $Id: ContextContainer.java,v 1.1 2000/12/30 14:46:24 jvanzyl Exp $
*/
public class ContextContainer
{
/**
* Context that will be used that actually contains
* the objects/beans
*/
private Context context = null;
/**
* cache for node/context specific introspection information
*/
private Hashtable introspectionCache = new Hashtable();
/**
* Current template name.
*/
private String strCurrentTemplate = "<undef>";
public Object get(String key)
{
return context.get(key);
}
public void put(String key, Object value)
{
context.put(key,value);
}
/**
* set the current template name
*
* @param s current template name to set
*/
public void setCurrentTemplateName( String s )
{
strCurrentTemplate = s;
return;
}
/**
* get the current template name
*
* @return String current template name
*/
public String getCurrentTemplateName()
{
return strCurrentTemplate;
}
/**
* Set context
*/
public void setContext(Context context)
{
this.context = context;
}
/**
* Get the context
*/
public Context getContext()
{
return context;
}
public void remove(Object o)
{
context.remove(o);
}
/**
* returns an IntrospectionCache Data (@see IntrospectionCacheData)
* object if exists for the key
*
* @param key key to find in cache
* @return cache object
*/
public IntrospectionCacheData icacheGet( Object key )
{
return ( IntrospectionCacheData ) introspectionCache.get( key );
}
/**
* places an IntrospectionCache Data (@see IntrospectionCacheData)
* element in the cache for specified key
*
* @param key key
* @param o IntrospectionCacheData object to place in cache
*/
public void icachePut( Object key, IntrospectionCacheData o )
{
introspectionCache.put( key, o );
}
}
1.1 jakarta-velocity/whiteboard/jvz/DefaultContext.java
Index: DefaultContext.java
===================================================================
package org.apache.velocity.context;
/*
* 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 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/>.
*/
import java.util.HashMap;
/**
* DefaultContext.java
*
* Default velocity context type that is simply uses
* hashtable for storage.
*
*/
public class DefaultContext extends AbstractContext
{
private HashMap context = new HashMap();
public Object internalGet( String key )
{
return context.get( key );
}
public Object internalPut( String key, Object value )
{
return context.put( key, value );
}
public Object internalRemove(Object key)
{
return context.remove( key );
}
public boolean internalContainsKey(String key)
{
return context.containsKey(key);
}
/**
* Clones this context object
* @return Object an instance of this Context
*/
public Object clone()
{
DefaultContext clone = null;
try
{
clone = (DefaultContext) super.clone();
clone.context = (HashMap) context.clone();
}
catch (CloneNotSupportedException cnse)
{
}
return clone;
}
}