1 18 19 package org.apache.tools.ant.util.regexp; 20 21 import java.util.Vector ; 22 import org.apache.oro.text.regex.MatchResult; 23 import org.apache.oro.text.regex.Pattern; 24 import org.apache.oro.text.regex.Perl5Compiler; 25 import org.apache.oro.text.regex.Perl5Matcher; 26 import org.apache.tools.ant.BuildException; 27 28 32 public class JakartaOroMatcher implements RegexpMatcher { 33 34 private String pattern; 35 protected final Perl5Compiler compiler = new Perl5Compiler(); 37 protected final Perl5Matcher matcher = new Perl5Matcher(); 38 40 43 public JakartaOroMatcher() { 44 } 45 46 50 public void setPattern(String pattern) { 51 this.pattern = pattern; 52 } 53 54 58 public String getPattern() { 59 return this.pattern; 60 } 61 62 68 protected Pattern getCompiledPattern(int options) 69 throws BuildException { 70 try { 71 Pattern p = compiler.compile(pattern, getCompilerOptions(options)); 73 return p; 74 } catch (Exception e) { 75 throw new BuildException(e); 76 } 77 } 78 79 85 public boolean matches(String argument) throws BuildException { 86 return matches(argument, MATCH_DEFAULT); 87 } 88 89 96 public boolean matches(String input, int options) 97 throws BuildException { 98 Pattern p = getCompiledPattern(options); 99 return matcher.contains(input, p); 100 } 101 102 113 public Vector getGroups(String argument) throws BuildException { 114 return getGroups(argument, MATCH_DEFAULT); 115 } 116 117 128 public Vector getGroups(String input, int options) 129 throws BuildException { 130 if (!matches(input, options)) { 131 return null; 132 } 133 Vector v = new Vector (); 134 MatchResult mr = matcher.getMatch(); 135 int cnt = mr.groups(); 136 for (int i = 0; i < cnt; i++) { 137 String match = mr.group(i); 138 if (match == null) { 140 match = ""; 141 } 142 v.addElement(match); 143 } 144 return v; 145 } 146 147 152 protected int getCompilerOptions(int options) { 153 int cOptions = Perl5Compiler.DEFAULT_MASK; 154 155 if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) { 156 cOptions |= Perl5Compiler.CASE_INSENSITIVE_MASK; 157 } 158 if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) { 159 cOptions |= Perl5Compiler.MULTILINE_MASK; 160 } 161 if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) { 162 cOptions |= Perl5Compiler.SINGLELINE_MASK; 163 } 164 165 return cOptions; 166 } 167 168 } 169 | Popular Tags |