1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.wikimodel.xml.sax; |
21 |
|
|
22 |
|
import java.util.LinkedHashMap; |
23 |
|
import java.util.Map; |
24 |
|
import java.util.Map.Entry; |
25 |
|
import java.util.logging.Level; |
26 |
|
import java.util.logging.Logger; |
27 |
|
|
28 |
|
import org.xml.sax.Attributes; |
29 |
|
import org.xml.sax.ContentHandler; |
30 |
|
import org.xwiki.rendering.wikimodel.WikiPageUtil; |
31 |
|
import org.xwiki.rendering.wikimodel.xml.ITagListener; |
32 |
|
|
33 |
|
|
34 |
|
@version |
35 |
|
@since |
36 |
|
|
|
|
| 55% |
Uncovered Elements: 85 (189) |
Complexity: 58 |
Complexity Density: 0.54 |
|
37 |
|
public class WemToSax implements ITagListener |
38 |
|
{ |
39 |
|
private static Logger log = Logger.getLogger(WemToSax.class.getName()); |
40 |
|
|
41 |
|
public final static String USER_NS = "http://www.wikimodel.org/ns/user-defined-params#"; |
42 |
|
|
43 |
|
public final static String USER_PREFIX = "u"; |
44 |
|
|
45 |
|
public final static String WEM_NS = "http://www.wikimodel.org/ns/wem#"; |
46 |
|
|
47 |
|
public final static String WEM_PREFIX = "w"; |
48 |
|
|
49 |
|
private int fDepth; |
50 |
|
|
51 |
|
private ContentHandler fHandler; |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
6 |
public WemToSax(ContentHandler handler)... |
54 |
|
{ |
55 |
6 |
fHandler = handler; |
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
|
@see |
60 |
|
|
61 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
62 |
48 |
public void beginTag(... |
63 |
|
String tagName, |
64 |
|
Map<String, String> tagParams, |
65 |
|
Map<String, String> userParams) |
66 |
|
{ |
67 |
48 |
String qualifiedTagName = null; |
68 |
48 |
try { |
69 |
48 |
qualifiedTagName = getQualifiedName(WEM_PREFIX, tagName); |
70 |
48 |
if (fDepth == 0) { |
71 |
6 |
fHandler.startDocument(); |
72 |
6 |
fHandler.startPrefixMapping(WEM_PREFIX, WEM_NS); |
73 |
6 |
fHandler.startPrefixMapping(USER_PREFIX, USER_NS); |
74 |
|
} |
75 |
48 |
Attributes atts = getAttributes(tagParams, userParams); |
76 |
48 |
fHandler.startElement(WEM_NS, tagName, qualifiedTagName, atts); |
77 |
48 |
fDepth++; |
78 |
|
} catch (Throwable t) { |
79 |
0 |
handleError("[BEGIN_TAG]" |
80 |
|
+ " Tag: '" |
81 |
|
+ tagName |
82 |
|
+ "'. QualifiedTagName: '" |
83 |
|
+ qualifiedTagName |
84 |
|
+ "'. " |
85 |
|
+ "TagParams: [" |
86 |
|
+ tagParams |
87 |
|
+ "]. UserParams: [" |
88 |
|
+ userParams |
89 |
|
+ "]", t); |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
@see |
95 |
|
|
96 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
97 |
48 |
public void endTag(... |
98 |
|
String tagName, |
99 |
|
Map<String, String> tagParams, |
100 |
|
Map<String, String> userParams) |
101 |
|
{ |
102 |
48 |
String qualifiedTagName = null; |
103 |
48 |
try { |
104 |
48 |
qualifiedTagName = getQualifiedName(WEM_PREFIX, tagName); |
105 |
48 |
fHandler.endElement(WEM_NS, tagName, qualifiedTagName); |
106 |
48 |
fDepth--; |
107 |
48 |
if (fDepth == 0) { |
108 |
6 |
fHandler.endDocument(); |
109 |
|
} |
110 |
|
} catch (Throwable t) { |
111 |
0 |
handleError("[END_TAG]" |
112 |
|
+ " Tag: '" |
113 |
|
+ tagName |
114 |
|
+ "'. QualifiedTagName: '" |
115 |
|
+ qualifiedTagName |
116 |
|
+ "'. " |
117 |
|
+ "TagParams: [" |
118 |
|
+ tagParams |
119 |
|
+ "]. UserParams: [" |
120 |
|
+ userParams |
121 |
|
+ "]", t); |
122 |
|
} |
123 |
|
} |
124 |
|
|
|
|
| 78.9% |
Uncovered Elements: 4 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
125 |
48 |
private Attributes getAttributes(... |
126 |
|
Map<String, String> tagParams, |
127 |
|
Map<String, String> userParams) |
128 |
|
{ |
129 |
48 |
final Map<String, String> attrs = new LinkedHashMap<String, String>(); |
130 |
48 |
for (Map.Entry<String, String> entry : tagParams.entrySet()) { |
131 |
58 |
String qName = getQualifiedName(WEM_PREFIX, entry.getKey()); |
132 |
58 |
if (qName == null) { |
133 |
0 |
continue; |
134 |
|
} |
135 |
58 |
String value = entry.getValue(); |
136 |
58 |
attrs.put(qName, value); |
137 |
|
} |
138 |
48 |
for (Map.Entry<String, String> entry : userParams.entrySet()) { |
139 |
6 |
String qName = getQualifiedName(USER_PREFIX, entry.getKey()); |
140 |
6 |
if (qName == null) { |
141 |
0 |
continue; |
142 |
|
} |
143 |
6 |
String value = entry.getValue(); |
144 |
6 |
attrs.put(qName, value); |
145 |
|
} |
146 |
48 |
@SuppressWarnings("unchecked") |
147 |
|
final Map.Entry<String, String>[] array = attrs.entrySet().toArray( |
148 |
|
new Map.Entry[attrs.size()]); |
149 |
|
|
150 |
48 |
return new Attributes() |
151 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
128 |
private Map.Entry<String, String> getEntry(int index)... |
153 |
|
{ |
154 |
128 |
return array[index]; |
155 |
|
} |
156 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
157 |
0 |
public int getIndex(String qName)... |
158 |
|
{ |
159 |
0 |
int result = -1; |
160 |
0 |
for (int i = 0; i < array.length; i++) { |
161 |
0 |
if (qName.equals(array[i].getKey())) { |
162 |
0 |
result = i; |
163 |
0 |
break; |
164 |
|
} |
165 |
|
} |
166 |
0 |
return result; |
167 |
|
} |
168 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
169 |
0 |
public int getIndex(String uri, String localName)... |
170 |
|
{ |
171 |
0 |
String prefix = null; |
172 |
0 |
if (WEM_NS.equals(uri)) { |
173 |
0 |
prefix = WEM_PREFIX; |
174 |
0 |
} else if (USER_NS.equals(uri)) { |
175 |
0 |
prefix = USER_PREFIX; |
176 |
|
} |
177 |
0 |
if (prefix == null) { |
178 |
0 |
return -1; |
179 |
|
} |
180 |
0 |
return getIndex(prefix + ":" + localName); |
181 |
|
} |
182 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
183 |
48 |
public int getLength()... |
184 |
|
{ |
185 |
48 |
return array.length; |
186 |
|
} |
187 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
188 |
64 |
public String getLocalName(int index)... |
189 |
|
{ |
190 |
64 |
Map.Entry<String, String> entry = getEntry(index); |
191 |
64 |
if (entry == null) { |
192 |
0 |
return null; |
193 |
|
} |
194 |
64 |
return entry.getKey(); |
195 |
|
} |
196 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
197 |
128 |
public String getQName(int index)... |
198 |
|
{ |
199 |
128 |
if (index < 0 || index >= array.length) { |
200 |
0 |
return null; |
201 |
|
} |
202 |
128 |
Entry<String, String> entry = array[index]; |
203 |
128 |
return entry.getKey(); |
204 |
|
} |
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
64 |
public String getType(int index)... |
207 |
|
{ |
208 |
64 |
return "CDATA"; |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0 |
public String getType(String qName)... |
212 |
|
{ |
213 |
0 |
return "CDATA"; |
214 |
|
} |
215 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
216 |
0 |
public String getType(String uri, String localName)... |
217 |
|
{ |
218 |
0 |
return "CDATA"; |
219 |
|
} |
220 |
|
|
|
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 4 |
Complexity Density: 1 |
|
221 |
64 |
public String getURI(int index)... |
222 |
|
{ |
223 |
64 |
String qName = getQName(index); |
224 |
64 |
if (qName == null) { |
225 |
0 |
return null; |
226 |
|
} |
227 |
6 |
return qName.startsWith(WEM_PREFIX) ? WEM_NS : qName |
228 |
|
.startsWith(USER_PREFIX) ? USER_NS : null; |
229 |
|
} |
230 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 1.5 |
|
231 |
64 |
private String getValue(Entry<String, String> entry)... |
232 |
|
{ |
233 |
64 |
String value = entry != null ? entry.getValue() : null; |
234 |
64 |
return value != null ? value : ""; |
235 |
|
} |
236 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
237 |
64 |
public String getValue(int index)... |
238 |
|
{ |
239 |
64 |
Entry<String, String> entry = getEntry(index); |
240 |
64 |
return getValue(entry); |
241 |
|
} |
242 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
243 |
0 |
public String getValue(String qName)... |
244 |
|
{ |
245 |
0 |
int idx = getIndex(qName); |
246 |
0 |
Entry<String, String> entry = idx >= 0 ? array[idx] : null; |
247 |
0 |
return getValue(entry); |
248 |
|
} |
249 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 1 |
|
250 |
0 |
public String getValue(String uri, String localName)... |
251 |
|
{ |
252 |
0 |
int idx = getIndex(uri, localName); |
253 |
0 |
Entry<String, String> entry = idx >= 0 ? array[idx] : null; |
254 |
0 |
return entry != null ? entry.getValue() : null; |
255 |
|
} |
256 |
|
}; |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
@param |
261 |
|
@return |
262 |
|
|
|
|
| 64.3% |
Uncovered Elements: 5 (14) |
Complexity: 6 |
Complexity Density: 0.75 |
|
263 |
160 |
private String getQualifiedName(String prefix, String tagName)... |
264 |
|
{ |
265 |
160 |
if (tagName == null || tagName.length() == 0) { |
266 |
0 |
return null; |
267 |
|
} |
268 |
160 |
tagName = WikiPageUtil.escapeXmlAttribute(tagName); |
269 |
160 |
tagName = tagName.replaceAll(" ", "-"); |
270 |
160 |
boolean valid = WikiPageUtil.isValidXmlName(tagName, false); |
271 |
160 |
if (!valid) { |
272 |
0 |
return null; |
273 |
|
} |
274 |
160 |
return prefix != null && !"".equals(prefix) |
275 |
|
? prefix + ":" + tagName |
276 |
|
: tagName; |
277 |
|
} |
278 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
279 |
0 |
private void handleError(String s, Throwable e)... |
280 |
|
{ |
281 |
0 |
log.log(Level.SEVERE, s, e); |
282 |
0 |
if (e instanceof Error) { |
283 |
0 |
throw ((Error) e); |
284 |
|
} |
285 |
0 |
throw new RuntimeException(e); |
286 |
|
} |
287 |
|
|
288 |
|
|
289 |
|
@see |
290 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
291 |
0 |
public void onCDATA(String content)... |
292 |
|
{ |
293 |
|
|
294 |
|
|
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
@see |
299 |
|
|
300 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
301 |
0 |
public void onTag(... |
302 |
|
String tagName, |
303 |
|
Map<String, String> tagParams, |
304 |
|
Map<String, String> userParams) |
305 |
|
{ |
306 |
0 |
beginTag(tagName, tagParams, userParams); |
307 |
0 |
endTag(tagName, tagParams, userParams); |
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
|
@see |
312 |
|
|
|
|
| 66.7% |
Uncovered Elements: 10 (30) |
Complexity: 8 |
Complexity Density: 0.44 |
|
313 |
20 |
public void onText(String content)... |
314 |
|
{ |
315 |
20 |
try { |
316 |
20 |
if (content != null) { |
317 |
20 |
char[] array = content.toCharArray(); |
318 |
20 |
int start = 0; |
319 |
20 |
int i; |
320 |
79 |
for (i = 0; i < array.length; i++) { |
321 |
59 |
char c = array[i]; |
322 |
59 |
int ch = c; |
323 |
59 |
if (WikiPageUtil.isValidXmlChar(ch)) { |
324 |
59 |
boolean toEscape = (ch == '\'' |
325 |
|
|| ch == '\"' |
326 |
|
|| ch == '[' |
327 |
|
|| ch == ']' |
328 |
|
|| ch == '&' || ch == '|'); |
329 |
59 |
if (!toEscape) { |
330 |
59 |
continue; |
331 |
|
} |
332 |
|
} |
333 |
0 |
if (i != start) { |
334 |
0 |
fHandler.characters(array, start, i - start); |
335 |
0 |
start = i; |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
} |
341 |
20 |
if (i != start) { |
342 |
20 |
fHandler.characters(array, start, i - start); |
343 |
|
} |
344 |
|
} |
345 |
|
} catch (Throwable e) { |
346 |
0 |
handleError("onText error " + content, e); |
347 |
|
} |
348 |
|
} |
349 |
|
} |