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
82   195   27   27.33
46   136   0.33   3
3     9  
1    
 
  ListSyntaxFilter       Line # 46 82 0% 27 5 96.2% 0.96183205
 
  (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.text.MessageFormat;
23    import java.util.regex.Matcher;
24    import java.util.regex.Pattern;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.phase.Initializable;
34    import org.xwiki.component.phase.InitializationException;
35    import org.xwiki.rendering.parser.xwiki10.AbstractFilter;
36    import org.xwiki.rendering.parser.xwiki10.FilterContext;
37    import org.xwiki.rendering.parser.xwiki10.util.CleanUtil;
38   
39    /**
40    * @version $Id: 8ef3af651b74e936c2886a0dfc34de3c423a837c $
41    * @since 1.8M1
42    */
43    @Component
44    @Named("list")
45    @Singleton
 
46    public class ListSyntaxFilter extends AbstractFilter implements Initializable
47    {
48    private static final Pattern LISTYNTAX_PATTERN =
49    Pattern.compile("(?:^" + VelocityFilter.SPACEGROUP_OC_SPATTERN
50    + "([-#*]++|[-#*iIaA1ghHkKj]++\\.)[\\p{Blank}]++[^\r\n]++([ \t]*+[\r\n]++)*+)++", Pattern.MULTILINE);
51   
52    private static final Pattern LISTITEMSYNTAX_PATTERN =
53    Pattern.compile("^(" + VelocityFilter.SPACEGROUP_OC_SPATTERN
54    + ")([-#*]++|[-#*iIaA1ghHkKj]++\\.)([\\p{Blank}]++[^\r\n]++)([ \t]*+[\r\n]++)*+", Pattern.MULTILINE);
55   
56    /**
57    * The logger to log.
58    */
59    @Inject
60    private Logger logger;
61   
 
62  92 toggle @Override
63    public void initialize() throws InitializationException
64    {
65  92 setPriority(900);
66    }
67   
 
68  92 toggle @Override
69    public String filter(String content, FilterContext filterContext)
70    {
71  92 StringBuffer result = new StringBuffer();
72   
73  92 Matcher matcher = LISTYNTAX_PATTERN.matcher(content);
74   
75  92 int currentIndex = 0;
76  99 for (; matcher.find(); currentIndex = matcher.end()) {
77  7 String before = content.substring(currentIndex, matcher.start());
78   
79  7 if (currentIndex > 0) {
80  2 before = CleanUtil.setTrailingNewLines(CleanUtil.setLeadingNewLines(before, 2), 2);
81    }
82   
83  7 result.append(before);
84  7 result.append(filterList(matcher.group(0), filterContext));
85    }
86   
87  92 if (currentIndex == 0) {
88  87 return content;
89    }
90   
91  5 result.append(CleanUtil.setLeadingNewLines(content.substring(currentIndex), 2));
92   
93  5 return result.toString();
94    }
95   
 
96  7 toggle public String filterList(String content, FilterContext filterContext)
97    {
98  7 StringBuffer listResult = new StringBuffer();
99   
100  7 Matcher matcher = LISTITEMSYNTAX_PATTERN.matcher(content);
101   
102  7 int currentIndex = 0;
103  7 char currentListSign = 0;
104  43 for (; matcher.find(); currentIndex = matcher.end()) {
105  36 String before = content.substring(currentIndex, matcher.start());
106   
107  36 if (currentIndex > 0) {
108  29 before = CleanUtil.setLeadingNewLines(before, 1);
109    }
110   
111  36 StringBuffer listItemResult = new StringBuffer();
112   
113  36 String listSigns = matcher.group(2);
114  36 char listSign = listSigns.charAt(0);
115   
116  36 String listString;
117  36 String listStyle = "";
118   
119  36 if (listSign == '#') {
120  1 listString = StringUtils.repeat('1', listSigns.length()) + ".";
121  35 } else if (listSign == '1') {
122  3 listString = listSigns;
123    } else {
124  32 if (listSign == '-') {
125  2 listStyle = "square";
126  2 listString = StringUtils.repeat('*', listSigns.length());
127  30 } else if (listSign == 'a') {
128  2 listStyle = "lower-alpha";
129  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
130  28 } else if (listSign == 'A') {
131  2 listStyle = "upper-alpha";
132  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
133  26 } else if (listSign == 'i') {
134  2 listStyle = "lower-roman";
135  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
136  24 } else if (listSign == 'I') {
137  2 listStyle = "upper-roman";
138  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
139  22 } else if (listSign == 'g') {
140  2 listStyle = "lower-greek";
141  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
142  20 } else if (listSign == 'h') {
143  2 listStyle = "hiragana";
144  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
145  18 } else if (listSign == 'H') {
146  2 listStyle = "hiragana-iroha";
147  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
148  16 } else if (listSign == 'k') {
149  2 listStyle = "katakana";
150  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
151  14 } else if (listSign == 'K') {
152  2 listStyle = "katakana-iroha";
153  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
154  12 } else if (listSign == 'j') {
155  2 listStyle = "hebrew";
156  2 listString = StringUtils.repeat('*', listSigns.length() - 1);
157  10 } else if (listSign == '*') {
158  10 listString = listSigns;
159    } else {
160    // This should never append
161  0 this.logger.error("Unknown list sign: " + listSign);
162   
163  0 listString = StringUtils.repeat('*', listSigns.length());
164    }
165    }
166   
167  36 if (listSign != currentListSign) {
168  19 if (currentListSign != 0 && currentIndex > 0) {
169  12 before = CleanUtil.setTrailingNewLines(before, 2);
170    }
171   
172  19 if (listStyle.length() > 0) {
173  11 listItemResult.append(filterContext.addProtectedContent(MessageFormat.format(
174    "(% style=\"list-style-type: {0}\" %)\n", listStyle), false));
175    }
176    }
177  36 listItemResult.append(filterContext.addProtectedContent(matcher.group(1) + listString, false));
178   
179  36 listItemResult.append(matcher.group(3));
180   
181  36 listResult.append(before);
182  36 listResult.append(listItemResult);
183   
184  36 currentListSign = listSign;
185    }
186   
187  7 if (currentIndex == 0) {
188  0 return content;
189    }
190   
191  7 listResult.append(content.substring(currentIndex));
192   
193  7 return CleanUtil.extractVelocity(listResult, filterContext);
194    }
195    }