source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/filters/StripLineBreaks.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 4.5 KB
Line 
1/*
2 * Copyright 2002,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 */
17package org.apache.tools.ant.filters;
18
19import java.io.IOException;
20import java.io.Reader;
21import org.apache.tools.ant.types.Parameter;
22
23/**
24 * Filter to flatten the stream to a single line.
25 *
26 * Example:
27 *
28 * <pre>&lt;striplinebreaks/&gt;</pre>
29 *
30 * Or:
31 *
32 * <pre>&lt;filterreader
33 * classname=&quot;org.apache.tools.ant.filters.StripLineBreaks&quot;/&gt;</pre>
34 *
35 */
36public final class StripLineBreaks
37 extends BaseParamFilterReader
38 implements ChainableReader {
39 /**
40 * Line-breaking characters.
41 * What should we do on funny IBM mainframes with odd line endings?
42 */
43 private static final String DEFAULT_LINE_BREAKS = "\r\n";
44
45 /** Parameter name for the line-breaking characters parameter. */
46 private static final String LINE_BREAKS_KEY = "linebreaks";
47
48 /** The characters that are recognized as line breaks. */
49 private String lineBreaks = DEFAULT_LINE_BREAKS;
50
51 /**
52 * Constructor for "dummy" instances.
53 *
54 * @see BaseFilterReader#BaseFilterReader()
55 */
56 public StripLineBreaks() {
57 super();
58 }
59
60 /**
61 * Creates a new filtered reader.
62 *
63 * @param in A Reader object providing the underlying stream.
64 * Must not be <code>null</code>.
65 */
66 public StripLineBreaks(final Reader in) {
67 super(in);
68 }
69
70 /**
71 * Returns the next character in the filtered stream, only including
72 * characters not in the set of line-breaking characters.
73 *
74 * @return the next character in the resulting stream, or -1
75 * if the end of the resulting stream has been reached
76 *
77 * @exception IOException if the underlying stream throws an IOException
78 * during reading
79 */
80 public final int read() throws IOException {
81 if (!getInitialized()) {
82 initialize();
83 setInitialized(true);
84 }
85
86 int ch = in.read();
87 while (ch != -1) {
88 if (lineBreaks.indexOf(ch) == -1) {
89 break;
90 } else {
91 ch = in.read();
92 }
93 }
94 return ch;
95 }
96
97 /**
98 * Sets the line-breaking characters.
99 *
100 * @param lineBreaks A String containing all the characters to be
101 * considered as line-breaking.
102 */
103 public final void setLineBreaks(final String lineBreaks) {
104 this.lineBreaks = lineBreaks;
105 }
106
107 /**
108 * Returns the line-breaking characters as a String.
109 *
110 * @return a String containing all the characters considered as
111 * line-breaking
112 */
113 private final String getLineBreaks() {
114 return lineBreaks;
115 }
116
117 /**
118 * Creates a new StripLineBreaks using the passed in
119 * Reader for instantiation.
120 *
121 * @param rdr A Reader object providing the underlying stream.
122 * Must not be <code>null</code>.
123 *
124 * @return a new filter based on this configuration, but filtering
125 * the specified reader
126 */
127 public final Reader chain(final Reader rdr) {
128 StripLineBreaks newFilter = new StripLineBreaks(rdr);
129 newFilter.setLineBreaks(getLineBreaks());
130 newFilter.setInitialized(true);
131 return newFilter;
132 }
133
134 /**
135 * Parses the parameters to set the line-breaking characters.
136 */
137 private final void initialize() {
138 String userDefinedLineBreaks = null;
139 Parameter[] params = getParameters();
140 if (params != null) {
141 for (int i = 0; i < params.length; i++) {
142 if (LINE_BREAKS_KEY.equals(params[i].getName())) {
143 userDefinedLineBreaks = params[i].getValue();
144 break;
145 }
146 }
147 }
148 if (userDefinedLineBreaks != null) {
149 lineBreaks = userDefinedLineBreaks;
150 }
151 }
152}
Note: See TracBrowser for help on using the repository browser.