1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.xdomxml10.internal.parser.parameter

File MetaDataParser.java

 

Coverage histogram

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

Code metrics

24
53
8
1
169
121
22
0.42
6.62
8
2.75

Classes

Class Line # Actions
MetaDataParser 32 53 0% 22 16
0.811764781.2%
 

Contributing tests

This file is covered by 2 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.rendering.xdomxml10.internal.parser.parameter;
21   
22    import java.lang.reflect.Type;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import org.xml.sax.Attributes;
27    import org.xml.sax.SAXException;
28    import org.xml.sax.helpers.DefaultHandler;
29    import org.xwiki.properties.ConverterManager;
30    import org.xwiki.rendering.listener.MetaData;
31   
 
32    public class MetaDataParser extends DefaultHandler implements ValueParser<MetaData>
33    {
34    private Map<String, Type> typeMapping = new HashMap<String, Type>();
35   
36    private Map<String, ValueParser< ? >> handlers = new HashMap<String, ValueParser< ? >>();
37   
38    private MetaData metaData = new MetaData();
39   
40    private StringBuffer stringValue = new StringBuffer();
41   
42    private int level = 0;
43   
44    private String currentEntry;
45   
46    private Type currentType = String.class;
47   
48    private ValueParser< ? > currentParser;
49   
50    private final ConverterManager converter;
51   
 
52  4 toggle public MetaDataParser(ConverterManager converter)
53    {
54  4 this.converter = converter;
55   
56  4 this.typeMapping.put(Integer.class.getSimpleName().toLowerCase(), Integer.class);
57  4 this.typeMapping.put(Long.class.getSimpleName().toLowerCase(), Long.class);
58  4 this.typeMapping.put(Boolean.class.getSimpleName().toLowerCase(), Boolean.class);
59  4 this.typeMapping.put(Double.class.getSimpleName().toLowerCase(), Double.class);
60  4 this.typeMapping.put(Float.class.getSimpleName().toLowerCase(), Float.class);
61  4 this.typeMapping.put(Character.class.getSimpleName().toLowerCase(), Character.class);
62   
63  4 this.handlers.put("stringmap", new CustomParametersParser());
64  4 this.handlers.put("resourcereference", new ResourceReferenceParser());
65    }
66   
 
67  0 toggle public MetaDataParser(MetaDataParser metaDataParser)
68    {
69  0 this.converter = metaDataParser.converter;
70   
71  0 this.typeMapping = metaDataParser.typeMapping;
72  0 this.handlers = metaDataParser.handlers;
73    }
74   
 
75  0 toggle public void putHandler(String handlerId, ValueParser< ? > handler)
76    {
77  0 this.handlers.put(handlerId.toLowerCase(), handler);
78    }
79   
 
80  8 toggle @Override
81    public MetaData getValue()
82    {
83  8 return this.metaData;
84    }
85   
86    // ContentHandler
87   
 
88  8 toggle @Override
89    public void characters(char[] ch, int start, int length) throws SAXException
90    {
91  8 if (this.currentParser != null) {
92  2 this.currentParser.characters(ch, start, length);
93    } else {
94  6 this.stringValue.append(ch, start, length);
95    }
96    }
97   
 
98  14 toggle @Override
99    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
100    {
101  14 if (this.level > 0) {
102  10 if (this.currentParser != null) {
103  2 this.currentParser.startElement(uri, localName, qName, attributes);
104    } else {
105  8 this.currentEntry = qName;
106  8 String name = attributes.getValue("name");
107  8 if (name != null) {
108  0 this.currentEntry = name;
109    }
110  8 String type = attributes.getValue("type");
111  8 if (type != null) {
112  4 this.currentType = this.typeMapping.get(type.toLowerCase());
113  4 if (this.currentType == null) {
114  2 this.currentParser = this.handlers.get(type.toLowerCase());
115  2 if (this.currentParser == null && "metadata".equalsIgnoreCase(type)) {
116  0 this.currentParser = createMetaDataParser();
117    }
118   
119  2 if (this.currentParser != null) {
120  2 this.currentParser.startElement(uri, localName, qName, attributes);
121    }
122    }
123    }
124    }
125    }
126   
127  14 ++this.level;
128    }
129   
 
130  0 toggle protected MetaDataParser createMetaDataParser()
131    {
132  0 return new MetaDataParser(this);
133    }
134   
 
135  14 toggle @Override
136    public void endElement(String uri, String localName, String qName) throws SAXException
137    {
138  14 --this.level;
139   
140  14 if (this.level > 0) {
141  10 if (this.currentParser != null) {
142  4 if (this.level > 1) {
143  2 this.currentParser.endElement(uri, localName, qName);
144    } else {
145  2 this.metaData.addMetaData(this.currentEntry, this.currentParser.getValue());
146  2 this.currentType = null;
147  2 this.currentParser = null;
148    }
149    } else {
150  6 Object value;
151  6 if (this.currentType != null) {
152  6 try {
153  6 value = this.converter.convert(this.currentType, this.stringValue.toString());
154    } catch (Exception e) {
155  0 value = this.stringValue.toString();
156    }
157    } else {
158  0 value = this.stringValue.toString();
159    }
160   
161  6 this.metaData.addMetaData(this.currentEntry, value);
162   
163  6 this.stringValue.setLength(0);
164  6 this.currentType = null;
165  6 this.currentParser = null;
166    }
167    }
168    }
169    }