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

File XHTMLWikiPrinter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

12
52
19
1
208
144
28
0.54
2.74
19
1.47

Classes

Class Line # Actions
XHTMLWikiPrinter 35 52 0% 28 10
0.879518188%
 

Contributing tests

This file is covered by 584 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.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: b70588d8298022323b31ed20a14298d72a955030 $
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  412907 toggle public XHTMLWikiPrinter(WikiPrinter printer)
51    {
52  412864 super(printer);
53    }
54   
 
55  4509755 toggle @Override
56    public void printXML(String str)
57    {
58  4509847 handleSpaceWhenInText();
59  4509693 super.printXML(str);
60  4509864 this.hasTextBeenPrinted = true;
61    }
62   
 
63  197504 toggle @Override
64    public void printXMLElement(String name)
65    {
66  197504 handleSpaceWhenStartElement();
67  197504 super.printXMLElement(name);
68    }
69   
 
70  197504 toggle @Override
71    public void printXMLElement(String name, String[][] attributes)
72    {
73  197504 handleSpaceWhenStartElement();
74  197504 super.printXMLElement(name, attributes);
75    }
76   
 
77  3859 toggle @Override
78    public void printXMLElement(String name, Map<String, String> attributes)
79    {
80  3859 handleSpaceWhenStartElement();
81  3859 super.printXMLElement(name, attributes);
82    }
83   
 
84  54482 toggle @Override
85    public void printXMLStartElement(String name)
86    {
87  54486 handleSpaceWhenStartElement();
88  54485 super.printXMLStartElement(name);
89    }
90   
 
91  13669 toggle @Override
92    public void printXMLStartElement(String name, String[][] attributes)
93    {
94  13669 handleSpaceWhenStartElement();
95  13669 super.printXMLStartElement(name, attributes);
96    }
97   
 
98  595532 toggle @Override
99    public void printXMLStartElement(String name, Map<String, String> attributes)
100    {
101  595546 handleSpaceWhenStartElement();
102  595544 super.printXMLStartElement(name, attributes);
103    }
104   
 
105  663665 toggle @Override
106    public void printXMLStartElement(String name, Attributes attributes)
107    {
108  663707 handleSpaceWhenStartElement();
109  663704 super.printXMLStartElement(name, attributes);
110    }
111   
 
112  663683 toggle @Override
113    public void printXMLEndElement(String name)
114    {
115  663703 handleSpaceWhenEndlement();
116  663703 super.printXMLEndElement(name);
117  663694 this.elementEnded = true;
118    }
119   
 
120  184 toggle @Override
121    public void printXMLComment(String content)
122    {
123  184 printXMLComment(content, false);
124    }
125   
 
126  368 toggle @Override
127    public void printXMLComment(String content, boolean escape)
128    {
129  368 handleSpaceWhenStartElement();
130  368 super.printXMLComment(content, escape);
131  368 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  1988937 toggle public void printSpace()
152    {
153  1989024 this.spaceCount++;
154    }
155   
 
156  281032 toggle @Override
157    public void printRaw(String raw)
158    {
159  281033 handleSpaceWhenStartElement();
160  281034 super.printRaw(raw);
161  281032 this.elementEnded = true;
162    }
163   
 
164  4509431 toggle private void handleSpaceWhenInText()
165    {
166  4509609 if (this.elementEnded || this.hasTextBeenPrinted) {
167  3960255 handleSpaceWhenStartElement();
168    } else {
169  549470 handleSpaceWhenEndlement();
170    }
171    }
172   
 
173  5967719 toggle private void handleSpaceWhenStartElement()
174    {
175    // Use case: <tag1>something <tag2>...
176    // Use case: <tag1>something <!--...
177  5967940 if (this.spaceCount > 0) {
178  1951839 if (!this.isInCData && !this.isInPreserveElement) {
179    // The first space is a normal space
180  1951838 super.printXML(" ");
181  1975973 for (int i = 0; i < this.spaceCount - 1; i++) {
182  24144 printEntity("&nbsp;");
183    }
184    } else {
185  0 super.printXML(StringUtils.repeat(' ', this.spaceCount));
186    }
187    }
188  5967921 this.spaceCount = 0;
189  5967942 this.elementEnded = false;
190  5967939 this.hasTextBeenPrinted = false;
191    }
192   
 
193  1213022 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  1213192 <