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 | 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 | |
|
57 | |
|
58 | |
|
59 | |
|
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 | |
|
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 | |
|
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 | |
} |