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

File SxDocumentSource.java

 

Coverage histogram

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

Code metrics

14
41
4
1
177
113
14
0.34
10.25
4
3.5

Classes

Class Line # Actions
SxDocumentSource 51 41 0% 14 7
0.8813559488.1%
 

Contributing tests

No tests hitting this source file were found.

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.web.sx;
21   
22    import java.io.StringWriter;
23    import java.util.List;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.apache.commons.lang3.exception.ExceptionUtils;
27    import org.apache.velocity.VelocityContext;
28    import org.slf4j.Logger;
29    import org.slf4j.LoggerFactory;
30    import org.xwiki.lesscss.compiler.LESSCompiler;
31    import org.xwiki.lesscss.compiler.LESSCompilerException;
32    import org.xwiki.lesscss.resources.LESSResourceReference;
33    import org.xwiki.lesscss.resources.LESSResourceReferenceFactory;
34    import org.xwiki.model.reference.ObjectPropertyReference;
35    import org.xwiki.velocity.VelocityManager;
36    import org.xwiki.velocity.XWikiVelocityException;
37   
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.doc.XWikiDocument;
40    import com.xpn.xwiki.objects.BaseObject;
41    import com.xpn.xwiki.objects.BaseObjectReference;
42    import com.xpn.xwiki.web.Utils;
43   
44    /**
45    * Wiki Document source for Skin Extensions. This is the standard source for Skin Extensions, using an XWiki object of
46    * an extension class in a wiki document.
47    *
48    * @version $Id: d785133e3ab8a0edcc2ef0cb6397776e5e0fdf01 $
49    * @since 1.7M2
50    */
 
51    public class SxDocumentSource implements SxSource
52    {
53    /** The name of the property in the script extension object which contains the script content. */
54    private static final String CONTENT_PROPERTY_NAME = "code";
55   
56    /** The name of the property in the script extension object which contains the content type. */
57    private static final String CONTENT_TYPE_PROPERTY_NAME = "contentType";
58   
59    /** The name of the property in the script extension object which tells us if the content should be parsed. */
60    private static final String PARSE_CONTENT_PROPERTY_NAME = "parse";
61   
62    /** The name of the property in the script extension object which contains the cache policy. */
63    private static final String CACHE_POLICY_PROPERTY_NAME = "cache";
64   
65    /** The name of the property in the script extension object which contains the name of the object. */
66    private static final String NAME_PROPERTY_NAME = "name";
67   
68    /** Logging helper. */
69    private static final Logger LOGGER = LoggerFactory.getLogger(SxDocumentSource.class);
70   
71    /** The document containing the extension. */
72    private XWikiDocument document;
73   
74    /** The current XWikiContext. */
75    private XWikiContext context;
76   
77    /** The type of Extension for getting the right kind of object from the document. */
78    private Extension extension;
79   
80    /**
81    * Constructor for this extension source.
82    *
83    * @param context The XWikiContext
84    * @param extension The Extension type
85    */
 
86  489 toggle public SxDocumentSource(XWikiContext context, Extension extension)
87    {
88  488 this.context = context;
89  486 this.document = context.getDoc();
90  487 this.extension = extension;
91    }
92   
 
93  489 toggle @Override
94    public CachePolicy getCachePolicy()
95    {
96  489 CachePolicy finalCache = CachePolicy.LONG;
97   
98  489 if (this.document.getObjects(this.extension.getClassName()) != null) {
99  488 for (BaseObject sxObj : this.document.getObjects(this.extension.getClassName())) {
100  502 if (sxObj == null) {
101  0 continue;
102    }
103  502 try {
104  502 CachePolicy cache =
105    CachePolicy.valueOf(StringUtils.upperCase(StringUtils.defaultIfEmpty(sxObj
106    .getStringValue(CACHE_POLICY_PROPERTY_NAME), "LONG")));
107  502 if (cache.compareTo(finalCache) > 0) {
108  314 finalCache = cache;
109    }
110    } catch (Exception ex) {
111  0 LOGGER.warn("SX object [{}#{}] has an invalid cache policy: [{}]",
112    new Object[]{this.document.getFullName(), sxObj.getStringValue(NAME_PROPERTY_NAME),
113    sxObj.getStringValue(CACHE_POLICY_PROPERTY_NAME)});
114    }
115    }
116    }
117  489 return finalCache;
118    }
119   
 
120  487 toggle @Override
121    public String getContent()
122    {
123  488 StringBuilder resultBuilder = new StringBuilder();
124   
125  488 List<BaseObject> objects = this.document.getObjects(this.extension.getClassName());
126  487 if (objects != null) {
127  486 for (BaseObject sxObj : objects) {
128  500 if (sxObj == null) {
129  0 continue;
130    }
131  501 String sxContent = sxObj.getLargeStringValue(CONTENT_PROPERTY_NAME);
132  501 int parse = sxObj.getIntValue(PARSE_CONTENT_PROPERTY_NAME);
133  502 if ("LESS".equals(sxObj.getStringValue(CONTENT_TYPE_PROPERTY_NAME))) {
134  1 LESSCompiler lessCompiler = Utils.getComponent(LESSCompiler.class);
135  1 LESSResourceReferenceFactory lessResourceReferenceFactory =
136    Utils.getComponent(LESSResourceReferenceFactory.class);
137  1 ObjectPropertyReference objectPropertyReference =
138    new ObjectPropertyReference(CONTENT_PROPERTY_NAME, new BaseObjectReference(
139    sxObj.getXClassReference(), sxObj.getNumber(), sxObj.getDocumentReference()));
140  1 LESSResourceReference lessResourceReference =
141    lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference);
142  1 try {
143  1 sxContent = lessCompiler.compile(lessResourceReference, true, (parse == 1), false);
144    } catch (LESSCompilerException e) {
145    // Set the error message in a CSS comment to help the developer understand why its SSX is not
146    // working (it will work only if the CSS minifier is not used).
147  0 sxContent = String.format("/* LESS errors while parsing skin extension [%s]. */\n/* %s */",
148    sxObj.getStringValue(NAME_PROPERTY_NAME), ExceptionUtils.getRootCauseMessage(e));
149    }
150  500 } else if (parse == 1) {
151  426 try {
152  426 StringWriter writer = new StringWriter();
153  424 VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);
154  426 VelocityContext vcontext = velocityManager.getVelocityContext();
155  425 velocityManager.getVelocityEngine().evaluate(vcontext, writer,
156    this.document.getPrefixedFullName(), sxContent);
157  426 sxContent = writer.toString();
158    } catch (XWikiVelocityException ex) {
159  0 LOGGER.warn("Velocity errors while parsing skin extension [{}] with content [{}]: ",
160    this.document.getPrefixedFullName(), sxContent, ExceptionUtils.getRootCauseMessage(ex));
161    }
162    }
163    // Also add a newline, in case the different object contents don't end with a blank
164    // line, and could cause syntax errors when concatenated.
165  502 resultBuilder.append(sxContent + "\n");
166    }
167    }
168  489 return resultBuilder.toString();
169    }
170   
 
171  978 toggle @Override
172    public long getLastModifiedDate()
173    {
174  978 return this.document.getDate().getTime();
175    }
176   
177    }