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: * Converts tabs to spaces.
026: *
027: * Example:
028: *
029: * <pre><tabtospaces tablength="8"/></pre>
030: *
031: * Or:
032: *
033: * <pre><filterreader classname="org.apache.tools.ant.filters.TabsToSpaces">
034: * <param name="tablength" value="8"/>
035: * </filterreader></pre>
036: *
037: */
038: public final class TabsToSpaces extends BaseParamFilterReader implements
039: ChainableReader {
040: /** The default tab length. */
041: private static final int DEFAULT_TAB_LENGTH = 8;
042:
043: /** Parameter name for the length of a tab. */
044: private static final String TAB_LENGTH_KEY = "tablength";
045:
046: /** Tab length in this filter. */
047: private int tabLength = DEFAULT_TAB_LENGTH;
048:
049: /** The number of spaces still to be read to represent the last-read tab. */
050: private int spacesRemaining = 0;
051:
052: /**
053: * Constructor for "dummy" instances.
054: *
055: * @see BaseFilterReader#BaseFilterReader()
056: */
057: public TabsToSpaces() {
058: super ();
059: }
060:
061: /**
062: * Creates a new filtered reader.
063: *
064: * @param in A Reader object providing the underlying stream.
065: * Must not be <code>null</code>.
066: */
067: public TabsToSpaces(final Reader in) {
068: super (in);
069: }
070:
071: /**
072: * Returns the next character in the filtered stream, converting tabs
073: * to the specified number of spaces.
074: *
075: * @return the next character in the resulting stream, or -1
076: * if the end of the resulting stream has been reached
077: *
078: * @exception IOException if the underlying stream throws an IOException
079: * during reading
080: */
081: public int read() throws IOException {
082: if (!getInitialized()) {
083: initialize();
084: setInitialized(true);
085: }
086:
087: int ch = -1;
088:
089: if (spacesRemaining > 0) {
090: spacesRemaining--;
091: ch = ' ';
092: } else {
093: ch = in.read();
094: if (ch == '\t') {
095: spacesRemaining = tabLength - 1;
096: ch = ' ';
097: }
098: }
099: return ch;
100: }
101:
102: /**
103: * Sets the tab length.
104: *
105: * @param tabLength the number of spaces to be used when converting a tab.
106: */
107: public void setTablength(final int tabLength) {
108: this .tabLength = tabLength;
109: }
110:
111: /**
112: * Returns the tab length.
113: *
114: * @return the number of spaces used when converting a tab
115: */
116: private int getTablength() {
117: return tabLength;
118: }
119:
120: /**
121: * Creates a new TabsToSpaces using the passed in
122: * Reader for instantiation.
123: *
124: * @param rdr A Reader object providing the underlying stream.
125: * Must not be <code>null</code>.
126: *
127: * @return a new filter based on this configuration, but filtering
128: * the specified reader
129: */
130: public Reader chain(final Reader rdr) {
131: TabsToSpaces newFilter = new TabsToSpaces(rdr);
132: newFilter.setTablength(getTablength());
133: newFilter.setInitialized(true);
134: return newFilter;
135: }
136:
137: /**
138: * Parses the parameters to set the tab length.
139: */
140: private void initialize() {
141: Parameter[] params = getParameters();
142: if (params != null) {
143: for (int i = 0; i < params.length; i++) {
144: if (params[i] != null) {
145: if (TAB_LENGTH_KEY.equals(params[i].getName())) {
146: tabLength = new Integer(params[i].getValue())
147: .intValue();
148: break;
149: }
150: }
151: }
152: }
153: }
154: }
|