KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > ConcatFilter


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;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import org.apache.tools.ant.types.Parameter;
26
27 /**
28  * Concats a file before and/or after the file.
29  *
30  * <p>Example:<pre>
31  * <copy todir="build">
32  * <fileset dir="src" includes="*.java"/>
33  * <filterchain>
34  * <concatfilter prepend="apache-license-java.txt"/>
35  * </filterchain>
36  * </copy>
37  * </pre>
38  * Copies all java sources from <i>src</i> to <i>build</i> and adds the
39  * content of <i>apache-license-java.txt</i> add the beginning of each
40  * file.</p>
41  *
42  * @since 1.6
43  * @version 2003-09-23
44  */

45 public final class ConcatFilter extends BaseParamFilterReader
46     implements ChainableReader {
47
48     /** File to add before the content. */
49     private File JavaDoc prepend;
50
51     /** File to add after the content. */
52     private File JavaDoc append;
53
54     /** Reader for prepend-file. */
55     private Reader JavaDoc prependReader = null;
56
57     /** Reader for append-file. */
58     private Reader JavaDoc appendReader = null;
59
60     /**
61      * Constructor for "dummy" instances.
62      *
63      * @see BaseFilterReader#BaseFilterReader()
64      */

65     public ConcatFilter() {
66         super();
67     }
68
69     /**
70      * Creates a new filtered reader.
71      *
72      * @param in A Reader object providing the underlying stream.
73      * Must not be <code>null</code>.
74      */

75     public ConcatFilter(final Reader JavaDoc in) {
76         super(in);
77     }
78
79     /**
80      * Returns the next character in the filtered stream. If the desired
81      * number of lines have already been read, the resulting stream is
82      * effectively at an end. Otherwise, the next character from the
83      * underlying stream is read and returned.
84      *
85      * @return the next character in the resulting stream, or -1
86      * if the end of the resulting stream has been reached
87      *
88      * @exception IOException if the underlying stream throws an IOException
89      * during reading
90      */

91     public int read() throws IOException JavaDoc {
92         // do the "singleton" initialization
93
if (!getInitialized()) {
94             initialize();
95             setInitialized(true);
96         }
97
98         int ch = -1;
99
100         // The readers return -1 if they end. So simply read the "prepend"
101
// after that the "content" and at the end the "append" file.
102
if (prependReader != null) {
103             ch = prependReader.read();
104             if (ch == -1) {
105                 // I am the only one so I have to close the reader
106
prependReader.close();
107                 prependReader = null;
108             }
109         }
110         if (ch == -1) {
111             ch = super.read();
112         }
113         if (ch == -1) {
114             // don't call super.close() because that reader is used
115
// on other places ...
116
if (appendReader != null) {
117                 ch = appendReader.read();
118                 if (ch == -1) {
119                     // I am the only one so I have to close the reader
120
appendReader.close();
121                     appendReader = null;
122                 }
123             }
124         }
125
126         return ch;
127     }
128
129     /**
130      * Sets <i>prepend</i> attribute.
131      * @param prepend new value
132      */

133     public void setPrepend(final File JavaDoc prepend) {
134         this.prepend = prepend;
135     }
136
137     /**
138      * Returns <i>prepend</i> attribute.
139      * @return prepend attribute
140      */

141     public File JavaDoc getPrepend() {
142         return prepend;
143     }
144
145     /**
146      * Sets <i>append</i> attribute.
147      * @param append new value
148      */

149     public void setAppend(final File JavaDoc append) {
150         this.append = append;
151     }
152
153     /**
154      * Returns <i>append</i> attribute.
155      * @return append attribute
156      */

157     public File JavaDoc getAppend() {
158         return append;
159     }
160
161     /**
162      * Creates a new ConcatReader using the passed in
163      * Reader for instantiation.
164      *
165      * @param rdr A Reader object providing the underlying stream.
166      * Must not be <code>null</code>.
167      *
168      * @return a new filter based on this configuration, but filtering
169      * the specified reader
170      */

171     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
172         ConcatFilter newFilter = new ConcatFilter(rdr);
173         newFilter.setPrepend(getPrepend());
174         newFilter.setAppend(getAppend());
175         // Usually the initialized is set to true. But here it must not.
176
// Because the prepend and append readers have to be instantiated
177
// on runtime
178
//newFilter.setInitialized(true);
179
return newFilter;
180     }
181
182     /**
183      * Scans the parameters list for the "lines" parameter and uses
184      * it to set the number of lines to be returned in the filtered stream.
185      * also scan for skip parameter.
186      */

187     private void initialize() throws IOException JavaDoc {
188         // get parameters
189
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 JavaDoc(params[i].getValue()));
194                     continue;
195                 }
196                 if ("append".equals(params[i].getName())) {
197                     setAppend(new File JavaDoc(params[i].getValue()));
198                     continue;
199                 }
200             }
201         }
202         if (prepend != null) {
203             if (!prepend.isAbsolute()) {
204                 prepend = new File JavaDoc(getProject().getBaseDir(), prepend.getPath());
205             }
206             prependReader = new BufferedReader JavaDoc(new FileReader JavaDoc(prepend));
207         }
208         if (append != null) {
209             if (!append.isAbsolute()) {
210                 append = new File JavaDoc(getProject().getBaseDir(), append.getPath());
211             }
212             appendReader = new BufferedReader JavaDoc(new FileReader JavaDoc(append));
213         }
214    }
215 }
216
Popular Tags