Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../../img/srcFileCovDistChart5.png 84% of files have more coverage
61   150   39   15.25
54   106   0.64   4
4     9.75  
1    
 
  TableRadeoxMacroConverter       Line # 39 61 0% 39 61 48.7% 0.48739496
 
  (2)
 
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.macro;
21   
22    import java.util.StringTokenizer;
23   
24    import javax.inject.Named;
25    import javax.inject.Singleton;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.rendering.parser.xwiki10.FilterContext;
29    import org.xwiki.rendering.parser.xwiki10.macro.AbstractRadeoxMacroConverter;
30    import org.xwiki.rendering.parser.xwiki10.macro.RadeoxMacroParameters;
31   
32    /**
33    * @version $Id: 9b8962d1d1071c1c346115dc2b67f00b365039de $
34    * @since 1.8M1
35    */
36    @Component
37    @Named("table")
38    @Singleton
 
39    public class TableRadeoxMacroConverter extends AbstractRadeoxMacroConverter
40    {
 
41  2 toggle @Override
42    public String convert(String name, RadeoxMacroParameters parameters, String content, FilterContext filterContext)
43    {
44  2 StringBuffer result = new StringBuffer();
45   
46  2 StringTokenizer tokenizer = new StringTokenizer(content, "|\n", true);
47  2 String lastToken = null;
48  2 boolean firstCell = true;
49  2 int nbLines = 0;
50  33 while (tokenizer.hasMoreTokens()) {
51  31 String token = tokenizer.nextToken();
52    // If a token contains [, then all tokens up to one containing a ] are concatenated. Kind of a block marker.
53  31 if (token.indexOf('[') != -1 && token.indexOf(']') == -1) {
54  0 String linkToken = "";
55  0 while (token.indexOf(']') == -1 && tokenizer.hasMoreTokens()) {
56  0 linkToken += token;
57  0 token = tokenizer.nextToken();
58    }
59  0 token = linkToken + token;
60    }
61  31 if ("\n".equals(token)) {
62    // New line: either new row, or a literal newline.
63  7 lastToken = (lastToken == null) ? "" : lastToken;
64  7 if (!lastToken.endsWith("\\")) {
65    // A new row, not a literal newline.
66    // If the last cell didn't contain any data, then it was skipped. Add a blank cell to compensate.
67  7 if (("".equals(lastToken) || "|".equals(lastToken)) && !firstCell) {
68  1 result.append(filterContext.addProtectedContent(nbLines == 1 ? "|=" : "|", false));
69  1 result.append(" ");
70    }
71  7 if (nbLines > 0) {
72  5 result.append('\n');
73    }
74  7 ++nbLines;
75    } else {
76    // A continued row, with a literal newline.
77  0 String cell = lastToken;
78    // Keep concatenating while the cell data ends with \\
79  0 while (cell.endsWith("\\") && tokenizer.hasMoreTokens()) {
80  0 token = tokenizer.nextToken();
81  0 if (!"|".equals(token)) {
82  0 cell = cell + token;
83    } else {
84  0 break;
85    }
86    }
87  0 firstCell = false;
88  0 result.append(filterContext.addProtectedContent(nbLines == 1 ? "|=" : "|", false));
89  0 result.append(cell.trim());
90  0 if (!tokenizer.hasMoreTokens()) {
91  0 result.append('\n');
92  0 ++nbLines;
93    }
94    }
95  24 } else if (!"|".equals(token)) {
96    // Cell data
97  14 if (!token.endsWith("\\")) {
98    // If the cell data ends with \\, then it will be continued. Current data is stored in lastToken.
99  14 result.append(filterContext.addProtectedContent(nbLines == 1 ? "|=" : "|", false));
100  14 String value = token.trim();
101  14 result.append(value.length() > 0 ? value : " ");
102  14 firstCell = false;
103  0 } else if (!tokenizer.hasMoreTokens()) {
104    // Remove backslashes from the end
105  0 while (token.endsWith("\\")) {
106  0 token = token.substring(0, token.length() - 1);
107    }
108  0 result.append(filterContext.addProtectedContent(nbLines == 1 ? "|=" : "|", false));
109  0 String value = token.trim();
110  0 result.append(value.length() > 0 ? value : " ");
111    }
112  10 } else if ("|".equals(token)) {
113    // Cell delimiter
114  10 if ((null == lastToken || "".equals(lastToken)) && !firstCell || "|".equals(lastToken)) {
115    // If the last cell didn't contain any data, then it was skipped. Add a blank cell to compensate.
116  0 result.append(filterContext.addProtectedContent(nbLines == 1 ? "|=" : "|", false));
117  0 result.append(" ");
118  0 firstCell = false;
119  10 } else if (lastToken.endsWith("\\")) {
120    // The last cell wasn't added because it ended with a continuation mark (\\). Add it now.
121  0 result.append(filterContext.addProtectedContent(nbLines == 1 ? "|=" : "|", false));
122  0 String value = lastToken.trim();
123  0 result.append(value.length() > 0 ? value : " ");
124  0 firstCell = false;
125    }
126    }
127  31 lastToken = token;
128    }
129   
130  2 return result.toString();
131    }
132   
 
133  6 toggle @Override
134    public boolean supportContent()
135    {
136  6 return true;
137    }
138   
 
139  4 toggle @Override
140    public boolean isInline()
141    {
142  4 return false;
143    }
144   
 
145  2 toggle @Override
146    public boolean protectResult()
147    {
148  2 return false;
149    }
150    }