1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.definition.dao;
22
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29
30 import org.apache.tiles.Definition;
31 import org.apache.tiles.definition.DefinitionsFactoryException;
32 import org.apache.tiles.util.LocaleUtil;
33
34 /***
35 * A definition DAO that uses {@link Locale} as a customization key and loads
36 * definitions from URLs. It does not cache definitions in any way.
37 *
38 * @version $Rev: 672360 $ $Date: 2008-06-27 21:12:29 +0200 (ven, 27 giu 2008) $
39 * @since 2.1.0
40 */
41 public class LocaleUrlDefinitionDAO extends BaseLocaleUrlDefinitionDAO {
42
43 /***
44 * <p>
45 * Returns a definition, given its name and the customization key.
46 * </p>
47 * <strong>WARNING!</strong> This method is slow! It loads all the
48 * definitions and then selects the needed one.
49 *
50 * @param name The name of the definition.
51 * @param customizationKey The customization key.
52 * @return The requested definition, if found, otherwise <code>null</code>.
53 * The inheritance of the definition must not be resolved.
54 * @since 2.1.0
55 */
56 public Definition getDefinition(String name, Locale customizationKey) {
57 Map<String, Definition> defsMap = getDefinitions(customizationKey);
58 return defsMap != null ? defsMap.get(name) : null;
59 }
60
61 /*** {@inheritDoc} */
62 public Map<String, Definition> getDefinitions(Locale customizationKey) {
63 List<String> postfixes = LocaleUtil.calculatePostfixes(customizationKey);
64 Map<String, Definition> localeDefsMap = new HashMap<String, Definition>();
65 for (Object postfix : postfixes) {
66
67 for (URL url : sourceURLs) {
68 String path = url.toExternalForm();
69
70 String newPath = LocaleUtil.concatPostfix(path,
71 (String) postfix);
72 try {
73 URL newUrl = new URL(newPath);
74 Map<String, Definition> defsMap = loadDefinitionsFromURL(newUrl);
75 if (defsMap != null) {
76 localeDefsMap.putAll(defsMap);
77 }
78 } catch (MalformedURLException e) {
79 throw new DefinitionsFactoryException("Error parsing URL "
80 + newPath, e);
81 }
82 }
83 }
84
85 return localeDefsMap;
86 }
87 }