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

File AttachmentListMetadataSerializer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
15
4
1
132
62
6
0.4
3.75
4
1.5

Classes

Class Line # Actions
AttachmentListMetadataSerializer 55 15 0% 6 5
0.782608778.3%
 

Contributing tests

This file is covered by 5 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.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    * A serializer for saving the metadata from a list of XWikiAttachments.
39    * the format is:
40    * <attachment-list serializer="attachment-archive-meta/1.0">
41    * <attachment>
42    * (XML formatted meta data of first version)
43    * </attachment>
44    * <attachment>
45    * (XML formatted meta data of second version)
46    * </attachment>
47    * </attachment-list>
48    *
49    * @version $Id: 68a5a6c5751f783934ebfcce9d237693a0b19b18 $
50    * @since 3.0M2
51    */
52    @Component
53    @Named("attachment-list-meta/1.0")
54    @Singleton
 
55    public class AttachmentListMetadataSerializer
56    extends AbstractXMLSerializer<List<XWikiAttachment>, List<XWikiAttachment>>
57    {
58    /**
59    * The root element for serialized element.
60    */
61    private static final String ROOT_ELEMENT_NAME = "attachment-list";
62   
63    /**
64    * Root node paramter which must be present in order to attempt parsing.
65    */
66    private static final String SERIALIZER_PARAM = "serializer";
67   
68    /**
69    * Value of SERIALIZER_PARAM must be this in order to continue parsing.
70    */
71    private static final String THIS_SERIALIZER = "attachment-list-meta/1.0";
72   
73    /**
74    * Needed to serialize/parse the individual attachments.
75    */
76    @Inject
77    @Named("attachment-meta/1.0")
78    private XMLSerializer<XWikiAttachment, XWikiAttachment> attachSerializer;
79   
80    /**
81    * Default constructor. For component manager.
82    */
 
83  0 toggle public AttachmentListMetadataSerializer()
84    {
85    // Do nothing.
86    }
87   
88    /**
89    * Testing Constructor.
90    * Dependencied specified.
91    *
92    * @param attachSerializer the serializer used to serialize/parse the individual attachments.
93    */
 
94  5 toggle public AttachmentListMetadataSerializer(
95    final XMLSerializer<XWikiAttachment, XWikiAttachment> attachSerializer)
96    {
97  5 this.attachSerializer = attachSerializer;
98    }
99   
 
100  4 toggle @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   
 
117  4 toggle @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    }