1/*2 * $Id: WildcardPatternDefinitionResolver.java 823662 2009-10-09 18:48:03Z 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 */2122package org.apache.tiles.definition.pattern;
2324import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.LinkedHashSet;
27import java.util.List;
28import java.util.Map;
29import java.util.Set;
3031import org.apache.tiles.Definition;
32import org.apache.tiles.util.WildcardHelper;
3334/***35 * Uses wildcards syntax to match definition names and its parameters.36 *37 * @param <T> The type of the customization key.38 * @version $Rev: 823662 $ $Date: 2009-10-09 20:48:03 +0200 (ven, 09 ott 2009) $39 * @since 2.2.040 */41publicclass WildcardPatternDefinitionResolver<T> implements
42 PatternDefinitionResolver<T> {
4344/***45 * Stores patterns depending on the locale they refer to.46 *47 * @since 2.2.048 */49private Map<T, List<WildcardMapping>> localePatternPaths =
50new HashMap<T, List<WildcardMapping>>();
5152/***53 * An object that helps in resolving definitions with wildcards.54 *55 * @since 2.2.056 */57privateWildcardHelper wildcardHelper = newWildcardHelper();
5859/*** {@inheritDoc} */60public Definition resolveDefinition(String name, T customizationKey) {
61 Definition retValue = null;
62if (localePatternPaths.containsKey(customizationKey)) {
63 retValue = resolveWildcardDefinition(localePatternPaths
64 .get(customizationKey), name);
65 }
66return retValue;
67 }
6869/*** {@inheritDoc} */70public Map<String, Definition> storeDefinitionPatterns(Map<String, Definition> localeDefsMap,
71 T customizationKey) {
72 List<WildcardMapping> lpaths = localePatternPaths
73 .get(customizationKey);
74if (lpaths == null) {
75 lpaths = new ArrayList<WildcardMapping>();
76 localePatternPaths.put(customizationKey, lpaths);
77 }
7879return addWildcardPaths(lpaths, localeDefsMap);
80 }
8182/***83 * Adds wildcard paths that are stored inside a normal definition map.84 *85 * @param paths The list containing the currently stored paths.86 * @param defsMap The definition map to parse.87 * @return The map of the definitions not recognized as containing88 * definition patterns.89 */90private Map<String, Definition> addWildcardPaths(List<WildcardMapping> paths,
91 Map<String, Definition> defsMap) {
92 Set<String> excludedKeys = new LinkedHashSet<String>();
93for (Map.Entry<String, Definition> de : defsMap.entrySet()) {
94 String key = de.getKey();
95if (key.indexOf('*') != -1) {
96 paths.add(new WildcardMapping(key,
97new Definition(de.getValue())));
98 } else {
99 excludedKeys.add(key);
100 }
101 }
102return PatternUtil.createExtractedMap(defsMap, excludedKeys);
103 }
104105/***106 * Try to resolve a wildcard definition.107 *108 * @param paths The list containing the currently stored paths.109 * @param name The name of the definition to resolve.110 * @return A definition, if found, or <code>null</code> if not.111 */112private Definition resolveWildcardDefinition(
113 List<WildcardMapping> paths, String name) {
114 Definition d = null;
115116for (WildcardMapping wm : paths) {
117 List<String> vars = wildcardHelper.match(name, wm.getPattern());
118if (vars != null) {
119 d = PatternUtil.replacePlaceholders(wm.getDefinition(), name, vars.toArray());
120break;
121 }
122 }
123124return d;
125 }
126127/***128 * Maps a pattern with a definition in cache.129 *130 * @since 2.2.0131 */132privateclass WildcardMapping {
133134/***135 * The compiled pattern.136 */137privateint[] pattern;
138139/***140 * The definition that matches the pattern.141 */142private Definition definition;
143144/***145 * Constructor.146 *147 * @param pattern The compiled pattern.148 * @param definition A definition that matches the pattern.149 *150 * @since 2.2.0151 */152public WildcardMapping(String pattern, Definition definition) {
153this.pattern = wildcardHelper.compilePattern(pattern);
154this.definition = definition;
155 }
156157/***158 * Returns the definition.159 *160 * @return The definition.161 * @since 2.2.0162 */163public Definition getDefinition() {
164return definition;
165 }
166167/***168 * Returns the compiled pattern.169 *170 * @return The pattern.171 * @since 2.2.0172 */173publicint[] getPattern() {
174return pattern;
175 }
176 }
177 }