1. Project Clover database Tue Dec 20 2016 21:24:09 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
46
27
1
290
186
43
0.93
1.7
27
1.59

Classes

Class Line # Actions
PlainTextChainingRenderer 44 46 0% 43 3
0.9690721696.9%
 

Contributing tests

This file is covered by 188 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: 1908a09138c1a7ad7d1b0e52f5db99ed9aaf5878 $
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  87054 toggle public PlainTextChainingRenderer(LinkLabelGenerator linkLabelGenerator, ListenerChain listenerChain)
76    {
77  87054 setListenerChain(listenerChain);
78   
79  87050 this.linkLabelGenerator = linkLabelGenerator;
80    }
81   
82    // State
83   
84    /**
85    * @return the {@link BlockStateChainingListener} from the listeners chain
86    */
 
87  508 toggle private BlockStateChainingListener getBlockState()
88    {
89  508 return (BlockStateChainingListener) getListenerChain().getListener(BlockStateChainingListener.class);
90    }
91   
92    /**
93    * @return the {@link EmptyBlockChainingListener} from the listeners chain
94    */
 
95  145 toggle protected EmptyBlockChainingListener getEmptyBlockState()
96    {
97  145 return (EmptyBlockChainingListener) getListenerChain().getListener(EmptyBlockChainingListener.class);
98    }
99   
100    // Events
101   
 
102  5436 toggle @Override
103    public void beginParagraph(Map<String, String> parameters)
104    {
105  5436 printEmptyLine();
106    }
107   
 
108  5708 toggle @Override
109    public void onNewLine()
110    {
111  5708 getPrinter().print(NL);
112    }
113   
 
114  145 toggle @Override
115    public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
116    {
117  145 if (getEmptyBlockState().isCurrentContainerBlockEmpty()) {
118  49 ResourceType resourceType = reference.getType();
119  49 if ((ResourceType.DOCUMENT.equals(resourceType) || ResourceType.SPACE.equals(resourceType))
120    && this.linkLabelGenerator != null) {
121  37 getPrinter().print(this.linkLabelGenerator.generate(reference));
122    } else {
123  12 getPrinter().print(reference.getReference());
124    }
125    }
126    }
127   
 
128  86 toggle @Override
129    public void beginHeader(HeaderLevel level, String id, Map<String, String> parameters)
130    {
131  86 printEmptyLine();
132    }
133   
 
134  413535 toggle @Override
135    public void onWord(String word)
136    {
137  413533 getPrinter().print(word);
138    }
139   
 
140  141 toggle @Override
141    public void beginList(ListType type, Map<String, String> parameters)
142    {
143  141 if (getBlockState().getListDepth() == 1) {
144  52 printEmptyLine();
145    } else {
146  89 getPrinter().print(NL);
147    }
148    }
149   
 
150  208 toggle @Override
151    public void beginListItem()
152    {
153  208 if (getBlockState().getListItemIndex() > 0) {
154  67 getPrinter().print(NL);
155    }
156   
157    // TODO: maybe add some syntax here like a - or not
158    }
159   
 
160  414124 toggle @Override
161    public void onSpace()
162    {
163  414122 getPrinter().print(" ");
164    }
165   
 
166  336158 toggle @Override
167    public void onSpecialSymbol(char symbol)
168    {
169  336157 getPrinter().print(String.valueOf(symbol));
170    }
171   
 
172  2 toggle @Override
173    public void onHorizontalLine(Map<String, String> parameters)
174    {
175  2 printEmptyLine();
176  2 getPrinter().print("----");
177    }
178   
 
179  78 toggle @Override
180    public void onEmptyLines(int count)
181    {
182  78 getPrinter().print(StringUtils.repeat(NL, count));
183    }
184   
 
185  6 toggle @Override
186    public void onVerbatim(String content, boolean inline, Map<String, String> parameters)
187    {
188  6 getPrinter().print(content);
189    }
190   
191    /**
192    * {@inheritDoc}
193    *
194    * @since 2.0RC1
195    */
 
196  7 toggle @Override
197    public void beginDefinitionList(Map<String, String> parameters)
198    {
199  7 if (getBlockState().getDefinitionListDepth() == 1 && !getBlockState().isInList()) {
200  3 printEmptyLine();
201    } else {
202  4 getPrinter().print(NL);
203    }
204    }
205   
 
206  8 toggle @Override
207    public void beginDefinitionTerm()
208    {
209  8 if (getBlockState().getDefinitionListItemIndex() > 0) {
210  1 getPrinter().print(NL);
211    }
212    }
213   
 
214  8 toggle @Override
215    public void beginDefinitionDescription()
216    {
217  8 if (getBlockState().getDefinitionListItemIndex() > 0) {
218  8 getPrinter().print(NL);
219    }
220    }
221   
 
222  4 toggle @Override
223    public void beginQuotationLine()
224    {
225  4 if (getBlockState().getQuotationLineIndex() > 0) {
226  3 getPrinter().print(NL);
227    } else {
228  1 printEmptyLine();
229    }
230    }
231   
 
232  14 toggle @Override
233    public void beginTable(Map<String, String> parameters)
234    {
235  14 printEmptyLine();
236    }
237   
 
238  98 toggle @Override
239    public void beginTableCell(Map<String, String> parameters)
240    {
241  98 if (getBlockState().getCellCol() > 0) {
242  69 getPrinter().print("\t");
243    }
244    }
245   
 
246  52 toggle @Override
247    public void beginTableHeadCell(Map<String, String> parameters)
248    {
249  52 beginTableCell(parameters);
250    }
251   
 
252  29 toggle @Override
253    public void beginTableRow(Map<String, String> parameters)
254    {
255  29 if (getBlockState().getCellRow() > 0) {
256  15 getPrinter().print(NL);
257    }
258    }
259   
260    /**
261    * {@inheritDoc}
262    *
263    * @since 2.5RC1
264    */
 
265  49 toggle @Override
266    public void onImage(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
267    {
268    // TODO: maybe something could be done here
269    }
270   
 
271  93 toggle @Override
272    public void onRawText(String text, Syntax syntax)
273    {
274  93 if (syntax == null || Syntax.PLAIN_1_0.equals(syntax)) {
275  1 getPrinter().print(text);
276    }
277    }
278   
279    /**
280    * Add an empty line to the printer.
281    */
 
282  5594 toggle private void printEmptyLine()
283    {
284  5595 if (this.isFirstElementRendered) {
285  158 getPrinter().print(NL + NL);
286    } else {
287  5436 this.isFirstElementRendered = true;
288    }
289    }
290    }