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.skin; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Named; |
24 |
|
import javax.inject.Provider; |
25 |
|
import javax.inject.Singleton; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.StringUtils; |
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.model.reference.AttachmentReference; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
33 |
|
import org.xwiki.model.reference.EntityReference; |
34 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
35 |
|
import org.xwiki.model.reference.LocalDocumentReference; |
36 |
|
import org.xwiki.model.reference.ObjectPropertyReference; |
37 |
|
import org.xwiki.skin.Resource; |
38 |
|
import org.xwiki.skin.Skin; |
39 |
|
|
40 |
|
import com.xpn.xwiki.XWiki; |
41 |
|
import com.xpn.xwiki.XWikiContext; |
42 |
|
import com.xpn.xwiki.XWikiException; |
43 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
44 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
45 |
|
import com.xpn.xwiki.internal.mandatory.XWikiSkinFileOverrideClassDocumentInitializer; |
46 |
|
import com.xpn.xwiki.objects.BaseObject; |
47 |
|
import com.xpn.xwiki.objects.BaseProperty; |
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
@since |
52 |
|
|
53 |
|
@Component(roles = WikiSkinUtils.class) |
54 |
|
@Singleton |
|
|
| 87.9% |
Uncovered Elements: 12 (99) |
Complexity: 29 |
Complexity Density: 0.5 |
|
55 |
|
public class WikiSkinUtils |
56 |
|
{ |
57 |
|
public static final LocalDocumentReference SKINCLASS_REFERENCE = new LocalDocumentReference("XWiki", "XWikiSkins"); |
58 |
|
|
59 |
|
public static final String SKINCLASS_BASESKIN = "baseskin"; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final String NO_VALUE = "---"; |
66 |
|
|
67 |
|
@Inject |
68 |
|
private Provider<XWikiContext> xcontextProvider; |
69 |
|
|
70 |
|
@Inject |
71 |
|
@Named("currentmixed") |
72 |
|
private DocumentReferenceResolver<String> currentMixedDocumentReferenceResolver; |
73 |
|
|
74 |
|
@Inject |
75 |
|
private EntityReferenceSerializer<String> referenceSerializer; |
76 |
|
|
77 |
|
@Inject |
78 |
|
private Logger logger; |
79 |
|
|
|
|
| 84.2% |
Uncovered Elements: 3 (19) |
Complexity: 6 |
Complexity Density: 0.46 |
|
80 |
306465 |
public XWikiDocument getSkinDocument(String skin)... |
81 |
|
{ |
82 |
306471 |
XWikiContext xcontext = this.xcontextProvider.get(); |
83 |
306481 |
if (xcontext != null) { |
84 |
306471 |
DocumentReference skinReference = this.currentMixedDocumentReferenceResolver.resolve(skin); |
85 |
306486 |
XWiki xwiki = xcontext.getWiki(); |
86 |
306484 |
if (xwiki != null && xwiki.getStore() != null) { |
87 |
306483 |
XWikiDocument doc; |
88 |
306482 |
try { |
89 |
306481 |
doc = xwiki.getDocument(skinReference, xcontext); |
90 |
|
} catch (XWikiException e) { |
91 |
0 |
this.logger.error("Faied to get document [{}]", skinReference, e); |
92 |
|
|
93 |
0 |
return null; |
94 |
|
} |
95 |
306489 |
if (!doc.isNew()) { |
96 |
376 |
return doc; |
97 |
|
} |
98 |
|
} |
99 |
|
} |
100 |
|
|
101 |
306114 |
return null; |
102 |
|
} |
103 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
104 |
124 |
private BaseObject getSkinObject(String skin)... |
105 |
|
{ |
106 |
124 |
XWikiDocument skinDocument = getSkinDocument(skin); |
107 |
|
|
108 |
124 |
return skinDocument != null ? skinDocument.getXObject(SKINCLASS_REFERENCE) : null; |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
111 |
303647 |
public boolean isWikiSkin(String id)... |
112 |
|
{ |
113 |
303651 |
return getSkinDocument(id) != null; |
114 |
|
} |
115 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
116 |
77 |
public Resource<?> getResource(String resourceName, Skin skin)... |
117 |
|
{ |
118 |
77 |
Resource<?> source = null; |
119 |
|
|
120 |
77 |
XWikiDocument skinDocument = getSkinDocument(skin.getId()); |
121 |
77 |
if (skinDocument != null) { |
122 |
77 |
source = getSkinResourceFromDocumentSkin(resourceName, skinDocument, skin); |
123 |
|
} |
124 |
|
|
125 |
77 |
return source; |
126 |
|
} |
127 |
|
|
|
|
| 82.6% |
Uncovered Elements: 4 (23) |
Complexity: 5 |
Complexity Density: 0.33 |
|
128 |
77 |
private Resource<?> getSkinResourceFromDocumentSkin(String resource, XWikiDocument skinDocument, Skin skin)... |
129 |
|
{ |
130 |
77 |
if (skinDocument != null) { |
131 |
|
|
132 |
77 |
BaseObject obj = skinDocument.getXObject(XWikiSkinFileOverrideClassDocumentInitializer.DOCUMENT_REFERENCE, |
133 |
|
XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_PATH, resource, false); |
134 |
77 |
if (obj != null) { |
135 |
0 |
ObjectPropertyReference reference = new ObjectPropertyReference( |
136 |
|
XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT, |
137 |
|
obj.getReference()); |
138 |
0 |
return new ObjectPropertyWikiResource(getPath(reference), skin, reference, |
139 |
|
skinDocument.getAuthorReference(), this.xcontextProvider, |
140 |
|
obj.getLargeStringValue(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT)); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
77 |
BaseProperty<ObjectPropertyReference> property = getSkinResourceProperty(resource, skinDocument); |
145 |
77 |
if (property != null) { |
146 |
2 |
ObjectPropertyReference reference = property.getReference(); |
147 |
2 |
return new ObjectPropertyWikiResource(getPath(reference), skin, reference, |
148 |
|
skinDocument.getAuthorReference(), this.xcontextProvider, (String) property.getValue()); |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
75 |
String normalizedResourceName = StringUtils.replaceChars(resource, '/', '.'); |
155 |
75 |
XWikiAttachment attachment = skinDocument.getAttachment(normalizedResourceName); |
156 |
75 |
if (attachment != null) { |
157 |
1 |
AttachmentReference reference = attachment.getReference(); |
158 |
1 |
return new AttachmentWikiResource(getPath(reference), skin, reference, attachment.getAuthorReference(), |
159 |
|
this.xcontextProvider); |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
74 |
return null; |
164 |
|
} |
165 |
|
|
|
|
| 94.7% |
Uncovered Elements: 1 (19) |
Complexity: 6 |
Complexity Density: 0.55 |
|
166 |
77 |
private BaseProperty<ObjectPropertyReference> getSkinResourceProperty(String resource, XWikiDocument skinDocument)... |
167 |
|
{ |
168 |
|
|
169 |
77 |
BaseObject skinObject = skinDocument.getXObject(SKINCLASS_REFERENCE); |
170 |
77 |
if (skinObject != null) { |
171 |
8 |
BaseProperty<ObjectPropertyReference> resourceProperty = |
172 |
|
(BaseProperty<ObjectPropertyReference>) skinObject.safeget(resource); |
173 |
|
|
174 |
|
|
175 |
8 |
if (resourceProperty == null) { |
176 |
6 |
String escapedTemplateName = StringUtils.replaceChars(resource, '/', '.'); |
177 |
6 |
resourceProperty = (BaseProperty<ObjectPropertyReference>) skinObject.safeget(escapedTemplateName); |
178 |
|
} |
179 |
|
|
180 |
8 |
if (resourceProperty != null) { |
181 |
2 |
Object value = resourceProperty.getValue(); |
182 |
2 |
if (value instanceof String && StringUtils.isNotEmpty((String) value)) { |
183 |
2 |
return resourceProperty; |
184 |
|
} |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
75 |
return null; |
189 |
|
} |
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
191 |
25 |
public String getSkinProperty(String skin, String property)... |
192 |
|
{ |
193 |
25 |
BaseObject obj = getSkinObject(skin); |
194 |
25 |
if (obj != null) { |
195 |
2 |
return obj.getStringValue(property); |
196 |
|
} |
197 |
23 |
return null; |
198 |
|
} |
199 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
200 |
99 |
public String getParentId(String id)... |
201 |
|
{ |
202 |
99 |
BaseObject skinObject = getSkinObject(id); |
203 |
99 |
if (skinObject != null) { |
204 |
8 |
String parentId = skinObject.getStringValue(SKINCLASS_BASESKIN); |
205 |
|
|
206 |
8 |
if (StringUtils.isNotBlank(parentId) && !NO_VALUE.equals(parentId)) { |
207 |
0 |
return parentId; |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
99 |
return null; |
212 |
|
} |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
214 |
3 |
private String getPath(EntityReference reference)... |
215 |
|
{ |
216 |
3 |
return this.referenceSerializer.serialize(reference); |
217 |
|
} |
218 |
|
} |