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.util.HashMap; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.model.EntityType; |
31 |
|
import org.xwiki.model.internal.reference.AbstractEntityReferenceResolver; |
32 |
|
import org.xwiki.model.reference.EntityReference; |
33 |
|
import org.xwiki.model.reference.EntityReferenceProvider; |
34 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
35 |
|
|
36 |
|
|
37 |
|
@link |
38 |
|
@link |
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
43 |
|
@Component |
44 |
|
@Named("solr") |
45 |
|
@Singleton |
|
|
| 90.3% |
Uncovered Elements: 7 (72) |
Complexity: 19 |
Complexity Density: 0.43 |
|
46 |
|
public class SolrFieldStringEntityReferenceResolver extends AbstractEntityReferenceResolver implements |
47 |
|
EntityReferenceResolver<String> |
48 |
|
{ |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
static final char SEPARATOR = '.'; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@SuppressWarnings("serial") |
58 |
|
private static final Map<EntityType, EntityType[]> ENTITY_TYPES = new HashMap<EntityType, EntityType[]>() |
59 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
60 |
1 |
{... |
61 |
1 |
put(EntityType.DOCUMENT, new EntityType[]{EntityType.DOCUMENT, EntityType.SPACE, EntityType.WIKI}); |
62 |
1 |
put(EntityType.ATTACHMENT, new EntityType[]{EntityType.ATTACHMENT, EntityType.DOCUMENT, EntityType.SPACE, |
63 |
|
EntityType.WIKI}); |
64 |
1 |
put(EntityType.SPACE, new EntityType[]{EntityType.SPACE, EntityType.WIKI}); |
65 |
1 |
put(EntityType.OBJECT, new EntityType[]{EntityType.OBJECT, EntityType.DOCUMENT, EntityType.SPACE, |
66 |
|
EntityType.WIKI}); |
67 |
1 |
put(EntityType.OBJECT_PROPERTY, new EntityType[]{EntityType.OBJECT_PROPERTY, EntityType.OBJECT, |
68 |
|
EntityType.DOCUMENT, EntityType.SPACE, EntityType.WIKI}); |
69 |
1 |
put(EntityType.CLASS_PROPERTY, new EntityType[]{EntityType.CLASS_PROPERTY, EntityType.DOCUMENT, |
70 |
|
EntityType.SPACE, EntityType.WIKI}); |
71 |
|
} |
72 |
|
}; |
73 |
|
|
74 |
|
@Inject |
75 |
|
@Named("current") |
76 |
|
private EntityReferenceProvider currentEntityReferenceProvider; |
77 |
|
|
|
|
| 83.3% |
Uncovered Elements: 5 (30) |
Complexity: 7 |
Complexity Density: 0.39 |
|
78 |
5 |
@Override... |
79 |
|
public EntityReference resolve(String entityReferenceRepresentation, EntityType type, Object... parameters) |
80 |
|
{ |
81 |
5 |
EntityType[] entityTypesForType = ENTITY_TYPES.get(type); |
82 |
5 |
if (entityTypesForType == null) { |
83 |
0 |
throw new RuntimeException("No parsing definition found for Entity Type [" + type + "]"); |
84 |
|
} |
85 |
|
|
86 |
5 |
EntityReference entityReference = null; |
87 |
5 |
int entityTypeOffset = 0; |
88 |
5 |
int offset = entityReferenceRepresentation.length() - 1; |
89 |
22 |
while (offset >= 0) { |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
17 |
entityTypeOffset = Math.min(entityTypeOffset, entityTypesForType.length - 2); |
94 |
|
|
95 |
17 |
StringBuilder entityName = new StringBuilder(); |
96 |
17 |
offset = readEntityName(entityReferenceRepresentation, entityName, offset); |
97 |
17 |
EntityReference parent = getNewEntityReference(entityName.reverse().toString(), |
98 |
|
entityTypesForType[entityTypeOffset++], parameters); |
99 |
17 |
entityReference = entityReference == null ? parent : entityReference.appendParent(parent); |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
11 |
for (int i = entityTypeOffset; i < entityTypesForType.length; i++) { |
104 |
6 |
EntityReference parent = resolveDefaultReference(entityTypesForType[i], parameters); |
105 |
6 |
if (parent != null) { |
106 |
6 |
entityReference = entityReference == null ? parent : entityReference.appendParent(parent); |
107 |
|
} else { |
108 |
|
|
109 |
0 |
break; |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
5 |
return entityReference; |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
@param |
122 |
|
@param |
123 |
|
@param |
124 |
|
@return |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 6 |
Complexity Density: 0.6 |
|
126 |
108 |
private int readCharacter(String input, StringBuilder output, int offset)... |
127 |
|
{ |
128 |
108 |
if (offset >= 0) { |
129 |
103 |
char c = input.charAt(offset); |
130 |
103 |
if (c == SEPARATOR) { |
131 |
19 |
if (offset == 0 || (offset > 0 && input.charAt(offset - 1) != SEPARATOR)) { |
132 |
12 |
return offset; |
133 |
|
} else { |
134 |
7 |
output.append(c); |
135 |
7 |
return offset - 2; |
136 |
|
} |
137 |
|
} else { |
138 |
84 |
output.append(c); |
139 |
84 |
return offset - 1; |
140 |
|
} |
141 |
|
} |
142 |
5 |
return offset; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
@param |
149 |
|
@param |
150 |
|
@param |
151 |
|
@return |
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
153 |
17 |
private int readEntityName(String input, StringBuilder output, int inputOffset)... |
154 |
|
{ |
155 |
17 |
int oldOffset; |
156 |
17 |
int offset = inputOffset; |
157 |
17 |
do { |
158 |
108 |
oldOffset = offset; |
159 |
108 |
offset = readCharacter(input, output, offset); |
160 |
108 |
} while (offset != oldOffset); |
161 |
17 |
return offset - 1; |
162 |
|
} |
163 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
164 |
17 |
private EntityReference getNewEntityReference(String name, EntityType type, Object... parameters)... |
165 |
|
{ |
166 |
17 |
if ("".equals(name)) { |
167 |
0 |
return resolveDefaultReference(type, parameters); |
168 |
|
} else { |
169 |
17 |
return new EntityReference(name, type); |
170 |
|
} |
171 |
|
} |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
173 |
4 |
@Override... |
174 |
|
protected EntityReference getDefaultReference(EntityType type, Object... parameters) |
175 |
|
{ |
176 |
4 |
return currentEntityReferenceProvider.getDefaultReference(type); |
177 |
|
} |
178 |
|
} |