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

File DoxiaSinkListener.java

 

Coverage histogram

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

Code metrics

10
75
52
1
445
303
65
0.87
1.44
52
1.25

Classes

Class Line # Actions
DoxiaSinkListener 39 75 0% 65 56
0.591240959.1%
 

Contributing tests

This file is covered by 13 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.internal.renderer.doxia;
21   
22    import java.util.Map;
23   
24    import org.apache.maven.doxia.sink.Sink;
25    import org.xwiki.rendering.listener.Format;
26    import org.xwiki.rendering.listener.HeaderLevel;
27    import org.xwiki.rendering.listener.ListType;
28    import org.xwiki.rendering.listener.Listener;
29    import org.xwiki.rendering.listener.MetaData;
30    import org.xwiki.rendering.listener.reference.ResourceReference;
31    import org.xwiki.rendering.syntax.Syntax;
32   
33    /**
34    * Send Listener events to the Sink (except for Table Rows events which are sent in {@link DoxiaListener}.
35    *
36    * @version $Id: fe21fb7f8b7cb8967c4e11030f402c3a906ef251 $
37    * @since 4.3M1
38    */
 
39    public class DoxiaSinkListener implements Listener
40    {
41    /**
42    * The Doxia Renderer to which to emit events to.
43    */
44    private Sink sink;
45   
46    /**
47    * Since we need to tell Doxia the section level and since XWiki Events only give the level for begin/endHeader
48    * events, we need to remember the header level in order to be able to properly close the section by sending
49    * the correct Doxia event.
50    */
51    private HeaderLevel headerLevel;
52   
53    /**
54    * @param sink the Doxia Renderer to which to emit events to
55    */
 
56  21 toggle public DoxiaSinkListener(Sink sink)
57    {
58  21 this.sink = sink;
59    }
60   
 
61  21 toggle @Override
62    public void beginDocument(MetaData metadata)
63    {
64  21 this.sink.head();
65  21 this.sink.head_();
66  21 this.sink.body();
67    }
68   
 
69  21 toggle @Override
70    public void endDocument(MetaData metadata)
71    {
72  21 this.sink.body_();
73    }
74   
 
75  0 toggle @Override
76    public void beginGroup(Map<String, String> parameters)
77    {
78    // Do nothing since Doxia doesn't support groups
79    }
80   
 
81  0 toggle @Override
82    public void endGroup(Map<String, String> parameters)
83    {
84    // Do nothing since Doxia doesn't support groups
85    }
86   
 
87  2 toggle @Override
88    public void onVerbatim(String content, boolean inline, Map<String, String> parameters)
89    {
90    // TODO: Handle parameters
91  2 this.sink.verbatim(null);
92  2 this.sink.text(content);
93  2 this.sink.verbatim_();
94    }
95   
 
96  10 toggle @Override
97    public void beginFormat(Format format, Map<String, String> parameters)
98    {
99    // TODO: Handle parameters
100  10 switch (format) {
101  6 case BOLD:
102  6 this.sink.bold();
103  6 break;
104  4 case ITALIC:
105  4 this.sink.italic();
106  4 break;
107  0 case STRIKEDOUT:
108    // TODO: Implement when we move to Doxia 1.0 beta 1.
109    // See http://jira.codehaus.org/browse/DOXIA-204
110  0 break;
111  0 case UNDERLINED:
112    // TODO: Implement when we move to Doxia 1.0 beta 1.
113    // See http://jira.codehaus.org/browse/DOXIA-204
114  0 break;
115  0 default:
116    // Unhandled format, don't do anything.
117    }
118    }
119   
 
120  10 toggle @Override
121    public void endFormat(Format format, Map<String, String> parameters)
122    {
123    // TODO: Handle parameters
124  10 switch (format) {
125  6 case BOLD:
126  6 this.sink.bold_();
127  6 break;
128  4 case ITALIC:
129  4 this.sink.italic_();
130  4 break;
131  0 case STRIKEDOUT:
132    // TODO: Implement when we move to Doxia 1.0 beta 1.
133    // See http://jira.codehaus.org/browse/DOXIA-204
134  0 break;
135  0 case UNDERLINED:
136    // TODO: Implement when we move to Doxia 1.0 beta 1.
137    // See http://jira.codehaus.org/browse/DOXIA-204
138  0 break;
139  0 default:
140    // Unhandled format, don't do anything.
141    }
142    }
143   
 
144  10 toggle @Override
145    public void beginList(ListType type, Map<String, String> parameters)
146    {
147  10 if (type == ListType.BULLETED) {
148  10 this.sink.list();
149    } else {
150    // TODO: Handle other numerotations (Roman, etc)
151  0 this.sink.numberedList(Sink.NUMBERING_DECIMAL);
152    }
153    }
154   
 
155  18 toggle @Override
156    public void beginListItem()
157    {
158  18 this.sink.listItem();
159    }
160   
 
161  0 toggle @Override
162    public void beginMacroMarker(String name, Map<String, String> parameters, String content, boolean isInline)
163    {
164    // Don't do anything since Doxia doesn't have macro markers and anyway we shouldn't
165    // do anything.
166    }
167   
 
168  18 toggle @Override
169    public void beginParagraph(Map<String, String> parameters)
170    {
171  18 this.sink.paragraph();
172    }
173   
 
174  12 toggle @Override
175    public void beginSection(Map<String, String> parameters)
176    {
177    // Note: The logic is in beginHeader.
178    }
179   
 
180  12 toggle @Override
181    public void beginHeader(HeaderLevel level, String id, Map<String, String> parameters)
182    {
183    // Doxia has only 5 section levels!
184  12 int levelAsInt = (level.getAsInt() < 6) ? level.getAsInt() : 5;
185  12 this.sink.section(levelAsInt, null);
186  12 this.sink.sectionTitle(levelAsInt, null);
187   
188    // Remember the header level for endSection() handling.
189  12 this.headerLevel = level;
190    }
191   
 
192  10 toggle @Override
193    public void endList(ListType type, Map<String, String> parameters)
194    {
195  10 if (type == ListType.BULLETED) {
196  10 this.sink.list_();
197    } else {
198  0 this.sink.numberedList_();
199    }
200    }
201   
 
202  18 toggle @Override
203    public void endListItem()
204    {
205  18 this.sink.listItem_();
206    }
207   
 
208  0 toggle @Override
209    public void endMacroMarker(String name, Map<String, String> parameters, String content, boolean isInline)
210    {
211    // Don't do anything since Doxia doesn't have macro markers and anyway we shouldn't
212    // do anything.
213    }
214   
 
215  18 toggle @Override
216    public void endParagraph(Map<String, String> parameters)
217    {
218  18 this.sink.paragraph_();
219    }
220   
 
221  12 toggle @Override
222    public void endSection(Map<String, String> parameters)
223    {
224    // Doxia has only 5 section levels!
225  12 int levelAsInt = (this.headerLevel.getAsInt() < 6) ? this.headerLevel.getAsInt() : 5;
226  12 this.sink.section_(levelAsInt);
227    }
228   
 
229  12 toggle @Override
230    public void endHeader(HeaderLevel level, String id, Map<String, String> parameters)
231    {
232    // Doxia has only 5 section levels!
233  12 int levelAsInt = (level.getAsInt() < 6) ? level.getAsInt() : 5;
234  12 this.sink.sectionTitle_(levelAsInt);
235    }
236   
 
237  0 toggle @Override
238    public void onMacro(String id, Map<String, String> parameters, String content, boolean inline)
239    {
240    // Don't do anything since macros have already been transformed so this method
241    // should not be called.
242    }
243   
 
244  0 toggle @Override
245    public void onNewLine()
246    {
247    // TODO: Decide when to generate a line break and when to generate a new line
248   
249    // Since there's no On NewLine event in Doxia we simply generate text
250  0 this.sink.text("\n");
251    }
252   
 
253  74 toggle @Override
254    public void onSpace()
255    {
256    // Since there's no On Space event in Doxia we simply generate text
257  74 this.sink.text(" ");
258    }
259   
 
260  0 toggle @Override
261    public void onSpecialSymbol(char symbol)
262    {
263    // Since there's no On Special Symbol event in Doxia we simply generate text
264  0 this.sink.text("" + symbol);
265    }
266   
 
267  130 toggle @Override
268    public void onWord(String word)
269    {
270  130 this.sink.text(word);
271    }
272   
 
273  0 toggle @Override
274    public void onId(String name)
275    {
276    // TODO: Find out what to do...
277    }
278   
 
279  0 toggle @Override
280    public void onRawText(String text, Syntax syntax)
281    {
282    // TODO: Ensure this is correct. The problem is that Doxia doesn't seem to have a syntax
283    // associated with the raw text so I'm not sure how the renderers (sink in Doxia language)
284    // can decide whether to print it or not.
285  0 this.sink.rawText(text);
286    }
287   
 
288  0 toggle @Override
289    public void onHorizontalLine(Map<String, String> parameters)
290    {
291    // TODO: Handle parameters
292  0 this.sink.horizontalRule();
293    }
294   
 
295  0 toggle @Override
296    public void onEmptyLines(int count)
297    {
298    // TODO: Find what to do...
299    }
300   
 
301  0 toggle @Override
302    public void beginDefinitionList(Map<String, String> parameters)
303    {
304    // TODO: Handle parameters
305  0 this.sink.definitionList();
306    }
307   
 
308  0 toggle @Override
309    public void endDefinitionList(Map<String, String> parameters)
310    {
311    // TODO: Handle parameters
312  0 this.sink.definitionList_();
313    }
314   
 
315  0 toggle @Override
316    public void beginDefinitionTerm()
317    {
318  0 this.sink.definedTerm();
319    }
320   
 
321  0 toggle @Override
322    public void beginDefinitionDescription()
323    {
324  0 this.sink.definition();
325    }
326   
 
327  0 toggle @Override
328    public void endDefinitionTerm()
329    {
330  0 this.sink.definedTerm_();
331    }
332   
 
333  0 toggle @Override
334    public void endDefinitionDescription()
335    {
336  0 this.sink.definition_();
337    }
338   
 
339  0 toggle @Override
340    public void beginQuotation(Map<String, String> parameters)
341    {
342    // TODO: Doxia doesn't seem to have support for quotation... Find out what to do...
343    }
344   
 
345  0 toggle @Override
346    public void endQuotation(Map<String, String> parameters)
347    {
348    // TODO: Doxia doesn't seem to have support for quotation... Find out what to do...
349    }
350   
 
351  0 toggle @Override
352    public void beginQuotationLine()
353    {
354    // TODO: Doxia doesn't seem to have support for quotation... Find out what to do...
355    }
356   
 
357  0 toggle @Override
358    public void endQuotationLine()
359    {
360    // TODO: Doxia doesn't seem to have support for quotation... Find out what to do...
361    }
362   
 
363  3 toggle @Override
364    public void beginTable(Map<String, String> parameters)
365    {
366  3 this.sink.table();
367    }
368   
 
369  12 toggle @Override
370    public void beginTableCell(Map<String, String> parameters)
371    {
372  12 this.sink.tableCell();
373    }
374   
 
375  6 toggle @Override
376    public void beginTableHeadCell(Map<String, String> parameters)
377    {
378  6 this.sink.tableHeaderCell();
379    }
380   
 
381  9 toggle @Override
382    public void beginTableRow(Map<String, String> parameters)
383    {
384  9 this.sink.tableRow();
385    }
386   
 
387  3 toggle @Override
388    public void endTable(Map<String, String> parameters)
389    {
390  3 this.sink.table_();
391    }
392   
 
393  12 toggle @Override
394    public void endTableCell(Map<String, String> parameters)
395    {
396  12 this.sink.tableCell_();
397    }
398   
 
399  6 toggle @Override
400    public void endTableHeadCell(Map<String, String> parameters)
401    {
402  6 this.sink.tableHeaderCell_();
403    }
404   
 
405  9 toggle @Override
406    public void endTableRow(Map<String, String> parameters)
407    {
408  9 this.sink.tableRow_();
409    }
410   
 
411  0 toggle @Override
412    public void beginLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
413    {
414  0 this.sink.link(reference.getReference());
415    }
416   
 
417  0 toggle @Override
418    public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
419    {
420  0 this.sink.link_();
421    }
422   
 
423  0 toggle @Override
424    public void onImage(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
425    {
426  0 this.sink.figure();
427    // TODO: handle special XWiki format for image locations. How do we pass image bits to Doxia?
428    // TODO: Handle parameters
429    // TODO: Handle free standing URI (if supported by Doxia)
430  0 this.sink.figureGraphics(reference.getReference());
431  0 this.sink.figure_();
432    }
433   
 
434  0 toggle @Override
435    public void beginMetaData(MetaData metadata)
436    {
437    // Doxia doesn't support the notion of metadata
438    }
439   
 
440  0 toggle @Override
441    public void endMetaData(MetaData metadata)
442    {
443    // Doxia doesn't support the notion of metadata
444    }
445    }