aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/filters/FixCrLfFilter.java
blob: 8a37924c708be034af00280ead67eff93391c541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
/*
 *  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>
 * &lt;&lt;fixcrlf tab=&quot;add&quot; eol=&quot;crlf&quot; eof=&quot;asis&quot;/&gt;
 * </pre>
 *
 * Or:
 *
 * <pre>
 * &lt;filterreader classname=&quot;org.apache.tools.ant.filters.FixCrLfFilter&quot;&gt;
 *   &lt;param eol=&quot;crlf&quot; tab=&quot;asis&quot;/&gt;
 *  &lt;/filterreader&gt;
 * </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 association with the &quot;<i><b>tab</b></i>&quot;
     * 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.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.
        if (eol != CrLf.ASIS) {
            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.
     */
    public void setFixlast(boolean fixlast) {
        this.fixlast = fixlast;
    }

    /**
     * Indicate whether this stream contains Java source.
     *
     * This attribute is only used in association with the &quot;<i><b>tab</b></i>&quot;
     * 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);
        }

        /**
         * 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"
                        numEOL = 2;
                        push(c2);
                    } else if (c1 == '\n') {
                        // EOL was "\r\n"
                        push(c2);
                    } else {
                        // EOL was "\r"
                        push(c2);
                        push(c1);
                    }
                default:
                    // Fall tru
                }
                if (numEOL > 0) {
                    while (numEOL-- > 0) {
                        push(eol);
                        normalizedEOL += eol.length;
                    }
                    previousWasEOL = true;
                    thisChar = read();
                } else if (!atEnd) {
                    previousWasEOL = false;
                }
            } else {
                normalizedEOL--;
            }
            return thisChar;
        }
    }

    private static class AddEofFilter extends SimpleFilterReader {
        private int lastChar = -1;

        public AddEofFilter(Reader in) {
            super(in);
        }

        public int read() throws IOException {
            int thisChar = super.read();

            // if source is EOF but last character was NOT ctrl-z, return ctrl-z
            if (thisChar == -1) {
                if (lastChar != CTRLZ) {
                    lastChar = CTRLZ;
                    return lastChar;
                }
            } else {
                lastChar = thisChar;
            }
            return thisChar;
        }
    }

    private static class RemoveEofFilter extends SimpleFilterReader {
        private int lookAhead = -1;

        public RemoveEofFilter(Reader in) {
            super(in);

            try {
                lookAhead = in.read();
            } catch (IOException e) {
                lookAhead = -1;
            }
        }

        public int read() throws IOException {
            int lookAhead2 = super.read();

            // If source at EOF and lookAhead is ctrl-z, return EOF (NOT ctrl-z)
            if (lookAhead2 == -1 && lookAhead == CTRLZ) {
                return -1;
            }
            // Return current look-ahead
            int i = lookAhead;
            lookAhead = lookAhead2;
            return i;
        }
    }

    private static class AddTabFilter extends SimpleFilterReader {
        private int columnNumber = 0;

        private int tabLength = 0;

        public AddTabFilter(Reader in, int tabLength) {
            super(in);
            this.tabLength = tabLength;
        }

        public int read() throws IOException {
            int c = super.read();

            switch (c) {
            case '\r':
            case '\n':
                columnNumber = 0;
                break;
            case ' ':
                columnNumber++;
                if (!editsBlocked()) {
                    int colNextTab = ((columnNumber + tabLength - 1) / tabLength) * tabLength;
                    int countSpaces = 1;
                    int numTabs = 0;

                    scanWhitespace: while ((c = super.read()) != -1) {
                        switch (c) {
                        case ' ':
                            if (++columnNumber == colNextTab) {
                                numTabs++;
                                countSpaces = 0;
                                colNextTab += tabLength;
                            } else {
                                countSpaces++;
                            }
                            break;
                        case '\t':
                            columnNumber = colNextTab;
                            numTabs++;
                            countSpaces = 0;
                            colNextTab += tabLength;
                            break;
                        default:
                            push(c);
                            break scanWhitespace;
                        }
                    }
                    while (countSpaces-- > 0) {
                        push(' ');
                        columnNumber--;
                    }
                    while (numTabs-- > 0) {
                        push('\t');
                        columnNumber -= tabLength;
                    }
                    c = super.read();
                    switch (c) {
                    case ' ':
                        columnNumber++;
                        break;
                    case '\t':
                        columnNumber += tabLength;
                        break;
                    default:
                        // Fall tru
                    }
                }
                break;
            case '\t':
                columnNumber = ((columnNumber + tabLength - 1) / tabLength) * tabLength;
                break;
            default:
                columnNumber++;
            }
            return c;
        }
    }

    private static class RemoveTabFilter extends SimpleFilterReader {
        private int columnNumber = 0;

        private int tabLength = 0;

        public RemoveTabFilter(Reader in, int tabLength) {
            super(in);

            this.tabLength = tabLength;
        }

        public int read() throws IOException {
            int c = super.read();

            switch (c) {
            case '\r':
            case '\n':
                columnNumber = 0;
                break;
            case '\t':
                int width = tabLength - columnNumber % tabLength;

                if (!editsBlocked()) {
                    for (; width > 1; width--) {
                        push(' ');
                    }
                    c = ' ';
                }
                columnNumber += width;
                break;
            default:
                columnNumber++;
            }
            return c;
        }
    }

    /**
     * Enumerated attribute with the values "asis", "add" and "remove".
     */
    public static class AddAsisRemove extends EnumeratedAttribute {
        private static final AddAsisRemove ASIS = newInstance("asis");

        private static final AddAsisRemove ADD = newInstance("add");

        private static final AddAsisRemove REMOVE = newInstance("remove");

        /** {@inheritDoc}. */
        public String[] getValues() {
            return new String[] {"add", "asis", "remove"};
        }

        /**
         * Equality depending in the index.
         * @param other the object to test equality against.
         * @return true if the object has the same index as this.
         */
        public boolean equals(Object other) {
            return other instanceof AddAsisRemove
                    && getIndex() == ((AddAsisRemove) other).getIndex();
        }

        /**
         * Hashcode depending on the index.
         * @return the index as the hashcode.
         */
        public int hashCode() {
            return getIndex();
        }

        AddAsisRemove resolve() throws IllegalStateException {
            if (this.equals(ASIS)) {
                return ASIS;
            }
            if (this.equals(ADD)) {
                return ADD;
            }
            if (this.equals(REMOVE)) {
                return REMOVE;
            }
            throw new IllegalStateException("No replacement for " + this);
        }

        // Works like clone() but doesn't show up in the Javadocs
        private AddAsisRemove newInstance() {
            return newInstance(getValue());
        }

        /**
         * Create an instance of this enumerated value based on the string value.
         * @param value the value to use.
         * @return an enumerated instance.
         */
        public static AddAsisRemove newInstance(String value) {
            AddAsisRemove a = new AddAsisRemove();
            a.setValue(value);
            return a;
        }
    }

    /**
     * Enumerated attribute with the values "asis", "cr", "lf" and "crlf".
     */
    public static class CrLf extends EnumeratedAttribute {
        private static final CrLf ASIS = newInstance("asis");

        private static final CrLf CR = newInstance("cr");

        private static final CrLf CRLF = newInstance("crlf");

        private static final CrLf DOS = newInstance("dos");

        private static final CrLf LF = newInstance("lf");

        private static final CrLf MAC = newInstance("mac");

        private static final CrLf UNIX = newInstance("unix");

        /**
         * @see EnumeratedAttribute#getValues
         */
        /** {@inheritDoc}. */
        public String[] getValues() {
            return new String[] {"asis", "cr", "lf", "crlf", "mac", "unix", "dos"};
        }

        /**
         * Equality depending in the index.
         * @param other the object to test equality against.
         * @return true if the object has the same index as this.
         */
        public boolean equals(Object other) {
            return other instanceof CrLf && getIndex() == ((CrLf) other).getIndex();
        }

        /**
         * Hashcode depending on the index.
         * @return the index as the hashcode.
         */
        public int hashCode() {
            return getIndex();
        }

        CrLf resolve() {
            if (this.equals(ASIS)) {
                return ASIS;
            }
            if (this.equals(CR) || this.equals(MAC)) {
                return CR;
            }
            if (this.equals(CRLF) || this.equals(DOS)) {
                return CRLF;
            }
            if (this.equals(LF) || this.equals(UNIX)) {
                return LF;
            }
            throw new IllegalStateException("No replacement for " + this);
        }

        // Works like clone() but doesn't show up in the Javadocs
        private CrLf newInstance() {
            return newInstance(getValue());
        }

        /**
         * Create an instance of this enumerated value based on the string value.
         * @param value the value to use.
         * @return an enumerated instance.
         */
        public static CrLf newInstance(String value) {
            CrLf c = new CrLf();
            c.setValue(value);
            return c;
        }
    }
}