1 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 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 protected Pattern compilePattern(String 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 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 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 pattern, String input) 96 { 97 Pattern compiledPattern = getCompiledPattern(pattern); 98 99 return getPatternMatcher().matches(input, compiledPattern); 100 } 101 102 public boolean contains(String pattern, String input) 103 { 104 Pattern compiledPattern = getCompiledPattern(pattern); 105 106 return getPatternMatcher().contains(input, compiledPattern); 107 } 108 109 public String getEscapedPatternString(String pattern) 110 { 111 String result = (String ) _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 133 public RegexpMatch[] getMatches(String pattern, String input) 134 { 135 Pattern compiledPattern = getCompiledPattern(pattern); 136 137 PatternMatcher matcher = getPatternMatcher(); 138 PatternMatcherInput matcherInput = new PatternMatcherInput(input); 139 140 List matches = new ArrayList (); 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 163 public String [] getMatches(String pattern, String input, int subgroup) 164 { 165 Pattern compiledPattern = getCompiledPattern(pattern); 166 167 PatternMatcher matcher = getPatternMatcher(); 168 PatternMatcherInput matcherInput = new PatternMatcherInput(input); 169 170 List matches = new ArrayList (); 171 172 while (matcher.contains(matcherInput, compiledPattern)) 173 { 174 MatchResult match = matcher.getMatch(); 175 176 String matchedInput = match.group(subgroup); 177 178 matches.add(matchedInput); 179 } 180 181 return (String []) matches.toArray(new String [matches.size()]); 182 } 183 184 } 185 | Popular Tags |