Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
19   97   8   6.33
10   56   0.42   3
3     2.67  
1    
 
  EscapeFilter       Line # 43 19 0% 8 4 87.5% 0.875
 
  (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.xwiki.component.annotation.Component;
29    import org.xwiki.component.phase.Initializable;
30    import org.xwiki.component.phase.InitializationException;
31    import org.xwiki.rendering.parser.xwiki10.AbstractFilter;
32    import org.xwiki.rendering.parser.xwiki10.FilterContext;
33   
34    /**
35    * Match 1.0 escape syntax.
36    *
37    * @version $Id: 7703ceb45d8c42f3f69f199d97967877bb23e427 $
38    * @since 1.9M2
39    */
40    @Component
41    @Named("escape")
42    @Singleton
 
43    public class EscapeFilter extends AbstractFilter implements Initializable
44    {
45    /**
46    * Regex pattern for matching 1.0 syntax escaping.
47    */
48    public static final Pattern ESCAPE_PATTERN = Pattern.compile("\\\\(\\\\\\\\)(?!\\\\)|\\\\(.)");
49   
50    public static final String ESCAPE_SUFFIX = "escape";
51   
 
52  92 toggle @Override
53    public void initialize() throws InitializationException
54    {
55  92 setPriority(25);
56    }
57   
 
58  92 toggle @Override
59    public String filter(String content, FilterContext filterContext)
60    {
61  92 return filter(content, filterContext, true);
62    }
63   
 
64  184 toggle public String filter(String content, FilterContext filterContext, boolean clean)
65    {
66  184 StringBuffer result = new StringBuffer();
67   
68  184 Matcher matcher = ESCAPE_PATTERN.matcher(content);
69  184 int currentIndex = 0;
70  211 for (; matcher.find(); currentIndex = matcher.end()) {
71  27 result.append(content.substring(currentIndex, matcher.start()));
72   
73  27 if (clean) {
74  12 if (matcher.group(2) != null) {
75  12 String escaped = matcher.group(2);
76  12 if ("\\".equals(escaped)) {
77  0 result.append("\\\\");
78    } else {
79  12 result.append(filterContext.addProtectedContent(matcher.group(2), ESCAPE_SUFFIX, true));
80    }
81    } else {
82  0 result.append(filterContext.addProtectedContent("\\", ESCAPE_SUFFIX, true));
83    }
84    } else {
85  15 result.append(filterContext.addProtectedContent(matcher.group(0), ESCAPE_SUFFIX, true));
86    }
87    }
88   
89  184 if (currentIndex == 0) {
90  173 return content;
91    }
92   
93  11 result.append(content.substring(currentIndex));
94   
95  11 return result.toString();
96    }
97    }