1 /*
2 * $Id: PrefixedPatternDefinitionResolver.java 942880 2010-05-10 19:58:07Z 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.definition.pattern;
23
24 import java.util.HashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import org.apache.tiles.Definition;
31 import org.apache.tiles.Expression;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36 * This resolver allows the use of multiple pattern matching languages. The
37 * syntax of definition names must be <code>LANGUAGENAME:expression</code>.<br>
38 * The different languages must be registered through the use of
39 * {@link #registerDefinitionPatternMatcherFactory(String, DefinitionPatternMatcherFactory)}
40 * method before using this resolver.
41 *
42 * @param <T> The type of the customization key.
43 * @version $Rev: 942880 $ $Date: 2010-05-11 05:58:07 +1000 (Tue, 11 May 2010) $
44 * @since 2.2.0
45 */
46 public class PrefixedPatternDefinitionResolver<T> extends
47 AbstractPatternDefinitionResolver<T> {
48
49 /**
50 * The logging object.
51 */
52 private Logger logger = LoggerFactory.getLogger(getClass());
53
54 /**
55 * Matches languages names to the corresponding
56 * {@link DefinitionPatternMatcherFactory}.
57 */
58 private Map<String, DefinitionPatternMatcherFactory> language2matcherFactory;
59
60 /**
61 * Constructor.
62 *
63 * @since 2.2.0
64 */
65 public PrefixedPatternDefinitionResolver() {
66 language2matcherFactory = new HashMap<String, DefinitionPatternMatcherFactory>();
67 }
68
69 /**
70 * Registers a {@link DefinitionPatternMatcherFactory} connected to a
71 * particular language.
72 *
73 * @param language The name of the language.
74 * @param factory The pattern matcher factory to register.
75 * @since 2.2.0
76 */
77 public void registerDefinitionPatternMatcherFactory(String language,
78 DefinitionPatternMatcherFactory factory) {
79 language2matcherFactory.put(language, factory);
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 protected Map<String, Definition> addDefinitionsAsPatternMatchers(
85 List<DefinitionPatternMatcher> matchers,
86 Map<String, Definition> defsMap) {
87 Set<String> excludedKeys = new LinkedHashSet<String>();
88 for (Map.Entry<String, Definition> entry : defsMap.entrySet()) {
89 String key = entry.getKey();
90 Expression expression = Expression
91 .createExpressionFromDescribedExpression(key);
92 if (expression.getLanguage() != null) {
93 DefinitionPatternMatcherFactory factory = language2matcherFactory
94 .get(expression.getLanguage());
95 if (factory != null) {
96 DefinitionPatternMatcher matcher = factory
97 .createDefinitionPatternMatcher(expression
98 .getExpression(), new Definition(entry
99 .getValue()));
100 matchers.add(matcher);
101 } else {
102 logger.warn("Cannot find a DefinitionPatternMatcherFactory for expression '{}'",
103 key);
104 }
105 } else {
106 excludedKeys.add(key);
107 }
108 }
109 return PatternUtil.createExtractedMap(defsMap, excludedKeys);
110 }
111 }