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

File PlainTextChainingRenderer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

24
47
28
1
296
191
45
0.96
1.68
28
1.61

Classes

Class Line # Actions
PlainTextChainingRenderer 44 47 0% 45 3
0.96969797%
 

Contributing tests

This file is covered by 190 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.plain;
21   
22    import java.util.Map;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.xwiki.rendering.listener.HeaderLevel;
26    import org.xwiki.rendering.listener.ListType;
27    import org.xwiki.rendering.listener.chaining.BlockStateChainingListener;
28    import org.xwiki.rendering.listener.chaining.EmptyBlockChainingListener;
29    import org.xwiki.rendering.listener.chaining.ListenerChain;
30    import org.xwiki.rendering.listener.reference.ResourceReference;
31    import org.xwiki.rendering.listener.reference.ResourceType;
32    import org.xwiki.rendering.renderer.AbstractChainingPrintRenderer;
33    import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator;
34    import org.xwiki.rendering.syntax.Syntax;
35   
36    /**
37    * Print only plain text information. For example it remove anything which need a specific syntax a simple plain text
38    * editor can't support like the style, link, image, etc. This renderer is mainly used to generate a simple as possible
39    * label like in a TOC.
40    *
41    * @version $Id: e30a63d8c338fd38b5aca66196f98a882d6ceae6 $
42    * @since 1.9M1
43    */
 
44    public class PlainTextChainingRenderer extends AbstractChainingPrintRenderer
45    {
46    /**
47    * New Line character.
48    */
49    private static final String NL = "\n";
50   
51    /**
52    * True if no empty line has been printed.
53    */
54    private boolean isFirstElementRendered;
55   
56    /**
57    * Generate link label.
58    */
59    private LinkLabelGenerator linkLabelGenerator;
60   
61    /**
62    * The plain text renderer supports when no link label generator is set.
63    *
64    * @param listenerChain the listener chain
65    */
 
66  0 toggle public PlainTextChainingRenderer(ListenerChain listenerChain)
67    {
68  0 this(null, listenerChain);
69    }
70   
71    /**
72    * @param linkLabelGenerator the link label generator
73    * @param listenerChain the listener chain
74    */
 
75  1354630 toggle public PlainTextChainingRenderer(LinkLabelGenerator linkLabelGenerator, ListenerChain listenerChain)
76    {
77  1354644 setListenerChain(listenerChain);
78   
79  1354640 this.linkLabelGenerator = linkLabelGenerator;
80    }
81   
82    // State
83   
84    /**
85    * @return the {@link BlockStateChainingListener} from the listeners chain
86    */
 
87  46818 toggle private BlockStateChainingListener getBlockState()
88    {
89  46818 return (BlockStateChainingListener) getListenerChain().getListener(BlockStateChainingListener.class);
90    }
91   
92    /**
93    * @return the {@link EmptyBlockChainingListener} from the listeners chain
94    */
 
95  4589 toggle protected EmptyBlockChainingListener getEmptyBlockState()
96    {
97  4589 return (EmptyBlockChainingListener) getListenerChain().getListener(EmptyBlockChainingListener.class);
98    }
99   
100    // Events
101   
 
102  17165 toggle @Override
103    public void beginParagraph(Map<String, String> parameters)
104    {
105  17165 printEmptyLine();
106    }
107   
 
108  186726 toggle @Override
109    public void onNewLine()
110    {
111  186726 getPrinter().print(NL);
112    }
113   
 
114  4589 toggle @Override
115    public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
116    {
117  4589 if (getEmptyBlockState().isCurrentContainerBlockEmpty()) {
118  721 ResourceType resourceType = reference.getType();
119  721 if ((ResourceType.DOCUMENT.equals(resourceType) || ResourceType.SPACE.equals(resourceType)
120    || ResourceType.PAGE.equals(resourceType)) && this.linkLabelGenerator != null) {
121  328 getPrinter().print(this.linkLabelGenerator.generate(reference));
122    } else {
123  393 getPrinter().print(reference.getReference());
124    }
125    }
126    }
127   
 
128  6904 toggle @Override
129    public void beginHeader(HeaderLevel level, String id, Map<String, String> parameters)
130    {
131  6904 printEmptyLine();
132    }
133   
 
134  5198449 toggle @Override
135    public void onWord(String word)
136    {
137  5198465 getPrinter().print(word);
138    }
139   
 
140  4285 toggle @Override
141    public void beginList(ListType type, Map<String, String> parameters)
142    {
143  4285 if (getBlockState().getListDepth() == 1) {
144  2385 printEmptyLine();
145    } else {
146  1900 getPrinter().print(NL);
147    }
148    }
149   
 
150  9620 toggle @Override
151    public void beginListItem()
152    {
153  9620 if (getBlockState().getListItemIndex() > 0) {
154  5335 getPrinter().print(NL);
155    }
156   
157    // TODO: maybe add some syntax here like a - or not
158    }
159   
 
160  9620 toggle @Override
161    public void beginListItem(Map<String, String> parameters)
162    {
163  9620 beginListItem();
164    }
165   
 
166  2782244 toggle @Override
167    public void onSpace()
168    {
169  2782251 getPrinter().print(" ");
170    }
171   
 
172  2305081 toggle @Override
173    public void onSpecialSymbol(char symbol)
174    {
175  2305081 getPrinter().print(String.valueOf(symbol));
176    }
177   
 
178  137 toggle @Override
179    public void onHorizontalLine(Map<String, String> parameters)
180    {
181  137 printEmptyLine();
182  137 getPrinter().print("----");
183    }
184   
 
185  132 toggle @Override
186    public void onEmptyLines(int count)
187    {
188  132 getPrinter().print(StringUtils.repeat(NL, count));
189    }
190   
 
191  5957 toggle @Override
192    public void onVerbatim(String content, boolean inline, Map<String, String> parameters)
193    {
194  5957 getPrinter().print(content);
195    }
196   
197    /**
198    * {@inheritDoc}
199    *
200    * @since 2.0RC1
201    */
 
202  271 toggle @Override
203    public void beginDefinitionList(Map<String, String> parameters)
204    {
205  271 if (getBlockState().getDefinitionListDepth() == 1 && !getBlockState().isInList()) {
206  201 printEmptyLine();
207    } else {
208  70 getPrinter().print(NL);
209    }
210    }
211   
 
212  272 toggle @Override
213    public void beginDefinitionTerm()
214    {
215  272 if (getBlockState().getDefinitionListItemIndex() > 0) {
216  1 getPrinter().print(NL);
217    }
218    }
219   
 
220  272 toggle @Override
221    public void beginDefinitionDescription()
222    {
223  272 if (getBlockState().getDefinitionListItemIndex() > 0) {
224  272 getPrinter().print(NL);
225    }
226    }
227   
 
228  202 toggle @Override
229    public void beginQuotationLine()
230    {
231  202 if (getBlockState().getQuotationLineIndex() > 0) {
232  69 getPrinter().print(NL);
233    } else {
234  133 printEmptyLine();
235    }
236    }
237   
 
238  1851 toggle @Override
239    public void beginTable(Map<String, String> parameters)
240    {
241  1851 printEmptyLine();
242    }
243   
 
244  23434 toggle @Override
245    public void beginTableCell(Map<String, String> parameters)
246    {
247  23434 if (getBlockState().getCellCol() > 0) {
248  15382 getPrinter().print("\t");
249    }
250    }
251   
 
252  5134 toggle @Override
253    public void beginTableHeadCell(Map<String, String> parameters)
254    {
255  5134 beginTableCell(parameters);
256    }
257   
 
258  8259 toggle @Override
259    public void beginTableRow(Map<String, String> parameters)
260    {
261  8259 if (getBlockState().getCellRow() > 0) {
262  6480 getPrinter().print(NL);
263    }
264    }
265   
266    /**
267    * {@inheritDoc}
268    *
269    * @since 2.5RC1
270    */
 
271  781 toggle @Override
272    public void onImage(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
273    {
274    // TODO: maybe something could be done here
275    }
276   
 
277  3247 toggle @Override
278    public void onRawText(String text, Syntax syntax)
279    {
280  3247 if (syntax == null || Syntax.PLAIN_1_0.equals(syntax)) {
281  3 getPrinter().print(text);
282    }
283    }
284   
285    /**
286    * Add an empty line to the printer.
287    */
 
288  28774 toggle private void printEmptyLine()
289    {
290  28774 if (this.isFirstElementRendered) {
291  14485 getPrinter().print(NL + NL);
292    } else {
293  14288 this.isFirstElementRendered = true;
294    }
295    }
296    }