1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.xar.internal.property

File ListXarObjectPropertySerializer.java

 

Coverage histogram

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

Code metrics

18
25
3
1
102
61
16
0.64
8.33
3
5.33

Classes

Class Line # Actions
ListXarObjectPropertySerializer 42 25 0% 16 7
0.8478260684.8%
 

Contributing tests

This file is covered by 27 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.xar.internal.property;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Singleton;
26    import javax.xml.stream.XMLStreamConstants;
27    import javax.xml.stream.XMLStreamException;
28    import javax.xml.stream.XMLStreamReader;
29    import javax.xml.stream.XMLStreamWriter;
30   
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.xar.internal.XarObjectPropertySerializer;
33   
34    /**
35    * {@link List} based implementation of {@link XarObjectPropertySerializer}.
36    *
37    * @version $Id: db38719cb34f47c946c4c60bd41ce9b0448ba8d4 $
38    * @since 5.4M1
39    */
40    @Component(hints = {"StaticList", "DBList", "DBTreeList", "Page" })
41    @Singleton
 
42    public class ListXarObjectPropertySerializer implements XarObjectPropertySerializer
43    {
44    /**
45    * The local name of the <code>value</code> XML element.
46    */
47    public static final String ELEMENT_VALUE = "value";
48   
 
49  7537 toggle @Override
50    public Object read(XMLStreamReader reader) throws XMLStreamException
51    {
52  7537 StringBuilder content = new StringBuilder();
53   
54  14218 for (int eventType = reader.next(); eventType != XMLStreamConstants.END_ELEMENT; eventType = reader.next()) {
55  6834 if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
56    || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
57  6681 content.append(reader.getText());
58  153 } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
59    || eventType == XMLStreamConstants.COMMENT) {
60    // skipping
61  153 } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
62  0 throw new XMLStreamException("unexpected end of document when reading element text content");
63  153 } else if (eventType == XMLStreamConstants.START_ELEMENT) {
64  153 return readList(reader);
65    } else {
66  0 throw new XMLStreamException("Unexpected event type " + eventType, reader.getLocation());
67    }
68    }
69   
70  7384 return content.toString();
71    }
72   
 
73  153 toggle private List<String> readList(XMLStreamReader reader) throws XMLStreamException
74    {
75  153 List<String> list = new ArrayList<String>();
76   
77  539 for (; reader.getEventType() == XMLStreamConstants.START_ELEMENT; reader.nextTag()) {
78  386 reader.require(XMLStreamConstants.START_ELEMENT, null, ELEMENT_VALUE);
79   
80  386 list.add(reader.getElementText());
81    }
82   
83  153 return list;
84    }
85   
 
86  19897 toggle @Override
87    public void write(XMLStreamWriter writer, Object value) throws XMLStreamException
88    {
89  19897 if (value instanceof List) {
90  2988 List<String> list = (List<String>) value;
91  2988 for (String element : list) {
92  1033 if (value != null) {
93  1033 writer.writeStartElement(ELEMENT_VALUE);
94  1033 writer.writeCharacters(element);
95  1033 writer.writeEndElement();
96    }
97    }
98  16909 } else if (value != null) {
99  16909 writer.writeCharacters(value.toString());
100    }
101    }
102    }