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

File DeletedAttachmentMetadataSerializer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

4
16
2
1
121
60
5
0.31
8
2
2.5

Classes

Class Line # Actions
DeletedAttachmentMetadataSerializer 45 16 0% 5 22
0.00%
 

Contributing tests

No tests hitting this source file were found.

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 com.xpn.xwiki.doc.XWikiAttachment;
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29    import org.dom4j.Element;
30    import org.dom4j.dom.DOMElement;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.store.legacy.doc.internal.DeletedFilesystemAttachment;
33    import org.xwiki.store.legacy.doc.internal.MutableDeletedFilesystemAttachment;
34    import org.xwiki.store.serialization.xml.XMLSerializer;
35   
36    /**
37    * A serializer for saving the metadata from a deleted XWikiAttachment.
38    *
39    * @version $Id: ae8ce59a5db3f580d378c7f233591164b288b8c2 $
40    * @since 3.0M3
41    */
42    @Component
43    @Named("deleted-attachment-meta/1.0")
44    @Singleton
 
45    public class DeletedAttachmentMetadataSerializer
46    extends AbstractXMLSerializer<DeletedFilesystemAttachment, MutableDeletedFilesystemAttachment>
47    {
48    /**
49    * The root element for serialized element.
50    */
51    private static final String ROOT_ELEMENT_NAME = "deletedattachment";
52   
53    /**
54    * Root node paramter which must be present in order to attempt parsing.
55    */
56    private static final String SERIALIZER_PARAM = "serializer";
57   
58    /**
59    * Value of SERIALIZER_PARAM must be this in order to continue parsing.
60    */
61    private static final String THIS_SERIALIZER = "deletedattachment-meta/1.0";
62   
63    /**
64    * Interpret a node by this name as the document full name of the deleter's user document.
65    */
66    private static final String DELETER = "deleter";
67   
68    /**
69    * Interpret this node as the date of deletion in seconds from the epoch.
70    */
71    private static final String DATE_DELETED = "datedeleted";
72   
73    /**
74    * Interpret this node as the an attachment to be parsed by the attachment-meta serializer.
75    */
76    private static final String ATTACHMENT = "attachment";
77   
78    /**
79    * Needed to serialize/parse the deleted attachment metadata.
80    */
81    @Inject
82    @Named("attachment-meta/1.0")
83    private XMLSerializer<XWikiAttachment, XWikiAttachment> attachSerializer;
84   
 
85  0 toggle @Override
86    public MutableDeletedFilesystemAttachment parse(final Element docel) throws IOException
87    {
88  0 if (!ROOT_ELEMENT_NAME.equals(docel.getName())) {
89  0 throw new IOException("XML not recognizable as deleted attachment metadata, "
90    + "expecting <deletedattachment> tag");
91    }
92  0 if (docel.attribute(SERIALIZER_PARAM) == null
93    || !THIS_SERIALIZER.equals(docel.attribute(SERIALIZER_PARAM).getValue()))
94    {
95  0 throw new IOException("Cannot parse this deleted attachment metadata, "
96    + "it was saved with a different serializer.");
97    }
98  0 final MutableDeletedFilesystemAttachment out = new MutableDeletedFilesystemAttachment();
99   
100  0 out.setDeleter(docel.element(DELETER).getText());
101  0 out.setDate(new Date(Long.parseLong(docel.element(DATE_DELETED).getText())));
102  0 out.setAttachment(this.attachSerializer.parse(docel.element(ATTACHMENT)), null);
103   
104  0 return out;
105    }
106   
 
107  0 toggle @Override
108    public void serialize(final DeletedFilesystemAttachment delAttach, final XMLWriter writer)
109    throws IOException
110    {
111  0 final Element docel = new DOMElement(ROOT_ELEMENT_NAME);
112  0 docel.addAttribute(SERIALIZER_PARAM, THIS_SERIALIZER);
113  0 writer.writeOpen(docel);
114   
115  0 writer.write(new DOMElement(DELETER).addText(delAttach.getDeleter()));
116  0 writer.write(new DOMElement(DATE_DELETED).addText(delAttach.getDate().getTime() + ""));
117  0 this.attachSerializer.serialize(delAttach.getAttachment(), writer);
118   
119  0 writer.writeClose(docel);
120    }
121    }