Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RegexpDefinitionPatternMatcher |
|
| 2.0;2 |
1 | /* | |
2 | * $Id: RegexpDefinitionPatternMatcher.java 795343 2009-07-18 11:26:09Z 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 | ||
22 | package org.apache.tiles.definition.pattern.regexp; | |
23 | ||
24 | import java.util.regex.Matcher; | |
25 | import java.util.regex.Pattern; | |
26 | ||
27 | import org.apache.tiles.Definition; | |
28 | import org.apache.tiles.definition.pattern.DefinitionPatternMatcher; | |
29 | import org.apache.tiles.definition.pattern.PatternUtil; | |
30 | ||
31 | /** | |
32 | * Matches regular expression patterns in definitions. | |
33 | * | |
34 | * @version $Rev: 795343 $ $Date: 2009-07-18 21:26:09 +1000 (Sat, 18 Jul 2009) $ | |
35 | * @since 2.2.0 | |
36 | */ | |
37 | public class RegexpDefinitionPatternMatcher implements DefinitionPatternMatcher { | |
38 | ||
39 | /** | |
40 | * The pattern to match. | |
41 | */ | |
42 | private Pattern pattern; | |
43 | ||
44 | /** | |
45 | * The definition to use as a basis. | |
46 | */ | |
47 | private Definition definition; | |
48 | ||
49 | /** | |
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.0 | |
55 | */ | |
56 | 2 | public RegexpDefinitionPatternMatcher(String pattern, Definition definition) { |
57 | 2 | this.pattern = Pattern.compile(pattern); |
58 | 2 | this.definition = definition; |
59 | 2 | } |
60 | ||
61 | /** {@inheritDoc} */ | |
62 | public Definition createDefinition(String definitionName) { | |
63 | 1 | Definition retValue = null; |
64 | 1 | Matcher matcher = pattern.matcher(definitionName); |
65 | 1 | if (matcher.matches()) { |
66 | 1 | int groupCount = matcher.groupCount() + 1; |
67 | 1 | Object[] vars = new Object[groupCount]; |
68 | 4 | for (int i = 0; i < groupCount; i++) { |
69 | 3 | vars[i] = matcher.group(i); |
70 | } | |
71 | 1 | retValue = PatternUtil.replacePlaceholders(definition, |
72 | definitionName, vars); | |
73 | } | |
74 | 1 | return retValue; |
75 | } | |
76 | } |