1 18 package org.apache.tools.ant.filters; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.io.File ; 23 import java.io.BufferedReader ; 24 import java.io.FileReader ; 25 import org.apache.tools.ant.types.Parameter; 26 27 45 public final class ConcatFilter extends BaseParamFilterReader 46 implements ChainableReader { 47 48 49 private File prepend; 50 51 52 private File append; 53 54 55 private Reader prependReader = null; 56 57 58 private Reader appendReader = null; 59 60 65 public ConcatFilter() { 66 super(); 67 } 68 69 75 public ConcatFilter(final Reader in) { 76 super(in); 77 } 78 79 91 public int read() throws IOException { 92 if (!getInitialized()) { 94 initialize(); 95 setInitialized(true); 96 } 97 98 int ch = -1; 99 100 if (prependReader != null) { 103 ch = prependReader.read(); 104 if (ch == -1) { 105 prependReader.close(); 107 prependReader = null; 108 } 109 } 110 if (ch == -1) { 111 ch = super.read(); 112 } 113 if (ch == -1) { 114 if (appendReader != null) { 117 ch = appendReader.read(); 118 if (ch == -1) { 119 appendReader.close(); 121 appendReader = null; 122 } 123 } 124 } 125 126 return ch; 127 } 128 129 133 public void setPrepend(final File prepend) { 134 this.prepend = prepend; 135 } 136 137 141 public File getPrepend() { 142 return prepend; 143 } 144 145 149 public void setAppend(final File append) { 150 this.append = append; 151 } 152 153 157 public File getAppend() { 158 return append; 159 } 160 161 171 public Reader chain(final Reader rdr) { 172 ConcatFilter newFilter = new ConcatFilter(rdr); 173 newFilter.setPrepend(getPrepend()); 174 newFilter.setAppend(getAppend()); 175 return newFilter; 180 } 181 182 187 private void initialize() throws IOException { 188 Parameter[] params = getParameters(); 190 if (params != null) { 191 for (int i = 0; i < params.length; i++) { 192 if ("prepend".equals(params[i].getName())) { 193 setPrepend(new File (params[i].getValue())); 194 continue; 195 } 196 if ("append".equals(params[i].getName())) { 197 setAppend(new File (params[i].getValue())); 198 continue; 199 } 200 } 201 } 202 if (prepend != null) { 203 if (!prepend.isAbsolute()) { 204 prepend = new File (getProject().getBaseDir(), prepend.getPath()); 205 } 206 prependReader = new BufferedReader (new FileReader (prepend)); 207 } 208 if (append != null) { 209 if (!append.isAbsolute()) { 210 append = new File (getProject().getBaseDir(), append.getPath()); 211 } 212 appendReader = new BufferedReader (new FileReader (append)); 213 } 214 } 215 } 216 | Popular Tags |