|
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 |
897
| protected Pattern compilePattern(String pattern)
|
|
51 |
| { |
|
52 |
897
| if (_patternCompiler == null)
|
|
53 |
396
| _patternCompiler = new Perl5Compiler();
|
|
54 |
| |
|
55 |
897
| try
|
|
56 |
| { |
|
57 |
897
| return _patternCompiler.compile(pattern, Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.READ_ONLY_MASK);
|
|
58 |
| } |
|
59 |
| catch (MalformedPatternException ex) |
|
60 |
| { |
|
61 |
6
| throw new ApplicationRuntimeException(ex);
|
|
62 |
| } |
|
63 |
| } |
|
64 |
| |
|
65 |
15486
| protected Pattern getCompiledPattern(String pattern)
|
|
66 |
| { |
|
67 |
15486
| Pattern result = (Pattern) _compiledPatterns.get(pattern);
|
|
68 |
| |
|
69 |
15486
| if (result == null)
|
|
70 |
| { |
|
71 |
897
| result = compilePattern(pattern);
|
|
72 |
891
| _compiledPatterns.put(pattern, result);
|
|
73 |
| } |
|
74 |
| |
|
75 |
15480
| return result;
|
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
3
| public void clear()
|
|
83 |
| { |
|
84 |
3
| _compiledPatterns.clear();
|
|
85 |
| } |
|
86 |
| |
|
87 |
15480
| protected PatternMatcher getPatternMatcher()
|
|
88 |
| { |
|
89 |
15480
| if (_matcher == null)
|
|
90 |
390
| _matcher = new Perl5Matcher();
|
|
91 |
| |
|
92 |
15480
| return _matcher;
|
|
93 |
| } |
|
94 |
| |
|
95 |
15372
| public boolean matches(String pattern, String input)
|
|
96 |
| { |
|
97 |
15372
| Pattern compiledPattern = getCompiledPattern(pattern);
|
|
98 |
| |
|
99 |
15369
| return getPatternMatcher().matches(input, compiledPattern);
|
|
100 |
| } |
|
101 |
| |
|
102 |
54
| public boolean contains(String pattern, String input)
|
|
103 |
| { |
|
104 |
54
| Pattern compiledPattern = getCompiledPattern(pattern);
|
|
105 |
| |
|
106 |
51
| return getPatternMatcher().contains(input, compiledPattern);
|
|
107 |
| } |
|
108 |
| |
|
109 |
30
| public String getEscapedPatternString(String pattern)
|
|
110 |
| { |
|
111 |
30
| String result = (String) _escapedPatternStrings.get(pattern);
|
|
112 |
| |
|
113 |
30
| if (result == null)
|
|
114 |
| { |
|
115 |
30
| result = Perl5Compiler.quotemeta(pattern);
|
|
116 |
| |
|
117 |
30
| _escapedPatternStrings.put(pattern, result);
|
|
118 |
| } |
|
119 |
| |
|
120 |
30
| return result;
|
|
121 |
| } |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
51
| public RegexpMatch[] getMatches(String pattern, String input)
|
|
134 |
| { |
|
135 |
51
| Pattern compiledPattern = getCompiledPattern(pattern);
|
|
136 |
| |
|
137 |
51
| PatternMatcher matcher = getPatternMatcher();
|
|
138 |
51
| PatternMatcherInput matcherInput = new PatternMatcherInput(input);
|
|
139 |
| |
|
140 |
51
| List matches = new ArrayList();
|
|
141 |
| |
|
142 |
51
| while (matcher.contains(matcherInput, compiledPattern))
|
|
143 |
| { |
|
144 |
54
| MatchResult match = matcher.getMatch();
|
|
145 |
| |
|
146 |
54
| matches.add(new RegexpMatch(match));
|
|
147 |
| } |
|
148 |
| |
|
149 |
51
| return (RegexpMatch[]) matches.toArray(new RegexpMatch[matches.size()]);
|
|
150 |
| } |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
9
| public String[] getMatches(String pattern, String input, int subgroup)
|
|
164 |
| { |
|
165 |
9
| Pattern compiledPattern = getCompiledPattern(pattern);
|
|
166 |
| |
|
167 |
9
| PatternMatcher matcher = getPatternMatcher();
|
|
168 |
9
| PatternMatcherInput matcherInput = new PatternMatcherInput(input);
|
|
169 |
| |
|
170 |
9
| List matches = new ArrayList();
|
|
171 |
| |
|
172 |
9
| while (matcher.contains(matcherInput, compiledPattern))
|
|
173 |
| { |
|
174 |
24
| MatchResult match = matcher.getMatch();
|
|
175 |
| |
|
176 |
24
| String matchedInput = match.group(subgroup);
|
|
177 |
| |
|
178 |
24
| matches.add(matchedInput);
|
|
179 |
| } |
|
180 |
| |
|
181 |
9
| return (String[]) matches.toArray(new String[matches.size()]);
|
|
182 |
| } |
|
183 |
| |
|
184 |
| } |