1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.xml.sax

File WemToSax.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

58
108
23
1
349
267
58
0.54
4.7
23
2.52

Classes

Class Line # Actions
WemToSax 37 108 0% 58 85
0.5502645455%
 

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.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 $Id: 61f05821aa7cffb5fdd4f183ef7218130a798bc1 $
35    * @since 4.0M1
36    */
 
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   
 
53  6 toggle public WemToSax(ContentHandler handler)
54    {
55  6 fHandler = handler;
56    }
57   
58    /**
59    * @see org.xwiki.rendering.wikimodel.xml.ITagListener#beginTag(java.lang.String,
60    * java.util.Map, java.util.Map)
61    */
 
62  48 toggle 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 org.xwiki.rendering.wikimodel.xml.ITagListener#endTag(java.lang.String,
95    * java.util.Map, java.util.Map)
96    */
 
97  48 toggle 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   
 
125  48 toggle 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    {
 
152  128 toggle private Map.Entry<String, String> getEntry(int index)
153    {
154  128 return array[index];
155    }
156   
 
157  0 toggle 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   
 
169  0 toggle 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   
 
183  48 toggle public int getLength()
184    {
185  48 return array.length;
186    }
187   
 
188  64 toggle 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   
 
197  128 toggle 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   
 
206  64 toggle public String getType(int index)
207    {
208  64 return "CDATA";
209    }
210   
 
211  0 toggle public String getType(String qName)
212    {
213  0 return "CDATA";
214    }
215   
 
216  0 toggle public String getType(String uri, String localName)
217    {
218  0 return "CDATA";
219    }
220   
 
221  64 toggle 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   
 
231  64 toggle 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   
 
237  64 toggle public String getValue(int index)
238    {
239  64 Entry<String, String> entry = getEntry(index);
240  64 return getValue(entry);
241    }
242   
 
243  0 toggle 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   
 
250  0 toggle 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 tagName
261    * @return
262    */
 
263  160 toggle 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   
 
279  0 toggle 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 org.xwiki.rendering.wikimodel.xml.ITagListener#onCDATA(java.lang.String)
290    */
 
291  0 toggle public void onCDATA(String content)
292    {
293    // FIXME: restore CDATA blocks notification
294    // onText(content);
295    }
296   
297    /**
298    * @see org.xwiki.rendering.wikimodel.xml.ITagListener#onTag(java.lang.String,
299    * java.util.Map, java.util.Map)
300    */
 
301  0 toggle 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 org.xwiki.rendering.wikimodel.xml.ITagListener#onText(java.lang.String)
312    */
 
313  20 toggle 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    // FIXME: add notifications about the found entity
338    // buf.append("&#x" + Integer.toHexString(array[i]) +
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    }