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 37 public final class StripLineBreaks 38 extends BaseParamFilterReader 39 implements ChainableReader { 40 44 private static final String DEFAULT_LINE_BREAKS = "\r\n"; 45 46 47 private static final String LINE_BREAKS_KEY = "linebreaks"; 48 49 50 private String lineBreaks = DEFAULT_LINE_BREAKS; 51 52 57 public StripLineBreaks() { 58 super(); 59 } 60 61 67 public StripLineBreaks(final Reader in) { 68 super(in); 69 } 70 71 81 public int read() throws IOException { 82 if (!getInitialized()) { 83 initialize(); 84 setInitialized(true); 85 } 86 87 int ch = in.read(); 88 while (ch != -1) { 89 if (lineBreaks.indexOf(ch) == -1) { 90 break; 91 } else { 92 ch = in.read(); 93 } 94 } 95 return ch; 96 } 97 98 104 public void setLineBreaks(final String lineBreaks) { 105 this.lineBreaks = lineBreaks; 106 } 107 108 114 private String getLineBreaks() { 115 return lineBreaks; 116 } 117 118 128 public Reader chain(final Reader rdr) { 129 StripLineBreaks newFilter = new StripLineBreaks(rdr); 130 newFilter.setLineBreaks(getLineBreaks()); 131 newFilter.setInitialized(true); 132 return newFilter; 133 } 134 135 138 private void initialize() { 139 String userDefinedLineBreaks = null; 140 Parameter[] params = getParameters(); 141 if (params != null) { 142 for (int i = 0; i < params.length; i++) { 143 if (LINE_BREAKS_KEY.equals(params[i].getName())) { 144 userDefinedLineBreaks = params[i].getValue(); 145 break; 146 } 147 } 148 } 149 if (userDefinedLineBreaks != null) { 150 lineBreaks = userDefinedLineBreaks; 151 } 152 } 153 } 154 | Popular Tags |