01: /*
02: * Copyright 2006, 2007 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package de.odysseus.el.samples.extensions;
17:
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import javax.el.ELContext;
22: import javax.el.ExpressionFactory;
23: import javax.el.ValueExpression;
24:
25: import de.odysseus.el.ExpressionFactoryImpl;
26: import de.odysseus.el.tree.TreeBuilder;
27: import de.odysseus.el.tree.TreeStore;
28: import de.odysseus.el.tree.impl.Builder;
29: import de.odysseus.el.tree.impl.Cache;
30: import de.odysseus.el.util.SimpleContext;
31:
32: public class NullProperties {
33: /**
34: * Sample usage: enable resolving <code>null</code> properties.
35: */
36: public static void main(String... args)
37: throws NoSuchMethodException {
38: // create our customized builder
39: TreeBuilder builder = new Builder(
40: Builder.Feature.NULL_PROPERTIES);
41:
42: // create our factory which uses our customized builder
43: ExpressionFactory f = new ExpressionFactoryImpl(new TreeStore(
44: builder, new Cache(10)));
45:
46: // create our context
47: ELContext context = new SimpleContext();
48:
49: // create our expression we want to evaluate
50: ValueExpression e = f.createValueExpression(context,
51: "${map[null]}", String.class);
52:
53: // create a map containing a value for key <code>null</code> and make it available
54: Map<String, String> map = new HashMap<String, String>();
55: map.put(null, "foo");
56: context.getELResolver().setValue(context, null, "map", map);
57:
58: // let's go...
59: System.out.println(e.getValue(context)); // --> "foo"
60: }
61: }
|