1 18 package org.apache.tools.ant.filters; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import org.apache.tools.ant.types.Parameter; 23 24 37 public final class PrefixLines 38 extends BaseParamFilterReader 39 implements ChainableReader { 40 41 private static final String PREFIX_KEY = "prefix"; 42 43 44 private String prefix = null; 45 46 47 private String queuedData = null; 48 49 54 public PrefixLines() { 55 super(); 56 } 57 58 64 public PrefixLines(final Reader in) { 65 super(in); 66 } 67 68 80 public int read() throws IOException { 81 if (!getInitialized()) { 82 initialize(); 83 setInitialized(true); 84 } 85 86 int ch = -1; 87 88 if (queuedData != null && queuedData.length() == 0) { 89 queuedData = null; 90 } 91 92 if (queuedData != null) { 93 ch = queuedData.charAt(0); 94 queuedData = queuedData.substring(1); 95 if (queuedData.length() == 0) { 96 queuedData = null; 97 } 98 } else { 99 queuedData = readLine(); 100 if (queuedData == null) { 101 ch = -1; 102 } else { 103 if (prefix != null) { 104 queuedData = prefix + queuedData; 105 } 106 return read(); 107 } 108 } 109 return ch; 110 } 111 112 119 public void setPrefix(final String prefix) { 120 this.prefix = prefix; 121 } 122 123 128 private String getPrefix() { 129 return prefix; 130 } 131 132 142 public Reader chain(final Reader rdr) { 143 PrefixLines newFilter = new PrefixLines(rdr); 144 newFilter.setPrefix(getPrefix()); 145 newFilter.setInitialized(true); 146 return newFilter; 147 } 148 149 152 private void initialize() { 153 Parameter[] params = getParameters(); 154 if (params != null) { 155 for (int i = 0; i < params.length; i++) { 156 if (PREFIX_KEY.equals(params[i].getName())) { 157 prefix = params[i].getValue(); 158 break; 159 } 160 } 161 } 162 } 163 } 164 | Popular Tags |