1 /*
2 * $Id: BasicAttributeEvaluatorFactory.java 788032 2009-06-24 14:08:32Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles.evaluator;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.tiles.Attribute;
28 import org.apache.tiles.Expression;
29
30 /**
31 * Basic implementation of {@link AttributeEvaluatorFactory}. It supports a
32 * default attribute evaluator, in case the language is not recognized.
33 *
34 * @version $Rev: 788032 $ $Date: 2009-06-25 00:08:32 +1000 (Thu, 25 Jun 2009) $
35 * @since 2.2.0
36 */
37 public class BasicAttributeEvaluatorFactory implements
38 AttributeEvaluatorFactory {
39
40 /**
41 * The default evaluator to return if it is not found in the map of known
42 * languages.
43 */
44 private AttributeEvaluator defaultEvaluator;
45
46 /**
47 * Maps names of expression languages to their attribute evaluator.
48 *
49 * @since 2.2.0
50 */
51 private Map<String, AttributeEvaluator> language2evaluator;
52
53 /**
54 * Constructor.
55 *
56 * @param defaultEvaluator The default evaluator to return if it is not
57 * found in the map of known languages.
58 * @since 2.2.0
59 */
60 public BasicAttributeEvaluatorFactory(AttributeEvaluator defaultEvaluator) {
61 this.defaultEvaluator = defaultEvaluator;
62 language2evaluator = new HashMap<String, AttributeEvaluator>();
63 }
64
65 /**
66 * Registers a known expression language with its attribute evaluator.
67 *
68 * @param language The name of the expression language.
69 * @param evaluator The associated attribute evaluator.
70 * @since 2.2.0
71 */
72 public void registerAttributeEvaluator(String language, AttributeEvaluator evaluator) {
73 language2evaluator.put(language, evaluator);
74 }
75
76 /** {@inheritDoc} */
77 public AttributeEvaluator getAttributeEvaluator(String language) {
78 AttributeEvaluator retValue = language2evaluator.get(language);
79 if (retValue == null) {
80 retValue = defaultEvaluator;
81 }
82 return retValue;
83 }
84
85 /** {@inheritDoc} */
86 public AttributeEvaluator getAttributeEvaluator(Attribute attribute) {
87 Expression expression = attribute.getExpressionObject();
88 if (expression != null) {
89 return getAttributeEvaluator(expression.getLanguage());
90 }
91 return defaultEvaluator;
92 }
93 }