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.tree;
17:
18: import javax.el.ELException;
19:
20: /**
21: * Tree store class.
22: * A tree store holds a {@link de.odysseus.el.tree.TreeBuilder} and a
23: * {@link de.odysseus.el.tree.TreeCache}, provided at construction time.
24: * The <code>get(String)</code> method is then used to serve expression trees.
25: *
26: * @author Christoph Beck
27: */
28: public class TreeStore {
29: private final TreeCache cache;
30: private final TreeBuilder builder;
31:
32: /**
33: * Constructor.
34: * @param builder the tree builder
35: * @param cache the tree cache (may be <code>null</code>)
36: */
37: public TreeStore(TreeBuilder builder, TreeCache cache) {
38: super ();
39:
40: this .builder = builder;
41: this .cache = cache;
42: }
43:
44: public TreeBuilder getBuilder() {
45: return builder;
46: }
47:
48: /**
49: * Get a {@link Tree}.
50: * If a tree for the given expression is present in the cache, it is
51: * taken from there; otherwise, the expression string is parsed and
52: * the resulting tree is added to the cache.
53: * @param expression expression string
54: * @return expression tree
55: */
56: public Tree get(String expression) throws ELException {
57: if (cache == null) {
58: return builder.build(expression);
59: }
60: Tree tree = cache.get(expression);
61: if (tree == null) {
62: cache.put(expression, tree = builder.build(expression));
63: }
64: return tree;
65: }
66: }
|