KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > diff > TextDiffOutput


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.diff;
17
18 import java.io.Writer JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.ResourceBundle JavaDoc;
21
22 public class TextDiffOutput implements DiffOutput {
23     private Writer JavaDoc writer;
24     private boolean markLines;
25     private final StringBuffer JavaDoc contentLine = new StringBuffer JavaDoc();
26     private final StringBuffer JavaDoc markLine = new StringBuffer JavaDoc();
27     private boolean currentLineHasChangedText = false;
28     private ResourceBundle JavaDoc bundle;
29
30     public TextDiffOutput(Writer JavaDoc writer, boolean markLines) {
31         this(writer, markLines, Locale.US);
32     }
33
34     public TextDiffOutput(Writer JavaDoc writer, boolean markLines, Locale JavaDoc locale) {
35         this.writer = writer;
36         this.markLines = markLines;
37         bundle = ResourceBundle.getBundle("org/outerj/daisy/diff/messages", locale);
38     }
39
40     public void startLine(DiffLineType type) throws Exception JavaDoc{
41         contentLine.setLength(0);
42         if (type == DiffLineType.UNCHANGED) {
43             contentLine.append(" ");
44         } else if (type == DiffLineType.ADDED) {
45             contentLine.append("+++ ");
46         } else if (type == DiffLineType.REMOVED) {
47             contentLine.append("--- ");
48         }
49         currentLineHasChangedText = false;
50         if (markLines)
51             markLine.setLength(0);
52     }
53
54     public void addUnchangedText(String JavaDoc text) throws Exception JavaDoc {
55         if (markLines) {
56             if (currentLineHasChangedText) {
57                 for (int i = 0; i < text.length(); i++)
58                     markLine.append(' ');
59             }
60         }
61         contentLine.append(text);
62     }
63
64     public void addChangedText(String JavaDoc text) throws Exception JavaDoc {
65         if (markLines) {
66             if (!currentLineHasChangedText) {
67                 currentLineHasChangedText = true;
68                 for (int i = 0; i < contentLine.length() - 4; i++) // minus 4 for the margin width
69
markLine.append(' ');
70             }
71             for (int i = 0; i < text.length(); i++)
72                 markLine.append('^');
73         }
74         contentLine.append(text);
75     }
76
77     public void endLine() throws Exception JavaDoc {
78         writer.write(contentLine.toString());
79         writer.write('\n');
80         if (markLines && currentLineHasChangedText) {
81             if (markLine.length() > 0) {
82                 writer.write(" ");
83                 writer.write(markLine.toString());
84                 writer.write('\n');
85             }
86         }
87     }
88
89     public void skippedLines(int linesSkipped) throws Exception JavaDoc {
90         writer.write("(" + linesSkipped + ' ' + bundle.getString("equal-lines-skipped") + ")\n");
91     }
92 }
93
Popular Tags