1 package org.apache.commons.ognl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27
28
29
30
31
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
57
58
59
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
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
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 }