1 18 package org.apache.tools.ant.filters; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.util.Vector ; 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.types.Parameter; 25 import org.apache.tools.ant.types.RegularExpression; 26 import org.apache.tools.ant.util.regexp.Regexp; 27 28 46 public final class LineContainsRegExp 47 extends BaseParamFilterReader 48 implements ChainableReader { 49 50 private static final String REGEXP_KEY = "regexp"; 51 52 53 private static final String NEGATE_KEY = "negate"; 54 55 56 private Vector regexps = new Vector (); 57 58 63 private String line = null; 64 65 private boolean negate = false; 66 67 72 public LineContainsRegExp() { 73 super(); 74 } 75 76 82 public LineContainsRegExp(final Reader in) { 83 super(in); 84 } 85 86 97 public int read() throws IOException { 98 if (!getInitialized()) { 99 initialize(); 100 setInitialized(true); 101 } 102 103 int ch = -1; 104 105 if (line != null) { 106 ch = line.charAt(0); 107 if (line.length() == 1) { 108 line = null; 109 } else { 110 line = line.substring(1); 111 } 112 } else { 113 final int regexpsSize = regexps.size(); 114 115 for (line = readLine(); line != null; line = readLine()) { 116 boolean matches = true; 117 for (int i = 0; matches && i < regexpsSize; i++) { 118 RegularExpression regexp 119 = (RegularExpression) regexps.elementAt(i); 120 Regexp re = regexp.getRegexp(getProject()); 121 matches = re.matches(line); 122 } 123 if (matches ^ isNegated()) { 124 break; 125 } 126 } 127 if (line != null) { 128 return read(); 129 } 130 } 131 return ch; 132 } 133 134 140 public void addConfiguredRegexp(final RegularExpression regExp) { 141 this.regexps.addElement(regExp); 142 } 143 144 153 private void setRegexps(final Vector regexps) { 154 this.regexps = regexps; 155 } 156 157 167 private Vector getRegexps() { 168 return regexps; 169 } 170 171 181 public Reader chain(final Reader rdr) { 182 LineContainsRegExp newFilter = new LineContainsRegExp(rdr); 183 newFilter.setRegexps(getRegexps()); 184 newFilter.setNegate(isNegated()); 185 return newFilter; 186 } 187 188 192 public void setNegate(boolean b) { 193 negate = b; 194 } 195 196 200 public boolean isNegated() { 201 return negate; 202 } 203 204 207 private void initialize() { 208 Parameter[] params = getParameters(); 209 if (params != null) { 210 for (int i = 0; i < params.length; i++) { 211 if (REGEXP_KEY.equals(params[i].getType())) { 212 String pattern = params[i].getValue(); 213 RegularExpression regexp = new RegularExpression(); 214 regexp.setPattern(pattern); 215 regexps.addElement(regexp); 216 } else if (NEGATE_KEY.equals(params[i].getType())) { 217 setNegate(Project.toBoolean(params[i].getValue())); 218 } 219 } 220 } 221 } 222 } 223 | Popular Tags |