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
13   95   5   6.5
6   55   0.38   2
2     2.5  
1    
 
  Escape20SyntaxFilter       Line # 41 13 0% 5 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.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    * @version $Id: a1aaae5234bf546a0cb274fbcdb0183f36c01c2b $
36    * @since 1.8M1
37    */
38    @Component
39    @Named("escape20")
40    @Singleton
 
41    public class Escape20SyntaxFilter extends AbstractFilter implements Initializable
42    {
43    private static final String LISTSYNTAX_SPATTERN =
44    Pattern.quote("*") + "|" + Pattern.quote(":") + "|" + Pattern.quote(";") + "|" + Pattern.quote("1.");
45   
46    private static final String CELLSYNTAX_SPATTERN =
47    Pattern.quote("|=") + "|" + Pattern.quote("!=") + "|" + Pattern.quote("!!") + "|" + Pattern.quote("|");
48   
49    private static final String FORMATSYNTAX_SPATTERN =
50    Pattern.quote("//") + "|" + Pattern.quote("**") + "|" + Pattern.quote("__") + "|" + Pattern.quote("--") + "|"
51    + Pattern.quote("^^") + "|" + Pattern.quote(",,") + "|" + Pattern.quote("##");
52   
53    private static final String NEWLINESYNTAX_CONTENT_SPATTERN = LISTSYNTAX_SPATTERN + "|" + Pattern.quote("=");
54   
55    private static final String NEWLINESYNTAX_SPATTERN = "^( *)((?:" + NEWLINESYNTAX_CONTENT_SPATTERN + "))";
56   
57    private static final String INLINESYNTAX_PATTERN =
58    FORMATSYNTAX_SPATTERN + "|" + CELLSYNTAX_SPATTERN + "|" + Pattern.quote("~") + "|" + Pattern.quote("{{") + "|"
59    + Pattern.quote("}}") + "|" + Pattern.quote("(((") + "|" + Pattern.quote(")))");
60   
61    private static final Pattern SYNTAX_PATTERN =
62    Pattern.compile("(" + NEWLINESYNTAX_SPATTERN + ")|(" + INLINESYNTAX_PATTERN + ")", Pattern.MULTILINE);
63   
 
64  92 toggle @Override
65    public void initialize() throws InitializationException
66    {
67  92 setPriority(10000);
68    }
69   
 
70  155 toggle @Override
71    public String filter(String content, FilterContext filterContext)
72    {
73  155 StringBuffer result = new StringBuffer();
74   
75  155 Matcher matcher = SYNTAX_PATTERN.matcher(content);
76  155 int currentIndex = 0;
77  175 for (; matcher.find(); currentIndex = matcher.end()) {
78  20 result.append(content.substring(currentIndex, matcher.start()));
79   
80  20 if (matcher.group(1) != null) {
81  2 result.append(matcher.group(2) + "~" + matcher.group(3));
82    } else {
83  18 result.append("~" + matcher.group(4));
84    }
85    }
86   
87  155 if (currentIndex == 0) {
88  146 return content;
89    }
90   
91  9 result.append(content.substring(currentIndex));
92   
93  9 return result.toString();
94    }
95    }