1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.parser.xhtml.wikimodel

File XWikiXHTMLWhitespaceXMLFilter.java

 

Coverage histogram

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

Code metrics

10
19
6
1
121
65
12
0.63
3.17
6
2

Classes

Class Line # Actions
XWikiXHTMLWhitespaceXMLFilter 39 19 0% 12 13
0.6285714562.9%
 

Contributing tests

This file is covered by 332 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: 56fccfa9458f6da8bb8fc14a3b0c3e98595514f2 $
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  334 toggle public XWikiXHTMLWhitespaceXMLFilter(XMLReader reader)
62    {
63  334 super(reader);
64    }
65   
 
66  334 toggle @Override
67    public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException
68    {
69  334 if (SAX_CONTAINS_WIKI_SYNTAX_PROPERTY.equalsIgnoreCase(name)) {
70  0 this.containsWikiSyntax = (Boolean) value;
71    } else {
72  334 super.setProperty(name, value);
73    }
74    }
75   
 
76  3 toggle @Override
77    public void endCDATA() throws SAXException
78    {
79  3 if (getContent().length() > 0 && this.containsWikiSyntax) {
80    // Make sure we clean head/trail white spaces
81  0 trimLeadingWhiteSpaces();
82  0 trimTrailingWhiteSpaces();
83    }
84  3 super.endCDATA();
85    }
86   
 
87  6605 toggle @Override
88    protected boolean shouldRemoveWhiteSpaces()
89    {
90    // Always remove leading/trailing white spaces if we're in wiki mode even if we're inside CDATA and PRE
91    // elements.
92  6605 return this.containsWikiSyntax || super.shouldRemoveWhiteSpaces();
93    }
94   
 
95  4468 toggle @Override
96    protected void cleanContentExtraWhiteSpaces()
97    {
98    // If the element texts can contain wiki syntax only clean whitespaces at beginning and end of texts.
99  4468 if (this.containsWikiSyntax) {
100  0 if (getContent().length() > 0) {
101  0 Matcher matcher = HTML_WHITESPACE_BOUNDARIES_PATTERN.matcher(getContent());
102  0 String result = matcher.replaceAll(" ");
103  0 getContent().setLength(0);
104  0 getContent().append(result);
105    }
106    } else {
107  4468 super.cleanContentExtraWhiteSpaces();
108    }
109    }
110   
 
111  291 toggle @Override
112    protected boolean isSemanticComment(String comment)
113    {
114  291 if (super.isSemanticComment(comment)) {
115  112 return true;
116    }
117   
118  179 return comment.startsWith("startwikilink:") || comment.startsWith("stopwikilink")
119    || comment.startsWith("startimage:") || comment.startsWith("stopimage");
120    }
121    }