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

File DeletedAttachmentIdMappingSerializer.java

 

Coverage histogram

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

Code metrics

4
18
2
1
116
58
5
0.28
9
2
2.5

Classes

Class Line # Actions
DeletedAttachmentIdMappingSerializer 44 18 0% 5 24
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.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    * A serializer for saving the mappings between longs and strings.
36    * This is needed because deleted attachments are retreived by database id style longs.
37    *
38    * @version $Id: b60be42cc7646d6571a22285455bd8109c70a081 $
39    * @since 3.0M3
40    */
41    @Component
42    @Named("deleted-attachment-id-mappings/1.0")
43    @Singleton
 
44    public class DeletedAttachmentIdMappingSerializer
45    extends AbstractXMLSerializer<Map<Long, String>, Map<Long, String>>
46    {
47    /**
48    * The root element for serialized element.
49    */
50    private static final String ROOT_ELEMENT_NAME = "deletedattachmentids";
51   
52    /**
53    * Root node paramter which must be present in order to attempt parsing.
54    */
55    private static final String SERIALIZER_PARAM = "serializer";
56   
57    /**
58    * Value of SERIALIZER_PARAM must be this in order to continue parsing.
59    */
60    private static final String THIS_SERIALIZER = "deleted-attachment-id-mappings/1.0";
61   
62    /**
63    * Interpret a node by this name as an entry in the map.
64    */
65    private static final String ENTRY = "entry";
66   
67    /**
68    * Interpret a node by this name as the long id number in base 10.
69    */
70    private static final String ID = "id";
71   
72    /**
73    * Interpret this node as the path on the filesystem, relitive to the storage location.
74    */
75    private static final String PATH = "path";
76   
 
77  0 toggle @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   
 
98  0 toggle @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    }