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
23   101   6   11.5
8   57   0.26   2
2     3  
1    
 
  SectionSyntaxFilter       Line # 43 23 0% 6 1 97% 0.969697
 
  (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.StringUtils;
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    import org.xwiki.rendering.parser.xwiki10.util.CleanUtil;
35   
36    /**
37    * @version $Id: 9820b52f567c1bffde1859ee22ae682e86521e5e $
38    * @since 1.8M1
39    */
40    @Component
41    @Named("section")
42    @Singleton
 
43    public class SectionSyntaxFilter extends AbstractFilter implements Initializable
44    {
45    private static final Pattern SECTIONSYNTAX_PATTERN =
46    Pattern.compile("^(" + VelocityFilter.SPACEGROUP_OC_SPATTERN + ")?(1(\\.1){0,5}+)[ \\t]++(.++)$",
47    Pattern.MULTILINE);
48   
 
49  92 toggle @Override
50    public void initialize() throws InitializationException
51    {
52  92 setPriority(1000);
53    }
54   
 
55  92 toggle @Override
56    public String filter(String content, FilterContext filterContext)
57    {
58  92 StringBuffer result = new StringBuffer();
59   
60  92 Matcher matcher = SECTIONSYNTAX_PATTERN.matcher(content);
61  92 int currentIndex = 0;
62  111 for (; matcher.find(); currentIndex = matcher.end()) {
63  19 String before = content.substring(currentIndex, matcher.start());
64   
65  19 before += matcher.group(1);
66   
67  19 if (currentIndex > 0) {
68    // 1.0 syntax section consume all following new lines
69  11 before = CleanUtil.removeLeadingNewLines(before);
70  11 before = "\n\n" + before;
71    }
72   
73  19 result.append(before);
74   
75  19 CleanUtil.setTrailingNewLines(result, 2);
76   
77  19 String headerSyntax =
78    filterContext.addProtectedContent(StringUtils.repeat('=', (matcher.group(2).length() + 1) / 2), false);
79   
80  19 String headerContent = headerSyntax + ' ' + matcher.group(4) + ' ' + headerSyntax;
81   
82  19 result.append(CleanUtil.extractVelocity(headerContent, filterContext));
83    }
84   
85  92 if (currentIndex == 0) {
86  84 return content;
87    }
88   
89  8 String end = content.substring(currentIndex);
90   
91  8 if (currentIndex > 0) {
92    // 1.0 syntax section consume all following new lines
93  8 end = CleanUtil.removeLeadingNewLines(end);
94  8 end = "\n\n" + end;
95    }
96   
97  8 result.append(end);
98   
99  8 return result.toString();
100    }
101    }