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 26 48 public final class LineContains 49 extends BaseParamFilterReader 50 implements ChainableReader { 51 52 private static final String CONTAINS_KEY = "contains"; 53 54 55 private static final String NEGATE_KEY = "negate"; 56 57 58 private Vector contains = new Vector (); 59 60 65 private String line = null; 66 67 private boolean negate = false; 68 69 74 public LineContains() { 75 super(); 76 } 77 78 84 public LineContains(final Reader in) { 85 super(in); 86 } 87 88 98 public int read() throws IOException { 99 if (!getInitialized()) { 100 initialize(); 101 setInitialized(true); 102 } 103 104 int ch = -1; 105 106 if (line != null) { 107 ch = line.charAt(0); 108 if (line.length() == 1) { 109 line = null; 110 } else { 111 line = line.substring(1); 112 } 113 } else { 114 final int containsSize = contains.size(); 115 116 for (line = readLine(); line != null; line = readLine()) { 117 boolean matches = true; 118 for (int i = 0; matches && i < containsSize; i++) { 119 String containsStr = (String ) contains.elementAt(i); 120 matches = line.indexOf(containsStr) >= 0; 121 } 122 if (matches ^ isNegated()) { 123 break; 124 } 125 } 126 if (line != null) { 127 return read(); 128 } 129 } 130 return ch; 131 } 132 133 139 public void addConfiguredContains(final Contains contains) { 140 this.contains.addElement(contains.getValue()); 141 } 142 143 147 public void setNegate(boolean b) { 148 negate = b; 149 } 150 151 155 public boolean isNegated() { 156 return negate; 157 } 158 159 166 private void setContains(final Vector contains) { 167 this.contains = contains; 168 } 169 170 179 private Vector getContains() { 180 return contains; 181 } 182 183 193 public Reader chain(final Reader rdr) { 194 LineContains newFilter = new LineContains(rdr); 195 newFilter.setContains(getContains()); 196 newFilter.setNegate(isNegated()); 197 return newFilter; 198 } 199 200 203 private void initialize() { 204 Parameter[] params = getParameters(); 205 if (params != null) { 206 for (int i = 0; i < params.length; i++) { 207 if (CONTAINS_KEY.equals(params[i].getType())) { 208 contains.addElement(params[i].getValue()); 209 } else if (NEGATE_KEY.equals(params[i].getType())) { 210 setNegate(Project.toBoolean(params[i].getValue())); 211 } 212 } 213 } 214 } 215 216 219 public static class Contains { 220 221 222 private String value; 223 224 230 public final void setValue(String contains) { 231 value = contains; 232 } 233 234 239 public final String getValue() { 240 return value; 241 } 242 } 243 } 244 | Popular Tags |