1 | /*
|
---|
2 | * Copyright 2003-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.filters;
|
---|
18 |
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.Reader;
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.BufferedReader;
|
---|
23 | import java.io.FileReader;
|
---|
24 | import org.apache.tools.ant.types.Parameter;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Concats a file before and/or after the file.
|
---|
28 | *
|
---|
29 | * <p>Example:<pre>
|
---|
30 | * <copy todir="build">
|
---|
31 | * <fileset dir="src" includes="*.java"/>
|
---|
32 | * <filterchain>
|
---|
33 | * <concatfilter prepend="apache-license-java.txt"/>
|
---|
34 | * </filterchain>
|
---|
35 | * </copy>
|
---|
36 | * </pre>
|
---|
37 | * Copies all java sources from <i>src</i> to <i>build</i> and adds the
|
---|
38 | * content of <i>apache-license-java.txt</i> add the beginning of each
|
---|
39 | * file.</p>
|
---|
40 | *
|
---|
41 | * @since 1.6
|
---|
42 | * @version 2003-09-23
|
---|
43 | */
|
---|
44 | public final class ConcatFilter extends BaseParamFilterReader
|
---|
45 | implements ChainableReader {
|
---|
46 |
|
---|
47 | /** File to add before the content. */
|
---|
48 | private File prepend;
|
---|
49 |
|
---|
50 | /** File to add after the content. */
|
---|
51 | private File append;
|
---|
52 |
|
---|
53 | /** Reader for prepend-file. */
|
---|
54 | private Reader prependReader = null;
|
---|
55 |
|
---|
56 | /** Reader for append-file. */
|
---|
57 | private Reader appendReader = null;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Constructor for "dummy" instances.
|
---|
61 | *
|
---|
62 | * @see BaseFilterReader#BaseFilterReader()
|
---|
63 | */
|
---|
64 | public ConcatFilter() {
|
---|
65 | super();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Creates a new filtered reader.
|
---|
70 | *
|
---|
71 | * @param in A Reader object providing the underlying stream.
|
---|
72 | * Must not be <code>null</code>.
|
---|
73 | */
|
---|
74 | public ConcatFilter(final Reader in) {
|
---|
75 | super(in);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Returns the next character in the filtered stream. If the desired
|
---|
80 | * number of lines have already been read, the resulting stream is
|
---|
81 | * effectively at an end. Otherwise, the next character from the
|
---|
82 | * underlying stream is read and returned.
|
---|
83 | *
|
---|
84 | * @return the next character in the resulting stream, or -1
|
---|
85 | * if the end of the resulting stream has been reached
|
---|
86 | *
|
---|
87 | * @exception IOException if the underlying stream throws an IOException
|
---|
88 | * during reading
|
---|
89 | */
|
---|
90 | public int read() throws IOException {
|
---|
91 | // do the "singleton" initialization
|
---|
92 | if (!getInitialized()) {
|
---|
93 | initialize();
|
---|
94 | setInitialized(true);
|
---|
95 | }
|
---|
96 |
|
---|
97 | int ch = -1;
|
---|
98 |
|
---|
99 | // The readers return -1 if they end. So simply read the "prepend"
|
---|
100 | // after that the "content" and at the end the "append" file.
|
---|
101 | if (prependReader != null) {
|
---|
102 | ch = prependReader.read();
|
---|
103 | if (ch == -1) {
|
---|
104 | // I am the only one so I have to close the reader
|
---|
105 | prependReader.close();
|
---|
106 | prependReader = null;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | if (ch == -1) {
|
---|
110 | ch = super.read();
|
---|
111 | }
|
---|
112 | if (ch == -1) {
|
---|
113 | // don't call super.close() because that reader is used
|
---|
114 | // on other places ...
|
---|
115 | if (appendReader != null) {
|
---|
116 | ch = appendReader.read();
|
---|
117 | if (ch == -1) {
|
---|
118 | // I am the only one so I have to close the reader
|
---|
119 | appendReader.close();
|
---|
120 | appendReader = null;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return ch;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Sets <i>prepend</i> attribute.
|
---|
130 | * @param prepend new value
|
---|
131 | */
|
---|
132 | public void setPrepend(final File prepend) {
|
---|
133 | this.prepend = prepend;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Returns <i>prepend</i> attribute.
|
---|
138 | * @return prepend attribute
|
---|
139 | */
|
---|
140 | public File getPrepend() {
|
---|
141 | return prepend;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Sets <i>append</i> attribute.
|
---|
146 | * @param append new value
|
---|
147 | */
|
---|
148 | public void setAppend(final File append) {
|
---|
149 | this.append = append;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns <i>append</i> attribute.
|
---|
154 | * @return append attribute
|
---|
155 | */
|
---|
156 | public File getAppend() {
|
---|
157 | return append;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Creates a new ConcatReader using the passed in
|
---|
162 | * Reader for instantiation.
|
---|
163 | *
|
---|
164 | * @param rdr A Reader object providing the underlying stream.
|
---|
165 | * Must not be <code>null</code>.
|
---|
166 | *
|
---|
167 | * @return a new filter based on this configuration, but filtering
|
---|
168 | * the specified reader
|
---|
169 | */
|
---|
170 | public Reader chain(final Reader rdr) {
|
---|
171 | ConcatFilter newFilter = new ConcatFilter(rdr);
|
---|
172 | newFilter.setPrepend(getPrepend());
|
---|
173 | newFilter.setAppend(getAppend());
|
---|
174 | // Usually the initialized is set to true. But here it must not.
|
---|
175 | // Because the prepend and append readers have to be instantiated
|
---|
176 | // on runtime
|
---|
177 | //newFilter.setInitialized(true);
|
---|
178 | return newFilter;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Scans the parameters list for the "lines" parameter and uses
|
---|
183 | * it to set the number of lines to be returned in the filtered stream.
|
---|
184 | * also scan for skip parameter.
|
---|
185 | */
|
---|
186 | private void initialize() throws IOException {
|
---|
187 | // get parameters
|
---|
188 | Parameter[] params = getParameters();
|
---|
189 | if (params != null) {
|
---|
190 | for (int i = 0; i < params.length; i++) {
|
---|
191 | if ("prepend".equals(params[i].getName())) {
|
---|
192 | setPrepend(new File(params[i].getValue()));
|
---|
193 | continue;
|
---|
194 | }
|
---|
195 | if ("append".equals(params[i].getName())) {
|
---|
196 | setAppend(new File(params[i].getValue()));
|
---|
197 | continue;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | if (prepend != null) {
|
---|
202 | if (!prepend.isAbsolute()) {
|
---|
203 | prepend = new File(getProject().getBaseDir(), prepend.getPath());
|
---|
204 | }
|
---|
205 | prependReader = new BufferedReader(new FileReader(prepend));
|
---|
206 | }
|
---|
207 | if (append != null) {
|
---|
208 | if (!append.isAbsolute()) {
|
---|
209 | append = new File(getProject().getBaseDir(), append.getPath());
|
---|
210 | }
|
---|
211 | appendReader = new BufferedReader(new FileReader(append));
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|