| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.lesscss.internal.compiler; |
| 21 |
|
|
| 22 |
|
import java.io.StringWriter; |
| 23 |
|
import java.util.concurrent.Semaphore; |
| 24 |
|
|
| 25 |
|
import javax.inject.Inject; |
| 26 |
|
import javax.inject.Provider; |
| 27 |
|
import javax.inject.Singleton; |
| 28 |
|
|
| 29 |
|
import org.xwiki.component.annotation.Component; |
| 30 |
|
import org.xwiki.component.phase.Initializable; |
| 31 |
|
import org.xwiki.component.phase.InitializationException; |
| 32 |
|
import org.xwiki.lesscss.compiler.LESSCompilerException; |
| 33 |
|
import org.xwiki.lesscss.internal.LESSConfiguration; |
| 34 |
|
import org.xwiki.lesscss.internal.cache.CachedCompilerInterface; |
| 35 |
|
import org.xwiki.lesscss.internal.compiler.less4j.Less4jCompiler; |
| 36 |
|
import org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference; |
| 37 |
|
import org.xwiki.lesscss.resources.LESSResourceReference; |
| 38 |
|
|
| 39 |
|
import com.github.sommeri.less4j.Less4jException; |
| 40 |
|
import com.xpn.xwiki.XWiki; |
| 41 |
|
import com.xpn.xwiki.XWikiContext; |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
@see |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@since |
| 48 |
|
@version |
| 49 |
|
|
| 50 |
|
@Component(roles = CachedLESSCompiler.class) |
| 51 |
|
@Singleton |
| |
|
| 97.5% |
Uncovered Elements: 1 (40) |
Complexity: 11 |
Complexity Density: 0.44 |
|
| 52 |
|
public class CachedLESSCompiler implements CachedCompilerInterface<String>, Initializable |
| 53 |
|
{ |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
public static final String MAIN_SKIN_STYLE_FILENAME = "style.less.vm"; |
| 58 |
|
|
| 59 |
|
private static final String SKIN_CONTEXT_KEY = "skin"; |
| 60 |
|
|
| 61 |
|
@Inject |
| 62 |
|
private Provider<XWikiContext> xcontextProvider; |
| 63 |
|
|
| 64 |
|
@Inject |
| 65 |
|
private Less4jCompiler less4JCompiler; |
| 66 |
|
|
| 67 |
|
@Inject |
| 68 |
|
private LESSConfiguration lessConfiguration; |
| 69 |
|
|
| 70 |
|
private Semaphore semaphore; |
| 71 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 72 |
34 |
@Override... |
| 73 |
|
public void initialize() throws InitializationException |
| 74 |
|
{ |
| 75 |
34 |
this.semaphore = new Semaphore(lessConfiguration.getMaximumSimultaneousCompilations(), true); |
| 76 |
|
} |
| 77 |
|
|
| |
|
| 95.7% |
Uncovered Elements: 1 (23) |
Complexity: 7 |
Complexity Density: 0.47 |
|
| 78 |
40 |
@Override... |
| 79 |
|
public String compute(LESSResourceReference lessResourceReference, boolean includeSkinStyle, boolean useVelocity, |
| 80 |
|
boolean useLESS, String skin) throws LESSCompilerException |
| 81 |
|
{ |
| 82 |
40 |
StringWriter source = new StringWriter(); |
| 83 |
|
|
| 84 |
40 |
try { |
| 85 |
40 |
semaphore.acquire(); |
| 86 |
40 |
if (lessResourceReference instanceof LESSSkinFileResourceReference || includeSkinStyle) { |
| 87 |
|
|
| 88 |
40 |
if (includeSkinStyle) { |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
2 |
source.write(String.format("@import (reference) \"%s\";%s", MAIN_SKIN_STYLE_FILENAME, |
| 93 |
|
System.lineSeparator())); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
|
| 97 |
40 |
source.write(lessResourceReference.getContent(skin)); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
40 |
String lessCode = source.toString(); |
| 102 |
40 |
if (useVelocity) { |
| 103 |
38 |
lessCode = executeVelocity(lessCode, skin); |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
|
| 107 |
40 |
if (useLESS) { |
| 108 |
39 |
return less4JCompiler.compile(lessCode, skin, lessConfiguration.isGenerateInlineSourceMaps()); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
1 |
return lessCode; |
| 113 |
|
} catch (Less4jException | InterruptedException e) { |
| 114 |
1 |
throw new LESSCompilerException(String.format("Failed to compile the resource [%s] with LESS.", |
| 115 |
|
lessResourceReference), e); |
| 116 |
|
} finally { |
| 117 |
40 |
semaphore.release(); |
| 118 |
|
} |
| 119 |
|
} |
| 120 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 121 |
38 |
private String executeVelocity(String source, String skin)... |
| 122 |
|
{ |
| 123 |
|
|
| 124 |
38 |
XWikiContext xcontext = xcontextProvider.get(); |
| 125 |
38 |
XWiki xwiki = xcontext.getWiki(); |
| 126 |
38 |
String currentSkin = xwiki.getSkin(xcontext); |
| 127 |
|
|
| 128 |
38 |
try { |
| 129 |
|
|
| 130 |
|
|
| 131 |
38 |
if (!currentSkin.equals(skin)) { |
| 132 |
2 |
xcontext.put(SKIN_CONTEXT_KEY, skin); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
38 |
return xwiki.evaluateVelocity(source, xcontext.getDoc().getPrefixedFullName()); |
| 136 |
|
|
| 137 |
|
} finally { |
| 138 |
|
|
| 139 |
38 |
if (!currentSkin.equals(skin)) { |
| 140 |
2 |
xcontext.put(SKIN_CONTEXT_KEY, currentSkin); |
| 141 |
|
} |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
} |