1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Array;
23 import java.lang.reflect.Field;
24 import java.lang.reflect.Method;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Map;
29
30
31
32
33
34
35
36 class ExpressionUtil
37 {
38
39 private static final Object[] NO_ARGS = {};
40
41 private static final Class[] NO_PARAMS = {};
42
43 private static final Class[] OBJECT_PARAM = { Object.class };
44
45 private static final Class[] STRING_PARAM = { String.class };
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public static Object evaluate( String expression, Map contexts )
62 {
63 Object value = null;
64
65 if ( expression != null && expression.length() > 0 )
66 {
67 List segments = Arrays.asList( expression.split( "/", 0 ) );
68 if ( !segments.isEmpty() )
69 {
70 Object context = contexts.get( segments.get( 0 ) );
71 if ( context != null )
72 {
73 value = evaluate( context, segments.subList( 1, segments.size() ) );
74 }
75 }
76 }
77
78 return value;
79 }
80
81
82
83
84
85
86
87
88 private static Object evaluate( Object context, List segments )
89 {
90 Object value = null;
91
92 if ( segments.isEmpty() )
93 {
94 value = context;
95 }
96 else if ( context != null )
97 {
98 Object target = null;
99 String segment = (String) segments.get( 0 );
100 if ( segment.length() <= 0 )
101 {
102 value = context;
103 }
104 else if ( context.getClass().isArray() && Character.isDigit( segment.charAt( 0 ) ) )
105 {
106 try
107 {
108 int index = Integer.parseInt( segment );
109 target = Array.get( context, index );
110 }
111 catch ( RuntimeException e )
112 {
113
114 }
115 }
116 else if ( ( context instanceof List ) && Character.isDigit( segment.charAt( 0 ) ) )
117 {
118 try
119 {
120 int index = Integer.parseInt( segment );
121 target = ( (List) context ).get( index );
122 }
123 catch ( RuntimeException e )
124 {
125
126 }
127 }
128 else
129 {
130 target = getProperty( context, segment );
131 }
132 value = evaluate( target, segments.subList( 1, segments.size() ) );
133 }
134
135 return value;
136 }
137
138
139
140
141
142
143
144
145 static Object getProperty( Object context, String property )
146 {
147 Object value;
148
149 Class type = context.getClass();
150 if ( context instanceof Collection )
151 {
152 type = Collection.class;
153 }
154 else if ( context instanceof Map )
155 {
156 type = Map.class;
157 }
158
159 try
160 {
161 try
162 {
163 Method method = type.getMethod( property, NO_PARAMS );
164 value = method.invoke( context, NO_ARGS );
165 }
166 catch ( NoSuchMethodException e )
167 {
168 try
169 {
170 String name = "get" + Character.toUpperCase( property.charAt( 0 ) ) + property.substring( 1 );
171 Method method = type.getMethod( name, NO_PARAMS );
172 value = method.invoke( context, NO_ARGS );
173 }
174 catch ( NoSuchMethodException e1 )
175 {
176 try
177 {
178 String name = "is" + Character.toUpperCase( property.charAt( 0 ) ) + property.substring( 1 );
179 Method method = type.getMethod( name, NO_PARAMS );
180 value = method.invoke( context, NO_ARGS );
181 }
182 catch ( NoSuchMethodException e2 )
183 {
184 try
185 {
186 Method method;
187 try
188 {
189 method = type.getMethod( "get", STRING_PARAM );
190 }
191 catch ( NoSuchMethodException e3 )
192 {
193 method = type.getMethod( "get", OBJECT_PARAM );
194 }
195 value = method.invoke( context, new Object[] { property } );
196 }
197 catch ( NoSuchMethodException e3 )
198 {
199 try
200 {
201 Field field = type.getField( property );
202 value = field.get( context );
203 }
204 catch ( NoSuchFieldException e4 )
205 {
206 if ( "length".equals( property ) && type.isArray() )
207 {
208 value = new Integer( Array.getLength( context ) );
209 }
210 else
211 {
212 throw e4;
213 }
214 }
215 }
216 }
217 }
218 }
219 }
220 catch ( Exception e )
221 {
222 value = null;
223 }
224 return value;
225 }
226
227 }