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
83   198   38   11.86
46   145   0.46   7
7     5.43  
1    
 
  XWikiReferenceParser       Line # 29 83 0% 38 12 91.2% 0.9117647
 
  (135)
 
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.wikimodel.xwiki.xwiki20;
21   
22    import org.xwiki.rendering.wikimodel.WikiParameters;
23    import org.xwiki.rendering.wikimodel.WikiReferenceParser;
24   
25    /**
26    * @version $Id: 4e16c533c61978c07df1ae9af11a7f0286cb7267 $
27    * @since 4.0M1
28    */
 
29    public class XWikiReferenceParser extends WikiReferenceParser
30    {
 
31  249 toggle @Override
32    protected String getLabel(String[] chunks)
33    {
34  249 return chunks[0];
35    }
36   
 
37  249 toggle @Override
38    protected String getLink(String[] chunks)
39    {
40  249 return chunks[1];
41    }
42   
 
43  249 toggle @Override
44    protected WikiParameters getParameters(String[] chunks)
45    {
46  249 return XWikiWikiParameters.newWikiParameters(chunks[2]);
47    }
48   
 
49  249 toggle @Override
50    protected String[] splitToChunks(String str)
51    {
52  249 String[] chunks = new String[3];
53   
54  249 char[] array = str.toCharArray();
55   
56  249 StringBuffer label = new StringBuffer();
57  249 StringBuffer reference = new StringBuffer();
58  249 StringBuffer parameters = new StringBuffer();
59   
60  249 boolean foundReference = false;
61  249 int i = 0;
62  249 int nb;
63  4512 for (boolean escaped = false; i < array.length; ++i) {
64  4386 char c = array[i];
65   
66  4386 if (!escaped) {
67  4350 if (array[i] == '~') {
68  36 escaped = true;
69  ? } else if ((nb = countFirstChar(array, i, '>')) >= 2) {
70  82 for (; nb > 2; --nb) {
71  1 label.append(array[i++]);
72    }
73  81 foundReference = true;
74  81 i += 2;
75  81 parseReference(array, i, reference, parameters);
76  81 break;
77  ? } else if ((nb = countFirstChar(array, i, '|')) >= 2) {
78  43 for (; nb > 2; --nb) {
79  1 label.append(array[i++]);
80    }
81  42 i += 2;
82  42 parameters.append(array, i, array.length - i);
83  42 break;
84  4191 } else if (c == '[' && i + 1 < array.length
85    && array[i + 1] == '[')
86    {
87  20 int endLink = findEndLink(array, i + 2);
88  20 if (endLink != -1) {
89    // If we find an internal link we skip it
90  9 label.append(array, i, endLink - i);
91  9 i = endLink - 1;
92    } else {
93  11 label.append("[[");
94  11 ++i;
95    }
96    } else {
97  4171 label.append(c);
98    }
99    } else {
100  36 label.append(c);
101  36 escaped = false;
102    }
103    }
104   
105  249 if (!foundReference) {
106  168 chunks[1] = label.toString();
107    } else {
108  81 chunks[0] = label.toString();
109  81 chunks[1] = reference.toString();
110    }
111   
112  249 if (parameters.length() > 0) {
113  65 chunks[2] = parameters.toString();
114    }
115   
116  249 return chunks;
117    }
118   
 
119  20 toggle private int findEndLink(char[] array, int i)
120    {
121  20 int linkdepth = 1;
122  20 int endLink = -1;
123   
124  458 for (boolean escaped = false; i < array.length; ++i) {
125  447 char c = array[i];
126   
127  447 if (!escaped) {
128  445 if (array[i] == '~') {
129  2 escaped = true;
130  443 } else if (c == '[' && i + 1 < array.length
131    && array[i + 1] == '[')
132    {
133  0 ++linkdepth;
134  0 ++i;
135  443 } else if (c == ']' && i + 1 < array.length
136    && array[i + 1] == ']')
137    {
138  9 --linkdepth;
139  9 ++i;
140  9 endLink = i + 1;
141  9 if (linkdepth == 0) {
142  9 break;
143    }
144    }
145    } else {
146  2 escaped = false;
147    }
148    }
149   
150  20 return endLink;
151    }
152   
153    /**
154    * Extract the link and the parameters.
155    *
156    * @param array the array to extract information from
157    * @param i the current position in the array
158    * @param reference the link buffer to fill
159    * @param parameters the parameters buffer to fill
160    */
 
161  81 toggle private void parseReference(char[] array, int i, StringBuffer reference,
162    StringBuffer parameters)
163    {
164  81 int nb;
165   
166  1024 for (boolean escaped = false; i < array.length; ++i) {
167  966 char c = array[i];
168   
169  966 if (!escaped) {
170  956 if (array[i] == '~' && !escaped) {
171  10 escaped = true;
172  ? } else if ((nb = countFirstChar(array, i, '|')) >= 2) {
173  23 for (; nb > 2; --nb) {
174  0 reference.append(array[i++]);
175    }
176  23 i += 2;
177  23 parameters.append(array, i, array.length - i);
178  23 break;
179    } else {
180  923 reference.append(c);
181    }
182    } else {
183  10 reference.append(c);
184  10 escaped = false;
185    }
186    }
187    }
188   
 
189  9493 toggle private int countFirstChar(char[] array, int i, char c)
190    {
191  9493 int nb = 0;
192  9800 for (; i < array.length && array[i] == c; ++i) {
193  307 ++nb;
194    }
195   
196  9493 return nb;
197    }
198    }