Coverage Report - org.apache.commons.ognl.ASTMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ASTMap
69%
25/36
87%
7/8
2.333
 
 1  
 package org.apache.commons.ognl;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
 23  
 
 24  
 import java.util.HashMap;
 25  
 import java.util.Map;
 26  
 
 27  
 /**
 28  
  * $Id: ASTMap.java 1194869 2011-10-29 11:10:16Z mcucchiara $
 29  
  *
 30  
  * @author Luke Blanshard (blanshlu@netscape.net)
 31  
  * @author Drew Davidson (drew@ognl.org)
 32  
  */
 33  
 class ASTMap
 34  
     extends SimpleNode
 35  
 {
 36  
     private String className;
 37  
 
 38  10
     private Map<OgnlContext, Class> defaultMapClassMap = new HashMap<OgnlContext, Class>();
 39  
 
 40  
     public ASTMap( int id )
 41  
     {
 42  10
         super( id );
 43  10
     }
 44  
 
 45  
     public ASTMap( OgnlParser p, int id )
 46  
     {
 47  0
         super( p, id );
 48  0
     }
 49  
 
 50  
     protected void setClassName( String value )
 51  
     {
 52  10
         className = value;
 53  10
     }
 54  
 
 55  
     /**
 56  
      * Get the class name for this map.
 57  
      *
 58  
      * @return the class name.
 59  
      * @since 4.0
 60  
      */
 61  
     String getClassName()
 62  
     {
 63  0
         return className;
 64  
     }
 65  
 
 66  
     protected Object getValueBody( OgnlContext context, Object source )
 67  
         throws OgnlException
 68  
     {
 69  
         Map answer;
 70  
 
 71  5
         if ( className == null )
 72  
         {
 73  3
             Class defaultMapClass = getDefaultMapClass( context );
 74  
             try
 75  
             {
 76  3
                 answer = (Map) defaultMapClass.newInstance();
 77  
             }
 78  0
             catch ( Exception ex )
 79  
             {
 80  
                 /* This should never happen */
 81  0
                 throw new OgnlException( "Default Map class '" + defaultMapClass.getName() + "' instantiation error",
 82  
                                          ex );
 83  3
             }
 84  3
         }
 85  
         else
 86  
         {
 87  
             try
 88  
             {
 89  2
                 answer = (Map) OgnlRuntime.classForName( context, className ).newInstance();
 90  
             }
 91  0
             catch ( Exception ex )
 92  
             {
 93  0
                 throw new OgnlException( "Map implementor '" + className + "' not found", ex );
 94  2
             }
 95  
         }
 96  
 
 97  14
         for ( int i = 0; i < jjtGetNumChildren(); ++i )
 98  
         {
 99  9
             ASTKeyValue kv = (ASTKeyValue) children[i];
 100  9
             Node k = kv.getKey(), v = kv.getValue();
 101  
 
 102  9
             answer.put( k.getValue( context, source ), ( v == null ) ? null : v.getValue( context, source ) );
 103  
         }
 104  
 
 105  5
         return answer;
 106  
     }
 107  
 
 108  
     public String toGetSourceString( OgnlContext context, Object target )
 109  
     {
 110  5
         throw new UnsupportedCompilationException( "Map expressions not supported as native java yet." );
 111  
     }
 112  
 
 113  
     public String toSetSourceString( OgnlContext context, Object target )
 114  
     {
 115  5
         throw new UnsupportedCompilationException( "Map expressions not supported as native java yet." );
 116  
     }
 117  
 
 118  
     public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
 119  
         throws OgnlException
 120  
     {
 121  0
         return visitor.visit( this, data );
 122  
     }
 123  
 
 124  
     private Class getDefaultMapClass( OgnlContext context )
 125  
     {
 126  3
         Class defaultMapClass = defaultMapClassMap.get( context );
 127  3
         if ( defaultMapClass != null )
 128  
         {
 129  0
             return defaultMapClass;
 130  
         }
 131  
 
 132  
         /* Try to get LinkedHashMap; if older JDK than 1.4 use HashMap */
 133  
         try
 134  
         {
 135  3
             defaultMapClass = OgnlRuntime.classForName( context, "java.util.LinkedHashMap" );
 136  
         }
 137  0
         catch ( ClassNotFoundException ex )
 138  
         {
 139  0
             defaultMapClass = HashMap.class;
 140  3
         }
 141  3
         defaultMapClassMap.put( context, defaultMapClass );
 142  3
         return defaultMapClass;
 143  
     }
 144  
 }