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. * Converts tabs to spaces.
  23. *
  24. * Example:
  25. *
  26. * <pre><tabtospaces tablength="8"/></pre>
  27. *
  28. * Or:
  29. *
  30. * <pre><filterreader classname="org.apache.tools.ant.filters.TabsToSpaces">
  31. * <param name="tablength" value="8"/>
  32. * </filterreader></pre>
  33. *
  34. */
  35. public final class TabsToSpaces
  36. extends BaseParamFilterReader
  37. implements ChainableReader {
  38. /** The default tab length. */
  39. private static final int DEFAULT_TAB_LENGTH = 8;
  40. /** Parameter name for the length of a tab. */
  41. private static final String TAB_LENGTH_KEY = "tablength";
  42. /** Tab length in this filter. */
  43. private int tabLength = DEFAULT_TAB_LENGTH;
  44. /** The number of spaces still to be read to represent the last-read tab. */
  45. private int spacesRemaining = 0;
  46. /**
  47. * Constructor for "dummy" instances.
  48. *
  49. * @see BaseFilterReader#BaseFilterReader()
  50. */
  51. public TabsToSpaces() {
  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 TabsToSpaces(final Reader in) {
  61. super(in);
  62. }
  63. /**
  64. * Returns the next character in the filtered stream, converting tabs
  65. * to the specified number of spaces.
  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 = -1;
  79. if (spacesRemaining > 0) {
  80. spacesRemaining--;
  81. ch = ' ';
  82. } else {
  83. ch = in.read();
  84. if (ch == '\t') {
  85. spacesRemaining = tabLength - 1;
  86. ch = ' ';
  87. }
  88. }
  89. return ch;
  90. }
  91. /**
  92. * Sets the tab length.
  93. *
  94. * @param tabLength the number of spaces to be used when converting a tab.
  95. */
  96. public final void setTablength(final int tabLength) {
  97. this.tabLength = tabLength;
  98. }
  99. /**
  100. * Returns the tab length.
  101. *
  102. * @return the number of spaces used when converting a tab
  103. */
  104. private final int getTablength() {
  105. return tabLength;
  106. }
  107. /**
  108. * Creates a new TabsToSpaces using the passed in
  109. * Reader for instantiation.
  110. *
  111. * @param rdr A Reader object providing the underlying stream.
  112. * Must not be <code>null</code>.
  113. *
  114. * @return a new filter based on this configuration, but filtering
  115. * the specified reader
  116. */
  117. public final Reader chain(final Reader rdr) {
  118. TabsToSpaces newFilter = new TabsToSpaces(rdr);
  119. newFilter.setTablength(getTablength());
  120. newFilter.setInitialized(true);
  121. return newFilter;
  122. }
  123. /**
  124. * Parses the parameters to set the tab length.
  125. */
  126. private final void initialize() {
  127. Parameter[] params = getParameters();
  128. if (params != null) {
  129. for (int i = 0; i < params.length; i++) {
  130. if (params[i] != null) {
  131. if (TAB_LENGTH_KEY.equals(params[i].getName())) {
  132. tabLength =
  133. new Integer(params[i].getValue()).intValue();
  134. break;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }