KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > RegexpMatcher


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.util;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
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  * Streamlines the interface to ORO by implicitly constructing the necessary compilers and matchers,
34  * and by caching compiled patterns.
35  *
36  * @author Howard Lewis Ship
37  * @since 3.0
38  */

39
40 public class RegexpMatcher
41 {
42     private PatternCompiler _patternCompiler;
43
44     private PatternMatcher _matcher;
45
46     private Map JavaDoc _compiledPatterns = new HashMap JavaDoc();
47
48     private Map JavaDoc _escapedPatternStrings = new HashMap JavaDoc();
49
50     protected Pattern compilePattern(String JavaDoc pattern)
51     {
52         if (_patternCompiler == null)
53             _patternCompiler = new Perl5Compiler();
54
55         try
56         {
57             return _patternCompiler.compile(pattern, Perl5Compiler.SINGLELINE_MASK);
58         }
59         catch (MalformedPatternException ex)
60         {
61             throw new ApplicationRuntimeException(ex);
62         }
63     }
64
65     protected Pattern getCompiledPattern(String JavaDoc pattern)
66     {
67         Pattern result = (Pattern) _compiledPatterns.get(pattern);
68
69         if (result == null)
70         {
71             result = compilePattern(pattern);
72             _compiledPatterns.put(pattern, result);
73         }
74
75         return result;
76     }
77
78     /**
79      * Clears any previously compiled patterns.
80      */

81
82     public void clear()
83     {
84         _compiledPatterns.clear();
85     }
86
87     protected PatternMatcher getPatternMatcher()
88     {
89         if (_matcher == null)
90             _matcher = new Perl5Matcher();
91
92         return _matcher;
93     }
94
95     public boolean matches(String JavaDoc pattern, String JavaDoc input)
96     {
97         Pattern compiledPattern = getCompiledPattern(pattern);
98
99         return getPatternMatcher().matches(input, compiledPattern);
100     }
101
102     public boolean contains(String JavaDoc pattern, String JavaDoc input)
103     {
104         Pattern compiledPattern = getCompiledPattern(pattern);
105
106         return getPatternMatcher().contains(input, compiledPattern);
107     }
108
109     public String JavaDoc getEscapedPatternString(String JavaDoc pattern)
110     {
111         String JavaDoc result = (String JavaDoc) _escapedPatternStrings.get(pattern);
112
113         if (result == null)
114         {
115             result = Perl5Compiler.quotemeta(pattern);
116
117             _escapedPatternStrings.put(pattern, result);
118         }
119
120         return result;
121     }
122
123     /**
124      * Given an input string, finds all matches in an input string for the pattern.
125      *
126      * @param pattern
127      * the regexp pattern for matching
128      * @param input
129      * the string to search for matches within
130      * @return array (possibly empty) of matches
131      * @since 4.0
132      */

133     public RegexpMatch[] getMatches(String JavaDoc pattern, String JavaDoc input)
134     {
135         Pattern compiledPattern = getCompiledPattern(pattern);
136
137         PatternMatcher matcher = getPatternMatcher();
138         PatternMatcherInput matcherInput = new PatternMatcherInput(input);
139
140         List JavaDoc matches = new ArrayList JavaDoc();
141
142         while (matcher.contains(matcherInput, compiledPattern))
143         {
144             MatchResult match = matcher.getMatch();
145
146             matches.add(new RegexpMatch(match));
147         }
148
149         return (RegexpMatch[]) matches.toArray(new RegexpMatch[matches.size()]);
150     }
151
152     /**
153      * Given an input string, finds all matches in an input string for the pattern.
154      *
155      * @param pattern
156      * the regexp pattern for matching
157      * @param input
158      * the string to search for matches within
159      * @param subgroup
160      * the group (sub-expression) within the pattern to return as a match
161      * @return array (possibly empty) of matching strings
162      */

163     public String JavaDoc[] getMatches(String JavaDoc pattern, String JavaDoc input, int subgroup)
164     {
165         Pattern compiledPattern = getCompiledPattern(pattern);
166
167         PatternMatcher matcher = getPatternMatcher();
168         PatternMatcherInput matcherInput = new PatternMatcherInput(input);
169
170         List JavaDoc matches = new ArrayList JavaDoc();
171
172         while (matcher.contains(matcherInput, compiledPattern))
173         {
174             MatchResult match = matcher.getMatch();
175
176             String JavaDoc matchedInput = match.group(subgroup);
177
178             matches.add(matchedInput);
179         }
180
181         return (String JavaDoc[]) matches.toArray(new String JavaDoc[matches.size()]);
182     }
183
184 }
185
Popular Tags