Project Tiles has retired. For details please refer to its Attic page.
LocaleUrlDefinitionDAO xref
View Javadoc

1   /*
2    * $Id: LocaleUrlDefinitionDAO.java 1297705 2012-03-06 20:44:30Z nlebas $
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.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  import org.apache.tiles.Definition;
30  import org.apache.tiles.request.ApplicationContext;
31  import org.apache.tiles.request.ApplicationResource;
32  import org.apache.tiles.request.locale.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: 1297705 $ $Date: 2012-03-07 07:44:30 +1100 (Wed, 07 Mar 2012) $
39   * @since 2.1.0
40   */
41  public class LocaleUrlDefinitionDAO extends BaseLocaleUrlDefinitionDAO {
42  
43      public LocaleUrlDefinitionDAO(ApplicationContext applicationContext) {
44          super(applicationContext);
45      }
46  
47      /**
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 the
52       * 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.0
59       */
60      public Definition getDefinition(String name, Locale customizationKey) {
61          Map<String, Definition> defsMap = getDefinitions(customizationKey);
62          return defsMap.get(name);
63      }
64  
65      /** {@inheritDoc} */
66      public 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 specific
70          for (Locale postfix : postfixes) {
71              // For each postfix, all the sources must be loaded.
72              for (ApplicationResource resource : sources) {
73                  ApplicationResource newResource = applicationContext.getResource(resource, postfix);
74                  if (newResource != null) {
75                      Map<String, Definition> defsMap = loadDefinitionsFromResource(newResource);
76                      if (defsMap != null) {
77                          localeDefsMap.putAll(defsMap);
78                      }
79                  }
80              }
81          }
82          return localeDefsMap;
83      }
84  
85      /**
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       */
90      private ArrayList<Locale> computeLocales(Locale customizationKey) {
91          Locale postfix;
92          if(customizationKey == null) {
93              postfix = Locale.ROOT;
94          } else {
95              postfix = customizationKey;
96          }
97          ArrayList<Locale> postfixes = new ArrayList<Locale>();
98          while (postfix != null) {
99              postfixes.add(postfix);
100             postfix = LocaleUtil.getParentLocale(postfix);
101         }
102         Collections.reverse(postfixes);
103         return postfixes;
104     }
105 }