aboutsummaryrefslogtreecommitdiffstats
path: root/lib/jdiff/src/util/DiffOutput.java
blob: 06d058d4d3e7884ac9fb74d29db27bab393365f2 (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
/*
 * DiffOutput.java
 * Copyright (c) 2001 Andre Kaplan
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


package jdiff.util;

import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;



/** A base class for printing edit scripts produced by Diff. */
public abstract class DiffOutput {
    /** Set to the lines of the files being compared. */
    protected Object[] file0;
    protected Object[] file1;

    protected String lineSeparator;
    protected Writer out;


    protected DiffOutput(Object[] a,Object[] b) {
        this.lineSeparator = System.getProperty("java.line.separator");
        this.out = new OutputStreamWriter(System.out);
        this.file0 = a;
        this.file1 = b;
    }


    public void setOut(Writer out) {
        this.out = out;
    }


    public void setLineSeparator(String lineSeparator) {
        this.lineSeparator = lineSeparator;
    }


    abstract public void writeScript(Diff.change script) throws IOException;


    protected void writeLine(String prefix, Object line) throws IOException {
        out.write(prefix + line.toString() + this.lineSeparator);
    }


    /** Write a pair of line numbers separated with sepChar.
        If the two numbers are identical, print just one number.

        Args a and b are internal line numbers (ranging from 0)
        We write the translated (real) line numbers ranging from 1).
    */
    protected void writeNumberRange(char sepChar, int a, int b)  throws IOException {
        /* Note: we can have b < a in the case of a range of no lines.
        In this case, we should write the line number before the range,
        which is b.  */
        if (++b > ++a) {
            out.write(Integer.toString(a));
            out.write(sepChar);
            out.write(Integer.toString(b));
        } else {
            out.write(Integer.toString(b));
        }
    }


    public static char changeLetter(int inserts, int deletes) {
        if (inserts == 0) {
            return 'd';
        } else if (deletes == 0) {
            return 'a';
        } else {
            return 'c';
        }
    }
}