1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.serialization.xml.internal; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.dom4j.Element; |
31 |
|
import org.dom4j.dom.DOMElement; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.store.serialization.xml.XMLSerializer; |
34 |
|
|
35 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("attachment-list-meta/1.0") |
54 |
|
@Singleton |
|
|
| 78.3% |
Uncovered Elements: 5 (23) |
Complexity: 6 |
Complexity Density: 0.4 |
|
55 |
|
public class AttachmentListMetadataSerializer |
56 |
|
extends AbstractXMLSerializer<List<XWikiAttachment>, List<XWikiAttachment>> |
57 |
|
{ |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private static final String ROOT_ELEMENT_NAME = "attachment-list"; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private static final String SERIALIZER_PARAM = "serializer"; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
private static final String THIS_SERIALIZER = "attachment-list-meta/1.0"; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@Inject |
77 |
|
@Named("attachment-meta/1.0") |
78 |
|
private XMLSerializer<XWikiAttachment, XWikiAttachment> attachSerializer; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
83 |
0 |
public AttachmentListMetadataSerializer()... |
84 |
|
{ |
85 |
|
|
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
5 |
public AttachmentListMetadataSerializer(... |
95 |
|
final XMLSerializer<XWikiAttachment, XWikiAttachment> attachSerializer) |
96 |
|
{ |
97 |
5 |
this.attachSerializer = attachSerializer; |
98 |
|
} |
99 |
|
|
|
|
| 66.7% |
Uncovered Elements: 4 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
100 |
4 |
@Override... |
101 |
|
public List<XWikiAttachment> parse(final Element docel) throws IOException |
102 |
|
{ |
103 |
4 |
if (!ROOT_ELEMENT_NAME.equals(docel.getName())) { |
104 |
0 |
throw new IOException("XML not recognizable as archive metadata, expecting <archive> tag"); |
105 |
|
} |
106 |
4 |
if (!THIS_SERIALIZER.equals(docel.attribute(SERIALIZER_PARAM).getValue())) { |
107 |
0 |
throw new IOException("Cannot parse this attachment archive metadata, it was saved with a " |
108 |
|
+ "different serializer."); |
109 |
|
} |
110 |
4 |
final List<XWikiAttachment> attachments = new ArrayList<XWikiAttachment>(docel.elements().size()); |
111 |
4 |
for (Element attach : ((List<Element>) docel.elements())) { |
112 |
12 |
attachments.add(this.attachSerializer.parse(attach)); |
113 |
|
} |
114 |
4 |
return attachments; |
115 |
|
} |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
117 |
4 |
@Override... |
118 |
|
public void serialize(final List<XWikiAttachment> attachments, |
119 |
|
final XMLWriter writer) |
120 |
|
throws IOException |
121 |
|
{ |
122 |
4 |
final Element docel = new DOMElement(ROOT_ELEMENT_NAME); |
123 |
4 |
docel.addAttribute(SERIALIZER_PARAM, THIS_SERIALIZER); |
124 |
4 |
writer.writeOpen(docel); |
125 |
|
|
126 |
4 |
for (XWikiAttachment attachment : attachments) { |
127 |
12 |
this.attachSerializer.serialize(attachment, writer); |
128 |
|
} |
129 |
|
|
130 |
4 |
writer.writeClose(docel); |
131 |
|
} |
132 |
|
} |