1/*2 * $Id: LocaleUrlDefinitionDAO.java 672360 2008-06-27 19:12:29Z apetrelli $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.net.MalformedURLException;
24import java.net.URL;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Locale;
28import java.util.Map;
2930import org.apache.tiles.Definition;
31import org.apache.tiles.definition.DefinitionsFactoryException;
32import org.apache.tiles.util.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: 672360 $ $Date: 2008-06-27 21:12:29 +0200 (ven, 27 giu 2008) $39 * @since 2.1.040 */41publicclassLocaleUrlDefinitionDAOextendsBaseLocaleUrlDefinitionDAO {
4243/***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 the48 * 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.055 */56public Definition getDefinition(String name, Locale customizationKey) {
57 Map<String, Definition> defsMap = getDefinitions(customizationKey);
58return defsMap != null ? defsMap.get(name) : null;
59 }
6061/*** {@inheritDoc} */62public Map<String, Definition> getDefinitions(Locale customizationKey) {
63 List<String> postfixes = LocaleUtil.calculatePostfixes(customizationKey);
64 Map<String, Definition> localeDefsMap = new HashMap<String, Definition>();
65for (Object postfix : postfixes) {
66// For each postfix, all the sources must be loaded.67for (URL url : sourceURLs) {
68 String path = url.toExternalForm();
6970 String newPath = LocaleUtil.concatPostfix(path,
71 (String) postfix);
72try {
73 URL newUrl = new URL(newPath);
74 Map<String, Definition> defsMap = loadDefinitionsFromURL(newUrl);
75if (defsMap != null) {
76 localeDefsMap.putAll(defsMap);
77 }
78 } catch (MalformedURLException e) {
79thrownewDefinitionsFactoryException("Error parsing URL "80 + newPath, e);
81 }
82 }
83 }
8485return localeDefsMap;
86 }
87 }