1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.search.solr.internal; |
21 |
|
|
22 |
|
import java.io.UnsupportedEncodingException; |
23 |
|
import java.net.URLDecoder; |
24 |
|
|
25 |
|
import javax.inject.Singleton; |
26 |
|
|
27 |
|
import org.xwiki.component.annotation.Component; |
28 |
|
import org.xwiki.search.solr.internal.api.SolrFieldNameEncoder; |
29 |
|
|
30 |
|
|
31 |
|
@link |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
@version |
40 |
|
@since |
41 |
|
|
42 |
|
@Component |
43 |
|
@Singleton |
|
|
| 96.2% |
Uncovered Elements: 2 (52) |
Complexity: 14 |
Complexity Density: 0.44 |
|
44 |
|
public class DefaultSolrFieldNameEncoder implements SolrFieldNameEncoder |
45 |
|
{ |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
private static final char ESCAPE = '$'; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
private static final int CASE_DIFF = 'a' - 'A'; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private static final String UTF8 = "UTF-8"; |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 5 |
Complexity Density: 0.33 |
|
61 |
8694 |
@Override... |
62 |
|
public String encode(final String fieldName) |
63 |
|
{ |
64 |
8694 |
if (fieldName == null) { |
65 |
1 |
return null; |
66 |
|
} |
67 |
|
|
68 |
8693 |
int offset = 0; |
69 |
8693 |
final int length = fieldName.length(); |
70 |
8693 |
final StringBuilder output = new StringBuilder(length); |
71 |
8693 |
boolean dirty = false; |
72 |
271132 |
while (offset < length) { |
73 |
262439 |
final int codePoint = fieldName.codePointAt(offset); |
74 |
262439 |
final char[] chars = Character.toChars(codePoint); |
75 |
262439 |
if (needsEncoding(codePoint)) { |
76 |
5 |
encode(chars, output); |
77 |
5 |
dirty = true; |
78 |
|
} else { |
79 |
262434 |
output.append(chars); |
80 |
|
} |
81 |
262439 |
offset += chars.length; |
82 |
|
} |
83 |
8693 |
return dirty ? output.toString() : fieldName; |
84 |
|
} |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
86 |
4 |
@Override... |
87 |
|
public String decode(String fieldName) |
88 |
|
{ |
89 |
4 |
try { |
90 |
4 |
if (fieldName != null) { |
91 |
3 |
return URLDecoder.decode(fieldName.replace('$', '%'), UTF8); |
92 |
|
} |
93 |
|
} catch (UnsupportedEncodingException e) { |
94 |
|
|
95 |
|
} |
96 |
|
|
97 |
1 |
return null; |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
@param@link |
102 |
|
@return |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
104 |
262439 |
protected boolean needsEncoding(int codePoint)... |
105 |
|
{ |
106 |
262439 |
return codePoint == ESCAPE |
107 |
|
|| !(Character.isJavaIdentifierPart(codePoint) || codePoint == '-' || codePoint == '.'); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
@param |
115 |
|
|
|
|
| 88.9% |
Uncovered Elements: 2 (18) |
Complexity: 5 |
Complexity Density: 0.42 |
|
116 |
5 |
protected void encode(char[] chars, StringBuilder output)... |
117 |
|
{ |
118 |
5 |
try { |
119 |
5 |
byte[] bytes = new String(chars).getBytes(UTF8); |
120 |
10 |
for (int i = 0, length = bytes.length; i < length; i++) { |
121 |
5 |
output.append(ESCAPE); |
122 |
5 |
char ch = Character.forDigit((bytes[i] >> 4) & 0xF, 16); |
123 |
|
|
124 |
5 |
if (Character.isLetter(ch)) { |
125 |
0 |
ch -= CASE_DIFF; |
126 |
|
} |
127 |
5 |
output.append(ch); |
128 |
5 |
ch = Character.forDigit(bytes[i] & 0xF, 16); |
129 |
5 |
if (Character.isLetter(ch)) { |
130 |
3 |
ch -= CASE_DIFF; |
131 |
|
} |
132 |
5 |
output.append(ch); |
133 |
|
} |
134 |
|
} catch (UnsupportedEncodingException e) { |
135 |
|
|
136 |
|
} |
137 |
|
} |
138 |
|
} |