1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.skin

File WikiSkinUtils.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

32
58
9
1
218
152
29
0.5
6.44
9
3.22

Classes

Class Line # Actions
WikiSkinUtils 55 58 0% 29 12
0.878787987.9%
 

Contributing tests

This file is covered by 33 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: b4cc7a7112f32791020de9717704dd4df6b02db2 $
51    * @since 6.4M1
52    */
53    @Component(roles = WikiSkinUtils.class)
54    @Singleton
 
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    * Represents no value (ie the default value will be used) in xproperties.
63    */
64    // TODO: remove when XWIKI-10853 is fixed
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   
 
80  306465 toggle 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   
 
104  124 toggle 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   
 
111  303647 toggle public boolean isWikiSkin(String id)
112    {
113  303651 return getSkinDocument(id) != null;
114    }
115   
 
116  77 toggle 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   
 
128  77 toggle private Resource<?> getSkinResourceFromDocumentSkin(String resource, XWikiDocument skinDocument, Skin skin)
129    {
130  77 if (skinDocument != null) {
131    // Try to find a XWikiSkinFileOverrideClass object
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    // Try parsing the object property
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    // Try parsing a document attachment
152    // Convert "/" into "." in order to support wiki skin attachments to override some resources located in
153    // subdirectories.
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   
 
166  77 toggle private BaseProperty<ObjectPropertyReference> getSkinResourceProperty(String resource, XWikiDocument skinDocument)
167    {
168    // Try parsing the object property
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    // If not found try by replacing '/' with '.'
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   
 
191  25 toggle 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   
 
200  99 toggle 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    // TODO: remove the NO_VALUE test when XWIKI-10853 is fixed
206  8 if (StringUtils.isNotBlank(parentId) && !NO_VALUE.equals(parentId)) {
207  0 return parentId;
208    }
209    }
210   
211  99 return null;
212    }
213   
 
214  3 toggle private String getPath(EntityReference reference)
215    {
216  3 return this.referenceSerializer.serialize(reference);
217    }
218    }