1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.doc; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.Reader; |
24 |
|
import java.io.StringReader; |
25 |
|
import java.util.Date; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.ArrayUtils; |
28 |
|
import org.apache.commons.lang3.StringUtils; |
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.slf4j.LoggerFactory; |
31 |
|
import org.suigeneris.jrcs.rcs.Archive; |
32 |
|
import org.suigeneris.jrcs.rcs.Version; |
33 |
|
import org.suigeneris.jrcs.rcs.impl.Node; |
34 |
|
import org.suigeneris.jrcs.util.ToString; |
35 |
|
|
36 |
|
import com.xpn.xwiki.XWikiContext; |
37 |
|
import com.xpn.xwiki.XWikiException; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
|
|
|
| 74.8% |
Uncovered Elements: 33 (131) |
Complexity: 42 |
Complexity Density: 0.51 |
|
44 |
|
public class XWikiAttachmentArchive implements Cloneable |
45 |
|
{ |
46 |
|
|
47 |
|
private static final String GENERIC_EXCEPTION_MESSAGE = |
48 |
|
"Exception while manipulating the archive for attachment {0}"; |
49 |
|
|
50 |
|
|
51 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(XWikiAttachmentArchive.class); |
52 |
|
|
53 |
|
|
54 |
|
private XWikiAttachment attachment; |
55 |
|
|
56 |
|
|
57 |
|
private Archive archive; |
58 |
|
|
59 |
|
|
60 |
|
@return |
61 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
62 |
2192 |
public long getId()... |
63 |
|
{ |
64 |
2192 |
return this.attachment.getId(); |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@param |
71 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
72 |
871 |
public void setId(final long id)... |
73 |
|
{ |
74 |
|
|
75 |
|
} |
76 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
77 |
442 |
@Override... |
78 |
|
public Object clone() |
79 |
|
{ |
80 |
442 |
XWikiAttachmentArchive attachmentarchive = null; |
81 |
442 |
try { |
82 |
442 |
attachmentarchive = getClass().newInstance(); |
83 |
|
} catch (Exception e) { |
84 |
|
|
85 |
0 |
LOGGER.error("Error while attachmentArchive.clone()", e); |
86 |
|
} |
87 |
|
|
88 |
442 |
attachmentarchive.setAttachment(getAttachment()); |
89 |
442 |
attachmentarchive.setRCSArchive(getRCSArchive()); |
90 |
|
|
91 |
442 |
return attachmentarchive; |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
@deprecated |
96 |
|
@return |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
98 |
461 |
@Deprecated... |
99 |
|
public Archive getRCSArchive() |
100 |
|
{ |
101 |
461 |
return this.archive; |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
@deprecated |
106 |
|
@param |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
443 |
@Deprecated... |
109 |
|
public void setRCSArchive(final Archive archive) |
110 |
|
{ |
111 |
443 |
this.archive = archive; |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
@return |
118 |
|
@throws |
119 |
|
@since |
120 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
121 |
22 |
public String getArchiveAsString() throws XWikiException... |
122 |
|
{ |
123 |
22 |
if (this.archive == null) { |
124 |
0 |
return ""; |
125 |
|
} else { |
126 |
22 |
return this.archive.toString(Archive.RCS_NEWLINE); |
127 |
|
} |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@param |
134 |
|
@return |
135 |
|
@throws |
136 |
|
@since |
137 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
138 |
0 |
public String getArchiveAsString(final XWikiContext context) throws XWikiException... |
139 |
|
{ |
140 |
0 |
if (this.archive == null) { |
141 |
0 |
if (context != null) { |
142 |
0 |
updateArchive(context); |
143 |
|
} |
144 |
|
} |
145 |
|
|
146 |
0 |
return getArchiveAsString(); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
@return |
153 |
|
|
154 |
|
@throws |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
156 |
883 |
public byte[] getArchive() throws XWikiException... |
157 |
|
{ |
158 |
883 |
if (this.archive == null) { |
159 |
774 |
return new byte[0]; |
160 |
|
} else { |
161 |
109 |
return this.archive.toByteArray(); |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
@param |
169 |
|
@return |
170 |
|
@throws |
171 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
172 |
0 |
public byte[] getArchive(final XWikiContext context) throws XWikiException... |
173 |
|
{ |
174 |
0 |
if (this.archive == null) { |
175 |
0 |
if (context != null) { |
176 |
0 |
updateArchive(context); |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
0 |
return getArchive(); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
@param |
187 |
|
@throws |
188 |
|
|
|
|
| 55.6% |
Uncovered Elements: 4 (9) |
Complexity: 5 |
Complexity Density: 0.71 |
|
189 |
40 |
public void setArchive(final byte[] data) throws XWikiException... |
190 |
|
{ |
191 |
40 |
if (ArrayUtils.isEmpty(data)) { |
192 |
0 |
this.archive = null; |
193 |
|
} else { |
194 |
40 |
try (ByteArrayInputStream is = new ByteArrayInputStream(data)) { |
195 |
40 |
this.archive = new Archive(getAttachment().getFilename(), is); |
196 |
|
} catch (Exception e) { |
197 |
0 |
Object[] args = {getAttachment().getFilename()}; |
198 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
199 |
|
XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT, GENERIC_EXCEPTION_MESSAGE, e, args); |
200 |
|
} |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
@param |
208 |
|
@throws |
209 |
|
@since |
210 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 5 |
Complexity Density: 0.71 |
|
211 |
15 |
public void setArchive(final String data) throws XWikiException... |
212 |
|
{ |
213 |
15 |
if (StringUtils.isEmpty(data)) { |
214 |
14 |
this.archive = null; |
215 |
|
} else { |
216 |
1 |
try (Reader reader = new StringReader(data)) { |
217 |
1 |
this.archive = new Archive(getAttachment().getFilename(), reader); |
218 |
|
} catch (Exception e) { |
219 |
0 |
Object[] args = {getAttachment().getFilename()}; |
220 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
221 |
|
XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT, GENERIC_EXCEPTION_MESSAGE, e, args); |
222 |
|
} |
223 |
|
} |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
@param |
230 |
|
@param |
231 |
|
@throws |
232 |
|
@deprecated@link |
233 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
234 |
0 |
@Deprecated... |
235 |
|
public void updateArchive(final byte[] data, final XWikiContext context) throws XWikiException |
236 |
|
{ |
237 |
0 |
updateArchive(context); |
238 |
|
} |
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
@param |
244 |
|
@throws |
245 |
|
@since |
246 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
247 |
57 |
public void updateArchive(final XWikiContext context) throws XWikiException... |
248 |
|
{ |
249 |
57 |
try { |
250 |
57 |
this.attachment.incrementVersion(); |
251 |
57 |
this.attachment.setDate(new Date()); |
252 |
57 |
final Object[] lines = ToString.stringToArray(this.attachment.toStringXML(true, false, context)); |
253 |
|
|
254 |
57 |
if (this.archive != null) { |
255 |
7 |
this.archive.addRevision(lines, ""); |
256 |
|
} else { |
257 |
50 |
this.archive = new Archive(lines, getAttachment().getFilename(), getAttachment().getVersion()); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
57 |
this.archive.findNode(this.archive.getRevisionVersion()).setAuthor("xwiki"); |
263 |
|
} catch (Exception e) { |
264 |
0 |
Object[] args = {getAttachment().getFilename()}; |
265 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
266 |
|
XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT, GENERIC_EXCEPTION_MESSAGE, e, args); |
267 |
|
} |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
|
@return |
272 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
273 |
592 |
public XWikiAttachment getAttachment()... |
274 |
|
{ |
275 |
592 |
return this.attachment; |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
@param |
283 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
284 |
1383 |
public void setAttachment(final XWikiAttachment attachment)... |
285 |
|
{ |
286 |
1383 |
this.attachment = attachment; |
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
|
@return |
291 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
292 |
3 |
public Version[] getVersions()... |
293 |
|
{ |
294 |
3 |
final Archive rcsArchive = getRCSArchive(); |
295 |
|
|
296 |
3 |
Version[] versions; |
297 |
3 |
if (rcsArchive != null) { |
298 |
2 |
final Node[] nodes = rcsArchive.changeLog(); |
299 |
2 |
versions = new Version[nodes.length]; |
300 |
8 |
for (int i = 0; i < nodes.length; i++) { |
301 |
6 |
versions[i] = nodes[i].getVersion(); |
302 |
|
} |
303 |
|
} else { |
304 |
|
|
305 |
1 |
versions = new Version[] {this.attachment.getRCSVersion()}; |
306 |
|
} |
307 |
|
|
308 |
3 |
return versions; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
@param |
315 |
|
@param |
316 |
|
@param |
317 |
|
@return |
318 |
|
@throws |
319 |
|
|
|
|
| 93.8% |
Uncovered Elements: 2 (32) |
Complexity: 7 |
Complexity Density: 0.32 |
|
320 |
16 |
public XWikiAttachment getRevision(final XWikiAttachment attachment, final String rev, final XWikiContext context)... |
321 |
|
throws XWikiException |
322 |
|
{ |
323 |
16 |
try { |
324 |
16 |
final Archive rcsArchive = getRCSArchive(); |
325 |
|
|
326 |
16 |
if (rcsArchive == null) { |
327 |
|
|
328 |
2 |
return this.attachment.getVersion().equals(rev) ? this.attachment : null; |
329 |
|
} |
330 |
|
|
331 |
14 |
final Version version = rcsArchive.getRevisionVersion(rev); |
332 |
14 |
if (version == null) { |
333 |
|
|
334 |
1 |
return null; |
335 |
|
} |
336 |
13 |
final Object[] lines = rcsArchive.getRevision(version); |
337 |
13 |
final StringBuilder content = new StringBuilder(); |
338 |
239 |
for (int i = 0; i < lines.length; i++) { |
339 |
226 |
String line = lines[i].toString(); |
340 |
226 |
content.append(line); |
341 |
226 |
if (i != lines.length - 1) { |
342 |
213 |
content.append("\n"); |
343 |
|
} |
344 |
|
} |
345 |
|
|
346 |
13 |
final String scontent = content.toString(); |
347 |
13 |
final XWikiAttachment revattach = new XWikiAttachment(); |
348 |
13 |
revattach.fromXML(scontent); |
349 |
13 |
revattach.setDoc(attachment.getDoc()); |
350 |
13 |
revattach.setVersion(rev); |
351 |
13 |
return revattach; |
352 |
|
} catch (Exception e) { |
353 |
0 |
final Object[] args = {attachment.getFilename()}; |
354 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
355 |
|
XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT, GENERIC_EXCEPTION_MESSAGE, e, args); |
356 |
|
} |
357 |
|
} |
358 |
|
} |