1/*2 * $Id: LocaleUrlDefinitionDAO.java 1297705 2012-03-06 20:44:30Z nlebas $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.definition.dao;
2223import java.util.ArrayList;
24import java.util.Collections;
25import java.util.HashMap;
26import java.util.Locale;
27import java.util.Map;
2829import org.apache.tiles.Definition;
30import org.apache.tiles.request.ApplicationContext;
31import org.apache.tiles.request.ApplicationResource;
32import org.apache.tiles.request.locale.LocaleUtil;
3334/**35 * A definition DAO that uses {@link Locale} as a customization key and loads36 * definitions from URLs. It does not cache definitions in any way.37 *38 * @version $Rev: 1297705 $ $Date: 2012-03-07 07:44:30 +1100 (Wed, 07 Mar 2012) $39 * @since 2.1.040 */41publicclassLocaleUrlDefinitionDAOextendsBaseLocaleUrlDefinitionDAO {
4243publicLocaleUrlDefinitionDAO(ApplicationContext applicationContext) {
44super(applicationContext);
45 }
4647/**48 * <p>49 * Returns a definition, given its name and the customization key.50 * </p>51 * <strong>WARNING!</strong> This method is slow! It loads all the52 * definitions and then selects the needed one.53 *54 * @param name The name of the definition.55 * @param customizationKey The customization key.56 * @return The requested definition, if found, otherwise <code>null</code>.57 * The inheritance of the definition must not be resolved.58 * @since 2.1.059 */60publicDefinition getDefinition(String name, Locale customizationKey) {
61 Map<String, Definition> defsMap = getDefinitions(customizationKey);
62return defsMap.get(name);
63 }
6465/** {@inheritDoc} */66public Map<String, Definition> getDefinitions(Locale customizationKey) {
67 ArrayList<Locale> postfixes = computeLocales(customizationKey);
68 Map<String, Definition> localeDefsMap = new HashMap<String, Definition>();
69// process the postfixes from the root to the most specific70for (Locale postfix : postfixes) {
71// For each postfix, all the sources must be loaded.72for (ApplicationResource resource : sources) {
73 ApplicationResource newResource = applicationContext.getResource(resource, postfix);
74if (newResource != null) {
75 Map<String, Definition> defsMap = loadDefinitionsFromResource(newResource);
76if (defsMap != null) {
77 localeDefsMap.putAll(defsMap);
78 }
79 }
80 }
81 }
82return localeDefsMap;
83 }
8485/**86 * Returns a list of locales from root to the customizationKey.87 * @param customizationKey the target Locale.88 * @return the list of its ancestors.89 */90private ArrayList<Locale> computeLocales(Locale customizationKey) {
91 Locale postfix;
92if(customizationKey == null) {
93 postfix = Locale.ROOT;
94 } else {
95 postfix = customizationKey;
96 }
97 ArrayList<Locale> postfixes = new ArrayList<Locale>();
98while (postfix != null) {
99 postfixes.add(postfix);
100 postfix = LocaleUtil.getParentLocale(postfix);
101 }
102 Collections.reverse(postfixes);
103return postfixes;
104 }
105 }