1 18 19 package org.apache.tools.ant.util.regexp; 20 21 import java.util.Vector ; 22 import java.util.regex.Matcher ; 23 import java.util.regex.Pattern ; 24 import java.util.regex.PatternSyntaxException ; 25 import org.apache.tools.ant.BuildException; 26 27 32 public class Jdk14RegexpMatcher implements RegexpMatcher { 33 34 private String pattern; 35 36 37 public Jdk14RegexpMatcher() { 38 } 39 40 44 public void setPattern(String pattern) { 45 this.pattern = pattern; 46 } 47 48 53 public String getPattern() { 54 return pattern; 55 } 56 57 63 protected Pattern getCompiledPattern(int options) 64 throws BuildException { 65 int cOptions = getCompilerOptions(options); 66 try { 67 Pattern p = Pattern.compile(this.pattern, cOptions); 68 return p; 69 } catch (PatternSyntaxException e) { 70 throw new BuildException(e); 71 } 72 } 73 74 80 public boolean matches(String argument) throws BuildException { 81 return matches(argument, MATCH_DEFAULT); 82 } 83 84 91 public boolean matches(String input, int options) 92 throws BuildException { 93 try { 94 Pattern p = getCompiledPattern(options); 95 return p.matcher(input).find(); 96 } catch (Exception e) { 97 throw new BuildException(e); 98 } 99 } 100 101 112 public Vector getGroups(String argument) throws BuildException { 113 return getGroups(argument, MATCH_DEFAULT); 114 } 115 116 127 public Vector getGroups(String input, int options) 128 throws BuildException { 129 Pattern p = getCompiledPattern(options); 130 Matcher matcher = p.matcher(input); 131 if (!matcher.find()) { 132 return null; 133 } 134 Vector v = new Vector (); 135 int cnt = matcher.groupCount(); 136 for (int i = 0; i <= cnt; i++) { 137 String match = matcher.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 = Pattern.UNIX_LINES; 155 156 if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) { 157 cOptions |= Pattern.CASE_INSENSITIVE; 158 } 159 if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) { 160 cOptions |= Pattern.MULTILINE; 161 } 162 if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) { 163 cOptions |= Pattern.DOTALL; 164 } 165 166 return cOptions; 167 } 168 169 } 170 | Popular Tags |