📄 fixcrlffilter.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.filters;import java.io.IOException;import java.io.Reader;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.taskdefs.condition.Os;import org.apache.tools.ant.types.EnumeratedAttribute;/** * Converts text to local OS formatting conventions, as well as repair text * damaged by misconfigured or misguided editors or file transfer programs. * <p> * This filter can take the following arguments: * <ul> * <li>eof * <li>eol * <li>fixlast * <li>javafiles * <li>tab * <li>tablength * </ul> * None of which are required. * <p> * This version generalises the handling of EOL characters, and allows for * CR-only line endings (the standard on Mac systems prior to OS X). Tab * handling has also been generalised to accommodate any tabwidth from 2 to 80, * inclusive. Importantly, it can leave untouched any literal TAB characters * embedded within Java string or character constants. * <p> * <em>Caution:</em> run with care on carefully formatted files. This may * sound obvious, but if you don't specify asis, presume that your files are * going to be modified. If "tabs" is "add" or "remove", whitespace characters * may be added or removed as necessary. Similarly, for EOLs, eol="asis" * actually means convert to your native O/S EOL convention while eol="crlf" or * cr="add" can result in CR characters being removed in one special case * accommodated, i.e., CRCRLF is regarded as a single EOL to handle cases where * other programs have converted CRLF into CRCRLF. * * <P> * Example: * * <pre> * <<fixcrlf tab="add" eol="crlf" eof="asis"/> * </pre> * * Or: * * <pre> * <filterreader classname="org.apache.tools.ant.filters.FixCrLfFilter"> * <param eol="crlf" tab="asis"/> * </filterreader> * </pre> * */public final class FixCrLfFilter extends BaseParamFilterReader implements ChainableReader { private static final int DEFAULT_TAB_LENGTH = 8; private static final int MIN_TAB_LENGTH = 2; private static final int MAX_TAB_LENGTH = 80; private static final char CTRLZ = '\u001A'; private int tabLength = DEFAULT_TAB_LENGTH; private CrLf eol; private AddAsisRemove ctrlz; private AddAsisRemove tabs; private boolean javafiles = false; private boolean fixlast = true; private boolean initialized = false; /** * Constructor for "dummy" instances. * * @see BaseFilterReader#BaseFilterReader() */ public FixCrLfFilter() { super(); } /** * Create a new filtered reader. * * @param in * A Reader object providing the underlying stream. Must not be * <code>null</code>. * @throws IOException on error. */ public FixCrLfFilter(final Reader in) throws IOException { super(in); } // Instance initializer: Executes just after the super() call in this // class's constructor. { tabs = AddAsisRemove.ASIS; if (Os.isFamily("mac") && !Os.isFamily("unix")) { ctrlz = AddAsisRemove.REMOVE; setEol(CrLf.MAC); } else if (Os.isFamily("dos")) { ctrlz = AddAsisRemove.ASIS; setEol(CrLf.DOS); } else { ctrlz = AddAsisRemove.REMOVE; setEol(CrLf.UNIX); } } /** * Create a new FixCrLfFilter using the passed in Reader for instantiation. * * @param rdr * A Reader object providing the underlying stream. Must not be * <code>null</code>. * * @return a new filter based on this configuration, but filtering the * specified reader. */ public Reader chain(final Reader rdr) { try { FixCrLfFilter newFilter = new FixCrLfFilter(rdr); newFilter.setJavafiles(getJavafiles()); newFilter.setEol(getEol()); newFilter.setTab(getTab()); newFilter.setTablength(getTablength()); newFilter.setEof(getEof()); newFilter.setFixlast(getFixlast()); newFilter.initInternalFilters(); return newFilter; } catch (IOException e) { throw new BuildException(e); } } /** * Get how DOS EOF (control-z) characters are being handled. * * @return values: * <ul> * <li>add: ensure that there is an eof at the end of the file * <li>asis: leave eof characters alone * <li>remove: remove any eof character found at the end * </ul> */ public AddAsisRemove getEof() { // Return copy so that the call must call setEof() to change the state // of fixCRLF return ctrlz.newInstance(); } /** * Get how EndOfLine characters are being handled. * * @return values: * <ul> * <li>asis: convert line endings to your O/S convention * <li>cr: convert line endings to CR * <li>lf: convert line endings to LF * <li>crlf: convert line endings to CRLF * </ul> */ public CrLf getEol() { // Return copy so that the call must call setEol() to change the state // of fixCRLF return eol.newInstance(); } /** * Get whether a missing EOL be added to the final line of the stream. * * @return true if a filtered file will always end with an EOL */ public boolean getFixlast() { return fixlast; } /** * Get whether the stream is to be treated as though it contains Java * source. * <P> * This attribute is only used in assocation with the "<i><b>tab</b></i>" * attribute. Tabs found in Java literals are protected from changes by this * filter. * * @return true if whitespace in Java character and string literals is * ignored. */ public boolean getJavafiles() { return javafiles; } /** * Return how tab characters are being handled. * * @return 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 AddAsisRemove getTab() { // Return copy so that the caller must call setTab() to change the state // of fixCRLF. return tabs.newInstance(); } /** * Get the tab length to use. * * @return the length of tab in spaces */ public int getTablength() { return tabLength; } private static String calculateEolString(CrLf eol) { // Calculate the EOL string per the current config if (eol == CrLf.ASIS) { return System.getProperty("line.separator"); } if (eol == CrLf.CR || eol == CrLf.MAC) { return "\r"; } if (eol == CrLf.CRLF || eol == CrLf.DOS) { return "\r\n"; } // assume (eol == CrLf.LF || eol == CrLf.UNIX) return "\n"; } /** * Wrap the input stream with the internal filters necessary to perform the * configuration settings. */ private void initInternalFilters() { // If I'm removing an EOF character, do so first so that the other // filters don't see that character. in = (ctrlz == AddAsisRemove.REMOVE) ? new RemoveEofFilter(in) : in; // Change all EOL characters to match the calculated EOL string. If // configured to do so, append a trailing EOL so that the file ends on // a EOL. in = new NormalizeEolFilter(in, calculateEolString(eol), getFixlast()); if (tabs != AddAsisRemove.ASIS) { // If filtering Java source, prevent changes to whitespace in // character and string literals. if (getJavafiles()) { in = new MaskJavaTabLiteralsFilter(in); } // Add/Remove tabs in = (tabs == AddAsisRemove.ADD) ? (Reader) new AddTabFilter(in, getTablength()) : (Reader) new RemoveTabFilter(in, getTablength()); } // Add missing EOF character in = (ctrlz == AddAsisRemove.ADD) ? new AddEofFilter(in) : in; initialized = true; } /** * Return the next character in the filtered stream. * * @return the next character in the resulting stream, or -1 if the end of * the resulting stream has been reached. * * @exception IOException * if the underlying stream throws an IOException during * reading. */ public synchronized int read() throws IOException { if (!initialized) { initInternalFilters(); } return in.read(); } /** * Specify how DOS EOF (control-z) characters are to be handled. * * @param attr * valid values: * <ul> * <li>add: ensure that there is an eof at the end of the file * <li>asis: leave eof characters alone * <li>remove: remove any eof character found at the end * </ul> */ public void setEof(AddAsisRemove attr) { ctrlz = attr.resolve(); } /** * Specify how end of line (EOL) characters are to be handled. * * @param attr * valid values: * <ul> * <li>asis: convert line endings to your O/S convention * <li>cr: convert line endings to CR * <li>lf: convert line endings to LF * <li>crlf: convert line endings to CRLF * </ul> */ public void setEol(CrLf attr) { eol = attr.resolve(); } /** * Specify whether a missing EOL will be added to the final line of input. * * @param fixlast * if true a missing EOL will be appended.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -