1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.annotation.internal.renderer

File AnnotationXHTMLChainingRenderer.java

 

Coverage histogram

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

Code metrics

22
126
45
1
478
349
60
0.48
2.8
45
1.33

Classes

Class Line # Actions
AnnotationXHTMLChainingRenderer 49 126 0% 60 82
0.575129557.5%
 

Contributing tests

This file is covered by 124 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.annotation.internal.renderer;
21   
22    import java.util.HashMap;
23    import java.util.List;
24    import java.util.Map;
25    import java.util.SortedMap;
26   
27    import org.xwiki.annotation.renderer.AnnotationEvent;
28    import org.xwiki.annotation.renderer.ChainingPrintRenderer;
29    import org.xwiki.annotation.renderer.AnnotationEvent.AnnotationEventType;
30    import org.xwiki.rendering.internal.renderer.xhtml.XHTMLChainingRenderer;
31    import org.xwiki.rendering.internal.renderer.xhtml.image.XHTMLImageRenderer;
32    import org.xwiki.rendering.internal.renderer.xhtml.link.XHTMLLinkRenderer;
33    import org.xwiki.rendering.listener.Format;
34    import org.xwiki.rendering.listener.HeaderLevel;
35    import org.xwiki.rendering.listener.MetaData;
36    import org.xwiki.rendering.listener.reference.ResourceReference;
37    import org.xwiki.rendering.listener.ListType;
38    import org.xwiki.rendering.listener.chaining.EventType;
39    import org.xwiki.rendering.listener.chaining.ListenerChain;
40    import org.xwiki.rendering.renderer.printer.XHTMLWikiPrinter;
41    import org.xwiki.rendering.syntax.Syntax;
42   
43    /**
44    * Extends the default XHTML renderer to add handling of annotations.<br>
45    *
46    * @version $Id: 614eb883528a4e60d569b7e1ca55032752d1791f $
47    * @since 2.3M1
48    */
 
49    public class AnnotationXHTMLChainingRenderer extends XHTMLChainingRenderer implements ChainingPrintRenderer
50    {
51    /**
52    * Map to store the events count to be able to identify an event in the emitted events.
53    */
54    private Map<EventType, Integer> eventsCount = new HashMap<EventType, Integer>();
55   
56    /**
57    * The annotations XHTML markers printer, used to handle annotations markers rendering and nesting.
58    */
59    private AnnotationMarkersXHTMLPrinter annotationsMarkerPrinter;
60   
61    /**
62    * Constructor from super class.
63    *
64    * @param linkRenderer the renderer for links
65    * @param imageRenderer the renderer for images
66    * @param listenerChain the listener chain in which to add this listener
67    */
 
68  132 toggle public AnnotationXHTMLChainingRenderer(XHTMLLinkRenderer linkRenderer, XHTMLImageRenderer imageRenderer,
69    ListenerChain listenerChain)
70    {
71  132 super(linkRenderer, imageRenderer, listenerChain);
72    }
73   
74    /**
75    * @return the annotations printer for this print renderer, used to handle annotations markers rendering and nesting
76    */
 
77  8780 toggle public AnnotationMarkersXHTMLPrinter getAnnotationsMarkerPrinter()
78    {
79  8780 if (annotationsMarkerPrinter == null) {
80  132 annotationsMarkerPrinter = new AnnotationMarkersXHTMLPrinter(getPrinter());
81    }
82  8780 return annotationsMarkerPrinter;
83    }
84   
 
85  4262 toggle @Override
86    protected XHTMLWikiPrinter getXHTMLWikiPrinter()
87    {
88  4262 return this.getAnnotationsMarkerPrinter();
89    }
90   
91    /**
92    * @return the annotation generator listener in this chain, holding the annotations state in the current rendering
93    */
 
94  2252 toggle protected AnnotationGeneratorChainingListener getAnnotationGenerator()
95    {
96  2252 return (AnnotationGeneratorChainingListener) getListenerChain().getListener(
97    AnnotationGeneratorChainingListener.class);
98    }
99   
 
100  1716 toggle @Override
101    public void onWord(String word)
102    {
103    // open all annotation markers which are closed and need to be opened
104  1716 getAnnotationsMarkerPrinter().openAllAnnotationMarkers();
105    // get the current annotation events
106  1716 SortedMap<Integer, List<AnnotationEvent>> annEvts = getAnnotationGenerator().getAnnotationEvents();
107  1716 if (annEvts != null && !annEvts.isEmpty()) {
108  137 getAnnotationsMarkerPrinter().printXMLWithAnnotations(word, annEvts);
109    } else {
110  1579 getXHTMLWikiPrinter().printXML(word);
111    }
112    }
113   
 
114  1494 toggle @Override
115    public void onSpace()
116    {
117  1494 getAnnotationsMarkerPrinter().openAllAnnotationMarkers();
118  1494 super.onSpace();
119    }
120   
 
121  506 toggle @Override
122    public void onSpecialSymbol(char symbol)
123    {
124    // open all annotation markers which are closed and need to be opened
125  506 getAnnotationsMarkerPrinter().openAllAnnotationMarkers();
126    // get the annotations state at this point
127  506 SortedMap<Integer, List<AnnotationEvent>> annEvts = getAnnotationGenerator().getAnnotationEvents();
128  506 if (annEvts != null && !annEvts.isEmpty()) {
129  2 getAnnotationsMarkerPrinter().printXMLWithAnnotations("" + symbol, annEvts);
130    } else {
131  504 getXHTMLWikiPrinter().printXML("" + symbol);
132    }
133    }
134   
 
135  22 toggle @Override
136    public void onVerbatim(String content, boolean inline, Map<String, String> parameters)
137    {
138  22 SortedMap<Integer, List<AnnotationEvent>> annEvts = getAnnotationGenerator().getAnnotationEvents();
139  22 if (inline) {
140  12 String ttEltName = "tt";
141  12 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
142  12 getXHTMLWikiPrinter().printXMLStartElement(ttEltName, new String[][] {{"class", "wikimodel-verbatim"}});
143  12 getAnnotationsMarkerPrinter().openAllAnnotationMarkers();
144  12 if (annEvts != null && annEvts.size() > 0) {
145  3 getAnnotationsMarkerPrinter().printXMLWithAnnotations(content, annEvts);
146    } else {
147  9 getXHTMLWikiPrinter().printXML(content);
148    }
149  12 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
150  12 getXHTMLWikiPrinter().printXMLEndElement(ttEltName);
151    } else {
152  10 String preEltName = "pre";
153  10 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
154  10 getXHTMLWikiPrinter().printXMLStartElement(preEltName, parameters);
155  10 getAnnotationsMarkerPrinter().openAllAnnotationMarkers();
156  10 if (annEvts != null && annEvts.size() > 0) {
157  2 getAnnotationsMarkerPrinter().printXMLWithAnnotations(content, annEvts);
158    } else {
159  8 getXHTMLWikiPrinter().printXML(content);
160    }
161  10 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
162  10 getXHTMLWikiPrinter().printXMLEndElement(preEltName);
163    }
164    }
165   
 
166  8 toggle @Override
167    public void onRawText(String text, Syntax syntax)
168    {
169    // FIXME: this is going to be messy, messy because of the raw block syntax which can be HTML and produce very
170    // invalid html.
171   
172  8 SortedMap<Integer, List<AnnotationEvent>> currentBookmarks = getAnnotationGenerator().getAnnotationEvents();
173   
174  8 if (currentBookmarks != null) {
175    // open all annotations that start in this event
176  0 for (Map.Entry<Integer, List<AnnotationEvent>> bookmark : currentBookmarks.entrySet()) {
177  0 for (AnnotationEvent annEvt : bookmark.getValue()) {
178  0 if (annEvt.getType() == AnnotationEventType.START) {
179  0 getAnnotationsMarkerPrinter().beginAnnotation(annEvt.getAnnotation());
180    }
181    }
182    }
183    }
184   
185    // open all annotation markers in case there was any annotation enclosing this block
186  8 getAnnotationsMarkerPrinter().openAllAnnotationMarkers();
187    // Store the raw text as it is ftm. Should handle syntax in the future
188  8 super.onRawText(text, syntax);
189   
190  8 if (currentBookmarks != null) {
191    // close all annotations that start in this event.
192  0 for (Map.Entry<Integer, List<AnnotationEvent>> bookmark : currentBookmarks.entrySet()) {
193  0 for (AnnotationEvent annEvt : bookmark.getValue()) {
194  0 if (annEvt.getType() == AnnotationEventType.END) {
195  0 getAnnotationsMarkerPrinter().endAnnotation(annEvt.getAnnotation());
196    }
197    }
198    }
199    }
200    }
201   
202    /**
203    * {@inheritDoc}
204    *
205    * @since 2.5RC1
206    */
 
207  16 toggle @Override
208    public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
209    {
210  16 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
211  16 super.endLink(reference, freestanding, parameters);
212    }
213   
 
214  0 toggle @Override
215    public void endDefinitionDescription()
216    {
217  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
218  0 super.endDefinitionDescription();
219    }
220   
 
221  0 toggle @Override
222    public void endDefinitionList(Map<String, String> parameters)
223    {
224  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
225  0 super.endDefinitionList(parameters);
226    }
227   
 
228  0 toggle @Override
229    public void endDefinitionTerm()
230    {
231  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
232  0 super.endDefinitionTerm();
233    }
234   
235    /**
236    * {@inheritDoc}
237    *
238    * @since 3.0M2
239    */
 
240  132 toggle @Override
241    public void endDocument(MetaData metadata)
242    {
243  132 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
244  132 super.endDocument(metadata);
245    }
246   
 
247  32 toggle @Override
248    public void endFormat(Format format, Map<String, String> parameters)
249    {
250  32 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
251  32 super.endFormat(format, parameters);
252    }
253   
 
254  0 toggle @Override
255    public void endGroup(Map<String, String> parameters)
256    {
257  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
258  0 super.endGroup(parameters);
259    }
260   
 
261  0 toggle @Override
262    public void endHeader(HeaderLevel level, String id, Map<String, String> parameters)
263    {
264  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
265  0 super.endHeader(level, id, parameters);
266    }
267   
 
268  0 toggle @Override
269    public void endList(ListType type, Map<String, String> parameters)
270    {
271  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
272  0 super.endList(type, parameters);
273    }
274   
 
275  0 toggle @Override
276    public void endListItem()
277    {
278  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
279  0 super.endListItem();
280    }
281   
 
282  138 toggle @Override
283    public void endParagraph(Map<String, String> parameters)
284    {
285  138 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
286  138 super.endParagraph(parameters);
287    }
288   
 
289  0 toggle @Override
290    public void endQuotation(Map<String, String> parameters)
291    {
292  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
293  0 super.endQuotation(parameters);
294    }
295   
 
296  0 toggle @Override
297    public void endQuotationLine()
298    {
299  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
300  0 super.endQuotationLine();
301    }
302   
 
303  0 toggle @Override
304    public void endSection(Map<String, String> parameters)
305    {
306  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
307  0 super.endSection(parameters);
308    }
309   
 
310  4 toggle @Override
311    public void endTable(Map<String, String> parameters)
312    {
313  4 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
314  4 super.endTable(parameters);
315    }
316   
 
317  16 toggle @Override
318    public void endTableCell(Map<String, String> parameters)
319    {
320  16 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
321  16 super.endTableCell(parameters);
322    }
323   
 
324  8 toggle @Override
325    public void endTableHeadCell(Map<String, String> parameters)
326    {
327  8 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
328  8 super.endTableHeadCell(parameters);
329    }
330   
 
331  12 toggle @Override
332    public void endTableRow(Map<String, String> parameters)
333    {
334  12 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
335  12 super.endTableRow(parameters);
336    }
337   
 
338  0 toggle @Override
339    public void beginDefinitionDescription()
340    {
341  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
342  0 super.beginDefinitionDescription();
343    }
344   
 
345  0 toggle @Override
346    public void beginDefinitionList(Map<String, String> parameters)
347    {
348  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
349  0 super.beginDefinitionList(parameters);
350    }
351   
 
352  0 toggle @Override
353    public void beginDefinitionTerm()
354    {
355  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
356  0 super.beginDefinitionTerm();
357    }
358   
 
359  32 toggle @Override
360    public void beginFormat(Format format, Map<String, String> parameters)
361    {
362  32 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
363  32 super.beginFormat(format, parameters);
364    }
365   
 
366  0 toggle @Override
367    public void beginGroup(Map<String, String> parameters)
368    {
369  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
370  0 super.beginGroup(parameters);
371    }
372   
 
373  0 toggle @Override
374    public void beginHeader(HeaderLevel level, String id, Map<String, String> parameters)
375    {
376  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
377  0 super.beginHeader(level, id, parameters);
378    }
379   
380    /**
381    * {@inheritDoc}
382    *
383    * @since 2.5RC1
384    */
 
385  16 toggle @Override
386    public void beginLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
387    {
388  16 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
389  16 super.beginLink(reference, freestanding, parameters);
390    }
391   
 
392  0 toggle @Override
393    public void beginList(ListType type, Map<String, String> parameters)
394    {
395  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
396  0 super.beginList(type, parameters);
397    }
398   
 
399  0 toggle @Override
400    public void beginListItem()
401    {
402  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
403  0 super.beginListItem();
404    }
405   
 
406  138 toggle @Override
407    public void beginParagraph(Map<String, String> parameters)
408    {
409  138 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
410  138 super.beginParagraph(parameters);
411    }
412   
 
413  0 toggle @Override
414    public void beginQuotation(Map<String, String> parameters)
415    {
416  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
417  0 super.beginQuotation(parameters);
418    }
419   
 
420  0 toggle @Override
421    public void beginQuotationLine()
422    {
423  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
424  0 super.beginQuotationLine();
425    }
426   
 
427  0 toggle @Override
428    public void beginSection(Map<String, String> parameters)
429    {
430  0 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
431  0 super.beginSection(parameters);
432    }
433   
 
434  4 toggle @Override
435    public void beginTable(Map<String, String> parameters)
436    {
437  4 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
438  4 super.beginTable(parameters);
439    }
440   
 
441  16 toggle @Override
442    public void beginTableCell(Map<String, String> parameters)
443    {
444  16 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
445  16 super.beginTableCell(parameters);
446    }
447   
 
448  8 toggle @Override
449    public void beginTableHeadCell(Map<String, String> parameters)
450    {
451  8 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
452  8 super.beginTableHeadCell(parameters);
453    }
454   
 
455  12 toggle @Override
456    public void beginTableRow(Map<String, String> parameters)
457    {
458  12 getAnnotationsMarkerPrinter().closeAllAnnotationMarkers();
459  12 super.beginTableRow(parameters);
460    }
461   
462    /**
463    * Helper function to get the current event count of the specified type, and increment it. Similar to a ++ operation
464    * on the Integer mapped to the passed event type.
465    *
466    * @param type the event type
467    * @return the current event count for the passed type.
468    */
 
469  0 toggle protected int getAndIncrement(EventType type)
470    {
471  0 Integer currentCount = eventsCount.get(type);
472  0 if (currentCount == null) {
473  0 currentCount = 0;
474    }
475  0 eventsCount.put(type, currentCount + 1);
476  0 return currentCount;
477    }
478    }