1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.model.reference; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.xwiki.component.annotation.Component; |
29 |
|
import org.xwiki.model.EntityType; |
30 |
|
import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer; |
31 |
|
import org.xwiki.model.reference.EntityReference; |
32 |
|
import org.xwiki.model.reference.EntityReferenceProvider; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
@version |
40 |
|
@since |
41 |
|
|
42 |
|
@Component |
43 |
|
@Singleton |
44 |
|
@Named("compact") |
|
|
| 94.7% |
Uncovered Elements: 4 (76) |
Complexity: 27 |
Complexity Density: 0.63 |
|
45 |
|
public class CompactStringEntityReferenceSerializer extends DefaultStringEntityReferenceSerializer |
46 |
|
{ |
47 |
|
@Inject |
48 |
|
@Named("current") |
49 |
|
private EntityReferenceProvider provider; |
50 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 6 |
Complexity Density: 0.5 |
|
51 |
178437 |
@Override... |
52 |
|
public String serialize(EntityReference reference, Object... parameters) |
53 |
|
{ |
54 |
178435 |
if (reference == null) { |
55 |
0 |
return null; |
56 |
|
} |
57 |
|
|
58 |
178434 |
StringBuilder representation = new StringBuilder(); |
59 |
|
|
60 |
178434 |
List<EntityReference> references = reference.getReversedReferenceChain(); |
61 |
709859 |
for (int i = 0; i < references.size();) { |
62 |
531414 |
EntityReference currentReference = references.get(i); |
63 |
531415 |
EntityType currentType = currentReference.getType(); |
64 |
|
|
65 |
|
|
66 |
531639 |
while (++i < references.size() && references.get(i).getType() == currentType) { |
67 |
221 |
currentReference = references.get(i); |
68 |
|
} |
69 |
|
|
70 |
531439 |
if (shouldSerialize(currentReference, representation, currentReference == reference, parameters)) { |
71 |
358159 |
serializeEntityReferenceType(currentReference, representation, currentReference == reference); |
72 |
|
} |
73 |
|
} |
74 |
|
|
75 |
178459 |
return representation.toString(); |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
@since |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 5 |
Complexity Density: 0.71 |
|
81 |
531413 |
protected boolean shouldSerialize(EntityReference currentReference, StringBuilder representation,... |
82 |
|
boolean isLastReference, Object... parameters) |
83 |
|
{ |
84 |
531400 |
boolean shouldPrint = false; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
531433 |
if (isLastReference || representation.length() > 0) { |
93 |
181182 |
shouldPrint = true; |
94 |
|
} else { |
95 |
350252 |
EntityReference defaultReference = resolveDefaultReference(currentReference.getType(), parameters); |
96 |
350266 |
if (defaultReference == null || !equal(defaultReference, currentReference)) { |
97 |
176962 |
shouldPrint = true; |
98 |
|
} |
99 |
|
} |
100 |
|
|
101 |
531429 |
return shouldPrint; |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
@param |
108 |
|
@since |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
110 |
358270 |
protected void serializeEntityReferenceType(EntityReference reference, StringBuilder representation,... |
111 |
|
boolean isLastReference) |
112 |
|
{ |
113 |
358274 |
EntityReference parent = reference.getParent(); |
114 |
358275 |
if (parent != null && parent.getType() == reference.getType()) { |
115 |
135 |
serializeEntityReferenceType(parent, representation, false); |
116 |
|
} |
117 |
|
|
118 |
358283 |
super.serializeEntityReference(reference, representation, isLastReference); |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 5 |
Complexity Density: 0.83 |
|
121 |
176165 |
protected boolean equal(EntityReference defaultReference, EntityReference currentReference)... |
122 |
|
{ |
123 |
176165 |
EntityReference defaultReferenceIt = defaultReference; |
124 |
176163 |
EntityReference currentReferenceIt = currentReference; |
125 |
|
|
126 |
349546 |
for (; defaultReferenceIt != null; defaultReferenceIt = defaultReferenceIt.getParent(), currentReferenceIt = |
127 |
|
currentReferenceIt.getParent()) { |
128 |
176262 |
if (currentReferenceIt == null || defaultReferenceIt.getType() != currentReferenceIt.getType() |
129 |
|
|| !defaultReferenceIt.getName().equals(currentReferenceIt.getName())) { |
130 |
2877 |
return false; |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
173289 |
return true; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
@since |
139 |
|
|
|
|
| 91.7% |
Uncovered Elements: 2 (24) |
Complexity: 8 |
Complexity Density: 0.57 |
|
140 |
176151 |
protected EntityReference resolveDefaultReference(EntityType type, Object... parameters)... |
141 |
|
{ |
142 |
176157 |
EntityReference resolvedDefaultReference = null; |
143 |
176162 |
if (parameters.length > 0 && parameters[0] instanceof EntityReference) { |
144 |
|
|
145 |
164273 |
EntityReference referenceParameter = (EntityReference) parameters[0]; |
146 |
164275 |
EntityReference extractedReference = referenceParameter.extractReference(type); |
147 |
164277 |
if (extractedReference != null) { |
148 |
164274 |
resolvedDefaultReference = extractedReference; |
149 |
|
|
150 |
|
|
151 |
164272 |
EntityReference parent = resolvedDefaultReference.getParent(); |
152 |
164272 |
while (parent != null && parent.getType() == resolvedDefaultReference.getType()) { |
153 |
0 |
parent = parent.getParent(); |
154 |
|
} |
155 |
164258 |
if (parent != null) { |
156 |
1043 |
resolvedDefaultReference = resolvedDefaultReference.removeParent(parent); |
157 |
|
} |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
176143 |
if (resolvedDefaultReference == null) { |
162 |
11883 |
resolvedDefaultReference = this.provider.getDefaultReference(type); |
163 |
|
} |
164 |
|
|
165 |
176184 |
return resolvedDefaultReference; |
166 |
|
} |
167 |
|
} |