1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 13:26:09 +0200 (sab, 18 lug 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 public RegexpDefinitionPatternMatcher(String pattern, Definition definition) {
57 this.pattern = Pattern.compile(pattern);
58 this.definition = definition;
59 }
60
61 /*** {@inheritDoc} */
62 public Definition createDefinition(String definitionName) {
63 Definition retValue = null;
64 Matcher matcher = pattern.matcher(definitionName);
65 if (matcher.matches()) {
66 int groupCount = matcher.groupCount() + 1;
67 Object[] vars = new Object[groupCount];
68 for (int i = 0; i < groupCount; i++) {
69 vars[i] = matcher.group(i);
70 }
71 retValue = PatternUtil.replacePlaceholders(definition,
72 definitionName, vars);
73 }
74 return retValue;
75 }
76 }