Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
43   279   39   1.65
22   176   0.91   26
26     1.5  
1    
 
  PlainTextChainingRenderer       Line # 43 43 0% 39 7 92.3% 0.9230769
 
  (113)
 
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   
35    /**
36    * Print only plain text information. For example it remove anything which need a specific syntax a simple plain text
37    * editor can't support like the style, link, image, etc. This renderer is mainly used to generate a simple as possible
38    * label like in a TOC.
39    *
40    * @version $Id: a67bcb27f462442da1dfaaa7e90205033fe8a75e $
41    * @since 1.9M1
42    */
 
43    public class PlainTextChainingRenderer extends AbstractChainingPrintRenderer
44    {
45    /**
46    * New Line character.
47    */
48    private static final String NL = "\n";
49   
50    /**
51    * True if no empty line has been printed.
52    */
53    private boolean isFirstElementRendered;
54   
55    /**
56    * Generate link label.
57    */
58    private LinkLabelGenerator linkLabelGenerator;
59   
60    /**
61    * The plain text renderer supports when no link label generator is set.
62    *
63    * @param listenerChain the listener chain
64    */
 
65  0 toggle public PlainTextChainingRenderer(ListenerChain listenerChain)
66    {
67  0 this(null, listenerChain);
68    }
69   
70    /**
71    * @param linkLabelGenerator the link label generator
72    * @param listenerChain the listener chain
73    */
 
74  257 toggle public PlainTextChainingRenderer(LinkLabelGenerator linkLabelGenerator, ListenerChain listenerChain)
75    {
76  257 setListenerChain(listenerChain);
77   
78  257 this.linkLabelGenerator = linkLabelGenerator;
79    }
80   
81    // State
82   
83    /**
84    * @return the {@link BlockStateChainingListener} from the listeners chain
85    */
 
86  77 toggle private BlockStateChainingListener getBlockState()
87    {
88  77 return (BlockStateChainingListener) getListenerChain().getListener(BlockStateChainingListener.class);
89    }
90   
91    /**
92    * @return the {@link EmptyBlockChainingListener} from the listeners chain
93    */
 
94  8 toggle protected EmptyBlockChainingListener getEmptyBlockState()
95    {
96  8 return (EmptyBlockChainingListener) getListenerChain().getListener(EmptyBlockChainingListener.class);
97    }
98   
99    // Events
100   
 
101  24 toggle @Override
102    public void beginParagraph(Map<String, String> parameters)
103    {
104  24 printEmptyLine();
105    }
106   
 
107  21 toggle @Override
108    public void onNewLine()
109    {
110  21 getPrinter().print(NL);
111    }
112   
 
113  8 toggle @Override
114    public void endLink(ResourceReference reference, boolean isFreeStandingURI, Map<String, String> parameters)
115    {
116  8 if (getEmptyBlockState().isCurrentContainerBlockEmpty()) {
117  7 if (reference.getType().equals(ResourceType.DOCUMENT) && this.linkLabelGenerator != null) {
118  1 getPrinter().print(this.linkLabelGenerator.generate(reference));
119    } else {
120  6 getPrinter().print(reference.getReference());
121    }
122    }
123    }
124   
 
125  6 toggle @Override
126    public void beginHeader(HeaderLevel level, String id, Map<String, String> parameters)
127    {
128  6 printEmptyLine();
129    }
130   
 
131  688 toggle @Override
132    public void onWord(String word)
133    {
134  688 getPrinter().print(word);
135    }
136   
 
137  11 toggle @Override
138    public void beginList(ListType listType, Map<String, String> parameters)
139    {
140  11 if (getBlockState().getListDepth() == 1) {
141  6 printEmptyLine();
142    } else {
143  5 getPrinter().print(NL);
144    }
145    }
146   
 
147  16 toggle @Override
148    public void beginListItem()
149    {
150  16 if (getBlockState().getListItemIndex() > 0) {
151  5 getPrinter().print(NL);
152    }
153   
154    // TODO: maybe add some syntax here like a - or not
155    }
156   
 
157  363 toggle @Override
158    public void onSpace()
159    {
160  363 getPrinter().print(" ");
161    }
162   
 
163  44 toggle @Override
164    public void onSpecialSymbol(char symbol)
165    {
166  44 getPrinter().print(String.valueOf(symbol));
167    }
168   
 
169  0 toggle @Override
170    public void onHorizontalLine(Map<String, String> parameters)
171    {
172  0 printEmptyLine();
173  0 getPrinter().print("----");
174    }
175   
 
176  2 toggle @Override
177    public void onEmptyLines(int count)
178    {
179  2 getPrinter().print(StringUtils.repeat(NL, count));
180    }
181   
 
182  1 toggle @Override
183    public void onVerbatim(String protectedString, boolean isInline, Map<String, String> parameters)
184    {
185  1 getPrinter().print(protectedString);
186    }
187   
188    /**
189    * {@inheritDoc}
190    *
191    * @since 2.0RC1
192    */
 
193  7 toggle @Override
194    public void beginDefinitionList(Map<String, String> parameters)
195    {
196  7 if (getBlockState().getDefinitionListDepth() == 1 && !getBlockState().isInList()) {
197  3 printEmptyLine();
198    } else {
199  4 getPrinter().print(NL);
200    }
201    }
202   
 
203  8 toggle @Override
204    public void beginDefinitionTerm()
205    {
206  8 if (getBlockState().getDefinitionListItemIndex() > 0) {
207  1 getPrinter().print(NL);
208    }
209    }
210   
 
211  8 toggle @Override
212    public void beginDefinitionDescription()
213    {
214  8 if (getBlockState().getDefinitionListItemIndex() > 0) {
215  8 getPrinter().print(NL);
216    }
217    }
218   
 
219  4 toggle @Override
220    public void beginQuotationLine()
221    {
222  4 if (getBlockState().getQuotationLineIndex() > 0) {
223  3 getPrinter().print(NL);
224    } else {
225  1 printEmptyLine();
226    }
227    }
228   
 
229  2 toggle @Override
230    public void beginTable(Map<String, String> parameters)
231    {
232  2 printEmptyLine();
233    }
234   
 
235  12 toggle @Override
236    public void beginTableCell(Map<String, String> parameters)
237    {
238  12 if (getBlockState().getCellCol() > 0) {
239  6 getPrinter().print("\t");
240    }
241    }
242   
 
243  4 toggle @Override
244    public void beginTableHeadCell(Map<String, String> parameters)
245    {
246  4 beginTableCell(parameters);
247    }
248   
 
249  6 toggle @Override
250    public void beginTableRow(Map<String, String> parameters)
251    {
252  6 if (getBlockState().getCellRow() > 0) {
253  4 getPrinter().print(NL);
254    }
255    }
256   
257    /**
258    * {@inheritDoc}
259    *
260    * @since 2.5RC1
261    */
 
262  0 toggle @Override
263    public void onImage(ResourceReference reference, boolean isFreeStandingURI, Map<String, String> parameters)
264    {
265    // TODO: maybe something could be done here
266    }
267   
268    /**
269    * Add an empty line to the printer.
270    */
 
271  42 toggle private void printEmptyLine()
272    {
273  42 if (this.isFirstElementRendered) {
274  16 getPrinter().print(NL + NL);
275    } else {
276  26 this.isFirstElementRendered = true;
277    }
278    }
279    }