1. Project Clover database Tue Dec 20 2016 21:24:09 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  1447 toggle @Override
32    protected String getLabel(String[] chunks)
33    {
34  1447 return chunks[0];
35    }
36   
 
37  1447 toggle @Override
38    protected String getLink(String[] chunks)
39    {
40  1447 return chunks[1];
41    }
42   
 
43  1447 toggle @Override
44    protected WikiParameters getParameters(String[] chunks)
45    {
46  1447 return XWikiWikiParameters.newWikiParameters(chunks[2]);
47    }
48   
 
49  1447 toggle @Override
50    protected String[] splitToChunks(String str)
51    {
52  1447 String[] chunks = new String[3];
53   
54  1447 char[] array = str.toCharArray();
55   
56  1447 StringBuffer label = new StringBuffer();
57  1447 StringBuffer reference = new StringBuffer();
58  1447 StringBuffer parameters = new StringBuffer();
59   
60  1447 boolean foundReference = false;
61  1447 int i = 0;
62  1447 int nb;
63  38555 for (boolean escaped = false; i < array.length; ++i) {
64  37983 char c = array[i];
65   
66  37983 if (!escaped) {
67  37949 if (array[i] == '~') {
68  34 escaped = true;
69  ? } else if ((nb = countFirstChar(array, i, '>')) >= 2) {
70  782 for (; nb > 2; --nb) {
71  1 label.append(array[i++]);
72    }
73  781 foundReference = true;
74  781 i += 2;
75  781 parseReference(array, i, reference, parameters);
76  781 break;
77  ? } else if ((nb = countFirstChar(array, i, '|')) >= 2) {
78  95 for (; nb > 2; --nb) {
79  1 label.append(array[i++]);
80    }
81  94 i += 2;
82  94 parameters.append(array, i, array.length - i);
83  94 break;
84  37040 } else if (c == '[' && i + 1 < array.length
85    && array[i + 1] == '[')
86    {
87  254 int endLink = findEndLink(array, i + 2);
88  254 if (endLink != -1) {
89    // If we find an internal link we skip it
90  241 label.append(array, i, endLink - i);
91  241 i = endLink - 1;
92    } else {
93  13 label.append("[[");
94  13 ++i;
95    }
96    } else {
97  36786 label.append(c);
98    }
99    } else {
100  34 label.append(c);
101  34 escaped = false;
102    }
103    }
104   
105  1447 if (!foundReference) {
106  666 chunks[1] = label.toString();
107    } else {
108  781 chunks[0] = label.toString();
109  781 chunks[1] = reference.toString();
110    }
111   
112  1447 if (parameters.length() > 0) {
113  218 chunks[2] = parameters.toString();
114    }
115   
116  1447 return chunks;
117    }
118   
 
119  254 toggle private int findEndLink(char[] array, int i)
120    {
121  254 int linkdepth = 1;
122  254 int endLink = -1;
123   
124  9259 for (boolean escaped = false; i < array.length; ++i) {
125  9246 char c = array[i];
126   
127  9246 if (!escaped) {
128  9244 if (array[i] == '~') {
129  2 escaped = true;
130  9242 } else if (c == '[' && i + 1 < array.length
131    && array[i + 1] == '[')
132    {
133  0 ++linkdepth;
134  0 ++i;
135  9242 } else if (c == ']' && i + 1 < array.length
136    && array[i + 1] == ']')
137    {
138  241 --linkdepth;
139  241 ++i;
140  241 endLink = i + 1;
141  241 if (linkdepth == 0) {
142  241 break;
143    }
144    }
145    } else {
146  2 escaped = false;
147    }
148    }
149   
150  254 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  781 toggle private void parseReference(char[] array, int i, StringBuffer reference,
162    StringBuffer parameters)
163    {
164  781 int nb;
165   
166  39714 for (boolean escaped = false; i < array.length; ++i) {
167  39057 char c = array[i];
168   
169  39057 if (!escaped) {
170  39047 if (array[i] == '~' && !escaped) {
171  10 escaped = true;
172  ? } else if ((nb = countFirstChar(array, i, '|')) >= 2) {
173  124 for (; nb > 2; --nb) {
174  0 reference.append(array[i++]);
175    }
176  124 i += 2;
177  124 parameters.append(array, i, array.length - i);
178  124 break;
179    } else {
180  38913 reference.append(c);
181    }
182    } else {
183  10 reference.append(c);
184  10 escaped = false;
185    }
186    }
187    }
188   
 
189  114086 toggle private int countFirstChar(char[] array, int i, char c)
190    {
191  114086 int nb = 0;
192  116099 for (; i < array.length && array[i] == c; ++i) {
193  2013 ++nb;
194    }
195   
196  114086 return nb;
197    }
198    }