1/*2 * $Id: RegexpPatternDefinitionResolver.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;
30import java.util.regex.Matcher;
31import java.util.regex.Pattern;
3233import org.apache.tiles.Definition;
3435/***36 * Matches definition patterns through the use of regular expressions. To allow the use of regular expression,37 * remember to set the definition name with a tilde (~) as the first character.38 *39 * @param <T> The customization key class.40 * @version $Rev: 823662 $ $Date: 2009-10-09 20:48:03 +0200 (ven, 09 ott 2009) $41 * @since 2.2.042 */43publicclass RegexpPatternDefinitionResolver<T> implements
44 PatternDefinitionResolver<T> {
4546/***47 * Maps a customization key to a pattern mapping list.48 */49private Map<T, List<PatternMapping>> key2patternMappingList = new HashMap<T, List<PatternMapping>>();
5051/*** {@inheritDoc} */52public Definition resolveDefinition(String name, T customizationKey) {
53 Definition retValue = null;
54 List<PatternMapping> mappings = key2patternMappingList.get(customizationKey);
55if (mappings != null) {
56for (PatternMapping mapping : mappings) {
57 Matcher matcher = mapping.pattern.matcher(name);
58if (matcher.matches()) {
59int groupCount = matcher.groupCount() + 1;
60 Object[] vars = new Object[groupCount];
61for (int i = 0; i < groupCount; i++) {
62 vars[i] = matcher.group(i);
63 }
64 retValue = PatternUtil.replacePlaceholders(mapping.definition, name, vars);
65break;
66 }
67 }
68 }
69return retValue;
70 }
7172/*** {@inheritDoc} */73public Map<String, Definition> storeDefinitionPatterns(Map<String, Definition> localeDefsMap,
74 T customizationKey) {
75 List<PatternMapping> patternMappingList = key2patternMappingList.get(customizationKey);
76if (patternMappingList == null) {
77 patternMappingList = new ArrayList<PatternMapping>();
78 key2patternMappingList.put(customizationKey, patternMappingList);
79 }
80return addRegexpMappings(localeDefsMap, patternMappingList);
81 }
8283/***84 * Adds the regular expression mappings.85 *86 * @param localeDefsMap The map containing the definitions.87 * @param patternMappingList The list of pattern mapping.88 * @return The map of the definitions not recognized as containing89 * definition patterns.90 */91private Map<String, Definition> addRegexpMappings(Map<String, Definition> localeDefsMap,
92 List<PatternMapping> patternMappingList) {
93 Set<String> excludedKeys = new LinkedHashSet<String>();
94for (Map.Entry<String, Definition> entry : localeDefsMap.entrySet()) {
95 String name = entry.getKey();
96if (name.startsWith("~")) {
97 patternMappingList.add(new PatternMapping(name.substring(1),
98new Definition(entry.getValue())));
99 } else {
100 excludedKeys.add(name);
101 }
102 }
103return PatternUtil.createExtractedMap(localeDefsMap, excludedKeys);
104 }
105106/***107 * Maps a pattern to a definition.108 *109 * @version $Rev: 823662 $ $Date: 2009-10-09 20:48:03 +0200 (ven, 09 ott 2009) $110 * @since 2.2.0111 */112privatestaticfinalclass PatternMapping {
113114/***115 * The pattern.116 */117private Pattern pattern;
118119/***120 * The definition.121 */122private Definition definition;
123124/***125 * Constructor.126 *127 * @param regexp The regular expression for the pattern.128 * @param definition The definition.129 * @since 2.2.0130 */131private PatternMapping(String regexp, Definition definition) {
132 pattern = Pattern.compile(regexp);
133this.definition = definition;
134 }
135 }
136 }