1/*2 * $Id: RegexpDefinitionPatternMatcher.java 795343 2009-07-18 11:26:09Z 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.regexp;
2324import java.util.regex.Matcher;
25import java.util.regex.Pattern;
2627import org.apache.tiles.Definition;
28import org.apache.tiles.definition.pattern.DefinitionPatternMatcher;
29import org.apache.tiles.definition.pattern.PatternUtil;
3031/***32 * Matches regular expression patterns in definitions.33 *34 * @version $Rev: 795343 $ $Date: 2009-07-18 13:26:09 +0200 (sab, 18 lug 2009) $35 * @since 2.2.036 */37publicclassRegexpDefinitionPatternMatcher implements DefinitionPatternMatcher {
3839/***40 * The pattern to match.41 */42private Pattern pattern;
4344/***45 * The definition to use as a basis.46 */47private Definition definition;
4849/***50 * Constructor.51 *52 * @param pattern The pattern to use, in string form.53 * @param definition The definition to use as a basis.54 * @since 2.2.055 */56publicRegexpDefinitionPatternMatcher(String pattern, Definition definition) {
57this.pattern = Pattern.compile(pattern);
58this.definition = definition;
59 }
6061/*** {@inheritDoc} */62public Definition createDefinition(String definitionName) {
63 Definition retValue = null;
64 Matcher matcher = pattern.matcher(definitionName);
65if (matcher.matches()) {
66int groupCount = matcher.groupCount() + 1;
67 Object[] vars = new Object[groupCount];
68for (int i = 0; i < groupCount; i++) {
69 vars[i] = matcher.group(i);
70 }
71 retValue = PatternUtil.replacePlaceholders(definition,
72 definitionName, vars);
73 }
74return retValue;
75 }
76 }