1 18 19 package org.apache.tools.ant.util.regexp; 20 21 import java.util.Vector ; 22 import org.apache.regexp.RE; 23 import org.apache.regexp.RESyntaxException; 24 import org.apache.tools.ant.BuildException; 25 26 30 public class JakartaRegexpMatcher implements RegexpMatcher { 31 32 private String pattern; 33 34 38 public void setPattern(String pattern) { 39 this.pattern = pattern; 40 } 41 42 46 public String getPattern() { 47 return pattern; 48 } 49 50 57 protected RE getCompiledPattern(int options) 58 throws BuildException { 59 int cOptions = getCompilerOptions(options); 60 try { 61 RE reg = new RE(pattern); 62 reg.setMatchFlags(cOptions); 63 return reg; 64 } catch (RESyntaxException e) { 65 throw new BuildException(e); 66 } 67 } 68 69 75 public boolean matches(String argument) throws BuildException { 76 return matches(argument, MATCH_DEFAULT); 77 } 78 79 86 public boolean matches(String input, int options) 87 throws BuildException { 88 return matches(input, getCompiledPattern(options)); 89 } 90 91 private boolean matches(String input, RE reg) { 92 return reg.match(input); 93 } 94 95 106 public Vector getGroups(String argument) throws BuildException { 107 return getGroups(argument, MATCH_DEFAULT); 108 } 109 110 121 public Vector getGroups(String input, int options) 122 throws BuildException { 123 RE reg = getCompiledPattern(options); 124 if (!matches(input, reg)) { 125 return null; 126 } 127 Vector v = new Vector (); 128 int cnt = reg.getParenCount(); 129 for (int i = 0; i < cnt; i++) { 130 String match = reg.getParen(i); 131 if (match == null) { 133 match = ""; 134 } 135 v.addElement(match); 136 } 137 return v; 138 } 139 140 145 protected int getCompilerOptions(int options) { 146 int cOptions = RE.MATCH_NORMAL; 147 148 if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) { 149 cOptions |= RE.MATCH_CASEINDEPENDENT; 150 } 151 if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) { 152 cOptions |= RE.MATCH_MULTILINE; 153 } 154 if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) { 155 cOptions |= RE.MATCH_SINGLELINE; 156 } 157 158 return cOptions; 159 } 160 161 } 162 | Popular Tags |