1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.skin

File WikiSkinUtils.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% 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 9
0.9090909490.9%
 

Contributing tests

This file is covered by 36 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  7301252 toggle public XWikiDocument getSkinDocument(String skin)
81    {
82  7305294 XWikiContext xcontext = this.xcontextProvider.get();
83  7309136 if (xcontext != null) {
84  7308204 DocumentReference skinReference = this.currentMixedDocumentReferenceResolver.resolve(skin);
85  7307757 XWiki xwiki = xcontext.getWiki();
86  7306735 if (xwiki != null && xwiki.getStore() != null) {
87  7307160 XWikiDocument doc;
88  7309019 try {
89  7308607 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  7306655 if (!doc.isNew()) {
96  5181543 return doc;
97    }
98    }
99    }
100   
101  2125039 return null;
102    }
103   
 
104  1167988 toggle private BaseObject getSkinObject(String skin)
105    {
106  1168619 XWikiDocument skinDocument = getSkinDocument(skin);
107   
108  1169430 return skinDocument != null ? skinDocument.getXObject(SKINCLASS_REFERENCE) : null;
109    }
110   
 
111  5170562 toggle public boolean isWikiSkin(String id)
112    {
113  5171320 return getSkinDocument(id) != null;
114    }
115   
 
116  962474 toggle public Resource<?> getResource(String resourceName, Skin skin)
117    {
118  962751 Resource<?> source = null;
119   
120  963381 XWikiDocument skinDocument = getSkinDocument(skin.getId());
121  963424 if (skinDocument != null) {
122  963398 source = getSkinResourceFromDocumentSkin(resourceName, skinDocument, skin);
123    }
124   
125  962817 return source;
126    }
127   
 
128  963007 toggle private Resource<?> getSkinResourceFromDocumentSkin(String resource, XWikiDocument skinDocument, Skin skin)
129    {
130  963281 if (skinDocument != null) {
131    // Try to find a XWikiSkinFileOverrideClass object
132  963208 BaseObject obj = skinDocument.getXObject(XWikiSkinFileOverrideClassDocumentInitializer.DOCUMENT_REFERENCE,
133    XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_PATH, resource, false);
134  963370 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  963398 BaseProperty<ObjectPropertyReference> property = getSkinResourceProperty(resource, skinDocument);
145  962392 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  962683 String normalizedResourceName = StringUtils.replaceChars(resource, '/', '.');
155  963149 XWikiAttachment attachment = skinDocument.getAttachment(normalizedResourceName);
156  963098 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  963095 return null;
164    }
165   
 
166  962811 toggle private BaseProperty<ObjectPropertyReference> getSkinResourceProperty(String resource, XWikiDocument skinDocument)
167    {
168    // Try parsing the object property
169  963044 BaseObject skinObject = skinDocument.getXObject(SKINCLASS_REFERENCE);
170  963316 if (skinObject != null) {
171  963092 BaseProperty<ObjectPropertyReference> resourceProperty =
172    (BaseProperty<ObjectPropertyReference>) skinObject.safeget(resource);
173   
174    // If not found try by replacing '/' with '.'
175  963166 if (resourceProperty == null) {
176  931378 String escapedTemplateName = StringUtils.replaceChars(resource, '/', '.');
177  931142 resourceProperty = (BaseProperty<ObjectPropertyReference>) skinObject.safeget(escapedTemplateName);
178    }
179   
180  962902 if (resourceProperty != null) {
181  31855 Object value = resourceProperty.getValue();
182  31855 if (value instanceof String && StringUtils.isNotEmpty((String) value)) {
183  2 return resourceProperty;
184    }
185    }
186    }
187   
188  963125 return null;
189    }
190   
 
191  104139 toggle public String getSkinProperty(String skin, String property)
192    {
193  104605 BaseObject obj = getSkinObject(skin);
194  104488 if (obj != null) {
195  104635 return obj.getStringValue(property);
196    }
197  23 return null;
198    }
199   
 
200  1063130 toggle public String getParentId(String id)
201    {
202  1063680 BaseObject skinObject = getSkinObject(id);
203  1064685 if (skinObject != null) {
204  1064818 String parentId = skinObject.getStringValue(SKINCLASS_BASESKIN);
205    // TODO: remove the NO_VALUE test when XWIKI-10853 is fixed
206  1063152 if (StringUtils.isNotBlank(parentId) && !NO_VALUE.equals(parentId)) {
207  1063813 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    }