1 package org.tigris.scarab.util; 2 3 48 49 import org.apache.oro.text.regex.MalformedPatternException; 50 import org.apache.oro.text.regex.Pattern; 51 import org.apache.oro.text.regex.PatternCompiler; 52 import org.apache.oro.text.regex.Perl5Compiler; 53 import org.apache.oro.text.regex.Perl5Matcher; 54 import org.apache.oro.text.regex.Perl5Substitution; 55 import org.apache.oro.text.regex.Substitution; 56 import org.apache.oro.text.regex.Util; 57 import org.apache.oro.util.Cache; 58 import org.apache.oro.util.CacheLRU; 59 60 67 public class RegexProcessor extends Object 68 { 69 70 protected static Cache patterns = new CacheLRU(); 71 protected static Cache substitutions = new CacheLRU(); 72 73 private PatternCompiler compiler; 74 75 public RegexProcessor() 76 { 77 compiler = new Perl5Compiler(); 78 } 79 80 public String process(String input, String regex, String substitution) throws MalformedPatternException 81 { 82 String result = input; 83 Pattern pattern = (Pattern) patterns.getElement(regex); 84 if (pattern == null) 85 { 86 pattern = compiler.compile(regex); 87 patterns.addElement(regex, pattern); 88 } 89 90 Substitution subst = (Substitution) substitutions.getElement(substitution); 91 if (subst == null) 92 { 93 subst = new Perl5Substitution(substitution); 94 substitutions.addElement(substitution, subst); 95 } 96 97 Perl5Matcher matcher = new Perl5Matcher(); 98 99 result = Util.substitute(matcher, pattern, subst, input, Util.SUBSTITUTE_ALL); 100 101 return result; 102 } 103 104 } 105 | Popular Tags |