1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.legacy.doc.internal; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.Comparator; |
26 |
|
import java.util.Date; |
27 |
|
import java.util.List; |
28 |
|
|
29 |
|
import org.suigeneris.jrcs.rcs.Archive; |
30 |
|
import org.suigeneris.jrcs.rcs.Version; |
31 |
|
import org.suigeneris.jrcs.rcs.impl.Node; |
32 |
|
import org.suigeneris.jrcs.util.ToString; |
33 |
|
|
34 |
|
import com.xpn.xwiki.XWikiContext; |
35 |
|
import com.xpn.xwiki.XWikiException; |
36 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
37 |
|
import com.xpn.xwiki.doc.XWikiAttachmentArchive; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 29.9% |
Uncovered Elements: 94 (134) |
Complexity: 32 |
Complexity Density: 0.34 |
|
45 |
|
public class ListAttachmentArchive extends XWikiAttachmentArchive |
46 |
|
{ |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
private static final String GENERIC_EXCEPTION_MESSAGE = |
51 |
|
"Exception while manipulating the archive for attachment {0}"; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
private static final String NOT_IMPLEMENTED_MESSAGE = |
57 |
|
"This function is not available in this implementation."; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private XWikiAttachment attachment; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private final List<XWikiAttachment> revisions = new ArrayList<XWikiAttachment>(); |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@param |
73 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
74 |
0 |
public ListAttachmentArchive(final XWikiAttachment attachment)... |
75 |
|
{ |
76 |
0 |
this.attachment = attachment; |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
|
84 |
|
|
|
|
| 73.3% |
Uncovered Elements: 4 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
85 |
5 |
public ListAttachmentArchive(final List<XWikiAttachment> revisions)... |
86 |
|
{ |
87 |
|
|
88 |
5 |
if (revisions.size() == 0) { |
89 |
0 |
return; |
90 |
|
} |
91 |
|
|
92 |
5 |
this.revisions.addAll(revisions); |
93 |
5 |
Collections.sort(this.revisions, XWikiAttachmentVersionComparitor.INSTANCE); |
94 |
|
|
95 |
|
|
96 |
5 |
long id = revisions.get(0).getId(); |
97 |
5 |
final String firstAttachName = revisions.get(0).getFilename(); |
98 |
|
|
99 |
5 |
for (XWikiAttachment attach : revisions) { |
100 |
15 |
if (attach.getId() != id) { |
101 |
0 |
throw new IllegalArgumentException("Attachment " + attach.getFilename() + " has a " |
102 |
|
+ "different ID than the first attachment ( " |
103 |
|
+ firstAttachName + " ) so they cannot all be " |
104 |
|
+ "revisions of the same attachment."); |
105 |
|
} |
106 |
15 |
attach.setAttachment_archive(this); |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
5 |
this.attachment = this.revisions.get(revisions.size() - 1); |
111 |
|
} |
112 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
113 |
0 |
@Override... |
114 |
|
public Object clone() |
115 |
|
{ |
116 |
0 |
final ListAttachmentArchive out = new ListAttachmentArchive(cloneAttachment(this.attachment)); |
117 |
0 |
out.attachment.setAttachment_archive(out); |
118 |
0 |
for (XWikiAttachment revision : this.revisions) { |
119 |
0 |
final XWikiAttachment revClone = cloneAttachment(revision); |
120 |
0 |
revClone.setAttachment_archive(out); |
121 |
0 |
out.revisions.add(revClone); |
122 |
|
} |
123 |
0 |
return out; |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
@link@link |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
@param |
132 |
|
@return |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
134 |
12 |
private static XWikiAttachment cloneAttachment(final XWikiAttachment original)... |
135 |
|
{ |
136 |
12 |
final XWikiAttachmentArchive arch = original.getAttachment_archive(); |
137 |
12 |
try { |
138 |
12 |
original.setAttachment_archive(null); |
139 |
12 |
return (XWikiAttachment) original.clone(); |
140 |
|
} finally { |
141 |
12 |
original.setAttachment_archive(arch); |
142 |
|
} |
143 |
|
} |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
0 |
@Override... |
146 |
|
@Deprecated |
147 |
|
public Archive getRCSArchive() |
148 |
|
{ |
149 |
0 |
throw new RuntimeException(NOT_IMPLEMENTED_MESSAGE); |
150 |
|
} |
151 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
0 |
@Override... |
153 |
|
@Deprecated |
154 |
|
public void setRCSArchive(final Archive rcsArchive) |
155 |
|
{ |
156 |
0 |
throw new RuntimeException(NOT_IMPLEMENTED_MESSAGE); |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
@param |
163 |
|
@return |
164 |
|
@throws |
165 |
|
|
166 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
167 |
0 |
private Archive toRCS(final XWikiContext context) throws Exception... |
168 |
|
{ |
169 |
0 |
final Version[] versions = this.getVersions(); |
170 |
0 |
Archive rcsArch = null; |
171 |
0 |
for (XWikiAttachment rev : this.revisions) { |
172 |
0 |
final String sdata = rev.toStringXML(true, false, context); |
173 |
0 |
final Object[] lines = ToString.stringToArray(sdata); |
174 |
0 |
if (rcsArch == null) { |
175 |
|
|
176 |
0 |
rcsArch = new Archive(lines, rev.getFilename(), rev.getVersion()); |
177 |
|
} else { |
178 |
0 |
rcsArch.addRevision(lines, ""); |
179 |
|
} |
180 |
|
} |
181 |
0 |
return rcsArch; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
@param |
186 |
|
@throws |
187 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 5 |
Complexity Density: 0.29 |
|
188 |
0 |
private void fromRCS(final Archive rcsArchive) throws Exception... |
189 |
|
{ |
190 |
0 |
if (rcsArchive == null) { |
191 |
0 |
return; |
192 |
|
} |
193 |
|
|
194 |
0 |
final Node[] nodes = rcsArchive.changeLog(); |
195 |
0 |
for (int i = nodes.length - 1; i > -1; i--) { |
196 |
0 |
final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion()); |
197 |
0 |
final StringBuilder content = new StringBuilder(); |
198 |
0 |
for (int j = 0; j < lines.length; j++) { |
199 |
0 |
String line = lines[j].toString(); |
200 |
0 |
content.append(line); |
201 |
0 |
if (j != lines.length - 1) { |
202 |
0 |
content.append("\n"); |
203 |
|
} |
204 |
|
} |
205 |
0 |
final XWikiAttachment rev = new XWikiAttachment(); |
206 |
0 |
rev.fromXML(content.toString()); |
207 |
0 |
rev.setDoc(this.getAttachment().getDoc()); |
208 |
0 |
rev.setAttachment_archive(this); |
209 |
|
|
210 |
|
|
211 |
0 |
rev.setVersion(nodes[i].getVersion().toString()); |
212 |
|
|
213 |
0 |
revisions.add(rev); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
@inheritDoc |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
@see |
224 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
225 |
0 |
@Override... |
226 |
|
public byte[] getArchive() |
227 |
|
{ |
228 |
0 |
return new byte[0]; |
229 |
|
} |
230 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
231 |
0 |
@Override... |
232 |
|
public byte[] getArchive(final XWikiContext context) throws XWikiException |
233 |
|
{ |
234 |
0 |
try { |
235 |
0 |
return this.toRCS(context).toByteArray(); |
236 |
|
} catch (Exception e) { |
237 |
0 |
if (e instanceof XWikiException) { |
238 |
0 |
throw (XWikiException) e; |
239 |
|
} |
240 |
0 |
Object[] args = { getAttachment().getFilename() }; |
241 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
242 |
|
XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT, |
243 |
|
GENERIC_EXCEPTION_MESSAGE, e, args); |
244 |
|
} |
245 |
|
} |
246 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 5 |
Complexity Density: 0.5 |
|
247 |
0 |
@Override... |
248 |
|
public void setArchive(final byte[] data) throws XWikiException |
249 |
|
{ |
250 |
0 |
this.revisions.clear(); |
251 |
|
|
252 |
0 |
if ((data != null) && (data.length == 0)) { |
253 |
0 |
try { |
254 |
0 |
final ByteArrayInputStream is = new ByteArrayInputStream(data); |
255 |
0 |
final Archive rcsArchive = new Archive(getAttachment().getFilename(), is); |
256 |
0 |
this.setRCSArchive(rcsArchive); |
257 |
|
} catch (Exception e) { |
258 |
0 |
if (e instanceof XWikiException) { |
259 |
0 |
throw (XWikiException) e; |
260 |
|
} |
261 |
0 |
Object[] args = { getAttachment().getFilename() }; |
262 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
263 |
|
XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT, |
264 |
|
GENERIC_EXCEPTION_MESSAGE, e, args); |
265 |
|
} |
266 |
|
} |
267 |
|
} |
268 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
269 |
0 |
@Override... |
270 |
|
public void updateArchive(final XWikiContext context) |
271 |
|
{ |
272 |
0 |
this.update(); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
279 |
0 |
private void update()... |
280 |
|
{ |
281 |
0 |
final XWikiAttachment attach = this.getAttachment(); |
282 |
0 |
attach.incrementVersion(); |
283 |
0 |
attach.setDate(new Date()); |
284 |
|
|
285 |
0 |
final XWikiAttachment clone = cloneAttachment(attach); |
286 |
0 |
clone.setAttachment_archive(this); |
287 |
0 |
this.revisions.add(clone); |
288 |
|
} |
289 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
290 |
26 |
@Override... |
291 |
|
public XWikiAttachment getAttachment() |
292 |
|
{ |
293 |
26 |
return this.attachment; |
294 |
|
} |
295 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
296 |
2 |
@Override... |
297 |
|
public void setAttachment(final XWikiAttachment attachment) |
298 |
|
{ |
299 |
2 |
this.attachment = attachment; |
300 |
|
} |
301 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
302 |
8 |
@Override... |
303 |
|
public Version[] getVersions() |
304 |
|
{ |
305 |
8 |
final Version[] versions = new Version[this.revisions.size()]; |
306 |
8 |
int i = this.revisions.size(); |
307 |
8 |
for (XWikiAttachment attach : this.revisions) { |
308 |
24 |
i--; |
309 |
24 |
versions[i] = attach.getRCSVersion(); |
310 |
|
} |
311 |
|
|
312 |
8 |
return versions; |
313 |
|
} |
314 |
|
|
|
|
| 76.9% |
Uncovered Elements: 3 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
315 |
12 |
@Override... |
316 |
|
public XWikiAttachment getRevision(final XWikiAttachment attachment, |
317 |
|
final String rev, |
318 |
|
final XWikiContext context) |
319 |
|
{ |
320 |
12 |
if (rev == null) { |
321 |
0 |
return null; |
322 |
|
} |
323 |
|
|
324 |
12 |
for (XWikiAttachment attach : this.revisions) { |
325 |
24 |
if (rev.equals(attach.getVersion())) { |
326 |
12 |
final XWikiAttachment out = cloneAttachment(attach); |
327 |
12 |
out.setAttachment_archive(this); |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
12 |
out.setDoc(attachment.getDoc()); |
332 |
|
|
333 |
12 |
return out; |
334 |
|
} |
335 |
|
} |
336 |
|
|
337 |
0 |
return null; |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
343 |
|
private static class XWikiAttachmentVersionComparitor implements Comparator<XWikiAttachment> |
344 |
|
{ |
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
public static final XWikiAttachmentVersionComparitor INSTANCE = |
349 |
|
new XWikiAttachmentVersionComparitor(); |
350 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
351 |
10 |
@Override... |
352 |
|
public int compare(final XWikiAttachment a, final XWikiAttachment b) |
353 |
|
{ |
354 |
10 |
return a.getRCSVersion().compareTo(b.getRCSVersion()); |
355 |
|
} |
356 |
|
} |
357 |
|
} |