1 18 package org.apache.tools.ant.filters; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import org.apache.tools.ant.types.Parameter; 23 24 38 public final class TabsToSpaces 39 extends BaseParamFilterReader 40 implements ChainableReader { 41 42 private static final int DEFAULT_TAB_LENGTH = 8; 43 44 45 private static final String TAB_LENGTH_KEY = "tablength"; 46 47 48 private int tabLength = DEFAULT_TAB_LENGTH; 49 50 51 private int spacesRemaining = 0; 52 53 58 public TabsToSpaces() { 59 super(); 60 } 61 62 68 public TabsToSpaces(final Reader in) { 69 super(in); 70 } 71 72 82 public int read() throws IOException { 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 108 public void setTablength(final int tabLength) { 109 this.tabLength = tabLength; 110 } 111 112 117 private int getTablength() { 118 return tabLength; 119 } 120 121 131 public Reader chain(final Reader rdr) { 132 TabsToSpaces newFilter = new TabsToSpaces(rdr); 133 newFilter.setTablength(getTablength()); 134 newFilter.setInitialized(true); 135 return newFilter; 136 } 137 138 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 (params[i].getValue()).intValue(); 149 break; 150 } 151 } 152 } 153 } 154 } 155 } 156 | Popular Tags |