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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vdslib.state;
import java.util.LinkedList;
import java.util.List;
/**
* TODO: document this
*/
public class Diff {
public static class Entry {
String id;
// Values set for entries that contain diff themselves
String preContent;
String postContent;
boolean bold = false; // Print content in bold. Used if very important
// Values set for entries that contains subdiffs
Diff subDiff;
boolean splitLine = false; // If set, split this content on multiple lines
public Entry(Object id, Object pre, Object post) {
this.id = id.toString();
preContent = pre.toString();
postContent = post.toString();
}
public Entry(Object id, Diff subDiff) {
this.id = id.toString();
this.subDiff = subDiff;
}
public Entry bold() { bold = true; return this; }
public Entry splitLine() { splitLine = true; return this; }
}
private List<Entry> diff = new LinkedList<Entry>();
public void add(Entry e) { diff.add(e); }
public boolean differs() { return (!diff.isEmpty()); }
class PrintProperties {
boolean insertLineBreaks = false;
boolean ommitGroupForSingleEntries = true;
String lineBreak = "\n";
String entrySeparator = ", ";
String idValueSeparator = ": ";
String keyValueSeparator = " => ";
String singleGroupSeparator = "";
String groupStart = "[";
String groupStop = "]";
String indent = " ";
String boldStart = "";
String boldStop = "";
}
public String toString() {
StringBuilder sb = new StringBuilder();
PrintProperties pp = new PrintProperties();
print(sb, "", pp, false);
return sb.toString();
}
public String toHtml() {
StringBuilder sb = new StringBuilder();
PrintProperties pp = new PrintProperties();
pp.lineBreak = "<br>\n";
pp.indent = " ";
pp.keyValueSeparator = " => ";
pp.insertLineBreaks = true;
pp.boldStart = "<b>";
pp.boldStop = "</b>";
print(sb, "", pp, false);
return sb.toString();
}
public void print(StringBuilder sb, String indent, PrintProperties pp, boolean splitLines) {
boolean first = true;
for (Entry e : diff) {
if (first) {
first = false;
} else {
sb.append(pp.entrySeparator);
if (splitLines && pp.insertLineBreaks) {
sb.append(pp.lineBreak).append(indent);
}
}
sb.append(e.id);
if (e.subDiff != null) {
sb.append(pp.idValueSeparator);
if (e.subDiff.diff.size() > 1 || !pp.ommitGroupForSingleEntries) {
sb.append(pp.groupStart);
} else {
sb.append(pp.singleGroupSeparator);
}
if (e.splitLine && pp.insertLineBreaks) {
sb.append(pp.lineBreak).append(indent + pp.indent);
}
e.subDiff.print(sb, indent + pp.indent, pp, e.splitLine);
if (e.splitLine && pp.insertLineBreaks) {
sb.append(pp.lineBreak).append(indent);
}
if (e.subDiff.diff.size() > 1 || !pp.ommitGroupForSingleEntries) {
sb.append(pp.groupStop);
}
} else {
if (!e.id.isEmpty()) {
sb.append(pp.idValueSeparator);
}
if (e.bold) {
sb.append(pp.boldStart).append(e.preContent).append(pp.boldStop)
.append(pp.keyValueSeparator)
.append(pp.boldStart).append(e.postContent).append(pp.boldStop);
} else {
sb.append(e.preContent)
.append(pp.keyValueSeparator)
.append(e.postContent);
}
}
}
}
}
|