Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../../img/srcFileCovDistChart7.png 76% of files have more coverage
20   123   12   3.33
12   67   0.6   6
6     2  
1    
 
  XWikiXHTMLWhitespaceXMLFilter       Line # 39 20 0% 12 14 63.2% 0.6315789
 
  (219)
 
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.internal.parser.xhtml.wikimodel;
21   
22    import java.util.regex.Matcher;
23    import java.util.regex.Pattern;
24   
25    import org.xml.sax.SAXException;
26    import org.xml.sax.SAXNotRecognizedException;
27    import org.xml.sax.SAXNotSupportedException;
28    import org.xml.sax.XMLReader;
29    import org.xwiki.rendering.wikimodel.xhtml.filter.XHTMLWhitespaceXMLFilter;
30   
31    /**
32    * Extension to the WikiModel {@link XHTMLWhitespaceXMLFilter} to support both the ability to not remove spaces inside
33    * XHTML elements containing wiki syntax (we only trim leading and trailing spaces) and to handle XWiki special XHTML
34    * comment placeholders (for recognizing links, images, etc).
35    *
36    * @version $Id: f9a4017af1c4075f4dd8ee32054a1a1673117394 $
37    * @since 2.1RC1
38    */
 
39    public class XWikiXHTMLWhitespaceXMLFilter extends XHTMLWhitespaceXMLFilter
40    {
41    /**
42    * The SAX property controlling whether XHTML elements can contain wiki syntax or not. This controls the whitespace
43    * stripping behavior.
44    */
45    public static final String SAX_CONTAINS_WIKI_SYNTAX_PROPERTY =
46    "http://xwiki.org/sax/properties/contains-wiki-syntax";
47   
48    /**
49    * The leading and trailing white spaces matching pattern.
50    */
51    private static final Pattern HTML_WHITESPACE_BOUNDARIES_PATTERN = Pattern.compile("^\\s+|\\s+$");
52   
53    /**
54    * Indicate if the element can contain wiki syntax.
55    */
56    private boolean containsWikiSyntax;
57   
58    /**
59    * @param reader the XML reader to use ro parse the input XHTML
60    */
 
61  219 toggle public XWikiXHTMLWhitespaceXMLFilter(XMLReader reader)
62    {
63  219 super(reader);
64    }
65   
 
66  219 toggle @Override
67    public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException
68    {
69  219 if (SAX_CONTAINS_WIKI_SYNTAX_PROPERTY.equalsIgnoreCase(name)) {
70  0 this.containsWikiSyntax = (Boolean) value;
71    } else {
72  219 super.setProperty(name, value);
73    }
74    }
75   
 
76  3 toggle @Override
77    public void endCDATA() throws SAXException
78    {
79  3 if (getContent().length() > 0) {
80  3 if (this.containsWikiSyntax) {
81    // Make sure we clean head/trail white spaces
82  0 trimLeadingWhiteSpaces();
83  0 trimTrailingWhiteSpaces();
84    }
85    }
86  3 super.endCDATA();
87    }
88   
 
89  4071 toggle @Override
90    protected boolean shouldRemoveWhiteSpaces()
91    {
92    // Always remove leading/trailing white spaces if we're in wiki mode even if we're inside CDATA and PRE
93    // elements.
94  4071 return this.containsWikiSyntax || super.shouldRemoveWhiteSpaces();
95    }
96   
 
97  3046 toggle @Override
98    protected void cleanContentExtraWhiteSpaces()
99    {
100    // If the element texts can contain wiki syntax only clean whitespaces at beginning and end of texts.
101  3046 if (this.containsWikiSyntax) {
102  0 if (getContent().length() > 0) {
103  0 Matcher matcher = HTML_WHITESPACE_BOUNDARIES_PATTERN.matcher(getContent());
104  0 String result = matcher.replaceAll(" ");
105  0 getContent().setLength(0);
106  0 getContent().append(result);
107    }
108    } else {
109  3046 super.cleanContentExtraWhiteSpaces();
110    }
111    }
112   
 
113  193 toggle @Override
114    protected boolean isSemanticComment(String comment)
115    {
116  193 if (super.isSemanticComment(comment)) {
117  54 return true;
118    }
119   
120  139 return comment.startsWith("startwikilink:") || comment.startsWith("stopwikilink")
121    || comment.startsWith("startimage:") || comment.startsWith("stopimage");
122    }
123    }