Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractPatternDefinitionResolver |
|
| 2.0;2 |
1 | /* | |
2 | * $Id: AbstractPatternDefinitionResolver.java 1539385 2013-11-06 16:24:13Z mck $ | |
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.ArrayList; | |
25 | import java.util.HashMap; | |
26 | import java.util.List; | |
27 | import java.util.Map; | |
28 | ||
29 | import org.apache.tiles.Definition; | |
30 | ||
31 | /** | |
32 | * A pattern definition resolver that stores {@link DefinitionPatternMatcher} | |
33 | * separated by customization key. <br> | |
34 | * Implementations should provide a way to translate a definition to a | |
35 | * {@link DefinitionPatternMatcher}. | |
36 | * | |
37 | * @param <T> The type of the customization key. | |
38 | * @version $Rev: 1539385 $ $Date: 2013-11-07 03:24:13 +1100 (Thu, 07 Nov 2013) $ | |
39 | * @since 2.2.0 | |
40 | */ | |
41 | 26 | public abstract class AbstractPatternDefinitionResolver<T> implements |
42 | PatternDefinitionResolver<T> { | |
43 | ||
44 | /** | |
45 | * Stores patterns depending on the locale they refer to. | |
46 | */ | |
47 | 26 | private Map<T, List<DefinitionPatternMatcher>> localePatternPaths = |
48 | new HashMap<T, List<DefinitionPatternMatcher>>(); | |
49 | ||
50 | /** {@inheritDoc} */ | |
51 | public Definition resolveDefinition(String name, T customizationKey) { | |
52 | 28 | Definition retValue = null; |
53 | 28 | if (localePatternPaths.containsKey(customizationKey)) { |
54 | 28 | retValue = searchAndResolveDefinition(localePatternPaths |
55 | .get(customizationKey), name); | |
56 | } | |
57 | 28 | return retValue; |
58 | } | |
59 | ||
60 | /** {@inheritDoc} */ | |
61 | public Map<String, Definition> storeDefinitionPatterns(Map<String, Definition> localeDefsMap, | |
62 | T customizationKey) { | |
63 | 48 | List<DefinitionPatternMatcher> lpaths = localePatternPaths |
64 | .get(customizationKey); | |
65 | 48 | if (lpaths == null) { |
66 | 32 | lpaths = new ArrayList<DefinitionPatternMatcher>(); |
67 | 32 | localePatternPaths.put(customizationKey, lpaths); |
68 | } | |
69 | ||
70 | 48 | return addDefinitionsAsPatternMatchers(lpaths, localeDefsMap); |
71 | } | |
72 | ||
73 | /** | |
74 | * Adds definitions, filtering and adding them to the list of definition | |
75 | * pattern matchers. Only a subset of definitions will be transformed into | |
76 | * definition pattern matchers. | |
77 | * | |
78 | * @param matchers The list containing the currently stored definition pattern | |
79 | * matchers. | |
80 | * @param defsMap The definition map to parse. | |
81 | * @return The map of the definitions not recognized as containing | |
82 | * definition patterns. | |
83 | * @since 2.2.1 | |
84 | */ | |
85 | protected abstract Map<String, Definition> addDefinitionsAsPatternMatchers( | |
86 | List<DefinitionPatternMatcher> matchers, | |
87 | Map<String, Definition> defsMap); | |
88 | ||
89 | /** | |
90 | * Try to resolve a definition by iterating all pattern matchers. | |
91 | * | |
92 | * @param paths The list containing the currently stored paths. | |
93 | * @param name The name of the definition to resolve. | |
94 | * @return A definition, if found, or <code>null</code> if not. | |
95 | */ | |
96 | private Definition searchAndResolveDefinition( | |
97 | List<DefinitionPatternMatcher> paths, String name) { | |
98 | 28 | Definition d = null; |
99 | ||
100 | 28 | for (DefinitionPatternMatcher wm : paths) { |
101 | 42 | d = wm.createDefinition(name); |
102 | 42 | if (d != null) { |
103 | 19 | break; |
104 | } | |
105 | 23 | } |
106 | ||
107 | 28 | return d; |
108 | } | |
109 | ||
110 | ||
111 | /** | |
112 | * Used to clear all entries in the localePatternPaths for a specific locale. Necessary when reloading definition | |
113 | * files to ensure that the list is cleared first | |
114 | * | |
115 | * @param customizationKey | |
116 | */ | |
117 | @Override | |
118 | public void clearPatternPaths(T customizationKey) { | |
119 | 2 | if (localePatternPaths.get(customizationKey) != null) |
120 | 2 | localePatternPaths.get(customizationKey).clear(); |
121 | 2 | } |
122 | } |