1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.annotation.internal.renderer

File PlainTextNormalizingChainingRenderer.java

 

Coverage histogram

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

Code metrics

14
58
41
1
356
242
51
0.88
1.41
41
1.24

Classes

Class Line # Actions
PlainTextNormalizingChainingRenderer 39 58 0% 51 76
0.3274336232.7%
 

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