KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.types.Parameter;
23
24 /**
25  * Filter to flatten the stream to a single line.
26  *
27  * Example:
28  *
29  * <pre>&lt;striplinebreaks/&gt;</pre>
30  *
31  * Or:
32  *
33  * <pre>&lt;filterreader
34  * classname=&quot;org.apache.tools.ant.filters.StripLineBreaks&quot;/&gt;</pre>
35  *
36  */

37 public final class StripLineBreaks
38     extends BaseParamFilterReader
39     implements ChainableReader {
40     /**
41      * Line-breaking characters.
42      * What should we do on funny IBM mainframes with odd line endings?
43      */

44     private static final String JavaDoc DEFAULT_LINE_BREAKS = "\r\n";
45
46     /** Parameter name for the line-breaking characters parameter. */
47     private static final String JavaDoc LINE_BREAKS_KEY = "linebreaks";
48
49     /** The characters that are recognized as line breaks. */
50     private String JavaDoc lineBreaks = DEFAULT_LINE_BREAKS;
51
52     /**
53      * Constructor for "dummy" instances.
54      *
55      * @see BaseFilterReader#BaseFilterReader()
56      */

57     public StripLineBreaks() {
58         super();
59     }
60
61     /**
62      * Creates a new filtered reader.
63      *
64      * @param in A Reader object providing the underlying stream.
65      * Must not be <code>null</code>.
66      */

67     public StripLineBreaks(final Reader JavaDoc in) {
68         super(in);
69     }
70
71     /**
72      * Returns the next character in the filtered stream, only including
73      * characters not in the set of line-breaking characters.
74      *
75      * @return the next character in the resulting stream, or -1
76      * if the end of the resulting stream has been reached
77      *
78      * @exception IOException if the underlying stream throws an IOException
79      * during reading
80      */

81     public int read() throws IOException JavaDoc {
82         if (!getInitialized()) {
83             initialize();
84             setInitialized(true);
85         }
86
87         int ch = in.read();
88         while (ch != -1) {
89             if (lineBreaks.indexOf(ch) == -1) {
90                 break;
91             } else {
92                 ch = in.read();
93             }
94         }
95         return ch;
96     }
97
98     /**
99      * Sets the line-breaking characters.
100      *
101      * @param lineBreaks A String containing all the characters to be
102      * considered as line-breaking.
103      */

104     public void setLineBreaks(final String JavaDoc lineBreaks) {
105         this.lineBreaks = lineBreaks;
106     }
107
108     /**
109      * Returns the line-breaking characters as a String.
110      *
111      * @return a String containing all the characters considered as
112      * line-breaking
113      */

114     private String JavaDoc getLineBreaks() {
115         return lineBreaks;
116     }
117
118     /**
119      * Creates a new StripLineBreaks using the passed in
120      * Reader for instantiation.
121      *
122      * @param rdr A Reader object providing the underlying stream.
123      * Must not be <code>null</code>.
124      *
125      * @return a new filter based on this configuration, but filtering
126      * the specified reader
127      */

128     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
129         StripLineBreaks newFilter = new StripLineBreaks(rdr);
130         newFilter.setLineBreaks(getLineBreaks());
131         newFilter.setInitialized(true);
132         return newFilter;
133     }
134
135     /**
136      * Parses the parameters to set the line-breaking characters.
137      */

138     private void initialize() {
139         String JavaDoc userDefinedLineBreaks = null;
140         Parameter[] params = getParameters();
141         if (params != null) {
142             for (int i = 0; i < params.length; i++) {
143                 if (LINE_BREAKS_KEY.equals(params[i].getName())) {
144                     userDefinedLineBreaks = params[i].getValue();
145                     break;
146                 }
147             }
148         }
149         if (userDefinedLineBreaks != null) {
150             lineBreaks = userDefinedLineBreaks;
151         }
152     }
153 }
154
Popular Tags