DiffInfo.java
6.23 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.version.Version
* javax.jcr.version.VersionException
* javax.jcr.version.VersionHistory
* javax.jcr.version.VersionIterator
* org.apache.commons.lang.time.FastDateFormat
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.commons;
import com.day.cq.commons.DiffService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.version.Version;
import javax.jcr.version.VersionException;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DiffInfo {
public static final FastDateFormat DATE_DEFAULT = FastDateFormat.getInstance((String)"dd.MM.yyyy HH:mm:ss");
private static final Logger log = LoggerFactory.getLogger(DiffInfo.class);
private final Resource content;
private final TYPE type;
public DiffInfo(Resource c, TYPE l) {
this.content = c;
this.type = l;
}
public Resource getContent() {
return this.content;
}
public TYPE getType() {
return this.type;
}
public String getDiffOutput(DiffService service, String current, String oldText, boolean isRichText) {
if (service != null) {
if (this.getType() == TYPE.ADDED) {
return service.diff(current, null, isRichText);
}
if (this.getType() == TYPE.REMOVED) {
return service.diff(null, oldText, isRichText);
}
return service.diff(current, oldText, isRichText);
}
return null;
}
public static Resource getVersionedResource(Resource res, String versionLabel) {
Node currentNode = (Node)res.adaptTo(Node.class);
if (currentNode == null) {
return null;
}
try {
Node versionNode = DiffInfo.getVersionedNode(currentNode, versionLabel);
if (versionNode != null) {
return res.getResourceResolver().getResource(versionNode.getPath());
}
}
catch (RepositoryException e) {
log.error("Error while trying to get versioned node for path " + res.getPath() + ", version " + versionLabel, (Throwable)e);
}
return null;
}
private static Node getVersionedNode(Node node, String versionTag) throws RepositoryException {
String path = node.getPath();
while (node != null && !node.isNodeType("mix:versionable")) {
if (node.getDepth() > 0) {
node = node.getParent();
continue;
}
node = null;
}
if (node == null) {
log.debug("getVersionedNode: No versionable node exists at path '{}'", (Object)path);
return null;
}
Version versionNode = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_DEFAULT.getPattern());
Date d = sdf.parse(versionTag);
Calendar versionDate = Calendar.getInstance();
versionDate.setTime(d);
versionNode = DiffInfo.getVersion(node, versionDate);
}
catch (ParseException e) {
// empty catch block
}
if (versionNode == null) {
try {
versionNode = node.getVersionHistory().getVersion(versionTag);
}
catch (VersionException ve) {
try {
versionNode = node.getVersionHistory().getVersionByLabel(versionTag);
}
catch (VersionException ve2) {
// empty catch block
}
}
}
if (versionNode == null) {
log.debug("getVersionedNode: Version '{}' not found for path '{}'", (Object)versionTag, (Object)path);
return null;
}
if (!node.getPath().equals(path)) {
String childPath;
Session session = node.getSession();
if (!session.itemExists(childPath = versionNode.getPath() + "/" + "jcr:frozenNode" + path.substring(node.getPath().length()))) {
log.debug("getVersionedNode: Version '{}' not found for path '{}'", (Object)versionTag, (Object)path);
return null;
}
versionNode = (Node)session.getItem(childPath);
} else if (versionNode.hasNode("jcr:frozenNode")) {
versionNode = versionNode.getNode("jcr:frozenNode");
} else {
log.debug("getVersionedNode: Frozen node not found in version '{}' for path '{}'", (Object)versionTag, (Object)path);
return null;
}
return versionNode;
}
private static Node getVersion(Node versionedNode, Calendar cal) throws RepositoryException {
ArrayList<Version> revisions = new ArrayList<Version>();
VersionIterator iter = versionedNode.getVersionHistory().getAllVersions();
while (iter.hasNext()) {
revisions.add(iter.nextVersion());
}
Collections.sort(revisions, new Comparator<Version>(){
@Override
public int compare(Version r1, Version r2) {
try {
return r2.getCreated().compareTo(r1.getCreated());
}
catch (RepositoryException re) {
return 0;
}
}
});
for (Version v : revisions) {
if (v.getCreated().compareTo(cal) > 0) continue;
return v;
}
return null;
}
public static enum TYPE {
ADDED,
REMOVED,
MOVED,
SAME;
private TYPE() {
}
}
}