1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.logging.internal.helpers; |
21 |
|
|
|
|
| 87.7% |
Uncovered Elements: 13 (106) |
Complexity: 28 |
Complexity Density: 0.45 |
|
22 |
|
public class MessageParser |
23 |
|
{ |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
public static final String ARGUMENT_STR = "{}"; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
static final char ARGUMENT_START = '{'; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
static final char ARGUMENT_STOP = '}'; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
private static final char ESCAPE_CHAR = '\\'; |
43 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
44 |
|
public static class MessageElement |
45 |
|
{ |
46 |
|
private String string; |
47 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
48 |
10616 |
protected MessageElement(String string)... |
49 |
|
{ |
50 |
10616 |
this.string = string; |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
6327 |
public String getString()... |
54 |
|
{ |
55 |
6327 |
return this.string; |
56 |
|
} |
57 |
|
} |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
59 |
|
public static class MessageIndex extends MessageElement |
60 |
|
{ |
61 |
|
private int index; |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
63 |
4301 |
public MessageIndex(String string, int index)... |
64 |
|
{ |
65 |
4301 |
super(string); |
66 |
|
|
67 |
4301 |
this.index = index; |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
20 |
public int getIndex()... |
71 |
|
{ |
72 |
20 |
return this.index; |
73 |
|
} |
74 |
|
} |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
76 |
|
public static class MessageString extends MessageElement |
77 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
6315 |
public MessageString(String string)... |
79 |
|
{ |
80 |
6315 |
super(string); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
|
private char[] buffer; |
85 |
|
|
86 |
|
private boolean translations; |
87 |
|
|
88 |
|
private int bufferIndex; |
89 |
|
|
90 |
|
private int currentMessageIndex; |
91 |
|
|
92 |
|
private boolean previousWasDoubleEscaped; |
93 |
|
|
94 |
|
private MessageElement currentMessageElement; |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
2213 |
public MessageParser(String buffer, boolean translations)... |
97 |
|
{ |
98 |
2213 |
this(buffer.toCharArray(), translations); |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
101 |
2213 |
public MessageParser(char[] buffer, boolean translations)... |
102 |
|
{ |
103 |
2213 |
this.buffer = buffer; |
104 |
2213 |
this.translations = translations; |
105 |
|
} |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
107 |
2212 |
public MessageElement getCurrentMessageElement()... |
108 |
|
{ |
109 |
2212 |
return this.currentMessageElement; |
110 |
|
} |
111 |
|
|
|
|
| 92.1% |
Uncovered Elements: 5 (63) |
Complexity: 13 |
Complexity Density: 0.32 |
|
112 |
12829 |
public MessageElement next()... |
113 |
|
{ |
114 |
12829 |
StringBuilder str = new StringBuilder(); |
115 |
|
|
116 |
12829 |
int i = this.bufferIndex; |
117 |
80125 |
for (; i < this.buffer.length; ++i) { |
118 |
75776 |
if (this.buffer[i] == ESCAPE_CHAR) { |
119 |
6 |
int nb = countEscaping(i); |
120 |
|
|
121 |
6 |
int iNext = i + nb; |
122 |
|
|
123 |
6 |
if (iNext == this.buffer.length) { |
124 |
0 |
i = iNext; |
125 |
0 |
break; |
126 |
|
} else { |
127 |
6 |
if (isMessageIndex(iNext) > iNext) { |
128 |
6 |
str.append(this.buffer, this.bufferIndex, iNext - 1 - this.bufferIndex); |
129 |
6 |
i = iNext; |
130 |
6 |
this.bufferIndex = i; |
131 |
|
|
132 |
|
|
133 |
6 |
if (nb > 1) { |
134 |
4 |
this.previousWasDoubleEscaped = true; |
135 |
4 |
this.currentMessageElement = new MessageString(str.toString()); |
136 |
|
|
137 |
4 |
return this.currentMessageElement; |
138 |
|
} |
139 |
|
} else { |
140 |
0 |
i = iNext; |
141 |
|
} |
142 |
|
} |
143 |
75770 |
} else if (this.buffer[i] == ARGUMENT_START) { |
144 |
8478 |
int iNext = isMessageIndex(i); |
145 |
8478 |
if (iNext != i) { |
146 |
8476 |
if (this.bufferIndex == i) { |
147 |
|
|
148 |
4301 |
String messageIndexString = String.valueOf(this.buffer, i, iNext - i); |
149 |
|
|
150 |
4301 |
int messageIndex; |
151 |
4301 |
if (this.translations && messageIndexString.length() > 2) { |
152 |
8 |
messageIndex = |
153 |
|
Integer.parseInt(messageIndexString.substring(1, messageIndexString.length() - 1)); |
154 |
|
} else { |
155 |
4293 |
messageIndex = this.currentMessageIndex; |
156 |
|
} |
157 |
4301 |
++this.currentMessageIndex; |
158 |
|
|
159 |
4301 |
if (this.previousWasDoubleEscaped) { |
160 |
4 |
this.currentMessageElement = new MessageIndex("\\" + messageIndexString, messageIndex); |
161 |
|
} else { |
162 |
4297 |
this.currentMessageElement = new MessageIndex(messageIndexString, messageIndex); |
163 |
|
} |
164 |
4301 |
this.bufferIndex = iNext; |
165 |
|
|
166 |
4301 |
return this.currentMessageElement; |
167 |
|
} else { |
168 |
|
|
169 |
4175 |
this.currentMessageElement = |
170 |
|
new MessageString(String.valueOf(this.buffer, this.bufferIndex, i - this.bufferIndex)); |
171 |
4175 |
this.bufferIndex = i; |
172 |
|
|
173 |
4175 |
return this.currentMessageElement; |
174 |
|
} |
175 |
|
} |
176 |
|
} |
177 |
|
} |
178 |
|
|
179 |
4349 |
if (i == this.bufferIndex) { |
180 |
2213 |
return null; |
181 |
|
} |
182 |
|
|
183 |
2136 |
this.currentMessageElement = new MessageString(String.valueOf(this.buffer, this.bufferIndex, i - this.bufferIndex)); |
184 |
|
|
185 |
2136 |
this.bufferIndex = i; |
186 |
|
|
187 |
2136 |
return this.currentMessageElement; |
188 |
|
} |
189 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
190 |
6 |
private int countEscaping(int current)... |
191 |
|
{ |
192 |
6 |
int nb = 1; |
193 |
|
|
194 |
12 |
for (int i = current + 1; i < this.buffer.length && this.buffer[i] == ESCAPE_CHAR; ++i) { |
195 |
6 |
++nb; |
196 |
|
} |
197 |
|
|
198 |
6 |
return nb; |
199 |
|
} |
200 |
|
|
|
|
| 70.4% |
Uncovered Elements: 8 (27) |
Complexity: 9 |
Complexity Density: 0.69 |
|
201 |
8484 |
private int isMessageIndex(int current)... |
202 |
|
{ |
203 |
8484 |
int i = current; |
204 |
|
|
205 |
8484 |
if (this.buffer[current] != ARGUMENT_START) { |
206 |
0 |
return current; |
207 |
|
} |
208 |
|
|
209 |
8484 |
++i; |
210 |
|
|
211 |
8484 |
if (i == this.buffer.length) { |
212 |
0 |
return current; |
213 |
|
} |
214 |
|
|
215 |
8484 |
if (this.translations) { |
216 |
47 |
for (; this.buffer[i] != ARGUMENT_STOP; ++i) { |
217 |
16 |
if (i == this.buffer.length) { |
218 |
0 |
return current; |
219 |
16 |
} else if (this.buffer[i] < '0' || this.buffer[i] > '9') { |
220 |
0 |
return current; |
221 |
|
} |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
8484 |
return this.buffer[i] == ARGUMENT_STOP ? i + 1 : current; |
226 |
|
} |
227 |
|
} |