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.types.Parameter; 24 25 50 public final class StripLineComments 51 extends BaseParamFilterReader 52 implements ChainableReader { 53 54 private static final String COMMENTS_KEY = "comment"; 55 56 57 private Vector comments = new Vector (); 58 59 60 private String line = null; 61 62 67 public StripLineComments() { 68 super(); 69 } 70 71 77 public StripLineComments(final Reader in) { 78 super(in); 79 } 80 81 92 public int read() throws IOException { 93 if (!getInitialized()) { 94 initialize(); 95 setInitialized(true); 96 } 97 98 int ch = -1; 99 100 if (line != null) { 101 ch = line.charAt(0); 102 if (line.length() == 1) { 103 line = null; 104 } else { 105 line = line.substring(1); 106 } 107 } else { 108 line = readLine(); 109 final int commentsSize = comments.size(); 110 111 while (line != null) { 112 for (int i = 0; i < commentsSize; i++) { 113 String comment = (String ) comments.elementAt(i); 114 if (line.startsWith(comment)) { 115 line = null; 116 break; 117 } 118 } 119 120 if (line == null) { 121 line = readLine(); 123 } else { 124 break; 125 } 126 } 127 128 if (line != null) { 129 return read(); 130 } 131 } 132 133 return ch; 134 } 135 136 142 public void addConfiguredComment(final Comment comment) { 143 comments.addElement(comment.getValue()); 144 } 145 146 152 private void setComments(final Vector comments) { 153 this.comments = comments; 154 } 155 156 161 private Vector getComments() { 162 return comments; 163 } 164 165 175 public Reader chain(final Reader rdr) { 176 StripLineComments newFilter = new StripLineComments(rdr); 177 newFilter.setComments(getComments()); 178 newFilter.setInitialized(true); 179 return newFilter; 180 } 181 182 185 private void initialize() { 186 Parameter[] params = getParameters(); 187 if (params != null) { 188 for (int i = 0; i < params.length; i++) { 189 if (COMMENTS_KEY.equals(params[i].getType())) { 190 comments.addElement(params[i].getValue()); 191 } 192 } 193 } 194 } 195 196 199 public static class Comment { 200 201 202 private String value; 203 204 210 public final void setValue(String comment) { 211 value = comment; 212 } 213 214 219 public final String getValue() { 220 return value; 221 } 222 } 223 } 224 | Popular Tags |