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
25   167   8   6.25
8   69   0.32   4
4     2  
1    
 
  FilterContext       Line # 38 25 0% 8 2 94.6% 0.9459459
 
  (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;
21   
22    import java.util.LinkedList;
23    import java.util.List;
24    import java.util.regex.Matcher;
25    import java.util.regex.Pattern;
26   
27    import org.apache.commons.lang3.StringUtils;
28   
29    /**
30    * The XWiki 1.0 to 2.0 conversion context.
31    * <p>
32    * A filter can register a part of the content to protect it. That way this content is not re-parsed by another filter.
33    * for example VelocityCommentsFilter register all Velocity comments.
34    *
35    * @version $Id: c4a38c99765a0a510c5b08d1b1e413db9303c289 $
36    * @since 1.8M1
37    */
 
38    public class FilterContext
39    {
40    public static final String XWIKI1020TOKEN_O = "\255";
41   
42    public static final String XWIKI1020TOKEN_OP = Pattern.quote(XWIKI1020TOKEN_O);
43   
44    public static final String XWIKI1020TOKEN_C = "\255";
45   
46    public static final String XWIKI1020TOKEN_CP = Pattern.quote(XWIKI1020TOKEN_C);
47   
48    private static final String XWIKI1020TOKEN_SF = "XWIKI1020TOKEN";
49   
50    private static final String XWIKI1020TOKENS_SF = XWIKI1020TOKEN_SF + "S";
51   
52    private static final String XWIKI1020TOKENI_SF = XWIKI1020TOKEN_SF + "I";
53   
54    public static final String XWIKI1020TOKEN_SF_SPATTERN = "(?:" + XWIKI1020TOKEN_SF + "[IS])";
55   
56    public static final String XWIKI1020TOKENS_SF_SPATTERN = "(?:" + XWIKI1020TOKEN_SF + "S)";
57   
58    public static final String XWIKI1020TOKENI_SF_SPATTERN = "(?:" + XWIKI1020TOKEN_SF + "I)";
59   
60    /**
61    * Match registered inline content identifier.
62    * <ul>
63    * <li>$1: the suffix</li>
64    * <li>$2: the index</li>
65    * </ul>
66    */
67    public static final Pattern XWIKI1020TOKENI_PATTERN =
68    Pattern.compile(XWIKI1020TOKEN_OP + FilterContext.XWIKI1020TOKENI_SF_SPATTERN + "(\\p{Alpha}*)(\\d+)"
69    + XWIKI1020TOKEN_CP);
70   
71    /**
72    * Match registered content identifier.
73    * <ul>
74    * <li>$1: the suffix</li>
75    * <li>$2: the index</li>
76    * </ul>
77    */
78    public static final Pattern XWIKI1020TOKENS_PATTERN =
79    Pattern.compile(XWIKI1020TOKEN_OP + FilterContext.XWIKI1020TOKENS_SF_SPATTERN + "(\\p{Alpha}*)(\\d+)"
80    + XWIKI1020TOKEN_CP);
81   
82    /**
83    * Match registered content identifier.
84    * <ul>
85    * <li>$1: the suffix</li>
86    * <li>$2: the index</li>
87    * </ul>
88    */
89    public static final Pattern XWIKI1020TOKEN_PATTERN =
90    Pattern.compile(XWIKI1020TOKEN_OP + FilterContext.XWIKI1020TOKEN_SF_SPATTERN + "(\\p{Alpha}*)(\\d+)"
91    + XWIKI1020TOKEN_CP);
92   
93    private List<String> protectedContentList = new LinkedList<String>();
94   
95    /**
96    * Register a content and return the corresponding identifier to be able the reinsert it after the conversion
97    * process.
98    *
99    * @param content the content to protect/register.
100    * @param inline indicate if i's a inline or not inline string.
101    * @return the content identifier to insert in place of provided content.
102    */
 
103  710 toggle public String addProtectedContent(String content, boolean inline)
104    {
105  710 return addProtectedContent(content, "", inline);
106    }
107   
 
108  1104 toggle public String addProtectedContent(String content, String suffix, boolean inline)
109    {
110  1104 if (StringUtils.isEmpty(content)) {
111  0 return "";
112    }
113   
114  1104 this.protectedContentList.add(content);
115   
116  1104 StringBuffer str = new StringBuffer();
117   
118  1104 str.append(XWIKI1020TOKEN_O);
119  1104 str.append(inline ? XWIKI1020TOKENI_SF : XWIKI1020TOKENS_SF);
120  1104 str.append(suffix);
121  1104 str.append(this.protectedContentList.size() - 1);
122  1104 str.append(XWIKI1020TOKEN_C);
123   
124  1104 return str.toString();
125    }
126   
127    /**
128    * @param index the identifier of the registered content.
129    * @return the registered content.
130    */
 
131  953 toggle public String getProtectedContent(int index)
132    {
133  953 return this.protectedContentList.get(index);
134    }
135   
136    /**
137    * Re-insert all protected/registered strings in to the global content.
138    *
139    * @param content the global content.
140    * @return the complete content.
141    */
 
142  471 toggle public String unProtect(String content)
143    {
144  471 StringBuffer result = new StringBuffer();
145  471 Matcher matcher = FilterContext.XWIKI1020TOKEN_PATTERN.matcher(content);
146   
147  471 int current = 0;
148  1408 while (matcher.find()) {
149  937 result.append(content.substring(current, matcher.start()));
150  937 current = matcher.end();
151   
152  937 int index = Integer.valueOf(matcher.group(2));
153   
154  937 String storedContent = getProtectedContent(index);
155   
156  937 result.append(storedContent);
157    }
158   
159  471 if (current == 0) {
160  229 return content;
161    }
162   
163  242 result.append(content.substring(current));
164   
165  242 return unProtect(result.toString());
166    }
167    }