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. * Attaches a prefix to every line.
  23. *
  24. * Example:
  25. * <pre><prefixlines prefix="Foo"/></pre>
  26. *
  27. * Or:
  28. *
  29. * <pre><filterreader classname="org.apache.tools.ant.filters.PrefixLines">
  30. * <param name="prefix" value="Foo"/>
  31. * </filterreader></pre>
  32. *
  33. */
  34. public final class PrefixLines
  35. extends BaseParamFilterReader
  36. implements ChainableReader {
  37. /** Parameter name for the prefix. */
  38. private static final String PREFIX_KEY = "prefix";
  39. /** The prefix to be used. */
  40. private String prefix = null;
  41. /** Data that must be read from, if not null. */
  42. private String queuedData = null;
  43. /**
  44. * Constructor for "dummy" instances.
  45. *
  46. * @see BaseFilterReader#BaseFilterReader()
  47. */
  48. public PrefixLines() {
  49. super();
  50. }
  51. /**
  52. * Creates a new filtered reader.
  53. *
  54. * @param in A Reader object providing the underlying stream.
  55. * Must not be <code>null</code>.
  56. */
  57. public PrefixLines(final Reader in) {
  58. super(in);
  59. }
  60. /**
  61. * Returns the next character in the filtered stream. One line is read
  62. * from the original input, and the prefix added. The resulting
  63. * line is then used until it ends, at which point the next original line
  64. * is read, etc.
  65. *
  66. * @return the next character in the resulting stream, or -1
  67. * if the end of the resulting stream has been reached
  68. *
  69. * @exception IOException if the underlying stream throws an IOException
  70. * during reading
  71. */
  72. public final int read() throws IOException {
  73. if (!getInitialized()) {
  74. initialize();
  75. setInitialized(true);
  76. }
  77. int ch = -1;
  78. if (queuedData != null && queuedData.length() == 0) {
  79. queuedData = null;
  80. }
  81. if (queuedData != null) {
  82. ch = queuedData.charAt(0);
  83. queuedData = queuedData.substring(1);
  84. if (queuedData.length() == 0) {
  85. queuedData = null;
  86. }
  87. } else {
  88. queuedData = readLine();
  89. if (queuedData == null) {
  90. ch = -1;
  91. } else {
  92. if (prefix != null) {
  93. queuedData = prefix + queuedData;
  94. }
  95. return read();
  96. }
  97. }
  98. return ch;
  99. }
  100. /**
  101. * Sets the prefix to add at the start of each input line.
  102. *
  103. * @param prefix The prefix to add at the start of each input line.
  104. * May be <code>null</code>, in which case no prefix
  105. * is added.
  106. */
  107. public final void setPrefix(final String prefix) {
  108. this.prefix = prefix;
  109. }
  110. /**
  111. * Returns the prefix which will be added at the start of each input line.
  112. *
  113. * @return the prefix which will be added at the start of each input line
  114. */
  115. private final String getPrefix() {
  116. return prefix;
  117. }
  118. /**
  119. * Creates a new PrefixLines filter 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 final Reader chain(final Reader rdr) {
  129. PrefixLines newFilter = new PrefixLines(rdr);
  130. newFilter.setPrefix(getPrefix());
  131. newFilter.setInitialized(true);
  132. return newFilter;
  133. }
  134. /**
  135. * Initializes the prefix if it is available from the parameters.
  136. */
  137. private final void initialize() {
  138. Parameter[] params = getParameters();
  139. if (params != null) {
  140. for (int i = 0; i < params.length; i++) {
  141. if (PREFIX_KEY.equals(params[i].getName())) {
  142. prefix = params[i].getValue();
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. }