1 18 package org.apache.tools.ant.filters.util; 19 20 import java.io.FilterReader ; 21 import java.io.IOException ; 22 import java.io.Reader ; 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.util.Vector ; 26 import org.apache.tools.ant.AntClassLoader; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.filters.BaseFilterReader; 30 import org.apache.tools.ant.filters.ChainableReader; 31 import org.apache.tools.ant.types.AntFilterReader; 32 import org.apache.tools.ant.types.FilterChain; 33 import org.apache.tools.ant.types.Parameter; 34 import org.apache.tools.ant.types.Parameterizable; 35 import org.apache.tools.ant.types.Path; 36 import org.apache.tools.ant.util.FileUtils; 37 38 42 public final class ChainReaderHelper { 43 44 private static final int DEFAULT_BUFFER_SIZE = 8192; 46 50 public Reader primaryReader; 51 52 55 public int bufferSize = DEFAULT_BUFFER_SIZE; 56 57 60 public Vector filterChains = new Vector (); 61 62 63 private Project project = null; 64 65 67 71 public void setPrimaryReader(Reader rdr) { 72 primaryReader = rdr; 73 } 74 75 79 public void setProject(final Project project) { 80 this.project = project; 81 } 82 83 88 public Project getProject() { 89 return project; 90 } 91 92 97 public void setBufferSize(int size) { 98 bufferSize = size; 99 } 100 101 106 public void setFilterChains(Vector fchain) { 107 filterChains = fchain; 108 } 109 110 115 public Reader getAssembledReader() throws BuildException { 116 if (primaryReader == null) { 117 throw new BuildException("primaryReader must not be null."); 118 } 119 120 Reader instream = primaryReader; 121 final int filterReadersCount = filterChains.size(); 122 final Vector finalFilters = new Vector (); 123 124 for (int i = 0; i < filterReadersCount; i++) { 125 final FilterChain filterchain = 126 (FilterChain) filterChains.elementAt(i); 127 final Vector filterReaders = filterchain.getFilterReaders(); 128 final int readerCount = filterReaders.size(); 129 for (int j = 0; j < readerCount; j++) { 130 finalFilters.addElement(filterReaders.elementAt(j)); 131 } 132 } 133 134 final int filtersCount = finalFilters.size(); 135 136 if (filtersCount > 0) { 137 for (int i = 0; i < filtersCount; i++) { 138 Object o = finalFilters.elementAt(i); 139 140 if (o instanceof AntFilterReader) { 141 final AntFilterReader filter 142 = (AntFilterReader) finalFilters.elementAt(i); 143 final String className = filter.getClassName(); 144 final Path classpath = filter.getClasspath(); 145 final Project pro = filter.getProject(); 146 if (className != null) { 147 try { 148 Class clazz = null; 149 if (classpath == null) { 150 clazz = Class.forName(className); 151 } else { 152 AntClassLoader al 153 = pro.createClassLoader(classpath); 154 clazz = Class.forName(className, true, al); 155 } 156 if (clazz != null) { 157 if (!FilterReader .class.isAssignableFrom(clazz)) { 158 throw new BuildException(className 159 + " does not extend java.io.FilterReader"); 160 } 161 final Constructor [] constructors = 162 clazz.getConstructors(); 163 int j = 0; 164 boolean consPresent = false; 165 for (; j < constructors.length; j++) { 166 Class [] types = constructors[j] 167 .getParameterTypes(); 168 if (types.length == 1 169 && types[0].isAssignableFrom(Reader .class)) { 170 consPresent = true; 171 break; 172 } 173 } 174 if (!consPresent) { 175 throw new BuildException(className 176 + " does not define a public constructor" 177 + " that takes in a Reader as its " 178 + "single argument."); 179 } 180 final Reader [] rdr = {instream}; 181 instream = 182 (Reader ) constructors[j].newInstance((Object []) rdr); 183 setProjectOnObject(instream); 184 if (Parameterizable.class.isAssignableFrom(clazz)) { 185 final Parameter[] params = filter.getParams(); 186 ((Parameterizable) 187 instream).setParameters(params); 188 } 189 } 190 } catch (final ClassNotFoundException cnfe) { 191 throw new BuildException(cnfe); 192 } catch (final InstantiationException ie) { 193 throw new BuildException(ie); 194 } catch (final IllegalAccessException iae) { 195 throw new BuildException(iae); 196 } catch (final InvocationTargetException ite) { 197 throw new BuildException(ite); 198 } 199 } 200 } else if (o instanceof ChainableReader) { 201 setProjectOnObject(o); 202 instream = ((ChainableReader) o).chain(instream); 203 setProjectOnObject(instream); 204 } 205 } 206 } 207 return instream; 208 } 209 210 215 private void setProjectOnObject(Object obj) { 216 if (project == null) { 217 return; 218 } 219 if (obj instanceof BaseFilterReader) { 220 ((BaseFilterReader) obj).setProject(project); 221 return; 222 } 223 project.setProjectReference(obj); 224 } 225 226 233 public String readFully(Reader rdr) 234 throws IOException { 235 return FileUtils.readFully(rdr, bufferSize); 236 } 237 } 238 | Popular Tags |