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.Date; |
24 |
|
|
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.dom4j.Element; |
29 |
|
import org.dom4j.dom.DOMElement; |
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
|
32 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@version |
38 |
|
@since |
39 |
|
|
40 |
|
@Component |
41 |
|
@Named("attachment-meta/1.0") |
42 |
|
@Singleton |
|
|
| 86.7% |
Uncovered Elements: 4 (30) |
Complexity: 5 |
Complexity Density: 0.21 |
|
43 |
|
public class AttachmentMetadataSerializer extends AbstractXMLSerializer<XWikiAttachment, XWikiAttachment> |
44 |
|
{ |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
private static final String ROOT_ELEMENT_NAME = "attachment"; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
private static final String SERIALIZER_PARAM = "serializer"; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private static final String THIS_SERIALIZER = "attachment-meta/1.0"; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static final String FILENAME = "filename"; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private static final String FILESIZE = "filesize"; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
private static final String AUTHOR = "author"; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
private static final String VERSION = "version"; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
private static final String COMMENT = "comment"; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
private static final String DATE = "date"; |
89 |
|
|
|
|
| 77.8% |
Uncovered Elements: 4 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
90 |
14 |
@Override... |
91 |
|
public XWikiAttachment parse(final Element docel) throws IOException |
92 |
|
{ |
93 |
14 |
if (!ROOT_ELEMENT_NAME.equals(docel.getName())) { |
94 |
0 |
throw new IOException("XML not recognizable as attachment metadata, expecting <attachment> tag"); |
95 |
|
} |
96 |
14 |
if (docel.attribute(SERIALIZER_PARAM) == null |
97 |
|
|| !THIS_SERIALIZER.equals(docel.attribute(SERIALIZER_PARAM).getValue())) |
98 |
|
{ |
99 |
0 |
throw new IOException("Cannot parse this attachment metadata, it was saved with a different " |
100 |
|
+ "serializer."); |
101 |
|
} |
102 |
14 |
final XWikiAttachment out = new XWikiAttachment(); |
103 |
|
|
104 |
14 |
out.setFilename(docel.element(FILENAME).getText()); |
105 |
14 |
out.setFilesize(Integer.parseInt(docel.element(FILESIZE).getText())); |
106 |
14 |
out.setAuthor(docel.element(AUTHOR).getText()); |
107 |
14 |
out.setVersion(docel.element(VERSION).getText()); |
108 |
14 |
out.setComment(docel.element(COMMENT).getText()); |
109 |
|
|
110 |
14 |
final String sdate = docel.element(DATE).getText(); |
111 |
14 |
final Date date = new Date(Long.parseLong(sdate)); |
112 |
14 |
out.setDate(date); |
113 |
|
|
114 |
14 |
return out; |
115 |
|
} |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
117 |
13 |
@Override... |
118 |
|
public void serialize(final XWikiAttachment attach, final XMLWriter writer) throws IOException |
119 |
|
{ |
120 |
13 |
final Element docel = new DOMElement(ROOT_ELEMENT_NAME); |
121 |
13 |
docel.addAttribute(SERIALIZER_PARAM, THIS_SERIALIZER); |
122 |
13 |
writer.writeOpen(docel); |
123 |
|
|
124 |
13 |
writer.write(new DOMElement(FILENAME).addText(attach.getFilename())); |
125 |
13 |
writer.write(new DOMElement(FILESIZE).addText(attach.getFilesize() + "")); |
126 |
13 |
writer.write(new DOMElement(AUTHOR).addText(attach.getAuthor())); |
127 |
13 |
writer.write(new DOMElement(VERSION).addText(attach.getVersion())); |
128 |
13 |
writer.write(new DOMElement(COMMENT).addText(attach.getComment())); |
129 |
13 |
writer.write(new DOMElement(DATE).addText(attach.getDate().getTime() + "")); |
130 |
|
|
131 |
13 |
writer.writeClose(docel); |
132 |
|
} |
133 |
|
} |