001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.filters;
019:
020: import java.io.IOException;
021: import java.io.Reader;
022: import org.apache.tools.ant.types.Parameter;
023:
024: /**
025: * Filter to flatten the stream to a single line.
026: *
027: * Example:
028: *
029: * <pre><striplinebreaks/></pre>
030: *
031: * Or:
032: *
033: * <pre><filterreader
034: * classname="org.apache.tools.ant.filters.StripLineBreaks"/></pre>
035: *
036: */
037: public final class StripLineBreaks extends BaseParamFilterReader
038: implements ChainableReader {
039: /**
040: * Line-breaking characters.
041: * What should we do on funny IBM mainframes with odd line endings?
042: */
043: private static final String DEFAULT_LINE_BREAKS = "\r\n";
044:
045: /** Parameter name for the line-breaking characters parameter. */
046: private static final String LINE_BREAKS_KEY = "linebreaks";
047:
048: /** The characters that are recognized as line breaks. */
049: private String lineBreaks = DEFAULT_LINE_BREAKS;
050:
051: /**
052: * Constructor for "dummy" instances.
053: *
054: * @see BaseFilterReader#BaseFilterReader()
055: */
056: public StripLineBreaks() {
057: super ();
058: }
059:
060: /**
061: * Creates a new filtered reader.
062: *
063: * @param in A Reader object providing the underlying stream.
064: * Must not be <code>null</code>.
065: */
066: public StripLineBreaks(final Reader in) {
067: super (in);
068: }
069:
070: /**
071: * Returns the next character in the filtered stream, only including
072: * characters not in the set of line-breaking characters.
073: *
074: * @return the next character in the resulting stream, or -1
075: * if the end of the resulting stream has been reached
076: *
077: * @exception IOException if the underlying stream throws an IOException
078: * during reading
079: */
080: public int read() throws IOException {
081: if (!getInitialized()) {
082: initialize();
083: setInitialized(true);
084: }
085:
086: int ch = in.read();
087: while (ch != -1) {
088: if (lineBreaks.indexOf(ch) == -1) {
089: break;
090: } else {
091: ch = in.read();
092: }
093: }
094: return ch;
095: }
096:
097: /**
098: * Sets the line-breaking characters.
099: *
100: * @param lineBreaks A String containing all the characters to be
101: * considered as line-breaking.
102: */
103: public 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 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 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 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: }
|