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

File XMLWikiPrinter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

12
52
20
1
315
160
41
0.79
2.6
20
2.05

Classes

Class Line # Actions
XMLWikiPrinter 43 52 0% 41 16
0.809523881%
 

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.io.IOException;
23    import java.util.Map;
24   
25    import org.dom4j.Element;
26    import org.dom4j.io.XMLWriter;
27    import org.dom4j.tree.DefaultComment;
28    import org.dom4j.tree.DefaultElement;
29    import org.dom4j.tree.DefaultEntity;
30    import org.xml.sax.Attributes;
31    import org.xml.sax.SAXException;
32    import org.xml.sax.helpers.AttributesImpl;
33    import org.xwiki.rendering.internal.renderer.printer.WikiWriter;
34    import org.xwiki.rendering.xml.internal.renderer.printer.XHTMLWriter;
35    import org.xwiki.xml.XMLUtils;
36   
37    /**
38    * Base toolkit class for all XML-based printers.
39    *
40    * @version $Id: 669007992fa328bdcede6592551f095ef08354ee $
41    * @since 1.9M1
42    */
 
43    public class XMLWikiPrinter
44    {
45    protected WikiWriter wikiWriter;
46   
47    protected XMLWriter xmlWriter;
48   
49    /**
50    * @param printer the object to which to write the XHTML output to
51    */
 
52  19601 toggle public XMLWikiPrinter(WikiPrinter printer)
53    {
54  19600 this.wikiWriter = new WikiWriter(printer);
55   
56  19601 this.xmlWriter = new XHTMLWriter(this.wikiWriter);
57    }
58   
 
59  0 toggle public XMLWriter getXMLWriter()
60    {
61  0 return this.xmlWriter;
62    }
63   
 
64  19598 toggle public void setWikiPrinter(WikiPrinter printer)
65    {
66  19597 this.wikiWriter.setWikiPrinter(printer);
67    }
68   
69    /**
70    * Print provided text. Takes care of xml escaping.
71    *
72    * @param str the string to print
73    */
 
74  54844 toggle public void printXML(String str)
75    {
76  54843 try {
77  54845 this.xmlWriter.write(str);
78    } catch (IOException e) {
79    // TODO: add error log here
80    }
81    }
82   
83    /**
84    * Print the xml element. In the form {@code <name/>}.
85    *
86    * @param name the xml element to print
87    */
 
88  405 toggle public void printXMLElement(String name)
89    {
90  405 printXMLElement(name, (String[][]) null);
91    }
92   
93    /**
94    * Print the xml element. In the form {@code <name att1="value1" att2="value2"/>}.
95    *
96    * @param name the xml element to print
97    * @param attributes the xml attributes of the element to print
98    */
 
99  405 toggle public void printXMLElement(String name, String[][] attributes)
100    {
101  405 Element element = new DefaultElement(name);
102   
103  405 if (attributes != null && attributes.length > 0) {
104  0 for (String[] entry : attributes) {
105  0 element.addAttribute(entry[0], entry[1]);
106    }
107    }
108   
109  405 try {
110  405 this.xmlWriter.write(element);
111    } catch (IOException e) {
112    // TODO: add error log here
113    }
114    }
115   
116    /**
117    * Print the xml element. In the form {@code <name att1="value1" att2="value2"/>}.
118    *
119    * @param name the xml element to print
120    * @param attributes the xml attributes of the element to print
121    */
 
122  500 toggle public void printXMLElement(String name, Map<String, String> attributes)
123    {
124  500 Element element = new DefaultElement(name);
125   
126  500 if (attributes != null && !attributes.isEmpty()) {
127  487 for (Map.Entry<String, String> entry : attributes.entrySet()) {
128  1095 element.addAttribute(entry.getKey(), entry.getValue());
129    }
130    }
131   
132  500 try {
133  500 this.xmlWriter.write(element);
134    } catch (IOException e) {
135    // TODO: add error log here
136    }
137    }
138   
139    /**
140    * Print the start tag of xml element. In the form {@code <name>}.
141    *
142    * @param name the xml element to print
143    */
 
144  5679 toggle public void printXMLStartElement(String name)
145    {
146  5679 printXMLStartElement(name, new AttributesImpl());
147    }
148   
149    /**
150    * Print the start tag of xml element. In the form {@code <name att1="value1" att2="value2">}.
151    *
152    * @param name the xml element to print
153    * @param attributes the xml attributes of the element to print
154    */
 
155  285 toggle public void printXMLStartElement(String name, String[][] attributes)
156    {
157  285 printXMLStartElement(name, createAttributes(attributes));
158    }
159   
160    /**
161    * Print the start tag of xml element. In the form {@code <name att1="value1" att2="value2">}.
162    *
163    * @param name the xml element to print
164    * @param attributes the xml attributes of the element to print
165    */
 
166  11503 toggle public void printXMLStartElement(String name, Map<String, String> attributes)
167    {
168  11503 printXMLStartElement(name, createAttributes(attributes));
169    }
170   
171    /**
172    * Print the start tag of xml element. In the form {@code <name att1="value1" att2="value2">}.
173    *
174    * @param name the xml element to print
175    * @param attributes the xml attributes of the element to print
176    */
 
177  17467 toggle public void printXMLStartElement(String name, Attributes attributes)
178    {
179  17467 try {
180  17467 this.xmlWriter.startElement("", name, name, attributes);
181    } catch (SAXException e) {
182    // TODO: add error log here
183    }
184    }
185   
186    /**
187    * Print the end tag of xml element. In the form {@code </name>}.
188    *
189    * @param name the xml element to print
190    */
 
191  17467 toggle public void printXMLEndElement(String name)
192    {
193  17467 try {
194  17467 this.xmlWriter.endElement("", name, name);
195    } catch (SAXException e) {
196    // TODO: add error log here
197    }
198    }
199   
200    /**
201    * Print a XML comment. Note that the content that you pass must be valid XML comment, ie not have <code>--</code>
202    * characters (or <code>-</code> if it's the last character). If you're not sure what the comment content will be
203    * use {@link #printXMLComment(String, boolean)} instead, passing true for the second parameter.
204    *
205    * @param content the comment content
206    */
 
207  0 toggle public void printXMLComment(String content)
208    {
209  0 printXMLComment(content, false);
210    }
211   
212    /**
213    * Print a XML comment.
214    *
215    * @param content the comment content
216    * @param escape indicate if comment content has to be escaped. XML content does not support -- and - (when it's the
217    * last character). Escaping is based on backslash. "- --\ -" give "- \-\-\\ \-\ ".
218    */
 
219  216 toggle public void printXMLComment(String content, boolean escape)
220    {
221  216 try {
222  216 this.xmlWriter.write(new DefaultComment(escape ? XMLUtils.escapeXMLComment(content) : content));
223    } catch (IOException e) {
224    // TODO: add error log here
225    }
226    }
227   
228    /**
229    * Start a CDATA section.
230    */
 
231  0 toggle public void printXMLStartCData()
232    {
233  0 try {
234  0 this.xmlWriter.startCDATA();
235    // Ensure that characters inside CDATA sections are not escaped
236  0 this.xmlWriter.setEscapeText(false);
237    } catch (Exception e) {
238    // TODO: handle exception
239    }
240    }
241   
242    /**
243    * End a CDATA section.
244    */
 
245  0 toggle public void printXMLEndCData()
246    {
247  0 try {
248  0 this.xmlWriter.setEscapeText(true);
249  0 this.xmlWriter.endCDATA();
250    } catch (Exception e) {
251    // TODO: handle exception
252    }
253    }
254   
 
255  213 toggle public void printEntity(String entity)
256    {
257  213 try {
258  213 this.xmlWriter.write(new DefaultEntity(entity, entity));
259    } catch (Exception e) {
260    // TODO: handle exception
261    }
262    }
263   
264    /**
265    * Print some text without escaping anything, it's supposed to be XML or at least contains only valid characters in
266    * XML text node.
267    *
268    * @param row the content
269    */
 
270  6614 toggle public void printRaw(String row)
271    {
272  6614 try {
273  6614 this.wikiWriter.write(row);
274    } catch (Exception e) {
275    // TODO: handle exception
276    }
277    }
278   
279    /**
280    * Convert provided table into {@link Attributes} to use in xml writer.
281    */
 
282  285 toggle private Attributes createAttributes(String[][] parameters)
283    {
284  285 AttributesImpl attributes = new AttributesImpl();
285   
286  285 if (parameters != null && parameters.length > 0) {
287  285 for (String[] entry : parameters) {
288  285 attributes.addAttribute(null, null, entry[0], null, entry[1]);
289    }
290    }
291   
292  285 return attributes;
293    }
294   
295    /**
296    * Convert provided map into {@link Attributes} to use in xml writer.
297    */
 
298  11503 toggle private Attributes createAttributes(Map<String, String> parameters)
299    {
300  11503 AttributesImpl attributes = new AttributesImpl();
301   
302  11503 if (parameters != null && !parameters.isEmpty()) {
303  8353 for (Map.Entry<String, String> entry : parameters.entrySet()) {
304  13109 String value = entry.getValue();
305  13109 String key = entry.getKey();
306   
307  13109 if (key != null && value != null) {
308</