1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.parser.xhtml.wikimodel

File XWikiXHTMLWhitespaceXMLFilter.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

12
20
6
1
123
67
12
0.6
3.33
6
2

Classes

Class Line # Actions
XWikiXHTMLWhitespaceXMLFilter 39 20 0% 12 14
0.631578963.2%
 

Contributing tests

This file is covered by 282 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.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: 062aee15248d3eaf8c3bac4fd9e99ccc227f7fd3 $
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  294 toggle public XWikiXHTMLWhitespaceXMLFilter(XMLReader reader)
62    {
63  294 super(reader);
64    }
65   
 
66  294 toggle @Override
67    public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException
68    {
69  294 if (SAX_CONTAINS_WIKI_SYNTAX_PROPERTY.equalsIgnoreCase(name)) {
70  0 this.containsWikiSyntax = (Boolean) value;
71    } else {
72  294 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  5604 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  5604 return this.containsWikiSyntax || super.shouldRemoveWhiteSpaces();
95    }
96   
 
97  4060 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  4060 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  4060 super.cleanContentExtraWhiteSpaces();
110    }
111    }
112   
 
113  245 toggle @Override
114    protected boolean isSemanticComment(String comment)
115    {
116  245 if (super.isSemanticComment(comment)) {
117  64 return true;
118    }
119   
120  181 return comment.startsWith("startwikilink:") || comment.startsWith("stopwikilink")
121    || comment.startsWith("startimage:") || comment.startsWith("stopimage");
122    }
123    }