Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../img/srcFileCovDistChart8.png 68% of files have more coverage
52   292   41   2.6
12   160   0.79   20
20     2.05  
1    
 
  XMLWikiPrinter       Line # 43 52 0% 41 17 79.8% 0.79761904
 
  (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.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.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: 5d2ae5b91475508e706e52a25784c3a699122c5d $
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  247 toggle public XMLWikiPrinter(WikiPrinter printer)
53    {
54  247 this.wikiWriter = new WikiWriter(printer);
55   
56  247 this.xmlWriter = new XHTMLWriter(this.wikiWriter);
57    }
58   
 
59  0 toggle public XMLWriter getXMLWriter()
60    {
61  0 return this.xmlWriter;
62    }
63   
 
64  247 toggle public void setWikiPrinter(WikiPrinter printer)
65    {
66  247 this.wikiWriter.setWikiPrinter(printer);
67    }
68   
69    /**
70    * Print provided text. Takes care of xml escaping.
71    */
 
72  1348 toggle public void printXML(String str)
73    {
74  1348 try {
75  1348 this.xmlWriter.write(str);
76    } catch (IOException e) {
77    // TODO: add error log here
78    }
79    }
80   
81    /**
82    * Print the xml element. In the form <name/>.
83    */
 
84  87 toggle public void printXMLElement(String name)
85    {
86  87 printXMLElement(name, (String[][]) null);
87    }
88   
89    /**
90    * Print the xml element. In the form <name att1="value1" att2="value2"/>.
91    */
 
92  87 toggle public void printXMLElement(String name, String[][] attributes)
93    {
94  87 Element element = new DefaultElement(name);
95   
96  87 if (attributes != null && attributes.length > 0) {
97  0 for (String[] entry : attributes) {
98  0 element.addAttribute(entry[0], entry[1]);
99    }
100    }
101   
102  87 try {
103  87 this.xmlWriter.write(element);
104    } catch (IOException e) {
105    // TODO: add error log here
106    }
107    }
108   
109    /**
110    * Print the xml element. In the form <name att1="value1" att2="value2"/>.
111    */
 
112  45 toggle public void printXMLElement(String name, Map<String, String> attributes)
113    {
114  45 Element element = new DefaultElement(name);
115   
116  45 if (attributes != null && !attributes.isEmpty()) {
117  35 for (Map.Entry<String, String> entry : attributes.entrySet()) {
118  103 element.addAttribute(entry.getKey(), entry.getValue());
119    }
120    }
121   
122  45 try {
123  45 this.xmlWriter.write(element);
124    } catch (IOException e) {
125    // TODO: add error log here
126    }
127    }
128   
129    /**
130    * Print the start tag of xml element. In the form &lt;name&gt;.
131    */
 
132  176 toggle public void printXMLStartElement(String name)
133    {
134  176 printXMLStartElement(name, new AttributesImpl());
135    }
136   
137    /**
138    * Print the start tag of xml element. In the form &lt;name att1="value1" att2="value2"&gt;.
139    */
 
140  73 toggle public void printXMLStartElement(String name, String[][] attributes)
141    {
142  73 printXMLStartElement(name, createAttributes(attributes));
143    }
144   
145    /**
146    * Print the start tag of xml element. In the form &lt;name att1="value1" att2="value2"&gt;.
147    */
 
148  696 toggle public void printXMLStartElement(String name, Map<String, String> attributes)
149    {
150  696 printXMLStartElement(name, createAttributes(attributes));
151    }
152   
153    /**
154    * Print the start tag of xml element. In the form &lt;name att1="value1" att2="value2"&gt;.
155    */
 
156  945 toggle public void printXMLStartElement(String name, Attributes attributes)
157    {
158  945 try {
159  945 this.xmlWriter.startElement("", name, name, attributes);
160    } catch (SAXException e) {
161    // TODO: add error log here
162    }
163    }
164   
165    /**
166    * Print the end tag of xml element. In the form &lt;/name&gt;.
167    */
 
168  945 toggle public void printXMLEndElement(String name)
169    {
170  945 try {
171  945 this.xmlWriter.endElement("", name, name);
172    } catch (SAXException e) {
173    // TODO: add error log here
174    }
175    }
176   
177    /**
178    * Print a XML comment. Note that the content that you pass must be valid XML comment, ie not have <code>--</code>
179    * characters (or <code>-</code> if it's the last character). If you're not sure what the comment content will be
180    * use {@link #printXMLComment(String, boolean)} instead, passing true for the second parameter.
181    *
182    * @param content the comment content
183    */
 
184  0 toggle public void printXMLComment(String content)
185    {
186  0 printXMLComment(content, false);
187    }
188   
189    /**
190    * Print a XML comment.
191    *
192    * @param content the comment content
193    * @param escape indicate if comment content has to be escaped. XML content does not support -- and - (when it's the
194    * last character). Escaping is based on backslash. "- --\ -" give "- \-\-\\ \-\ ".
195    */
 
196  160 toggle public void printXMLComment(String content, boolean escape)
197    {
198  160 try {
199  160 this.xmlWriter.write(new DefaultComment(escape ? XMLUtils.escapeXMLComment(content) : content));
200    } catch (IOException e) {
201    // TODO: add error log here
202    }
203    }
204   
205    /**
206    * Start a CDATA section.
207    */
 
208  0 toggle public void printXMLStartCData()
209    {
210  0 try {
211  0 this.xmlWriter.startCDATA();
212    // Ensure that characters inside CDATA sections are not escaped
213  0 this.xmlWriter.setEscapeText(false);
214    } catch (Exception e) {
215    // TODO: handle exception
216    }
217    }
218   
219    /**
220    * End a CDATA section.
221    */
 
222  0 toggle public void printXMLEndCData()
223    {
224  0 try {
225  0 this.xmlWriter.setEscapeText(true);
226  0 this.xmlWriter.endCDATA();
227    } catch (Exception e) {
228    // TODO: handle exception
229    }
230    }
231   
 
232  39 toggle public void printEntity(String entity)
233    {
234  39 try {
235  39 this.xmlWriter.write(new DefaultEntity(entity, entity));
236    } catch (Exception e) {
237    // TODO: handle exception
238    }
239    }
240   
241    /**
242    * Print some text without escaping anything, it's supposed to be XML or at least contains only valid characters in
243    * XML text node.
244    *
245    * @param row the content
246    */
 
247  14 toggle public void printRaw(String row)
248    {
249  14 try {
250  14 this.wikiWriter.write(row);
251    } catch (Exception e) {
252    // TODO: handle exception
253    }
254    }
255   
256    /**
257    * Convert provided table into {@link Attributes} to use in xml writer.
258    */
 
259  73 toggle private Attributes createAttributes(String[][] parameters)
260    {
261  73 AttributesImpl attributes = new AttributesImpl();
262   
263  73 if (parameters != null && parameters.length > 0) {
264  73 for (String[] entry : parameters) {
265  73 attributes.addAttribute(null, null, entry[0], null, entry[1]);
266    }
267    }
268   
269  73 return attributes;
270    }
271   
272    /**
273    * Convert provided map into {@link Attributes} to use in xml writer.
274    */
 
275  696 toggle private Attributes createAttributes(Map<String, String> parameters)
276    {
277  696 AttributesImpl attributes = new AttributesImpl();
278   
279  696 if (parameters != null && !parameters.isEmpty()) {
280  325 for (Map.Entry<String, String> entry : parameters.entrySet()) {
281  363 String value = entry.getValue();
282  363 String key = entry.getKey();
283   
284  363 if (key != null && value != null) {
285  363 attributes.addAttribute(null, null, key, null, value);
286    }
287    }
288    }
289   
290  696 return attributes;
291    }
292    }