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
17   102   5   8.5
6   59   0.29   2
2     2.5  
1    
 
  CodeMacroFilter       Line # 47 17 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.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
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.Filter;
34    import org.xwiki.rendering.parser.xwiki10.FilterContext;
35    import org.xwiki.rendering.parser.xwiki10.macro.RadeoxMacroConverter;
36    import org.xwiki.rendering.parser.xwiki10.util.CleanUtil;
37   
38    /**
39    * Converts Code Macro.
40    *
41    * @version $Id: a53680de8cf27f21241587a138c13b5f22389a2c $
42    * @since 1.8M1
43    */
44    @Component
45    @Named("code")
46    @Singleton
 
47    public class CodeMacroFilter extends AbstractFilter implements Initializable
48    {
49    private static final Pattern CODEMACRO_PATTERN =
50    Pattern.compile("\\{(code)(?::([^\\}]*))?\\}(.*?)\\{code\\}", Pattern.DOTALL);
51   
52    @Inject
53    @Named("code")
54    private RadeoxMacroConverter codeMacroConverter;
55   
56    @Inject
57    @Named("unescape")
58    private Filter unescapeFilter;
59   
60    private EscapeFilter escapeFilter = new EscapeFilter();
61   
 
62  92 toggle @Override
63    public void initialize() throws InitializationException
64    {
65  92 setPriority(10);
66    }
67   
 
68  92 toggle @Override
69    public String filter(String content, FilterContext filterContext)
70    {
71  92 StringBuffer result = new StringBuffer();
72   
73  92 String escapedContent = this.escapeFilter.filter(content, filterContext, false);
74   
75  92 Matcher matcher = CODEMACRO_PATTERN.matcher(escapedContent);
76  92 int currentIndex = 0;
77  96 for (; matcher.find(); currentIndex = matcher.end()) {
78  4 String before = escapedContent.substring(currentIndex, matcher.start());
79   
80  4 if (result.length() > 0) {
81  1 before = CleanUtil.setLeadingNewLines(before, 2);
82    }
83   
84  4 before = CleanUtil.setTrailingNewLines(before, 2);
85   
86  4 String macroResult =
87    this.codeMacroConverter.convert("code", RadeoxMacrosFilter.getMacroParameters(this.codeMacroConverter,
88    matcher.group(2)), matcher.group(3), filterContext);
89   
90  4 result.append(before);
91  4 result.append(filterContext.addProtectedContent(macroResult, false));
92    }
93   
94  92 if (currentIndex == 0) {
95  89 return content;
96    }
97   
98  3 result.append(CleanUtil.setLeadingNewLines(escapedContent.substring(currentIndex), 2));
99   
100  3 return this.unescapeFilter.filter(result.toString(), filterContext);
101    }
102    }