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 java.util.Collection; |
23 | |
import java.util.Map; |
24 | |
import java.util.Set; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 1 | public class MapPropertyAccessor |
34 | |
implements PropertyAccessor |
35 | |
{ |
36 | |
|
37 | |
public Object getProperty( Map<String, Object> context, Object target, Object name ) |
38 | |
throws OgnlException |
39 | |
{ |
40 | |
Object result; |
41 | |
@SuppressWarnings( "unchecked" ) |
42 | 205 | Map<Object, Object> map = (Map<Object, Object>) target; |
43 | 205 | Node currentNode = ( (OgnlContext) context ).getCurrentNode().jjtGetParent(); |
44 | 205 | boolean indexedAccess = false; |
45 | |
|
46 | 205 | if ( currentNode == null ) |
47 | |
{ |
48 | 0 | throw new OgnlException( "node is null for '" + name + "'" ); |
49 | |
} |
50 | 205 | if ( !( currentNode instanceof ASTProperty ) ) |
51 | |
{ |
52 | 5 | currentNode = currentNode.jjtGetParent(); |
53 | |
} |
54 | 205 | if ( currentNode instanceof ASTProperty ) |
55 | |
{ |
56 | 205 | indexedAccess = ( (ASTProperty) currentNode ).isIndexedAccess(); |
57 | |
} |
58 | |
|
59 | 205 | if ( ( name instanceof String ) && !indexedAccess ) |
60 | |
{ |
61 | 145 | if ( "size".equals( name ) ) |
62 | |
{ |
63 | 53 | result = map.size(); |
64 | |
} |
65 | 92 | else if ( "keys".equals( name ) || "keySet".equals( name ) ) |
66 | |
{ |
67 | 4 | result = map.keySet(); |
68 | |
} |
69 | 88 | else if ( "values".equals( name ) ) |
70 | |
{ |
71 | 2 | result = map.values(); |
72 | |
} |
73 | 86 | else if ( "isEmpty".equals( name ) ) |
74 | |
{ |
75 | 2 | result = map.isEmpty() ? Boolean.TRUE : Boolean.FALSE; |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 84 | result = map.get( name ); |
80 | |
} |
81 | |
} |
82 | |
else |
83 | |
{ |
84 | 60 | result = map.get( name ); |
85 | |
} |
86 | |
|
87 | 205 | return result; |
88 | |
} |
89 | |
|
90 | |
public void setProperty( Map<String, Object> context, Object target, Object name, Object value ) |
91 | |
throws OgnlException |
92 | |
{ |
93 | |
@SuppressWarnings( "unchecked" ) |
94 | 7 | Map<Object, Object> map = (Map<Object, Object>) target; |
95 | 7 | map.put( name, value ); |
96 | 7 | } |
97 | |
|
98 | |
public String getSourceAccessor( OgnlContext context, Object target, Object index ) |
99 | |
{ |
100 | 105 | Node currentNode = context.getCurrentNode().jjtGetParent(); |
101 | 105 | boolean indexedAccess = false; |
102 | |
|
103 | 105 | if ( currentNode == null ) |
104 | |
{ |
105 | 0 | throw new RuntimeException( "node is null for '" + index + "'" ); |
106 | |
} |
107 | |
|
108 | 105 | if ( !( currentNode instanceof ASTProperty ) ) |
109 | |
{ |
110 | 2 | currentNode = currentNode.jjtGetParent(); |
111 | |
} |
112 | |
|
113 | 105 | if ( currentNode instanceof ASTProperty ) |
114 | |
{ |
115 | 105 | indexedAccess = ( (ASTProperty) currentNode ).isIndexedAccess(); |
116 | |
} |
117 | |
|
118 | 105 | String indexStr = index.toString(); |
119 | |
|
120 | 105 | context.setCurrentAccessor( Map.class ); |
121 | 105 | context.setCurrentType( Object.class ); |
122 | |
|
123 | 105 | if ( String.class.isInstance( index ) && !indexedAccess ) |
124 | |
{ |
125 | 83 | String key = indexStr.replaceAll( "\"", "" ); |
126 | |
|
127 | 83 | if ( "size".equals( key ) ) |
128 | |
{ |
129 | 20 | context.setCurrentType( int.class ); |
130 | 20 | return ".size()"; |
131 | |
} |
132 | 63 | else if ( "keys".equals( key ) || "keySet".equals( key ) ) |
133 | |
{ |
134 | 3 | context.setCurrentType( Set.class ); |
135 | 3 | return ".keySet()"; |
136 | |
} |
137 | 60 | else if ( "values".equals( key ) ) |
138 | |
{ |
139 | 1 | context.setCurrentType( Collection.class ); |
140 | 1 | return ".values()"; |
141 | |
} |
142 | 59 | else if ( "isEmpty".equals( key ) ) |
143 | |
{ |
144 | 1 | context.setCurrentType( boolean.class ); |
145 | 1 | return ".isEmpty()"; |
146 | |
} |
147 | |
} |
148 | |
|
149 | 80 | return ".get(" + indexStr + ")"; |
150 | |
} |
151 | |
|
152 | |
public String getSourceSetter( OgnlContext context, Object target, Object index ) |
153 | |
{ |
154 | 29 | context.setCurrentAccessor( Map.class ); |
155 | 29 | context.setCurrentType( Object.class ); |
156 | |
|
157 | 29 | String indexStr = index.toString(); |
158 | |
|
159 | 29 | if ( String.class.isInstance( index ) ) |
160 | |
{ |
161 | 29 | String key = indexStr.replaceAll( "\"", "" ); |
162 | |
|
163 | 29 | if ( "size".equals( key ) || "keys".equals( key ) || "keySet".equals( key ) || "values".equals( key ) |
164 | |
|| "isEmpty".equals( key ) ) |
165 | |
{ |
166 | 8 | return ""; |
167 | |
} |
168 | |
} |
169 | |
|
170 | 21 | return ".put(" + indexStr + ", $3)"; |
171 | |
} |
172 | |
} |