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