KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > util > ChainReaderHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.filters.util;
19
20 import java.io.FilterReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.util.Vector JavaDoc;
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 /**
39  * Process a FilterReader chain.
40  *
41  */

42 public final class ChainReaderHelper {
43
44     // default buffer size
45
private static final int DEFAULT_BUFFER_SIZE = 8192;
46     // CheckStyle:VisibilityModifier OFF - bc
47
/**
48      * The primary reader to which the reader chain is to be attached.
49      */

50     public Reader JavaDoc primaryReader;
51
52     /**
53      * The size of the buffer to be used.
54      */

55     public int bufferSize = DEFAULT_BUFFER_SIZE;
56
57     /**
58      * Chain of filters
59      */

60     public Vector JavaDoc filterChains = new Vector JavaDoc();
61
62     /** The Ant project */
63     private Project project = null;
64
65     // CheckStyle:VisibilityModifier ON
66

67     /**
68      * Sets the primary reader
69      * @param rdr the reader object
70      */

71     public void setPrimaryReader(Reader JavaDoc rdr) {
72         primaryReader = rdr;
73     }
74
75     /**
76      * Set the project to work with
77      * @param project the current project
78      */

79     public void setProject(final Project project) {
80         this.project = project;
81     }
82
83     /**
84      * Get the project
85      *
86      * @return the current project
87      */

88     public Project getProject() {
89         return project;
90     }
91
92     /**
93      * Sets the buffer size to be used. Defaults to 8192,
94      * if this method is not invoked.
95      * @param size the buffer size to use
96      */

97     public void setBufferSize(int size) {
98         bufferSize = size;
99     }
100
101     /**
102      * Sets the collection of filter reader sets
103      *
104      * @param fchain the filter chains collection
105      */

106     public void setFilterChains(Vector JavaDoc fchain) {
107         filterChains = fchain;
108     }
109
110     /**
111      * Assemble the reader
112      * @return the assembled reader
113      * @exception BuildException if an error occurs
114      */

115     public Reader JavaDoc getAssembledReader() throws BuildException {
116         if (primaryReader == null) {
117             throw new BuildException("primaryReader must not be null.");
118         }
119
120         Reader JavaDoc instream = primaryReader;
121         final int filterReadersCount = filterChains.size();
122         final Vector JavaDoc finalFilters = new Vector JavaDoc();
123
124         for (int i = 0; i < filterReadersCount; i++) {
125             final FilterChain filterchain =
126                 (FilterChain) filterChains.elementAt(i);
127             final Vector JavaDoc 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 JavaDoc o = finalFilters.elementAt(i);
139
140                 if (o instanceof AntFilterReader) {
141                     final AntFilterReader filter
142                         = (AntFilterReader) finalFilters.elementAt(i);
143                     final String JavaDoc className = filter.getClassName();
144                     final Path classpath = filter.getClasspath();
145                     final Project pro = filter.getProject();
146                     if (className != null) {
147                         try {
148                             Class JavaDoc 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 JavaDoc.class.isAssignableFrom(clazz)) {
158                                     throw new BuildException(className
159                                         + " does not extend java.io.FilterReader");
160                                 }
161                                 final Constructor JavaDoc[] constructors =
162                                     clazz.getConstructors();
163                                 int j = 0;
164                                 boolean consPresent = false;
165                                 for (; j < constructors.length; j++) {
166                                     Class JavaDoc[] types = constructors[j]
167                                                       .getParameterTypes();
168                                     if (types.length == 1
169                                         && types[0].isAssignableFrom(Reader JavaDoc.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 JavaDoc[] rdr = {instream};
181                                 instream =
182                                     (Reader JavaDoc) constructors[j].newInstance((Object JavaDoc[]) 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 JavaDoc cnfe) {
191                             throw new BuildException(cnfe);
192                         } catch (final InstantiationException JavaDoc ie) {
193                             throw new BuildException(ie);
194                         } catch (final IllegalAccessException JavaDoc iae) {
195                             throw new BuildException(iae);
196                         } catch (final InvocationTargetException JavaDoc 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     /**
211      * helper method to set the project on an object.
212      * the reflection setProject does not work for anonymous/protected/private
213      * classes, even if they have public methods.
214      */

215     private void setProjectOnObject(Object JavaDoc 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     /**
227      * Read data from the reader and return the
228      * contents as a string.
229      * @param rdr the reader object
230      * @return the contents of the file as a string
231      * @exception IOException if an error occurs
232      */

233     public String JavaDoc readFully(Reader JavaDoc rdr)
234         throws IOException JavaDoc {
235         return FileUtils.readFully(rdr, bufferSize);
236     }
237 }
238
Popular Tags