Coverage report

  %line %branch
org.apache.commons.jelly.tags.velocity.JellyContextAdapter
0% 
0% 

 1  
 package org.apache.commons.jelly.tags.velocity;
 2  
 
 3  
 /*
 4  
  * Copyright 2001,2004 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.util.Set;
 20  
 import java.util.HashSet;
 21  
 import java.util.HashMap;
 22  
 
 23  
 import org.apache.commons.jelly.JellyContext;
 24  
 import org.apache.velocity.context.Context;
 25  
 
 26  
 /**
 27  
  * Adapts a JellyContext for use as a Velocity Context.  This context
 28  
  * can be used in either read-only or read-write mode.  When used as a
 29  
  * read-only adapter, items <tt>put</tt> or <tt>remove</tt>ed from the
 30  
  * Velocity context are not permitted to propogate to the JellyContext,
 31  
  * which is the default behavior.  The adapter can also be used in a
 32  
  * read-write mode.  This permits changes made by Velocity to propogate
 33  
  * to the JellyContext.
 34  
  *
 35  
  * @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
 36  
  * @version $Id: JellyContextAdapter.java,v 1.4 2004/09/09 12:23:16 dion Exp $
 37  
  */
 38  
 public class JellyContextAdapter implements Context
 39  
 {
 40  
     /** Flag to indicate read-only or read-write mode */
 41  0
     private boolean readOnly = true;
 42  
 
 43  
     /** The JellyContext being adapted */
 44  
     private JellyContext jellyContext;
 45  
 
 46  
     /** The store for Velocity in the event the adpater is read-only */
 47  0
     private HashMap class="keyword">privateContext = new HashMap();
 48  
 
 49  
     /**
 50  
      * Constructor.
 51  
      *
 52  
      * @param jellyContext The JellyContext to adapt
 53  
      */
 54  
     public JellyContextAdapter( JellyContext jellyContext )
 55  0
     {
 56  0
         this.jellyContext = jellyContext;
 57  0
     }
 58  
 
 59  
     /**
 60  
      * Sets the read-only flag for this adapter.  If the read-only flag
 61  
      * is set, changes to the Velocity Context will not be propogated to
 62  
      * the JellyContext.  Turning the read-only flag off enables changes
 63  
      * to propogate.
 64  
      *
 65  
      * @param readOnly If this parameter is <tt>true</tt>, the adapter
 66  
      * becomes read-only.  Setting the parameter to <tt>false</tt> the
 67  
      * adapter becomes read-write.
 68  
      */
 69  
     public void setReadOnly(boolean readOnly)
 70  
     {
 71  0
         this.readOnly = readOnly;
 72  0
     }
 73  
 
 74  
     /**
 75  
      * Tests if the adapter is read-only.
 76  
      *
 77  
      * @return <tt>true</tt> if the adpater is read-only; otherwise
 78  
      * returns <tt>false</tt>.
 79  
      */
 80  
     public boolean isReadOnly()
 81  
     {
 82  0
         return readOnly;
 83  
     }
 84  
 
 85  
     public boolean containsKey( Object key )
 86  
     {
 87  0
         if ( key == null )
 88  
         {
 89  0
             return false;
 90  
         }
 91  
 
 92  0
         if ( readOnly && privateContext.containsKey( key ) )
 93  
         {
 94  0
             return true;
 95  
         }
 96  
 
 97  0
         return jellyContext.getVariable( key.toString() ) != null ? true : false;
 98  
     }
 99  
 
 100  
     public Object get( String key )
 101  
     {
 102  0
         if ( key == null )
 103  
         {
 104  0
             return null;
 105  
         }
 106  
 
 107  0
         if ( readOnly && privateContext.containsKey( key ) )
 108  
         {
 109  0
             return privateContext.get( key );
 110  
         }
 111  
 
 112  0
         return jellyContext.getVariable( key );
 113  
     }
 114  
 
 115  
     public Object[] getKeys()
 116  
     {
 117  0
         Set keys = jellyContext.getVariables().keySet();
 118  
 
 119  0
         if ( readOnly )
 120  
         {
 121  0
             HashSet combinedKeys = new HashSet( keys );
 122  0
             combinedKeys.addAll( privateContext.keySet() );
 123  0
             keys = combinedKeys;
 124  
         }
 125  
 
 126  0
         return keys.toArray();
 127  
     }
 128  
 
 129  
     public Object put( String key, Object value )
 130  
     {
 131  
         Object oldValue;
 132  
 
 133  0
         if ( key == null || value == class="keyword">null )
 134  
         {
 135  0
             return null;
 136  
         }
 137  
 
 138  0
         if ( readOnly )
 139  
         {
 140  0
             oldValue = privateContext.put( key, value );
 141  
         }
 142  
         else
 143  
         {
 144  0
             oldValue = jellyContext.getVariable( key );
 145  0
             jellyContext.setVariable( key, value );
 146  
         }
 147  
 
 148  0
         return oldValue;
 149  
     }
 150  
 
 151  
     public Object remove( Object key )
 152  
     {
 153  
         Object oldValue;
 154  
 
 155  0
         if ( key == null )
 156  
         {
 157  0
             return null;
 158  
         }
 159  
 
 160  0
         if ( readOnly )
 161  
         {
 162  0
             oldValue = privateContext.remove( key );
 163  
         }
 164  
         else
 165  
         {
 166  0
             oldValue = jellyContext.getVariable( key.toString() );
 167  0
             jellyContext.removeVariable( key.toString() );
 168  
         }
 169  
 
 170  0
         return oldValue;
 171  
     }
 172  
 }
 173  
 

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.