1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.renderer.printer

File XHTMLWikiPrinter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% 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 554 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  19599 toggle public XHTMLWikiPrinter(WikiPrinter printer)
51    {
52  19598 super(printer);
53    }
54   
 
55  38947 toggle @Override
56    public void printXML(String str)
57    {
58  38944 handleSpaceWhenInText();
59  38945 super.printXML(str);
60  38947 this.hasTextBeenPrinted = true;
61    }
62   
 
63  405 toggle @Override
64    public void printXMLElement(String name)
65    {
66  405 handleSpaceWhenStartElement();
67  405 super.printXMLElement(name);
68    }
69   
 
70  405 toggle @Override
71    public void printXMLElement(String name, String[][] attributes)
72    {
73  405 handleSpaceWhenStartElement();
74  405 super.printXMLElement(name, attributes);
75    }
76   
 
77  500 toggle @Override
78    public void printXMLElement(String name, Map<String, String> attributes)
79    {
80  500 handleSpaceWhenStartElement();
81  500 super.printXMLElement(name, attributes);
82    }
83   
 
84  5679 toggle @Override
85    public void printXMLStartElement(String name)
86    {
87  5679 handleSpaceWhenStartElement();
88  5679 super.printXMLStartElement(name);
89    }
90   
 
91  285 toggle @Override
92    public void printXMLStartElement(String name, String[][] attributes)
93    {
94  285 handleSpaceWhenStartElement();
95  285 super.printXMLStartElement(name, attributes);
96    }
97   
 
98  11503 toggle @Override
99    public void printXMLStartElement(String name, Map<String, String> attributes)
100    {
101  11503 handleSpaceWhenStartElement();
102  11503 super.printXMLStartElement(name, attributes);
103    }
104   
 
105  17467 toggle @Override
106    public void printXMLStartElement(String name, Attributes attributes)
107    {
108  17467 handleSpaceWhenStartElement();
109  17467 super.printXMLStartElement(name, attributes);
110    }
111   
 
112  17467 toggle @Override
113    public void printXMLEndElement(String name)
114    {
115  17467 handleSpaceWhenEndlement();
116  17467 super.printXMLEndElement(name);
117  17467 this.elementEnded = true;
118    }
119   
 
120  108 toggle @Override
121    public void printXMLComment(String content)
122    {
123  108 printXMLComment(content, false);
124    }
125   
 
126  216 toggle @Override
127    public void printXMLComment(String content, boolean escape)
128    {
129  216 handleSpaceWhenStartElement();
130  216 super.printXMLComment(content, escape);
131  216 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  16110 toggle public void printSpace()
152    {
153  16111 this.spaceCount++;
154    }
155   
 
156  6614 toggle @Override
157    public void printRaw(String raw)
158    {
159  6614 handleSpaceWhenStartElement();
160  6614 super.printRaw(raw);
161  6614 this.elementEnded = true;
162    }
163   
 
164  38948 toggle private void handleSpaceWhenInText()
165    {
166  38944 if (this.elementEnded || this.hasTextBeenPrinted) {
167  18836 handleSpaceWhenStartElement();
168    } else {
169  20108 handleSpaceWhenEndlement();
170    }
171    }
172   
 
173  61909 toggle private void handleSpaceWhenStartElement()
174    {
175    // Use case: <tag1>something <tag2>...
176    // Use case: <tag1>something <!--...
177  61909 if (this.spaceCount > 0) {
178  15899 if (!this.isInCData && !this.isInPreserveElement) {
179    // The first space is a normal space
180  15898 super.printXML(" ");
181  15928 for (int i = 0; i < this.spaceCount - 1; i++) {
182  28 printEntity("&nbsp;");
183    }
184    } else {
185  0 super.printXML(StringUtils.repeat(' ', this.spaceCount));
186    }
187    }
188  61911 this.spaceCount = 0;
189  61909 this.elementEnded = false;
190  61910 this.hasTextBeenPrinted = false;
191    }
192   
 
193  37577 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  37578 if (!this.isInCData && !this.isInPreserveElement) {
198  37760 for (int i = 0; i < this.spaceCount; i++) {
199  185 printEntity("&nbsp;");
200    }
201    } else {
202  0 super.printXML(StringUtils.repeat(' ', this.spaceCount));
203    }
204  37575 this.spaceCount = 0;
205  37575 this.elementEnded = false;
206  37577 this.hasTextBeenPrinted = false;
207    }
208    }