Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../img/srcFileCovDistChart8.png 68% of files have more coverage
54   263   31   4.5
34   119   0.57   12
12     2.58  
1    
 
  CleanUtil       Line # 35 54 0% 31 28 72% 0.72
 
  (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.parser.xwiki10.util;
21   
22    import java.util.regex.Matcher;
23    import java.util.regex.Pattern;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.rendering.internal.parser.xwiki10.VelocityFilter;
27    import org.xwiki.rendering.parser.xwiki10.FilterContext;
28   
29    /**
30    * Contains syntax cleaning helpers.
31    *
32    * @version $Id: 4dbfe2fb0e9e1d6e04128c2bcfc96fefd50e4866 $
33    * @since 1.8M1
34    */
 
35    public final class CleanUtil
36    {
37    /**
38    * Match all the first new lines.
39    */
40    private static final Pattern STARTING_NL_GROUP_PATTERN = Pattern.compile("^\\n*");
41   
42    private static final Pattern STARTING_NLNOOUTPUT_GROUP_PATTERN =
43    Pattern.compile("^" + VelocityFilter.NLGROUP_SPATTERN);
44   
45    /**
46    * Match all the last new lines.
47    */
48    private static final Pattern ENDING_NL_GROUP_PATTERN = Pattern.compile("\\n*$");
49   
50    private static final Pattern ENDING_NLNOOUTPUT_GROUP_PATTERN =
51    Pattern.compile(VelocityFilter.NLGROUP_SPATTERN + "$");
52   
53    /**
54    * Match space, tab or new line.
55    */
56    private static final Pattern HTMLSPACEORNEWLINE_PATTERN = Pattern.compile("[\\s\\n]");
57   
58    /**
59    * Match XWiki 1.0 escaping syntax.
60    */
61    private static final Pattern ESCAPE_PATTERN = Pattern.compile("([^\\\\])\\\\\\\\|([^\\\\])\\\\");
62   
63    /**
64    * Utility class.
65    */
 
66  0 toggle private CleanUtil()
67    {
68   
69    }
70   
71    /**
72    * Replace all spaces/new line groupes by one space.
73    *
74    * @param content the content to convert.
75    * @return the converted string.
76    */
 
77  0 toggle public static String cleanSpacesAndNewLines(String content)
78    {
79  0 return HTMLSPACEORNEWLINE_PATTERN.matcher(content).replaceAll(" ");
80    }
81   
82    /**
83    * Remove first new lines if there is more than 0 and less or equals to the provided number.
84    *
85    * @param content the content to convert.
86    * @param nb the number of new lines to match.
87    * @param replaceWithSpace indicate if the removed new lines are replaced with a white space.
88    * @return the converted string.
89    */
 
90  0 toggle public static String removeLeadingNewLines(String content, int nb, boolean replaceWithSpace)
91    {
92  0 String cleanedContent = content;
93   
94  0 Matcher matcher = STARTING_NL_GROUP_PATTERN.matcher(content);
95   
96  0 int foundNb = matcher.find() ? matcher.end() - matcher.start() : 0;
97   
98  0 if (foundNb > 0 && foundNb <= nb) {
99  0 cleanedContent = content.substring(foundNb > nb ? nb : foundNb);
100  0 if (replaceWithSpace) {
101  0 cleanedContent = " " + cleanedContent;
102    }
103    }
104   
105  0 return cleanedContent;
106    }
107   
108    /**
109    * Remove last new lines if there is more than 0 and less or equals to the provided number.
110    *
111    * @param content the content to convert.
112    * @param nb the number of new lines to match.
113    * @param replaceWithSpace indicate if the removed new lines are replaced with a white space.
114    * @return the converted string.
115    */
 
116  30 toggle public static String removeTrailingNewLines(String content, int nb, boolean replaceWithSpace)
117    {
118  30 String cleanedContent = content;
119   
120  30 Matcher matcher = ENDING_NL_GROUP_PATTERN.matcher(content);
121   
122  30 int foundNb = matcher.find() ? matcher.end() - matcher.start() : 0;
123   
124  30 if (foundNb > 0 && foundNb <= nb) {
125  14 cleanedContent = content.substring(0, content.length() - (foundNb > nb ? nb : foundNb));
126  14 if (replaceWithSpace) {
127  14 cleanedContent = cleanedContent + " ";
128    }
129    }
130   
131  30 return cleanedContent;
132    }
133   
134    /**
135    * Check the provided string contains enough new lines at the beginning and add the need ones.
136    *
137    * @param content the content to convert.
138    * @param nb the number of new lines the string need to contains at the beginning.
139    * @return the converted string.
140    */
 
141  47 toggle public static String setLeadingNewLines(String content, int nb)
142    {
143  47 String cleanedContent = content;
144   
145  47 Matcher matcher = STARTING_NLNOOUTPUT_GROUP_PATTERN.matcher(content);
146   
147  47 int foundNb = matcher.find() ? StringUtils.countMatches(matcher.group(0), "\n") : 0;
148   
149  47 if (foundNb < nb) {
150  45 cleanedContent = StringUtils.repeat("\n", nb - foundNb) + content;
151    }
152   
153  47 return cleanedContent;
154    }
155   
156    /**
157    * Check the provided string contains enough new lines at the end and add the need ones.
158    *
159    * @param content the content to convert.
160    * @param nb the number of new lines the string need to contains at the end.
161    * @return the converted string.
162    */
 
163  22 toggle public static String setTrailingNewLines(String content, int nb)
164    {
165  22 String cleanedContent = content;
166   
167  22 Matcher matcher = ENDING_NLNOOUTPUT_GROUP_PATTERN.matcher(content);
168   
169  22 int foundNb = matcher.find() ? StringUtils.countMatches(matcher.group(0), "\n") : 0;
170   
171  22 if (foundNb < nb) {
172  18 cleanedContent = content + StringUtils.repeat("\n", nb - foundNb);
173    }
174   
175  22 return cleanedContent;
176    }
177   
178    /**
179    * Check the provided string contains enough new lines at the end and add the need ones.
180    *
181    * @param content the content to convert.
182    * @param nb the number of new lines the string need to contains at the end.
183    */
 
184  27 toggle public static void setTrailingNewLines(StringBuffer content, int nb)
185    {
186  27 Matcher matcher = ENDING_NLNOOUTPUT_GROUP_PATTERN.matcher(content);
187   
188  27 int foundNb = matcher.find() ? StringUtils.countMatches(matcher.group(0), "\n") : 0;
189   
190  27 if (foundNb < nb) {
191  16 content.append(StringUtils.repeat("\n", nb - foundNb));
192    }
193    }
194   
195    /**
196    * Remove all the first new lines.
197    *
198    * @param content the content to convert.
199    * @return the converted string.
200    */
 
201  113 toggle public static String removeLeadingNewLines(String content)
202    {
203  113 return STARTING_NL_GROUP_PATTERN.matcher(content).replaceAll("");
204    }
205   
206    /**
207    * Remove all the last new lines.
208    *
209    * @param content the content to convert.
210    * @return the converted string.
211    */
 
212  94 toggle public static String removeTrailingNewLines(String content)
213    {
214  94 return ENDING_NL_GROUP_PATTERN.matcher(content).replaceAll("");
215    }
216   
217    /**
218    * @param content the content to convert.
219    * @return the converted string.
220    */
 
221  0 toggle public static String convertEscape(String content)
222    {
223  0 return ESCAPE_PATTERN.matcher(content).replaceAll("$1~");
224    }
225   
 
226  26 toggle public static String extractVelocity(CharSequence content, FilterContext filterContext)
227    {
228  26 return extractVelocity(content, filterContext, false, false);
229    }
230   
 
231  40 toggle public static String extractVelocity(CharSequence content, FilterContext filterContext, boolean protect,
232    boolean inline)
233    {
234  40 String cleanedContent = content.toString();
235   
236  40 Matcher velocityOpenMatcher = VelocityFilter.VELOCITYOPEN_PATTERN.matcher(cleanedContent);
237  40 boolean velocityOpen = velocityOpenMatcher.find();
238  40 cleanedContent = velocityOpenMatcher.replaceFirst("");
239  40 Matcher velocityCloseMatcher = VelocityFilter.VELOCITYCLOSE_PATTERN.matcher(cleanedContent);
240  40 boolean velocityClose = velocityCloseMatcher.find();
241  40 cleanedContent = velocityCloseMatcher.replaceFirst("");
242   
243  40 StringBuffer buffer = new StringBuffer();
244   
245  40 boolean multilines = filterContext.unProtect(cleanedContent).indexOf("\n") != -1;
246   
247  40 if (velocityOpen) {
248  4 VelocityFilter.appendVelocityOpen(buffer, filterContext, multilines);
249    }
250   
251  40 if (protect) {
252  14 buffer.append(filterContext.addProtectedContent(cleanedContent, inline));
253    } else {
254  26 buffer.append(cleanedContent);
255    }
256   
257  40 if (velocityClose) {
258  6 VelocityFilter.appendVelocityClose(buffer, filterContext, multilines);
259    }
260   
261  40 return buffer.toString();
262    }
263    }