1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
|
|
| 81.2% |
Uncovered Elements: 16 (85) |
Complexity: 22 |
Complexity Density: 0.42 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
52 |
4 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
67 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
0 |
public void putHandler(String handlerId, ValueParser< ? > handler)... |
76 |
|
{ |
77 |
0 |
this.handlers.put(handlerId.toLowerCase(), handler); |
78 |
|
} |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
8 |
@Override... |
81 |
|
public MetaData getValue() |
82 |
|
{ |
83 |
8 |
return this.metaData; |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
88 |
8 |
@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 |
|
|
|
|
| 83.9% |
Uncovered Elements: 5 (31) |
Complexity: 9 |
Complexity Density: 0.53 |
|
98 |
14 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
0 |
protected MetaDataParser createMetaDataParser()... |
131 |
|
{ |
132 |
0 |
return new MetaDataParser(this); |
133 |
|
} |
134 |
|
|
|
|
| 88.5% |
Uncovered Elements: 3 (26) |
Complexity: 6 |
Complexity Density: 0.33 |
|
135 |
14 |
@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 |
|
} |