1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.lesscss

File LessCompilerScriptService.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
38
9
1
253
131
18
0.47
4.22
9
2

Classes

Class Line # Actions
LessCompilerScriptService 55 38 0% 18 2
0.962264296.2%
 

Contributing tests

This file is covered by 14 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 org.xwiki.lesscss;
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.exception.ExceptionUtils;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.lesscss.compiler.LESSCompiler;
30    import org.xwiki.lesscss.compiler.LESSCompilerException;
31    import org.xwiki.lesscss.internal.cache.ColorThemeCache;
32    import org.xwiki.lesscss.internal.cache.LESSResourcesCache;
33    import org.xwiki.lesscss.internal.colortheme.ColorTheme;
34    import org.xwiki.lesscss.internal.colortheme.ColorThemeReference;
35    import org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory;
36    import org.xwiki.lesscss.internal.colortheme.LESSColorThemeConverter;
37    import org.xwiki.lesscss.internal.skin.SkinReference;
38    import org.xwiki.lesscss.internal.skin.SkinReferenceFactory;
39    import org.xwiki.lesscss.resources.LESSResourceReferenceFactory;
40    import org.xwiki.script.service.ScriptService;
41    import org.xwiki.security.authorization.AuthorizationManager;
42    import org.xwiki.security.authorization.Right;
43   
44    import com.xpn.xwiki.XWikiContext;
45   
46    /**
47    * This script service provides a LESS preprocessor (http://lesscss.org/) for CSS generation.
48    *
49    * @since 6.1M1
50    * @version $Id: c053329ca675059d02e6061287fe3b38fc3aee82 $
51    */
52    @Component
53    @Named("lesscss")
54    @Singleton
 
55    public class LessCompilerScriptService implements ScriptService
56    {
57    @Inject
58    private LESSCompiler lessCompiler;
59   
60    @Inject
61    private LESSResourceReferenceFactory lessResourceReferenceFactory;
62   
63    @Inject
64    private LESSResourcesCache lessCache;
65   
66    @Inject
67    private ColorThemeCache colorThemeCache;
68   
69    @Inject
70    private Provider<XWikiContext> xcontextProvider;
71   
72    @Inject
73    private LESSColorThemeConverter lessColorThemeConverter;
74   
75    @Inject
76    private SkinReferenceFactory skinReferenceFactory;
77   
78    @Inject
79    private ColorThemeReferenceFactory colorThemeReferenceFactory;
80   
81    @Inject
82    private AuthorizationManager authorizationManager;
83   
84    /**
85    * Compile a LESS file located in the "less" directory of the current skin directory.
86    * Velocity will also be parsed on the file, but not on the files included via the @import directive.
87    * The result is cached by XWiki until the skin or the color theme is changed.
88    *
89    * @param fileName name of the file to compile
90    * @return the generated CSS, or an error message if some problem occurs
91    */
 
92  50 toggle public String compileSkinFile(String fileName)
93    {
94  50 return compileSkinFile(fileName, false);
95    }
96   
97    /**
98    * Compile a LESS file located in the "less" directory of the current skin directory.
99    * Velocity will also be parsed on the file, but not on the files included via the @import directive.
100    * The result is cached by XWiki until the skin or the color theme is changed.
101    *
102    * @param fileName name of the file to compile
103    * @param force force the computation, even if the output is already in the cache (not recommended)
104    * @return the generated CSS, or an error message if some problem occurs
105    */
 
106  53 toggle public String compileSkinFile(String fileName, boolean force)
107    {
108  53 try {
109  53 return lessCompiler.compile(lessResourceReferenceFactory.createReferenceForSkinFile(fileName), false, true,
110    force);
111    } catch (LESSCompilerException e) {
112  1 return ExceptionUtils.getRootCauseMessage(e);
113    }
114    }
115   
116    /**
117    * Compile a LESS file located in the "less" directory of the specified skin directory.
118    * Velocity will also be parsed on the file, but not on the files included via the @import directive.
119    * The result is cached by XWiki until the skin or the color theme is changed.
120    *
121    * @param fileName name of the file to compile
122    * @param skin name of the skin where the LESS file is located
123    * @return the generated CSS, or an error message if some problem occurs
124    */
 
125  2 toggle public String compileSkinFile(String fileName, String skin)
126    {
127  2 return compileSkinFile(fileName, skin, false);
128    }
129   
130    /**
131    * Compile a LESS file located in the "less" directory of the specified skin directory.
132    * Velocity will also be parsed on the file, but not on the files included via the @import directive.
133    * The result is cached by XWiki until the skin or the color theme is changed.
134    *
135    * @param fileName name of the file to compile
136    * @param skin name of the skin where the LESS file is located
137    * @param force force the computation, even if the output is already in the cache (not recommended)
138    * @return the generated CSS, or an error message if some problem occurs
139    */
 
140  5 toggle public String compileSkinFile(String fileName, String skin, boolean force)
141    {
142  5 try {
143  5 return lessCompiler.compile(lessResourceReferenceFactory.createReferenceForSkinFile(fileName), false, true,
144    skin, force);
145    } catch (LESSCompilerException e) {
146  1 return ExceptionUtils.getRootCauseMessage(e);
147    }
148    }
149   
150    /**
151    * Return a color theme from a LESS file located in the "less" directory of the current skin.
152    *
153    * @param filename name of the LESS file
154    * @return the corresponding Color Theme.
155    */
 
156  2081 toggle public ColorTheme getColorThemeFromSkinFile(String filename)
157    {
158  2077 try {
159  2078 return lessColorThemeConverter.getColorThemeFromSkinFile(filename, false);
160    } catch (LESSCompilerException e) {
161  1 return new ColorTheme();
162    }
163    }
164   
165    /**
166    * Return a color theme from a LESS file located in the "less" directory of the specified skin.
167    *
168    * @param filename name of the LESS file
169    * @param skin name of the skin where the LESS file is located
170    * @return the corresponding Color Theme.
171    */
 
172  2 toggle public ColorTheme getColorThemeFromSkinFile(String filename, String skin)
173    {
174  2 try {
175  2 return lessColorThemeConverter.getColorThemeFromSkinFile(filename, skin, false);
176    } catch (LESSCompilerException e) {
177  1 return new ColorTheme();
178    }
179    }
180   
181    /**
182    * Remove every generated files from the XWiki cache. The script calling this method needs the programming rights.
183    * @return true if the operation succeed
184    */
 
185  2 toggle public boolean clearCache()
186    {
187  2 XWikiContext xcontext = xcontextProvider.get();
188   
189    // Check if the current script has the programing rights
190  2 if (!authorizationManager.hasAccess(Right.PROGRAM, xcontext.getDoc().getAuthorReference(),
191    xcontext.getDoc().getDocumentReference())) {
192  1 return false;
193    }
194   
195  1 lessCache.clear();
196  1 colorThemeCache.clear();
197  1 return true;
198    }
199   
200    /**
201    * Remove every generated files corresponding to a color theme.
202    * The script calling this method needs the programming rights.
203    * @param colorTheme fullname of the color theme
204    * @return true if the operation succeed
205    */
 
206  2 toggle public boolean clearCacheFromColorTheme(String colorTheme)
207    {
208  2 XWikiContext xcontext = xcontextProvider.get();
209   
210    // Check if the current script has the programing rights
211  2 if (!authorizationManager.hasAccess(Right.PROGRAM, xcontext.getDoc().getAuthorReference(),
212    xcontext.getDoc().getDocumentReference())) {
213  1 return false;
214    }
215   
216  1 try {
217  1 ColorThemeReference colorThemeReference = colorThemeReferenceFactory.createReference(colorTheme);
218  1 lessCache.clearFromColorTheme(colorThemeReference);
219  1 colorThemeCache.clearFromColorTheme(colorThemeReference);
220  1 return true;
221    } catch (LESSCompilerException e) {
222  0 return false;
223    }
224    }
225   
226    /**
227    * Remove every generated files corresponding to a filesystem skin.
228    * The script calling this method needs the programming rights.
229    * @param skin name of the filesystem skin
230    * @return true if the operation succeed
231    *
232    * @since 6.4M2
233    */
 
234  2 toggle public boolean clearCacheFromSkin(String skin)
235    {
236  2 XWikiContext xcontext = xcontextProvider.get();
237   
238    // Check if the current script has the programing rights
239  2 if (!authorizationManager.hasAccess(Right.PROGRAM, xcontext.getDoc().getAuthorReference(),
240    xcontext.getDoc().getDocumentReference())) {
241  1 return false;
242    }
243   
244  1 try {
245  1 SkinReference skinReference = skinReferenceFactory.createReference(skin);
246  1 lessCache.clearFromSkin(skinReference);
247  1 colorThemeCache.clearFromSkin(skinReference);
248  1 return true;
249    } catch (LESSCompilerException e) {
250  0 return false;
251    }
252    }
253    }