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
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed 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 com.android.dumprendertree2;
import name.fraser.neil.plaintext.diff_match_patch;
import java.util.LinkedList;
/**
* Helper methods fo TextResult.getDiffAsHtml()
*/
public class VisualDiffUtils {
private static final int DONT_PRINT_LINE_NUMBER = -1;
/**
* Preprocesses the list of diffs so that new line characters appear only at the end of
* diff.text
*
* @param diffs
* @return
* LinkedList of diffs where new line character appears only on the end of
* diff.text
*/
public static LinkedList<diff_match_patch.Diff> splitDiffsOnNewline(
LinkedList<diff_match_patch.Diff> diffs) {
LinkedList<diff_match_patch.Diff> newDiffs = new LinkedList<diff_match_patch.Diff>();
String[] parts;
int lengthMinusOne;
for (diff_match_patch.Diff diff : diffs) {
parts = diff.text.split("\n", -1);
if (parts.length == 1) {
newDiffs.add(diff);
continue;
}
lengthMinusOne = parts.length - 1;
for (int i = 0; i < lengthMinusOne; i++) {
newDiffs.add(new diff_match_patch.Diff(diff.operation, parts[i] + "\n"));
}
if (!parts[lengthMinusOne].isEmpty()) {
newDiffs.add(new diff_match_patch.Diff(diff.operation, parts[lengthMinusOne]));
}
}
return newDiffs;
}
public static void generateExpectedResultLines(LinkedList<diff_match_patch.Diff> diffs,
LinkedList<Integer> lineNums, LinkedList<String> lines) {
String delSpan = "<span class=\"del\">";
String eqlSpan = "<span class=\"eql\">";
String line = "";
int i = 1;
diff_match_patch.Diff diff;
int size = diffs.size();
boolean isLastDiff;
for (int j = 0; j < size; j++) {
diff = diffs.get(j);
isLastDiff = j == size - 1;
switch (diff.operation) {
case DELETE:
line = processDiff(diff, lineNums, lines, line, i, delSpan, isLastDiff);
if (line.equals("")) {
i++;
}
break;
case INSERT:
// If the line is currently empty and this insertion is the entire line, the
// expected line is absent, so it has no line number.
if (diff.text.endsWith("\n") || isLastDiff) {
lineNums.add(line.equals("") ? DONT_PRINT_LINE_NUMBER : i++);
lines.add(line);
line = "";
}
break;
case EQUAL:
line = processDiff(diff, lineNums, lines, line, i, eqlSpan, isLastDiff);
if (line.equals("")) {
i++;
}
break;
}
}
}
public static void generateActualResultLines(LinkedList<diff_match_patch.Diff> diffs,
LinkedList<Integer> lineNums, LinkedList<String> lines) {
String insSpan = "<span class=\"ins\">";
String eqlSpan = "<span class=\"eql\">";
String line = "";
int i = 1;
diff_match_patch.Diff diff;
int size = diffs.size();
boolean isLastDiff;
for (int j = 0; j < size; j++) {
diff = diffs.get(j);
isLastDiff = j == size - 1;
switch (diff.operation) {
case INSERT:
line = processDiff(diff, lineNums, lines, line, i, insSpan, isLastDiff);
if (line.equals("")) {
i++;
}
break;
case DELETE:
// If the line is currently empty and deletion is the entire line, the
// actual line is absent, so it has no line number.
if (diff.text.endsWith("\n") || isLastDiff) {
lineNums.add(line.equals("") ? DONT_PRINT_LINE_NUMBER : i++);
lines.add(line);
line = "";
}
break;
case EQUAL:
line = processDiff(diff, lineNums, lines, line, i, eqlSpan, isLastDiff);
if (line.equals("")) {
i++;
}
break;
}
}
}
/**
* Generate or append a line for a given diff and add it to given collections if necessary.
* It puts diffs in HTML spans.
*
* @param diff
* @param lineNums
* @param lines
* @param line
* @param i
* @param begSpan
* @param forceOutputLine Force the current line to be output
* @return
*/
public static String processDiff(diff_match_patch.Diff diff, LinkedList<Integer> lineNums,
LinkedList<String> lines, String line, int i, String begSpan, boolean forceOutputLine) {
String endSpan = "</span>";
String br = " ";
if (diff.text.endsWith("\n") || forceOutputLine) {
lineNums.add(i);
/** TODO: Think of better way to replace stuff */
line += begSpan + diff.text.replace(" ", " ")
+ endSpan + br;
lines.add(line);
line = "";
} else {
line += begSpan + diff.text.replace(" ", " ") + endSpan;
}
return line;
}
public static String getHtml(LinkedList<Integer> lineNums1, LinkedList<String> lines1,
LinkedList<Integer> lineNums2, LinkedList<String> lines2) {
StringBuilder html = new StringBuilder();
int lineNum;
int size = lines1.size();
for (int i = 0; i < size; i++) {
html.append("<tr class=\"results\">");
html.append(" <td class=\"line_count\">");
lineNum = lineNums1.removeFirst();
if (lineNum > 0) {
html.append(lineNum);
}
html.append(" </td>");
html.append(" <td class=\"line\">");
html.append(lines1.removeFirst());
html.append(" </td>");
html.append(" <td class=\"space\"></td>");
html.append(" <td class=\"line_count\">");
lineNum = lineNums2.removeFirst();
if (lineNum > 0) {
html.append(lineNum);
}
html.append(" </td>");
html.append(" <td class=\"line\">");
html.append(lines2.removeFirst());
html.append(" </td>");
html.append("</tr>");
}
return html.toString();
}
}
|