1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.store.serialization.xml.internal

File AttachmentMetadataSerializer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

4
24
2
1
133
61
5
0.21
12
2
2.5

Classes

Class Line # Actions
AttachmentMetadataSerializer 43 24 0% 5 4
0.866666786.7%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * A serializer for saving the metadata from an XWikiAttachment.
36    *
37    * @version $Id: 0b56a2ea2a76aadbdce704309425f7ddc72b189a $
38    * @since 3.0M2
39    */
40    @Component
41    @Named("attachment-meta/1.0")
42    @Singleton
 
43    public class AttachmentMetadataSerializer extends AbstractXMLSerializer<XWikiAttachment, XWikiAttachment>
44    {
45    /**
46    * The root element for serialized element.
47    */
48    private static final String ROOT_ELEMENT_NAME = "attachment";
49   
50    /**
51    * Root node paramter which must be present in order to attempt parsing.
52    */
53    private static final String SERIALIZER_PARAM = "serializer";
54   
55    /**
56    * Value of SERIALIZER_PARAM must be this in order to continue parsing.
57    */
58    private static final String THIS_SERIALIZER = "attachment-meta/1.0";
59   
60    /**
61    * Interpret a node by this name as the filename of the attachment.
62    */
63    private static final String FILENAME = "filename";
64   
65    /**
66    * Interpret a node by this name as the size of the attachment in bytes.
67    */
68    private static final String FILESIZE = "filesize";
69   
70    /**
71    * Interpret a node by this name as the name of the attachment author.
72    */
73    private static final String AUTHOR = "author";
74   
75    /**
76    * Interpret a node by this name as a string representation of the RCS version of the attachment.
77    */
78    private static final String VERSION = "version";
79   
80    /**
81    * Interpret a node by this name as a comment left by the author on this attachment.
82    */
83    private static final String COMMENT = "comment";
84   
85    /**
86    * Interpret a node by this name as the date the attachment was saved.
87    */
88    private static final String DATE = "date";
89   
 
90  14 toggle @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   
 
117  13 toggle @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    }