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 |
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.Reader;
|
---|
21 | import org.apache.tools.ant.types.Parameter;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Converts tabs to spaces.
|
---|
25 | *
|
---|
26 | * Example:
|
---|
27 | *
|
---|
28 | * <pre><tabtospaces tablength="8"/></pre>
|
---|
29 | *
|
---|
30 | * Or:
|
---|
31 | *
|
---|
32 | * <pre><filterreader classname="org.apache.tools.ant.filters.TabsToSpaces">
|
---|
33 | * <param name="tablength" value="8"/>
|
---|
34 | * </filterreader></pre>
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public final class TabsToSpaces
|
---|
38 | extends BaseParamFilterReader
|
---|
39 | implements ChainableReader {
|
---|
40 | /** The default tab length. */
|
---|
41 | private static final int DEFAULT_TAB_LENGTH = 8;
|
---|
42 |
|
---|
43 | /** Parameter name for the length of a tab. */
|
---|
44 | private static final String TAB_LENGTH_KEY = "tablength";
|
---|
45 |
|
---|
46 | /** Tab length in this filter. */
|
---|
47 | private int tabLength = DEFAULT_TAB_LENGTH;
|
---|
48 |
|
---|
49 | /** The number of spaces still to be read to represent the last-read tab. */
|
---|
50 | private int spacesRemaining = 0;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Constructor for "dummy" instances.
|
---|
54 | *
|
---|
55 | * @see BaseFilterReader#BaseFilterReader()
|
---|
56 | */
|
---|
57 | public TabsToSpaces() {
|
---|
58 | super();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Creates a new filtered reader.
|
---|
63 | *
|
---|
64 | * @param in A Reader object providing the underlying stream.
|
---|
65 | * Must not be <code>null</code>.
|
---|
66 | */
|
---|
67 | public TabsToSpaces(final Reader in) {
|
---|
68 | super(in);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Returns the next character in the filtered stream, converting tabs
|
---|
73 | * to the specified number of spaces.
|
---|
74 | *
|
---|
75 | * @return the next character in the resulting stream, or -1
|
---|
76 | * if the end of the resulting stream has been reached
|
---|
77 | *
|
---|
78 | * @exception IOException if the underlying stream throws an IOException
|
---|
79 | * during reading
|
---|
80 | */
|
---|
81 | public final int read() throws IOException {
|
---|
82 | if (!getInitialized()) {
|
---|
83 | initialize();
|
---|
84 | setInitialized(true);
|
---|
85 | }
|
---|
86 |
|
---|
87 | int ch = -1;
|
---|
88 |
|
---|
89 | if (spacesRemaining > 0) {
|
---|
90 | spacesRemaining--;
|
---|
91 | ch = ' ';
|
---|
92 | } else {
|
---|
93 | ch = in.read();
|
---|
94 | if (ch == '\t') {
|
---|
95 | spacesRemaining = tabLength - 1;
|
---|
96 | ch = ' ';
|
---|
97 | }
|
---|
98 | }
|
---|
99 | 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 final 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 final 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 final 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 final 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 =
|
---|
147 | new Integer(params[i].getValue()).intValue();
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|