Diff.java
9.81 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Value
* javax.jcr.ValueFormatException
*/
package com.day.jcr.diff;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
public class Diff {
Map<Item, ChangeType> changes = new HashMap<Item, ChangeType>();
public Map<Item, ChangeType> getChanges() {
return this.changes;
}
public boolean hasChanges() {
return !this.changes.keySet().isEmpty();
}
public int getSize() {
return this.changes.keySet().size();
}
public static Diff compareNodes(Node a, Node b) throws Exception {
return Diff.compareNodes(a, b, new String[0]);
}
public static Diff compareNodes(Node a, Node b, String[] excludes) throws Exception {
Diff diff = new Diff();
diff.add(Diff.compareProperties(a, b, excludes));
HashMap<String, Node> targetNodes = new HashMap<String, Node>();
NodeIterator nIt = b.getNodes();
while (nIt.hasNext()) {
Node node = nIt.nextNode();
targetNodes.put(node.getName(), node);
}
nIt = a.getNodes();
while (nIt.hasNext()) {
Node ref = nIt.nextNode();
String name = ref.getName();
if (targetNodes.containsKey(name)) {
Node target = (Node)targetNodes.get(name);
diff.add(Diff.compareNodes(ref, target, excludes));
targetNodes.remove(name);
continue;
}
diff.add((Item)ref, ChangeType.DELETE);
}
Iterator names = targetNodes.keySet().iterator();
while (names.hasNext()) {
Node node = (Node)targetNodes.get(names.next());
diff.add((Item)node, ChangeType.ADD);
}
return diff;
}
public static Diff compareProperties(Node a, Node b) throws Exception {
return Diff.compareProperties(a, b, new String[0]);
}
public static Diff compareProperties(Node a, Node b, String[] excludes) throws Exception {
Diff diff = new Diff();
List<String> exludeNames = Arrays.asList(excludes);
HashMap<String, Property> targetProps = new HashMap<String, Property>();
PropertyIterator pIt = b.getProperties();
while (pIt.hasNext()) {
Property prop = pIt.nextProperty();
targetProps.put(prop.getName(), prop);
}
pIt = a.getProperties();
while (pIt.hasNext()) {
Property ref = pIt.nextProperty();
String name = ref.getName();
if (exludeNames.contains(name)) continue;
if (targetProps.containsKey(name)) {
Value[] targetValues;
Value[] refValues;
Property target = (Property)targetProps.get(name);
if (ref.getType() != target.getType()) {
diff.add((Item)ref, ChangeType.MODIFIED);
continue;
}
if (ref.isMultiple() != target.isMultiple()) {
diff.add((Item)ref, ChangeType.MODIFIED);
continue;
}
if (ref.isMultiple()) {
refValues = ref.getValues();
targetValues = target.getValues();
} else {
refValues = new Value[]{ref.getValue()};
targetValues = new Value[]{target.getValue()};
}
ChangeType ct = Diff.compare(refValues, targetValues);
if (ct == ChangeType.MODIFIED) {
diff.add((Item)ref, ct);
}
targetProps.remove(name);
continue;
}
diff.add((Item)ref, ChangeType.DELETE);
}
Iterator names = targetProps.keySet().iterator();
while (names.hasNext()) {
Property prop = (Property)targetProps.get(names.next());
if (exludeNames.contains(prop.getName())) continue;
diff.add((Item)prop, ChangeType.ADD);
}
return diff;
}
private static ChangeType compare(Value[] v1, Value[] v2) throws Exception {
if (v1.length != v2.length) {
return ChangeType.MODIFIED;
}
ChangeType ct = ChangeType.NONE;
for (int i = 0; i < v1.length && (ct = Diff.compare(v1[i], v2[i])) != ChangeType.MODIFIED; ++i) {
}
return ct;
}
private static ChangeType compare(Value v1, Value v2) throws Exception {
switch (v1.getType()) {
case 1: {
return Diff.compareString(v1, v2);
}
case 2: {
return Diff.compareBinary(v1, v2);
}
case 6: {
return Diff.compareBoolean(v1, v2);
}
case 5: {
return Diff.compareDate(v1, v2);
}
case 12: {
return Diff.compareDecimal(v1, v2);
}
case 4: {
return Diff.compareDouble(v1, v2);
}
case 3: {
return Diff.compareLong(v1, v2);
}
case 7: {
return Diff.compareString(v1, v2);
}
case 8: {
return Diff.compareString(v1, v2);
}
case 9: {
return Diff.compareString(v1, v2);
}
case 11: {
return Diff.compareString(v1, v2);
}
case 10: {
return Diff.compareString(v1, v2);
}
}
return ChangeType.NONE;
}
private static ChangeType compareBinary(Value v1, Value v2) throws Exception {
byte[] h2;
byte[] h1 = Diff.calcHash(v1.getBinary().getStream());
if (!Arrays.equals(h1, h2 = Diff.calcHash(v2.getBinary().getStream()))) {
return ChangeType.MODIFIED;
}
return ChangeType.NONE;
}
private static byte[] calcHash(InputStream is) throws RepositoryException, IOException, NoSuchAlgorithmException {
byte[] buf = new byte[is.available()];
is.read(buf);
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(buf);
return digest.digest();
}
private static ChangeType compareLong(Value v1, Value v2) throws ValueFormatException, RepositoryException {
if (v1.getLong() != v2.getLong()) {
return ChangeType.MODIFIED;
}
return ChangeType.NONE;
}
private static ChangeType compareDouble(Value v1, Value v2) throws ValueFormatException, RepositoryException {
if (v1.getDouble() != v2.getDouble()) {
return ChangeType.MODIFIED;
}
return ChangeType.NONE;
}
private static ChangeType compareDecimal(Value v1, Value v2) throws ValueFormatException, RepositoryException {
if (v1.getDecimal() != v2.getDecimal()) {
return ChangeType.MODIFIED;
}
return ChangeType.NONE;
}
private static ChangeType compareDate(Value v1, Value v2) throws ValueFormatException, RepositoryException {
if (v1.getDate().after(v2.getDate()) || v1.getDate().before(v2.getDate())) {
return ChangeType.MODIFIED;
}
return ChangeType.NONE;
}
private static ChangeType compareBoolean(Value v1, Value v2) throws ValueFormatException, RepositoryException {
if (v1.getBoolean() != v2.getBoolean()) {
return ChangeType.MODIFIED;
}
return ChangeType.NONE;
}
private static ChangeType compareString(Value v1, Value v2) throws RepositoryException {
if (!v1.getString().equals(v2.getString())) {
return ChangeType.MODIFIED;
}
return ChangeType.NONE;
}
private void add(Item item, ChangeType type) {
this.changes.put(item, type);
}
private void add(Diff diff) {
for (Item item : diff.changes.keySet()) {
this.changes.put(item, diff.changes.get((Object)item));
}
}
public void print(OutputStream out) throws RepositoryException {
PrintWriter w = new PrintWriter(out);
w.println(this.toString());
w.flush();
}
public String toString() {
StringBuffer buf = new StringBuffer();
for (Item item : this.changes.keySet()) {
try {
buf.append((Object)((Object)this.changes.get((Object)item)) + " " + item.getPath() + "\n");
}
catch (RepositoryException e) {}
}
return buf.toString();
}
static enum ChangeType {
ADD,
DELETE,
MODIFIED,
NONE;
private ChangeType() {
}
public String toString() {
switch (this) {
case ADD: {
return "A";
}
case DELETE: {
return "D";
}
case MODIFIED: {
return "M";
}
case NONE: {
return "N";
}
}
return Enum.super.toString();
}
}
}