1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.wikimodel.xwiki.xwiki20

File XWikiReferenceParser.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

46
83
7
1
198
145
38
0.46
11.86
7
5.43

Classes

Class Line # Actions
XWikiReferenceParser 29 83 0% 38 12
0.911764791.2%
 

Contributing tests

This file is covered by 185 tests. .

Source view

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  26673 toggle @Override
32    protected String getLabel(String[] chunks)
33    {
34  26673 return chunks[0];
35    }
36   
 
37  26673 toggle @Override
38    protected String getLink(String[] chunks)
39    {
40  26673 return chunks[1];
41    }
42   
 
43  26673 toggle @Override
44    protected WikiParameters getParameters(String[] chunks)
45    {
46  26674 return XWikiWikiParameters.newWikiParameters(chunks[2]);
47    }
48   
 
49  26673 toggle @Override
50    protected String[] splitToChunks(String str)
51    {
52  26674 String[] chunks = new String[3];
53   
54  26673 char[] array = str.toCharArray();
55   
56  26671 StringBuffer label = new StringBuffer();
57  26674 StringBuffer reference = new StringBuffer();
58  26673 StringBuffer parameters = new StringBuffer();
59   
60  26674 boolean foundReference = false;
61  26674 int i = 0;
62  26672 int nb;
63  490492 for (boolean escaped = false; i < array.length; ++i) {
64  488426 char c = array[i];
65   
66  488426 if (!escaped) {
67  488385 if (array[i] == '~') {
68  34 escaped = true;
69  ? } else if ((nb = countFirstChar(array, i, '>')) >= 2) {
70  23978 for (; nb > 2; --nb) {
71  1 label.append(array[i++]);
72    }
73  23978 foundReference = true;
74  23977 i += 2;
75  23977 parseReference(array, i, reference, parameters);
76  23977 break;
77  ? } else if ((nb = countFirstChar(array, i, '|')) >= 2) {
78  630 for (; nb > 2; --nb) {
79  1 label.append(array[i++]);
80    }
81  629 i += 2;
82  629 parameters.append(array, i, array.length - i);
83  629 break;
84  463752 } else if (c == '[' && i + 1 < array.length
85    && array[i + 1] == '[')
86    {
87  1002 int endLink = findEndLink(array, i + 2);
88  1002 if (endLink != -1) {
89    // If we find an internal link we skip it
90  989 label.append(array, i, endLink - i);
91  989 i = endLink - 1;
92    } else {
93  13 label.append("[[");
94  13 ++i;
95    }
96    } else {
97  462747 label.append(c);
98    }
99    } else {
100  34 label.append(c);
101  34 escaped = false;
102    }
103    }
104   
105  26673 if (!foundReference) {
106  2696 chunks[1] = label.toString();
107    } else {
108  23977 chunks[0] = label.toString();
109  23977 chunks[1] = reference.toString();
110    }
111   
112  26674 if (parameters.length() > 0) {
113  2568 chunks[2] = parameters.toString();
114    }
115   
116  26674 return chunks;
117    }
118   
 
119  1002 toggle private int findEndLink(char[] array, int i)
120    {
121  1002 int linkdepth = 1;
122  1002 int endLink = -1;
123   
124  45451 for (boolean escaped = false; i < array.length; ++i) {
125  45438 char c = array[i];
126   
127  45438 if (!escaped) {
128  45436 if (array[i] == '~') {
129  2 escaped = true;
130  45434 } else if (c == '[' && i + 1 < array.length
131    && array[i + 1] == '[')
132    {
133  0 ++linkdepth;
134  0 ++i;
135  45434 } else if (c == ']' && i + 1 < array.length
136    && array[i + 1] == ']')
137    {
138  989 --linkdepth;
139  989 ++i;
140  989 endLink = i + 1;
141  989 if (linkdepth == 0) {
142  989 break;
143    }
144    }
145    } else {
146  2 escaped = false;
147    }
148    }
149   
150  1002 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  23976 toggle private void parseReference(char[] array, int i, StringBuffer reference,
162    StringBuffer parameters)
163    {
164  23977 int nb;
165   
166  1341004 for (boolean escaped = false; i < array.length; ++i) {
167  1318969 char c = array[i];
168   
169  1318966 if (!escaped) {
170  1318952 if (array[i] == '~' && !escaped) {
171  10 escaped = true;
172  ? } else if ((nb = countFirstChar(array, i, '|')) >= 2) {
173  1939 for (; nb > 2; --nb) {
174  0 reference.append(array[i++]);
175    }
176  1939 i += 2;
177  1939 parameters.append(array, i, array.length - i);
178  1939 break;
179    } else {
180  1317006 reference.append(c);
181    }
182    } else {
183  10 reference.append(c);
184  10 escaped = false;
185    }
186    }
187    }
188   
 
189  2271628 toggle private int countFirstChar(char[] array, int i, char c)
190    {
191  2271658 int nb = 0;
192  2324765 for (; i < array.length && array[i] == c; ++i) {
193  53110 ++nb;
194    }
195   
196  2271650 return nb;
197    }
198    }