📄 fixcrlffilter.java
字号:
*/ public void setFixlast(boolean fixlast) { this.fixlast = fixlast; } /** * Indicate whether this stream contains Java source. * * This attribute is only used in assocation with the "<i><b>tab</b></i>" * attribute. * * @param javafiles * set to true to prevent this filter from changing tabs found in * Java literals. */ public void setJavafiles(boolean javafiles) { this.javafiles = javafiles; } /** * Specify how tab characters are to be handled. * * @param attr * valid values: * <ul> * <li>add: convert sequences of spaces which span a tab stop to * tabs * <li>asis: leave tab and space characters alone * <li>remove: convert tabs to spaces * </ul> */ public void setTab(AddAsisRemove attr) { tabs = attr.resolve(); } /** * Specify tab length in characters. * * @param tabLength * specify the length of tab in spaces. Valid values are between * 2 and 80 inclusive. The default for this parameter is 8. * @throws IOException on error. */ public void setTablength(int tabLength) throws IOException { if (tabLength < MIN_TAB_LENGTH || tabLength > MAX_TAB_LENGTH) { throw new IOException( "tablength must be between " + MIN_TAB_LENGTH + " and " + MAX_TAB_LENGTH); } this.tabLength = tabLength; } /** * This filter reader redirects all read I/O methods through its own read() * method. * * <P> * The input stream is already buffered by the copy task so this doesn't * significantly impact performance while it makes writing the individual * fix filters much easier. * </P> */ private static class SimpleFilterReader extends Reader { private static final int PREEMPT_BUFFER_LENGTH = 16; private Reader in; private int[] preempt = new int[PREEMPT_BUFFER_LENGTH]; private int preemptIndex = 0; public SimpleFilterReader(Reader in) { this.in = in; } public void push(char c) { push((int) c); } public void push(int c) { try { preempt[preemptIndex++] = c; } catch (ArrayIndexOutOfBoundsException e) { int[] p2 = new int[preempt.length * 2]; System.arraycopy(preempt, 0, p2, 0, preempt.length); preempt = p2; push(c); } } public void push(char[] cs, int start, int length) { for (int i = start + length - 1; i >= start;) { push(cs[i--]); } } public void push(char[] cs) { push(cs, 0, cs.length); } public void push(String s) { push(s.toCharArray()); } /** * Does this filter want to block edits on the last character returned * by read()? */ public boolean editsBlocked() { return in instanceof SimpleFilterReader && ((SimpleFilterReader) in).editsBlocked(); } public int read() throws java.io.IOException { return preemptIndex > 0 ? preempt[--preemptIndex] : in.read(); } public void close() throws java.io.IOException { in.close(); } public void reset() throws IOException { in.reset(); } public boolean markSupported() { return in.markSupported(); } public boolean ready() throws java.io.IOException { return in.ready(); } public void mark(int i) throws java.io.IOException { in.mark(i); } public long skip(long i) throws java.io.IOException { return in.skip(i); } public int read(char[] buf) throws java.io.IOException { return read(buf, 0, buf.length); } public int read(char[] buf, int start, int length) throws java.io.IOException { int count = 0; int c = 0; // CheckStyle:InnerAssignment OFF - leave alone while (length-- > 0 && (c = this.read()) != -1) { buf[start++] = (char) c; count++; } // if at EOF with no characters in the buffer, return EOF return (count == 0 && c == -1) ? -1 : count; } } private static class MaskJavaTabLiteralsFilter extends SimpleFilterReader { private boolean editsBlocked = false; private static final int JAVA = 1; private static final int IN_CHAR_CONST = 2; private static final int IN_STR_CONST = 3; private static final int IN_SINGLE_COMMENT = 4; private static final int IN_MULTI_COMMENT = 5; private static final int TRANS_TO_COMMENT = 6; private static final int TRANS_FROM_MULTI = 8; private int state; public MaskJavaTabLiteralsFilter(Reader in) { super(in); state = JAVA; } public boolean editsBlocked() { return editsBlocked || super.editsBlocked(); } public int read() throws IOException { int thisChar = super.read(); // Mask, block from being edited, all characters in constants. editsBlocked = (state == IN_CHAR_CONST || state == IN_STR_CONST); switch (state) { case JAVA: // The current character is always emitted. switch (thisChar) { case '\'': state = IN_CHAR_CONST; break; case '"': state = IN_STR_CONST; break; case '/': state = TRANS_TO_COMMENT; break; default: // Fall tru } break; case IN_CHAR_CONST: switch (thisChar) { case '\'': state = JAVA; break; default: // Fall tru } break; case IN_STR_CONST: switch (thisChar) { case '"': state = JAVA; break; default: // Fall tru } break; case IN_SINGLE_COMMENT: // The current character is always emitted. switch (thisChar) { case '\n': case '\r': // EOL state = JAVA; break; default: // Fall tru } break; case IN_MULTI_COMMENT: // The current character is always emitted. switch (thisChar) { case '*': state = TRANS_FROM_MULTI; break; default: // Fall tru } break; case TRANS_TO_COMMENT: // The current character is always emitted. switch (thisChar) { case '*': state = IN_MULTI_COMMENT; break; case '/': state = IN_SINGLE_COMMENT; break; case '\'': state = IN_CHAR_CONST; break; case '"': state = IN_STR_CONST; break; default: state = JAVA; } break; case TRANS_FROM_MULTI: // The current character is always emitted. switch (thisChar) { case '/': state = JAVA; break; default: // Fall tru } break; default: // Fall tru } return thisChar; } } private static class NormalizeEolFilter extends SimpleFilterReader { private boolean previousWasEOL; private boolean fixLast; private int normalizedEOL = 0; private char[] eol = null; public NormalizeEolFilter(Reader in, String eolString, boolean fixLast) { super(in); eol = eolString.toCharArray(); this.fixLast = fixLast; } public int read() throws IOException { int thisChar = super.read(); if (normalizedEOL == 0) { int numEOL = 0; boolean atEnd = false; switch (thisChar) { case CTRLZ: int c = super.read(); if (c == -1) { atEnd = true; if (fixLast && !previousWasEOL) { numEOL = 1; push(thisChar); } } else { push(c); } break; case -1: atEnd = true; if (fixLast && !previousWasEOL) { numEOL = 1; } break; case '\n': // EOL was "\n" numEOL = 1; break; case '\r': numEOL = 1; int c1 = super.read(); int c2 = super.read(); if (c1 == '\r' && c2 == '\n') { // EOL was "\r\r\n" } else if (c1 == '\r') { // EOL was "\r\r" - handle as two consecutive "\r" and // "\r"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -