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.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
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 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@version |
39 |
|
@since |
40 |
|
|
41 |
|
@Component |
42 |
|
@Named("deleted-attachment-id-mappings/1.0") |
43 |
|
@Singleton |
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 5 |
Complexity Density: 0.28 |
|
44 |
|
public class DeletedAttachmentIdMappingSerializer |
45 |
|
extends AbstractXMLSerializer<Map<Long, String>, Map<Long, String>> |
46 |
|
{ |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
private static final String ROOT_ELEMENT_NAME = "deletedattachmentids"; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
private static final String SERIALIZER_PARAM = "serializer"; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private static final String THIS_SERIALIZER = "deleted-attachment-id-mappings/1.0"; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final String ENTRY = "entry"; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private static final String ID = "id"; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private static final String PATH = "path"; |
76 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
77 |
0 |
@Override... |
78 |
|
public Map<Long, String> parse(final Element docel) throws IOException |
79 |
|
{ |
80 |
0 |
if (!ROOT_ELEMENT_NAME.equals(docel.getName())) { |
81 |
0 |
throw new IOException("XML not recognizable as attachment metadata, " |
82 |
|
+ "expecting <deletedattachmentids> tag"); |
83 |
|
} |
84 |
0 |
if (docel.attribute(SERIALIZER_PARAM) == null |
85 |
|
|| !THIS_SERIALIZER.equals(docel.attribute(SERIALIZER_PARAM).getValue())) |
86 |
|
{ |
87 |
0 |
throw new IOException("Cannot parse this deleted attachment id mapping, " |
88 |
|
+ "it was saved with a different serializer."); |
89 |
|
} |
90 |
0 |
final Map<Long, String> out = new HashMap<Long, String>(); |
91 |
|
|
92 |
0 |
for (Element entry : ((List<Element>) docel.elements(ENTRY))) { |
93 |
0 |
out.put(Long.valueOf(entry.element(ID).getText()), entry.element(PATH).getText()); |
94 |
|
} |
95 |
0 |
return out; |
96 |
|
} |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
98 |
0 |
@Override... |
99 |
|
public void serialize(final Map<Long, String> map, final XMLWriter writer) |
100 |
|
throws IOException |
101 |
|
{ |
102 |
0 |
final Element docel = new DOMElement(ROOT_ELEMENT_NAME); |
103 |
0 |
docel.addAttribute(SERIALIZER_PARAM, THIS_SERIALIZER); |
104 |
0 |
writer.writeOpen(docel); |
105 |
|
|
106 |
0 |
for (Long id : map.keySet()) { |
107 |
0 |
final Element entry = new DOMElement(ENTRY); |
108 |
0 |
writer.writeOpen(entry); |
109 |
0 |
writer.write(new DOMElement(ID).addText(id.toString())); |
110 |
0 |
writer.write(new DOMElement(PATH).addText(map.get(id))); |
111 |
0 |
writer.writeClose(entry); |
112 |
|
} |
113 |
|
|
114 |
0 |
writer.writeClose(docel); |
115 |
|
} |
116 |
|
} |