1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package org.apache.tiles.definition.dao; |
23 | |
|
24 | |
import java.util.HashSet; |
25 | |
import java.util.LinkedHashMap; |
26 | |
import java.util.Locale; |
27 | |
import java.util.Map; |
28 | |
import java.util.Set; |
29 | |
|
30 | |
import org.apache.tiles.Definition; |
31 | |
import org.apache.tiles.definition.NoSuchDefinitionException; |
32 | |
import org.apache.tiles.request.ApplicationContext; |
33 | |
import org.slf4j.Logger; |
34 | |
import org.slf4j.LoggerFactory; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class ResolvingLocaleUrlDefinitionDAO extends |
49 | |
CachingLocaleUrlDefinitionDAO { |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 15 | private final Logger log = LoggerFactory.getLogger(ResolvingLocaleUrlDefinitionDAO.class); |
55 | |
|
56 | |
public ResolvingLocaleUrlDefinitionDAO(ApplicationContext applicationContext) { |
57 | 15 | super(applicationContext); |
58 | 15 | } |
59 | |
|
60 | |
|
61 | |
@Override |
62 | |
protected Map<String, Definition> loadParentDefinitions(Locale parentLocale) { |
63 | 0 | return loadRawDefinitionsFromResources(parentLocale); |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
protected Map<String, Definition> loadDefinitions(Locale customizationKey) { |
68 | 14 | Map<String, Definition> localeDefsMap = super.loadDefinitions(customizationKey); |
69 | 14 | Map<String, Definition> defsMap = definitionResolver |
70 | |
.storeDefinitionPatterns(copyDefinitionMap(localeDefsMap), |
71 | |
customizationKey); |
72 | 14 | resolveInheritances(defsMap, customizationKey); |
73 | 14 | locale2definitionMap.put(customizationKey, defsMap); |
74 | 14 | return defsMap; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
@Override |
79 | |
protected Definition getDefinitionFromResolver(String name, |
80 | |
Locale customizationKey) { |
81 | 5 | Definition retValue = super.getDefinitionFromResolver(name, customizationKey); |
82 | 5 | if (retValue != null && retValue.getExtends() != null) { |
83 | 1 | Definition parent = getDefinition(retValue.getExtends(), customizationKey); |
84 | 1 | retValue.inherit(parent); |
85 | |
} |
86 | |
|
87 | 5 | return retValue; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
protected void resolveInheritances(Map<String, Definition> map, Locale locale) { |
99 | 14 | if (map != null) { |
100 | 14 | Set<String> alreadyResolvedDefinitions = new HashSet<String>(); |
101 | 14 | for (Definition definition : map.values()) { |
102 | 79 | resolveInheritance(definition, map, locale, |
103 | |
alreadyResolvedDefinitions); |
104 | 79 | } |
105 | |
} |
106 | 14 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
protected void resolveInheritance(Definition definition, |
124 | |
Map<String, Definition> definitions, Locale locale, |
125 | |
Set<String> alreadyResolvedDefinitions) { |
126 | |
|
127 | 87 | if (!definition.isExtending() |
128 | |
|| alreadyResolvedDefinitions.contains(definition.getName())) { |
129 | 79 | return; |
130 | |
} |
131 | |
|
132 | 8 | log.debug("Resolve definition for child name='{}' extends='{}.", |
133 | |
definition.getName(), definition.getExtends()); |
134 | |
|
135 | |
|
136 | 8 | alreadyResolvedDefinitions.add(definition.getName()); |
137 | |
|
138 | |
|
139 | 8 | Definition parent = definitions.get(definition.getExtends()); |
140 | 8 | if (parent == null) { |
141 | 1 | String msg = "Error while resolving definition inheritance: child '" |
142 | |
+ definition.getName() |
143 | |
+ "' can't find its ancestor '" |
144 | |
+ definition.getExtends() |
145 | |
+ "'. Please check your description file."; |
146 | |
|
147 | 1 | throw new NoSuchDefinitionException(msg); |
148 | |
} |
149 | |
|
150 | 7 | resolveInheritance(parent, definitions, locale, |
151 | |
alreadyResolvedDefinitions); |
152 | |
|
153 | 7 | definition.inherit(parent); |
154 | 7 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
@Override |
166 | |
protected Map<String, Definition> copyDefinitionMap( |
167 | |
Map<String, Definition> localeDefsMap) { |
168 | 28 | Map<String, Definition> retValue = new LinkedHashMap<String, Definition>( |
169 | |
localeDefsMap.size()); |
170 | |
|
171 | 28 | for (Map.Entry<String, Definition> entry : localeDefsMap.entrySet()) { |
172 | 182 | Definition definition = new Definition(entry.getValue()); |
173 | 182 | retValue.put(entry.getKey(), definition); |
174 | 182 | } |
175 | |
|
176 | 28 | return retValue; |
177 | |
} |
178 | |
} |