View Javadoc

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      private Map<OgnlContext, Class> defaultMapClassMap = new HashMap<OgnlContext, Class>();
39  
40      public ASTMap( int id )
41      {
42          super( id );
43      }
44  
45      public ASTMap( OgnlParser p, int id )
46      {
47          super( p, id );
48      }
49  
50      protected void setClassName( String value )
51      {
52          className = value;
53      }
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          return className;
64      }
65  
66      protected Object getValueBody( OgnlContext context, Object source )
67          throws OgnlException
68      {
69          Map answer;
70  
71          if ( className == null )
72          {
73              Class defaultMapClass = getDefaultMapClass( context );
74              try
75              {
76                  answer = (Map) defaultMapClass.newInstance();
77              }
78              catch ( Exception ex )
79              {
80                  /* This should never happen */
81                  throw new OgnlException( "Default Map class '" + defaultMapClass.getName() + "' instantiation error",
82                                           ex );
83              }
84          }
85          else
86          {
87              try
88              {
89                  answer = (Map) OgnlRuntime.classForName( context, className ).newInstance();
90              }
91              catch ( Exception ex )
92              {
93                  throw new OgnlException( "Map implementor '" + className + "' not found", ex );
94              }
95          }
96  
97          for ( int i = 0; i < jjtGetNumChildren(); ++i )
98          {
99              ASTKeyValue kv = (ASTKeyValue) children[i];
100             Node k = kv.getKey(), v = kv.getValue();
101 
102             answer.put( k.getValue( context, source ), ( v == null ) ? null : v.getValue( context, source ) );
103         }
104 
105         return answer;
106     }
107 
108     public String toGetSourceString( OgnlContext context, Object target )
109     {
110         throw new UnsupportedCompilationException( "Map expressions not supported as native java yet." );
111     }
112 
113     public String toSetSourceString( OgnlContext context, Object target )
114     {
115         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         return visitor.visit( this, data );
122     }
123 
124     private Class getDefaultMapClass( OgnlContext context )
125     {
126         Class defaultMapClass = defaultMapClassMap.get( context );
127         if ( defaultMapClass != null )
128         {
129             return defaultMapClass;
130         }
131 
132         /* Try to get LinkedHashMap; if older JDK than 1.4 use HashMap */
133         try
134         {
135             defaultMapClass = OgnlRuntime.classForName( context, "java.util.LinkedHashMap" );
136         }
137         catch ( ClassNotFoundException ex )
138         {
139             defaultMapClass = HashMap.class;
140         }
141         defaultMapClassMap.put( context, defaultMapClass );
142         return defaultMapClass;
143     }
144 }