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

File PlainTextNormalizingChainingRenderer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

12
55
39
1
341
229
48
0.87
1.41
39
1.23

Classes

Class Line # Actions
PlainTextNormalizingChainingRenderer 38 55 0% 48 69
0.349056634.9%
 

Contributing tests

This file is covered by 49 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.Map;
23   
24    import org.xwiki.annotation.content.AlteredContent;
25    import org.xwiki.annotation.content.ContentAlterer;
26    import org.xwiki.rendering.listener.HeaderLevel;
27    import org.xwiki.rendering.listener.ListType;
28    import org.xwiki.rendering.listener.chaining.ListenerChain;
29    import org.xwiki.rendering.renderer.AbstractChainingPrintRenderer;
30    import org.xwiki.rendering.syntax.Syntax;
31   
32    /**
33    * Plain text renderer that normalizes spaces in the printed text.
34    *
35    * @version $Id: 5244c167ad132a98ebdee65d48bed342d7f61c5d $
36    * @since 2.3M1
37    */
 
38    public class PlainTextNormalizingChainingRenderer extends AbstractChainingPrintRenderer
39    {
40    /**
41    * Normalizer for the content serialized by this listener, to clean all texts such as protected strings in various
42    * events.
43    */
44    private ContentAlterer textCleaner;
45   
46    /**
47    * Flag to signal that the renderer is currently rendering whitespaces (has rendered the first one) and should not
48    * append more. Starting true because we don't want to print beginning spaces.
49    */
50    private boolean isInWhitespace = true;
51   
52    /**
53    * Flag to signal that this renderer has currently printed something. Cache for checking that serializing the
54    * printer would return non zero characters, since serializing the printer at each step can be a bit too much.
55    */
56    private boolean hasPrinted;
57   
58    /**
59    * Builds an abstract plain text normalizing renderer with the passed text cleaner.
60    *
61    * @param textCleaner the text cleaner used to normalize the texts produced by the events
62    * @param listenerChain the listeners chain this listener is part of
63    */
 
64  98 toggle public PlainTextNormalizingChainingRenderer(ContentAlterer textCleaner, ListenerChain listenerChain)
65    {
66  98 this.textCleaner = textCleaner;
67  98 setListenerChain(listenerChain);
68    }
69   
 
70  939 toggle @Override
71    public void onWord(String word)
72    {
73  939 printText(word);
74    }
75   
 
76  216 toggle @Override
77    public void onSpecialSymbol(char symbol)
78    {
79  216 printText("" + symbol);
80    }
81   
 
82  0 toggle @Override
83    public void onVerbatim(String content, boolean inline, Map<String, String> parameters)
84    {
85  0 if (!inline || Character.isWhitespace(content.charAt(0))) {
86    // if there is a space right at the beginning of the verbatim string, or the verbatim string is block, we
87    // need to print a space
88  0 printSpace();
89    }
90   
91  0 AlteredContent cleanedContent = textCleaner.alter(content);
92  0 printText(cleanedContent.getContent().toString());
93   
94  0 if (!inline || Character.isWhitespace(content.charAt(content.length() - 1))) {
95    // print a space after
96  0 printSpace();
97    }
98    }
99   
 
100  0 toggle @Override
101    public void onRawText(String text, Syntax syntax)
102    {
103    // Similar approach to verbatim FTM. In the future, syntax specific cleaner could be used for various syntaxes
104    // (which would do the great job for HTML, for example)
105    // normalize the protected string before adding it to the plain text version
106  0 if (Character.isWhitespace(text.charAt(0))) {
107    // if there is a space right at the beginning of the raw text, we need to print a space
108  0 printSpace();
109    }
110   
111  0 AlteredContent cleanedContent = textCleaner.alter(text);
112  0 printText(cleanedContent.getContent().toString());
113   
114  0 if (Character.isWhitespace(text.charAt(text.length() - 1))) {
115    // if there is a space right at the end of the text, we need to print a space
116  0 printSpace();
117    }
118    }
119   
 
120  828 toggle @Override
121    public void onSpace()
122    {
123  828 printSpace();
124    }
125   
126    /**
127    * Print a space to the renderer's printer.
128    */
 
129  1070 toggle protected void printSpace()
130    {
131    // start printing whitespaces
132  1070 isInWhitespace = true;
133    }
134   
135    /**
136    * Prints a text to the renderer's printer.
137    *
138    * @param text the text to print
139    */
 
140  1155 toggle protected void printText(String text)
141    {
142    // if it's in whitespace and there was something printed before, print the remaining space, and then handle the
143    // current text
144  1155 if (isInWhitespace && hasPrinted) {
145  839 getPrinter().print(" ");
146    }
147  1155 getPrinter().print(text);
148  1155 hasPrinted = true;
149  1155 isInWhitespace = false;
150    }
151   
 
152  0 toggle @Override
153    public void onEmptyLines(int count)
154    {
155  0 if (count > 0) {
156  0 printSpace();
157    }
158    }
159   
 
160  4 toggle @Override
161    public void onNewLine()
162    {
163  4 printSpace();
164    }
165   
 
166  0 toggle @Override
167    public void onHorizontalLine(Map<String, String> parameters)
168    {
169  0 printSpace();
170    }
171   
172    // all next events are block, so spaces need to be printed around
173   
 
174  0 toggle @Override
175    public void beginDefinitionDescription()
176    {
177  0 printSpace();
178    }
179   
 
180  0 toggle @Override
181    public void endDefinitionDescription()
182    {
183  0 printSpace();
184    }
185   
 
186  0 toggle @Override
187    public void beginDefinitionList(Map<String, String> parameters)
188    {
189  0 printSpace();
190    }
191   
 
192  0 toggle @Override
193    public void endDefinitionList(Map<String, String> parameters)
194    {
195  0 printSpace();
196    }
197   
 
198  0 toggle @Override
199    public void beginDefinitionTerm()
200    {
201  0 printSpace();
202    }
203   
 
204  0 toggle @Override
205    public void endDefinitionTerm()
206    {
207  0 printSpace();
208    }
209   
 
210  0 toggle @Override
211    public void beginGroup(Map<String, String> parameters)
212    {
213  0 printSpace();
214    }
215   
 
216  0 toggle @Override
217    public void endGroup(Map<String, String> parameters)
218    {
219  0 printSpace();
220    }
221   
 
222  0 toggle @Override
223    public void beginHeader(HeaderLevel level, String id, Map<String, String> parameters)
224    {
225  0 printSpace();
226    }
227   
 
228  0 toggle @Override
229    public void endHeader(HeaderLevel level, String id, Map<String, String> parameters)
230    {
231  0 printSpace();
232    }
233   
 
234  0 toggle @Override
235    public void beginList(ListType type, Map<String, String> parameters)
236    {
237  0 printSpace();
238    }
239   
 
240  0 toggle @Override
241    public void endList(ListType type, Map<String, String> parameters)
242    {
243  0 printSpace();
244    }
245   
 
246  0 toggle @Override
247    public void beginListItem()
248    {
249  0 printSpace();
250    }
251   
 
252  0 toggle @Override
253    public void endListItem()
254    {
255  0 printSpace();
256    }
257   
 
258  112 toggle @Override
259    public void beginParagraph(Map<String, String> parameters)
260    {
261  112 printSpace();
262    }
263   
 
264  112 toggle @Override
265    public void endParagraph(Map<String, String> parameters)
266    {
267  112 printSpace();
268    }
269   
 
270  0 toggle @Override
271    public void beginQuotation(Map<String, String> parameters)
272    {
273  0 printSpace();
274    }
275   
 
276  0 toggle @Override
277    public void endQuotation(Map<String, String> parameters)
278    {
279  0 printSpace();
280    }
281   
 
282  0 toggle @Override
283    public void beginQuotationLine()
284    {
285  0 printSpace();
286    }
287   
 
288  0 toggle @Override
289    public void endQuotationLine()
290    {
291  0 printSpace();
292    }
293   
 
294  1 toggle @Override
295    public void beginTable(Map<String, String> parameters)
296    {
297  1 printSpace();
298    }
299   
 
300  1 toggle @Override
301    public void endTable(Map<String, String> parameters)
302    {
303  1 printSpace();
304    }
305   
 
306  2 toggle @Override
307    public void beginTableRow(Map<String, String> parameters)
308    {
309  2 printSpace();
310    }
311   
 
312  2 toggle @Override
313    public void endTableRow(Map<String, String> parameters)
314    {
315  2 printSpace();
316    }
317   
 
318  0 toggle @Override
319    public void beginTableHeadCell(Map<String, String> parameters)
320    {
321  0 printSpace();
322    }
323   
 
324  0 toggle @Override
325    public void endTableHeadCell(Map<String, String> parameters)
326    {
327  0 printSpace();
328    }
329   
 
330  4 toggle @Override
331    public void beginTableCell(Map<String, String> parameters)
332    {
333  4 printSpace();
334    }
335   
 
336  4 toggle @Override
337    public void endTableCell(Map<String, String> parameters)
338    {
339  4 printSpace();
340    }
341    }