Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
52   208   28   2.74
12   144   0.54   19
19     1.47  
1    
 
  XHTMLWikiPrinter       Line # 35 52 0% 28 10 88% 0.8795181
 
  (239)
 
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.renderer.printer;
21   
22    import java.util.Map;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.xml.sax.Attributes;
26   
27    /**
28    * Base toolkit class for all XHTML-based renderers. This printer handles whitespaces so that it prints " " when
29    * needed (i.e. when the spaces are at the beginning or at the end of an element's content or when there are more than 1
30    * contiguous spaces, except for CDATA sections and inside PRE elements. It also knows how to handle XHTML comments).
31    *
32    * @version $Id: 23081112f8d7950caa0c769fb2be0ebce2f749fe $
33    * @since 1.7M1
34    */
 
35    public class XHTMLWikiPrinter extends XMLWikiPrinter
36    {
37    private int spaceCount;
38   
39    private boolean isInCData;
40   
41    private boolean isInPreserveElement;
42   
43    private boolean elementEnded;
44   
45    private boolean hasTextBeenPrinted;
46   
47    /**
48    * @param printer the object to which to write the XHTML output to
49    */
 
50  247 toggle public XHTMLWikiPrinter(WikiPrinter printer)
51    {
52  247 super(printer);
53    }
54   
 
55  1003 toggle @Override
56    public void printXML(String str)
57    {
58  1003 handleSpaceWhenInText();
59  1003 super.printXML(str);
60  1003 this.hasTextBeenPrinted = true;
61    }
62   
 
63  87 toggle @Override
64    public void printXMLElement(String name)
65    {
66  87 handleSpaceWhenStartElement();
67  87 super.printXMLElement(name);
68    }
69   
 
70  87 toggle @Override
71    public void printXMLElement(String name, String[][] attributes)
72    {
73  87 handleSpaceWhenStartElement();
74  87 super.printXMLElement(name, attributes);
75    }
76   
 
77  45 toggle @Override
78    public void printXMLElement(String name, Map<String, String> attributes)
79    {
80  45 handleSpaceWhenStartElement();
81  45 super.printXMLElement(name, attributes);
82    }
83   
 
84  176 toggle @Override
85    public void printXMLStartElement(String name)
86    {
87  176 handleSpaceWhenStartElement();
88  176 super.printXMLStartElement(name);
89    }
90   
 
91  73 toggle @Override
92    public void printXMLStartElement(String name, String[][] attributes)
93    {
94  73 handleSpaceWhenStartElement();
95  73 super.printXMLStartElement(name, attributes);
96    }
97   
 
98  696 toggle @Override
99    public void printXMLStartElement(String name, Map<String, String> attributes)
100    {
101  696 handleSpaceWhenStartElement();
102  696 super.printXMLStartElement(name, attributes);
103    }
104   
 
105  945 toggle @Override
106    public void printXMLStartElement(String name, Attributes attributes)
107    {
108  945 handleSpaceWhenStartElement();
109  945 super.printXMLStartElement(name, attributes);
110    }
111   
 
112  945 toggle @Override
113    public void printXMLEndElement(String name)
114    {
115  945 handleSpaceWhenEndlement();
116  945 super.printXMLEndElement(name);
117  945 this.elementEnded = true;
118    }
119   
 
120  80 toggle @Override
121    public void printXMLComment(String content)
122    {
123  80 printXMLComment(content, false);
124    }
125   
 
126  160 toggle @Override
127    public void printXMLComment(String content, boolean escape)
128    {
129  160 handleSpaceWhenStartElement();
130  160 super.printXMLComment(content, escape);
131  160 this.elementEnded = true;
132    }
133   
 
134  0 toggle @Override
135    public void printXMLStartCData()
136    {
137  0 handleSpaceWhenStartElement();
138  0 super.printXMLStartCData();
139    }
140   
 
141  0 toggle @Override
142    public void printXMLEndCData()
143    {
144  0 handleSpaceWhenEndlement();
145  0 super.printXMLEndCData();
146    }
147   
148    /**
149    * This method should be used to print a space rather than calling <code>printXML(" ")</code>.
150    */
 
151  384 toggle public void printSpace()
152    {
153  384 this.spaceCount++;
154    }
155   
 
156  14 toggle @Override
157    public void printRaw(String raw)
158    {
159  14 handleSpaceWhenStartElement();
160  14 super.printRaw(raw);
161  14 this.elementEnded = true;
162    }
163   
 
164  1003 toggle private void handleSpaceWhenInText()
165    {
166  1003 if (this.elementEnded || this.hasTextBeenPrinted) {
167  487 handleSpaceWhenStartElement();
168    } else {
169  516 handleSpaceWhenEndlement();
170    }
171    }
172   
 
173  2770 toggle private void handleSpaceWhenStartElement()
174    {
175    // Use case: <tag1>something <tag2>...
176    // Use case: <tag1>something <!--...
177  2770 if (this.spaceCount > 0) {
178  345 if (!this.isInCData && !this.isInPreserveElement) {
179    // The first space is a normal space
180  345 super.printXML(" ");
181  352 for (int i = 0; i < this.spaceCount - 1; i++) {
182  7 printEntity("&nbsp;");
183    }
184    } else {
185  0 super.printXML(StringUtils.repeat(' ', this.spaceCount));
186    }
187    }
188  2770 this.spaceCount = 0;
189  2770 this.elementEnded = false;
190  2770 this.hasTextBeenPrinted = false;
191    }
192   
 
193  1461 toggle private void handleSpaceWhenEndlement()
194    {
195    // Use case: <tag1>something </tag1>...
196    // All spaces are &nbsp; spaces since otherwise they'll be all stripped by browsers
197  1461 if (!this.isInCData && !this.isInPreserveElement) {
198  1493 for (int i = 0; i < this.spaceCount; i++) {
199  32 printEntity("&nbsp;");
200    }
201    } else {
202  0 super.printXML(StringUtils.repeat(' ', this.spaceCount));
203    }
204  1461 this.spaceCount = 0;
205  1461 this.elementEnded = false;
206  1461 this.hasTextBeenPrinted = false;
207    }
208    }