1 /* 2 * $Id: LocaleUrlDefinitionDAO.java 672360 2008-06-27 19:12:29Z apetrelli $ 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 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 // For each postfix, all the sources must be loaded. 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 }