KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > TabsToSpaces


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.filters;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import org.apache.tools.ant.types.Parameter;
23
24 /**
25  * Converts tabs to spaces.
26  *
27  * Example:
28  *
29  * <pre>&lt;tabtospaces tablength=&quot;8&quot;/&gt;</pre>
30  *
31  * Or:
32  *
33  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.TabsToSpaces&quot;&gt;
34  * &lt;param name=&quot;tablength&quot; value=&quot;8&quot;/&gt;
35  * &lt;/filterreader&gt;</pre>
36  *
37  */

38 public final class TabsToSpaces
39     extends BaseParamFilterReader
40     implements ChainableReader {
41     /** The default tab length. */
42     private static final int DEFAULT_TAB_LENGTH = 8;
43
44     /** Parameter name for the length of a tab. */
45     private static final String JavaDoc TAB_LENGTH_KEY = "tablength";
46
47     /** Tab length in this filter. */
48     private int tabLength = DEFAULT_TAB_LENGTH;
49
50     /** The number of spaces still to be read to represent the last-read tab. */
51     private int spacesRemaining = 0;
52
53     /**
54      * Constructor for "dummy" instances.
55      *
56      * @see BaseFilterReader#BaseFilterReader()
57      */

58     public TabsToSpaces() {
59         super();
60     }
61
62     /**
63      * Creates a new filtered reader.
64      *
65      * @param in A Reader object providing the underlying stream.
66      * Must not be <code>null</code>.
67      */

68     public TabsToSpaces(final Reader JavaDoc in) {
69         super(in);
70     }
71
72     /**
73      * Returns the next character in the filtered stream, converting tabs
74      * to the specified number of spaces.
75      *
76      * @return the next character in the resulting stream, or -1
77      * if the end of the resulting stream has been reached
78      *
79      * @exception IOException if the underlying stream throws an IOException
80      * during reading
81      */

82     public int read() throws IOException JavaDoc {
83         if (!getInitialized()) {
84             initialize();
85             setInitialized(true);
86         }
87
88         int ch = -1;
89
90         if (spacesRemaining > 0) {
91             spacesRemaining--;
92             ch = ' ';
93         } else {
94             ch = in.read();
95             if (ch == '\t') {
96                 spacesRemaining = tabLength - 1;
97                 ch = ' ';
98             }
99         }
100         return ch;
101     }
102
103     /**
104      * Sets the tab length.
105      *
106      * @param tabLength the number of spaces to be used when converting a tab.
107      */

108     public void setTablength(final int tabLength) {
109         this.tabLength = tabLength;
110     }
111
112     /**
113      * Returns the tab length.
114      *
115      * @return the number of spaces used when converting a tab
116      */

117     private int getTablength() {
118         return tabLength;
119     }
120
121     /**
122      * Creates a new TabsToSpaces using the passed in
123      * Reader for instantiation.
124      *
125      * @param rdr A Reader object providing the underlying stream.
126      * Must not be <code>null</code>.
127      *
128      * @return a new filter based on this configuration, but filtering
129      * the specified reader
130      */

131     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
132         TabsToSpaces newFilter = new TabsToSpaces(rdr);
133         newFilter.setTablength(getTablength());
134         newFilter.setInitialized(true);
135         return newFilter;
136     }
137
138     /**
139      * Parses the parameters to set the tab length.
140      */

141     private void initialize() {
142         Parameter[] params = getParameters();
143         if (params != null) {
144             for (int i = 0; i < params.length; i++) {
145                 if (params[i] != null) {
146                     if (TAB_LENGTH_KEY.equals(params[i].getName())) {
147                         tabLength =
148                             new Integer JavaDoc(params[i].getValue()).intValue();
149                         break;
150                     }
151                 }
152             }
153         }
154     }
155 }
156
Popular Tags