FileDocCategorySizeDatePackage
FixCrLfFilter.javaAPI DocApache Ant 1.7030975Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.filters

FixCrLfFilter

public final class FixCrLfFilter extends BaseParamFilterReader implements ChainableReader
Converts text to local OS formatting conventions, as well as repair text damaged by misconfigured or misguided editors or file transfer programs.

This filter can take the following arguments:

  • eof
  • eol
  • fixlast
  • javafiles
  • tab
  • tablength
None of which are required.

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.

Caution: 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.

Example:

<<fixcrlf tab="add" eol="crlf" eof="asis"/>
Or:
<filterreader classname="org.apache.tools.ant.filters.FixCrLfFilter">
<param eol="crlf" tab="asis"/>
</filterreader>

Fields Summary
private static final char
CTRLZ
private int
tabLength
private CrLf
eol
private AddAsisRemove
ctrlz
private AddAsisRemove
tabs
private boolean
javafiles
private boolean
fixlast
private boolean
initialized
Constructors Summary
public FixCrLfFilter()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        super();
    
public FixCrLfFilter(Reader in)
Create a new filtered reader.

param
in A Reader object providing the underlying stream. Must not be null.
throws
IOException on error.

        super(in);
    
Methods Summary
private static java.lang.StringcalculateEolString(org.apache.tools.ant.filters.FixCrLfFilter$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";
    
public java.io.Readerchain(java.io.Reader rdr)
Create a new FixCrLfFilter using the passed in Reader for instantiation.

param
rdr A Reader object providing the underlying stream. Must not be null.
return
a new filter based on this configuration, but filtering the specified reader.

        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);
        }
    
        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);
        }
    
public org.apache.tools.ant.filters.FixCrLfFilter$AddAsisRemovegetEof()
Get how DOS EOF (control-z) characters are being handled.

return
values:
  • add: ensure that there is an eof at the end of the file
  • asis: leave eof characters alone
  • remove: remove any eof character found at the end

        // Return copy so that the call must call setEof() to change the state
        // of fixCRLF
        return ctrlz.newInstance();
    
public org.apache.tools.ant.filters.FixCrLfFilter$CrLfgetEol()
Get how EndOfLine characters are being handled.

return
values:
  • asis: convert line endings to your O/S convention
  • cr: convert line endings to CR
  • lf: convert line endings to LF
  • crlf: convert line endings to CRLF

        // Return copy so that the call must call setEol() to change the state
        // of fixCRLF
        return eol.newInstance();
    
public booleangetFixlast()
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

        return fixlast;
    
public booleangetJavafiles()
Get whether the stream is to be treated as though it contains Java source.

This attribute is only used in assocation with the "tab" 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.

        return javafiles;
    
public org.apache.tools.ant.filters.FixCrLfFilter$AddAsisRemovegetTab()
Return how tab characters are being handled.

return
values:
  • add: convert sequences of spaces which span a tab stop to tabs
  • asis: leave tab and space characters alone
  • remove: convert tabs to spaces

        // Return copy so that the caller must call setTab() to change the state
        // of fixCRLF.
        return tabs.newInstance();
    
public intgetTablength()
Get the tab length to use.

return
the length of tab in spaces

        return tabLength;
    
private voidinitInternalFilters()
Wrap the input stream with the internal filters necessary to perform the configuration settings.


        // 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;
    
public synchronized intread()
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.

        if (!initialized) {
            initInternalFilters();
        }
        return in.read();
    
public voidsetEof(org.apache.tools.ant.filters.FixCrLfFilter$AddAsisRemove attr)
Specify how DOS EOF (control-z) characters are to be handled.

param
attr valid values:
  • add: ensure that there is an eof at the end of the file
  • asis: leave eof characters alone
  • remove: remove any eof character found at the end

        ctrlz = attr.resolve();
    
public voidsetEol(org.apache.tools.ant.filters.FixCrLfFilter$CrLf attr)
Specify how end of line (EOL) characters are to be handled.

param
attr valid values:
  • asis: convert line endings to your O/S convention
  • cr: convert line endings to CR
  • lf: convert line endings to LF
  • crlf: convert line endings to CRLF

        eol = attr.resolve();
    
public voidsetFixlast(boolean fixlast)
Specify whether a missing EOL will be added to the final line of input.

param
fixlast if true a missing EOL will be appended.

        this.fixlast = fixlast;
    
public voidsetJavafiles(boolean javafiles)
Indicate whether this stream contains Java source. This attribute is only used in assocation with the "tab" attribute.

param
javafiles set to true to prevent this filter from changing tabs found in Java literals.

        this.javafiles = javafiles;
    
public voidsetTab(org.apache.tools.ant.filters.FixCrLfFilter$AddAsisRemove attr)
Specify how tab characters are to be handled.

param
attr valid values:
  • add: convert sequences of spaces which span a tab stop to tabs
  • asis: leave tab and space characters alone
  • remove: convert tabs to spaces

        tabs = attr.resolve();
    
public voidsetTablength(int tabLength)
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.

        if (tabLength < 2 || tabLength > 80) {
            throw new IOException("tablength must be between 2 and 80");
        }
        this.tabLength = tabLength;