/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-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","DVSL" 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.tools.dvsl;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Stack;
import org.apache.velocity.context.Context;
import org.apache.velocity.VelocityContext;
/**
* Context implementation that handles wrapping several
* contexts simultaneously. The style context gets
* special treatment, getting checked first.
*
* @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
*/
class DVSLContext extends VelocityContext
{
protected Context styleContext = null;
protected List contextList = new ArrayList();
/**
* Used to hold the nodes as we get invoked from
* within the document for applyTemplates() duties
*/
private Stack nodeStack = new Stack();
protected Map ctx = new HashMap();
public DVSLNode pushNode( DVSLNode n )
{
nodeStack.push( n );
return n;
}
public DVSLNode peekNode()
{
return (DVSLNode) nodeStack.peek();
}
public DVSLNode popNode()
{
return (DVSLNode) nodeStack.pop();
}
public void clearNode()
{
nodeStack.clear();
return;
}
public void clearContexts()
{
styleContext = null;
contextList.clear();
}
public void addContext( Context c )
{
if (c != null)
contextList.add( c );
}
public void setStyleContext( Context c )
{
styleContext = c;
}
/**
* retrieves value for key from internal
* storage
*
* @param key name of value to get
* @return value as object
*/
public Object internalGet( String key )
{
Object o = null;
/*
* special tokens
*/
if ( key.equals("node"))
{
return peekNode();
}
/*
* start with local storage
*/
o = ctx.get( key );
if ( o != null)
return o;
/*
* if that doesn't work, try style first
* then others
*/
if ( styleContext != null)
{
o = styleContext.get( key );
if ( o != null )
return o;
}
for( int i = 0; i < contextList.size(); i++)
{
Context c = (Context) contextList.get( i );
o = c.get( key );
if ( o != null )
return o;
}
return null;
}
/**
* stores the value for key to internal
* storage
*
* @param key name of value to store
* @param value value to store
* @return previous value of key as Object
*/
public Object internalPut( String key, Object value )
{
if ( key.equals("node"))
return null;
return ctx.put( key, value );
}
/**
* determines if there is a value for the
* given key
*
* @param key name of value to check
* @return true if non-null value in store
*/
public boolean internalContainsKey(Object key)
{
/*
* start with local storage
*/
if ( ctx.containsKey( key ))
return true;
/*
* if that doesn't work, try style first
* then others
*/
if ( styleContext != null && styleContext.containsKey( key ) )
return true;
for( int i = 0; i < contextList.size(); i++)
{
Context c = (Context) contextList.get( i );
if ( c.containsKey( key ))
return true;
}
return false;
}
/**
* returns array of keys
*
* $$$ GMJ todo
*
* @return keys as []
*/
public Object[] internalGetKeys()
{
return null;
}
/**
* remove a key/value pair from the
* internal storage
*
* @param key name of value to remove
* @return value removed
*/
public Object internalRemove(Object key)
{
return ctx.remove( key );
}
}