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

File XMLWikiPrinter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
56% 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 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.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  412663 toggle public XMLWikiPrinter(WikiPrinter printer)
53    {
54  412740 this.wikiWriter = new WikiWriter(printer);
55   
56  412763 this.xmlWriter = new XHTMLWriter(this.wikiWriter);
57    }
58   
 
59  0 toggle public XMLWriter getXMLWriter()
60    {
61  0 return this.xmlWriter;
62    }
63   
 
64  412975 toggle public void setWikiPrinter(WikiPrinter printer)
65    {
66  412997 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  6461404 toggle public void printXML(String str)
75    {
76  6461635 try {
77  6461670 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  197504 toggle public void printXMLElement(String name)
89    {
90  197504 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  197504 toggle public void printXMLElement(String name, String[][] attributes)
100    {
101  197504 Element element = new DefaultElement(name);
102   
103  197504 if (attributes != null && attributes.length > 0) {
104  0 for (String[] entry : attributes) {
105  0 element.addAttribute(entry[0], entry[1]);
106    }
107    }
108   
109  197504 try {
110  197504 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  3859 toggle public void printXMLElement(String name, Map<String, String> attributes)
123    {
124  3859 Element element = new DefaultElement(name);
125   
126  3859 if (attributes != null && !attributes.isEmpty()) {
127  3257 for (Map.Entry<String, String> entry : attributes.entrySet()) {
128  8857 element.addAttribute(entry.getKey(), entry.getValue());
129    }
130    }
131   
132  3859 try {
133  3859 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  54481 toggle public void printXMLStartElement(String name)
145    {
146  54486 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  13669 toggle public void printXMLStartElement(String name, String[][] attributes)
156    {
157  13669 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  595522 toggle public void printXMLStartElement(String name, Map<String, String> attributes)
167    {
168  595545 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  663662 toggle public void printXMLStartElement(String name, Attributes attributes)
178    {
179  663703 try {
180  663699 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  663669 toggle public void printXMLEndElement(String name)
192    {
193  663705 try {
194  663709 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  368 toggle public void printXMLComment(String content, boolean escape)
220    {
221  368 try {
222