Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
19   98   8   6.33
8   56   0.42   3
3     2.67  
1    
 
  UnescapeHTMLFilter       Line # 44 19 0% 8 0 100% 1.0
 
  (92)
 
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.xwiki10;
21   
22    import java.util.regex.Matcher;
23    import java.util.regex.Pattern;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.apache.commons.lang3.StringEscapeUtils;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.phase.Initializable;
31    import org.xwiki.component.phase.InitializationException;
32    import org.xwiki.rendering.parser.xwiki10.AbstractFilter;
33    import org.xwiki.rendering.parser.xwiki10.FilterContext;
34   
35    /**
36    * Unescape HTML escaped content outside of matched HTML macros.
37    *
38    * @version $Id: 0b350577909a8985034b27fd7fe6e923ea0afc7a $
39    * @since 1.8M1
40    */
41    @Component
42    @Named("unescapehtml")
43    @Singleton
 
44    public class UnescapeHTMLFilter extends AbstractFilter implements Initializable
45    {
46    public static final Pattern HTMLVELOCITY_SPATTERN =
47    Pattern.compile("(" + VelocityFilter.VELOCITYOPEN_SPATTERN + ")|(" + VelocityFilter.VELOCITYCLOSE_PATTERN
48    + ")|(" + HTMLFilter.HTMLOPEN_SPATTERN + ")|(" + HTMLFilter.HTMLCLOSE_SPATTERN + ")");
49   
 
50  92 toggle @Override
51    public void initialize() throws InitializationException
52    {
53  92 setPriority(3001);
54    }
55   
 
56  92 toggle @Override
57    public String filter(String content, FilterContext filterContext)
58    {
59  92 StringBuffer result = new StringBuffer();
60   
61  92 Matcher matcher = HTMLVELOCITY_SPATTERN.matcher(content);
62   
63  92 boolean inHTML = false;
64   
65  92 int currentIndex = 0;
66  170 for (; matcher.find(); currentIndex = matcher.end()) {
67  78 String before = content.substring(currentIndex, matcher.start());
68   
69  78 if (!inHTML) {
70  39 result.append(unescapeNonHtmlContent(before));
71    } else {
72  39 result.append(before);
73    }
74   
75  78 if (matcher.group(1) != null || matcher.group(3) != null) {
76  39 inHTML = true;
77    } else {
78  39 inHTML = false;
79    }
80   
81  78 result.append(matcher.group());
82    }
83   
84  92 if (currentIndex == 0) {
85  53 return unescapeNonHtmlContent(content);
86    }
87   
88    // Print remaining content
89  39 result.append(unescapeNonHtmlContent(content.substring(currentIndex)));
90   
91  39 return result.toString();
92    }
93   
 
94  131 toggle private String unescapeNonHtmlContent(String content)
95    {
96  131 return StringEscapeUtils.unescapeHtml4(content);
97    }
98    }