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

File AbstractXMLSerializer.java

 

Coverage histogram

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

Code metrics

0
20
2
1
95
50
5
0.25
10
2
2.5

Classes

Class Line # Actions
AbstractXMLSerializer 47 20 0% 5 3
0.863636486.4%
 

Contributing tests

This file is covered by 6 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.ByteArrayInputStream;
23    import java.io.ByteArrayOutputStream;
24    import java.io.IOException;
25    import java.io.InputStream;
26   
27    import org.dom4j.Document;
28    import org.dom4j.DocumentException;
29    import org.dom4j.io.OutputFormat;
30    import org.dom4j.io.SAXReader;
31    import org.xml.sax.SAXException;
32    import org.xwiki.component.annotation.ComponentRole;
33    import org.xwiki.store.serialization.xml.XMLSerializer;
34   
35    /**
36    * A Serializer which converts objects into XML Elements and back.
37    *
38    * @param <R> The class of object which the serializer can serialize (what it requires).
39    * @param <P> The class of object which will be provided by this serializer when it parses data.
40    * @version $Id: 9190cbe2d0953b5785682045b78dec1980c9bd98 $
41    * @since 3.0M2
42    */
43    // Note: We cannot replace @ComponentRole with @Role ATM since @Role supports generics and we have
44    // AbstractXMLSerializer<R, P extends R>. Changing it will thus break all code looking up components implementing this
45    // role.
46    @ComponentRole
 
47    public abstract class AbstractXMLSerializer<R, P extends R> implements XMLSerializer<R, P>
48    {
 
49  6 toggle @Override
50    public InputStream serialize(final R object) throws IOException
51    {
52    // This puts everything on the heap for now.
53    // if size becomes a major problem, the alternitive is to fork over another thread
54    // and use a PipedInputStream.
55  6 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
56   
57  6 final XMLWriter writer;
58  6 try {
59    // This should always be UTF-8 because it is a property of the serializer.
60  6 final OutputFormat of = new OutputFormat(" ", true, "UTF-8");
61  6 writer = new XMLWriter(baos, of);
62  6 writer.startDocument();
63    } catch (SAXException e) {
64  0 throw new IOException("Could not open the XML writer.");
65    }
66   
67  6 this.serialize(object, writer);
68   
69  6 try {
70  6 writer.endDocument();
71    } catch (SAXException e) {
72  0 throw new IOException("Could not close the XML writer.");
73    }
74   
75  6 return new ByteArrayInputStream(baos.toByteArray());
76    }
77   
 
78  7 toggle @Override
79    public P parse(final InputStream stream) throws IOException
80    {
81  7 final SAXReader reader = new SAXReader();
82   
83    // Remove nodes generated by indentation.
84  7 reader.setStripWhitespaceText(true);
85  7 reader.setMergeAdjacentText(true);
86   
87  7 final Document domdoc;
88  7 try {
89  7 domdoc = reader.read(stream);
90    } catch (DocumentException e) {
91  0 throw new IOException("Failed to parse XML, probably malformed input.");
92    }
93  7 return this.parse(domdoc.getRootElement());
94    }
95    }