1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.util; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.HashMap; |
19 |
| import java.util.List; |
20 |
| import java.util.Map; |
21 |
| |
22 |
| import org.apache.hivemind.ApplicationRuntimeException; |
23 |
| import org.apache.oro.text.regex.MalformedPatternException; |
24 |
| import org.apache.oro.text.regex.MatchResult; |
25 |
| import org.apache.oro.text.regex.Pattern; |
26 |
| import org.apache.oro.text.regex.PatternCompiler; |
27 |
| import org.apache.oro.text.regex.PatternMatcher; |
28 |
| import org.apache.oro.text.regex.PatternMatcherInput; |
29 |
| import org.apache.oro.text.regex.Perl5Compiler; |
30 |
| import org.apache.oro.text.regex.Perl5Matcher; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| public class RegexpMatcher |
41 |
| { |
42 |
| private PatternCompiler _patternCompiler; |
43 |
| |
44 |
| private PatternMatcher _matcher; |
45 |
| |
46 |
| private Map _compiledPatterns = new HashMap(); |
47 |
| |
48 |
| private Map _escapedPatternStrings = new HashMap(); |
49 |
| |
50 |
299
| protected Pattern compilePattern(String pattern)
|
51 |
| { |
52 |
299
| if (_patternCompiler == null)
|
53 |
132
| _patternCompiler = new Perl5Compiler();
|
54 |
| |
55 |
299
| try
|
56 |
| { |
57 |
299
| return _patternCompiler.compile(pattern, Perl5Compiler.SINGLELINE_MASK);
|
58 |
| } |
59 |
| catch (MalformedPatternException ex) |
60 |
| { |
61 |
2
| throw new ApplicationRuntimeException(ex);
|
62 |
| } |
63 |
| } |
64 |
| |
65 |
5036
| protected Pattern getCompiledPattern(String pattern)
|
66 |
| { |
67 |
5036
| Pattern result = (Pattern) _compiledPatterns.get(pattern);
|
68 |
| |
69 |
5036
| if (result == null)
|
70 |
| { |
71 |
299
| result = compilePattern(pattern);
|
72 |
297
| _compiledPatterns.put(pattern, result);
|
73 |
| } |
74 |
| |
75 |
5034
| return result;
|
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
1
| public void clear()
|
83 |
| { |
84 |
1
| _compiledPatterns.clear();
|
85 |
| } |
86 |
| |
87 |
5034
| protected PatternMatcher getPatternMatcher()
|
88 |
| { |
89 |
5034
| if (_matcher == null)
|
90 |
130
| _matcher = new Perl5Matcher();
|
91 |
| |
92 |
5034
| return _matcher;
|
93 |
| } |
94 |
| |
95 |
4998
| public boolean matches(String pattern, String input)
|
96 |
| { |
97 |
4998
| Pattern compiledPattern = getCompiledPattern(pattern);
|
98 |
| |
99 |
4997
| return getPatternMatcher().matches(input, compiledPattern);
|
100 |
| } |
101 |
| |
102 |
18
| public boolean contains(String pattern, String input)
|
103 |
| { |
104 |
18
| Pattern compiledPattern = getCompiledPattern(pattern);
|
105 |
| |
106 |
17
| return getPatternMatcher().contains(input, compiledPattern);
|
107 |
| } |
108 |
| |
109 |
10
| public String getEscapedPatternString(String pattern)
|
110 |
| { |
111 |
10
| String result = (String) _escapedPatternStrings.get(pattern);
|
112 |
| |
113 |
10
| if (result == null)
|
114 |
| { |
115 |
10
| result = Perl5Compiler.quotemeta(pattern);
|
116 |
| |
117 |
10
| _escapedPatternStrings.put(pattern, result);
|
118 |
| } |
119 |
| |
120 |
10
| return result;
|
121 |
| } |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
17
| public RegexpMatch[] getMatches(String pattern, String input)
|
134 |
| { |
135 |
17
| Pattern compiledPattern = getCompiledPattern(pattern);
|
136 |
| |
137 |
17
| PatternMatcher matcher = getPatternMatcher();
|
138 |
17
| PatternMatcherInput matcherInput = new PatternMatcherInput(input);
|
139 |
| |
140 |
17
| List matches = new ArrayList();
|
141 |
| |
142 |
17
| while (matcher.contains(matcherInput, compiledPattern))
|
143 |
| { |
144 |
18
| MatchResult match = matcher.getMatch();
|
145 |
| |
146 |
18
| matches.add(new RegexpMatch(match));
|
147 |
| } |
148 |
| |
149 |
17
| return (RegexpMatch[]) matches.toArray(new RegexpMatch[matches.size()]);
|
150 |
| } |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
3
| public String[] getMatches(String pattern, String input, int subgroup)
|
164 |
| { |
165 |
3
| Pattern compiledPattern = getCompiledPattern(pattern);
|
166 |
| |
167 |
3
| PatternMatcher matcher = getPatternMatcher();
|
168 |
3
| PatternMatcherInput matcherInput = new PatternMatcherInput(input);
|
169 |
| |
170 |
3
| List matches = new ArrayList();
|
171 |
| |
172 |
3
| while (matcher.contains(matcherInput, compiledPattern))
|
173 |
| { |
174 |
8
| MatchResult match = matcher.getMatch();
|
175 |
| |
176 |
8
| String matchedInput = match.group(subgroup);
|
177 |
| |
178 |
8
| matches.add(matchedInput);
|
179 |
| } |
180 |
| |
181 |
3
| return (String[]) matches.toArray(new String[matches.size()]);
|
182 |
| } |
183 |
| |
184 |
| } |