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

File TemplateLESSSource.java

 

Coverage histogram

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

Code metrics

2
13
5
1
103
46
7
0.54
2.6
5
1.4

Classes

Class Line # Actions
TemplateLESSSource 35 13 0% 7 3
0.8585%
 

Contributing tests

This file is covered by 1 test. .

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.internal.compiler.less4j;
21   
22    import org.apache.commons.lang3.StringUtils;
23    import org.xwiki.lesscss.internal.compiler.CachedLESSCompiler;
24    import org.xwiki.skin.Skin;
25    import org.xwiki.template.Template;
26    import org.xwiki.template.TemplateContent;
27    import org.xwiki.template.TemplateManager;
28   
29    /**
30    * Class that get the LESS code from skin templates. Execute velocity on .vm files too.
31    *
32    * @version $Id: cb4ae9484b3e16faa3e2e51a4f6a117af1d704a2 $
33    * @since 7.0RC1
34    */
 
35    public class TemplateLESSSource extends AbstractLESSSource
36    {
37    private static final String FILE_SEPARATOR = "/";
38   
39    private String templateName;
40   
41    /**
42    * Default constructor.
43    * @param templateManager the template manager component
44    * @param skin the skin holding the template
45    * @param templateName the name of the template
46    */
 
47  3297 toggle public TemplateLESSSource(TemplateManager templateManager, Skin skin, String templateName)
48    {
49  3297 super(templateManager, skin, getParentFolder(templateName));
50  3297 this.templateName = templateName;
51    }
52   
53    /**
54    * Get the parent folder of a path using "/" as file separator.
55    * @param templateName name of the template
56    * @return the parent folder path
57    */
 
58  3297 toggle private static String getParentFolder(String templateName)
59    {
60  3297 return StringUtils.substringBeforeLast(templateName, FILE_SEPARATOR);
61    }
62   
 
63  6730 toggle @Override
64    public String getContent() throws FileNotFound, CannotReadFile
65    {
66  6730 try {
67    // We execute velocity on the main skin file only (which is included by SSX objects using LESS).
68    //
69    // This a limitation we introduce because when we do an HTML export, we must execute velocity to know which
70    // resources have to be included in the ZIP file, but we avoid executing LESS and we use the cache instead
71    // (otherwise the export would be too slow).
72    //
73    // When we do an HTML export, we execute Velocity on the main skin file, but not on any .less.vm that the
74    // skin might have. Actually we have no way to know which .less.vm are included, without running LESS.
75    //
76    // That is why we do not execute Velocity on any ".less.vm" file but only on the main skin template.
77  6730 String mainSkinTemplate = "less/" + CachedLESSCompiler.MAIN_SKIN_STYLE_FILENAME;
78  6730 if (mainSkinTemplate.equals(templateName)) {
79  6 return templateManager.renderFromSkin(templateName, skin);
80    }
81   
82    // Otherwise, return the raw content
83  6724 Template template = templateManager.getTemplate(templateName, skin);
84  6724 TemplateContent templateContent = template.getContent();
85  6724 return templateContent.getContent();
86   
87    } catch (Exception e) {
88  0 throw new CannotReadFile();
89    }
90    }
91   
 
92  0 toggle @Override
93    public byte[] getBytes() throws FileNotFound, CannotReadFile
94    {
95  0 return getContent().getBytes();
96    }
97   
 
98  384251 toggle @Override
99    public String getName()
100    {
101  384251 return templateName;
102    }
103    }