Search in sources :

Example 1 with LESSCompilerException

use of org.xwiki.lesscss.compiler.LESSCompilerException in project xwiki-platform by xwiki.

the class SxDocumentSource method getContent.

@Override
public String getContent() {
    StringBuilder resultBuilder = new StringBuilder();
    List<BaseObject> objects = this.document.getObjects(this.extension.getClassName());
    if (objects != null) {
        for (BaseObject sxObj : objects) {
            if (sxObj == null) {
                continue;
            }
            String sxContent = sxObj.getLargeStringValue(CONTENT_PROPERTY_NAME);
            int parse = sxObj.getIntValue(PARSE_CONTENT_PROPERTY_NAME);
            if ("LESS".equals(sxObj.getStringValue(CONTENT_TYPE_PROPERTY_NAME))) {
                LESSCompiler lessCompiler = Utils.getComponent(LESSCompiler.class);
                LESSResourceReferenceFactory lessResourceReferenceFactory = Utils.getComponent(LESSResourceReferenceFactory.class);
                ObjectPropertyReference objectPropertyReference = new ObjectPropertyReference(CONTENT_PROPERTY_NAME, sxObj.getReference());
                LESSResourceReference lessResourceReference = lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference);
                try {
                    sxContent = lessCompiler.compile(lessResourceReference, true, (parse == 1), false);
                } catch (LESSCompilerException e) {
                    // Set the error message in a CSS comment to help the developer understand why its SSX is not
                    // working (it will work only if the CSS minifier is not used).
                    sxContent = String.format("/* LESS errors while parsing skin extension [%s]. */\n/* %s */", sxObj.getStringValue(NAME_PROPERTY_NAME), ExceptionUtils.getRootCauseMessage(e));
                }
            } else if (parse == 1) {
                try {
                    StringWriter writer = new StringWriter();
                    VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);
                    VelocityContext vcontext = velocityManager.getVelocityContext();
                    velocityManager.getVelocityEngine().evaluate(vcontext, writer, this.document.getPrefixedFullName(), sxContent);
                    sxContent = writer.toString();
                } catch (XWikiVelocityException ex) {
                    LOGGER.warn("Velocity errors while parsing skin extension [{}] with content [{}]: ", this.document.getPrefixedFullName(), sxContent, ExceptionUtils.getRootCauseMessage(ex));
                }
            }
            // Also add a newline, in case the different object contents don't end with a blank
            // line, and could cause syntax errors when concatenated.
            resultBuilder.append(sxContent + "\n");
        }
    }
    return resultBuilder.toString();
}
Also used : ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) VelocityContext(org.apache.velocity.VelocityContext) LESSCompiler(org.xwiki.lesscss.compiler.LESSCompiler) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) LESSResourceReferenceFactory(org.xwiki.lesscss.resources.LESSResourceReferenceFactory) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference)

Example 2 with LESSCompilerException

use of org.xwiki.lesscss.compiler.LESSCompilerException in project xwiki-platform by xwiki.

the class CachedLESSCompiler method compute.

@Override
public String compute(LESSResourceReference lessResourceReference, boolean includeSkinStyle, boolean useVelocity, boolean useLESS, String skin) throws LESSCompilerException {
    StringWriter source = new StringWriter();
    try {
        semaphore.acquire();
        if (lessResourceReference instanceof LESSSkinFileResourceReference || includeSkinStyle) {
            if (includeSkinStyle) {
                // Add the import line to the LESS resource.
                // We import this file to be able to use variables and mix-ins defined in it.
                // But we don't want it in the output.
                source.write(String.format("@import (reference) \"%s\";%s", MAIN_SKIN_STYLE_FILENAME, System.lineSeparator()));
            }
            // Get the content of the LESS resource
            source.write(lessResourceReference.getContent(skin));
        }
        // Parse the LESS content with Velocity
        String lessCode = source.toString();
        if (useVelocity) {
            lessCode = executeVelocity(lessCode, skin);
        }
        // Compile the LESS code
        if (useLESS) {
            return less4JCompiler.compile(lessCode, skin, lessConfiguration.isGenerateInlineSourceMaps());
        }
        // Otherwise return the raw LESS code
        return lessCode;
    } catch (Less4jException | InterruptedException e) {
        throw new LESSCompilerException(String.format("Failed to compile the resource [%s] with LESS.", lessResourceReference), e);
    } finally {
        semaphore.release();
    }
}
Also used : StringWriter(java.io.StringWriter) Less4jException(com.github.sommeri.less4j.Less4jException) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) LESSSkinFileResourceReference(org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference)

Example 3 with LESSCompilerException

use of org.xwiki.lesscss.compiler.LESSCompilerException in project xwiki-platform by xwiki.

the class AbstractCachedCompiler method getResult.

/**
 * Get the result of the compilation.
 * @param lessResourceReference reference to the LESS content
 * @param includeSkinStyle include the main LESS file of the skin in order to have variables and mix-ins
 * defined there
 * @param useVelocity either or not the resource be parsed by Velocity before compiling it
 * @param force force the computation, even if the output is already in the cache (not recommended)
 * @param skin name of the skin used for the context
 * @return the desired object
 * @throws LESSCompilerException if problems occur
 */
public T getResult(LESSResourceReference lessResourceReference, boolean includeSkinStyle, boolean useVelocity, String skin, boolean force) throws LESSCompilerException {
    // If the cache is disabled, we just compile
    if (lessContext.isCacheDisabled()) {
        return compiler.compute(lessResourceReference, includeSkinStyle, useVelocity, true, skin);
    }
    T result = null;
    SkinReference skinReference = skinReferenceFactory.createReference(skin);
    ColorThemeReference colorThemeReference = colorThemeReferenceFactory.createReference(currentColorThemeGetter.getCurrentColorTheme(true, "default"));
    // Only one computation is allowed in the same time per color theme, then the waiting threads will be able to
    // use the last result stored in the cache.
    Object mutex = cache.getMutex(lessResourceReference, skinReference, colorThemeReference);
    synchronized (mutex) {
        // Check if the result is in the cache
        if (!force) {
            result = cache.get(lessResourceReference, skinReference, colorThemeReference);
            if (result != null) {
                // we only do the Velocity Execution step.
                if (lessContext.isHtmlExport() && useVelocity && this instanceof DefaultLESSCompiler) {
                    compiler.compute(lessResourceReference, includeSkinStyle, true, false, skin);
                }
                return cloneResult(result);
            }
        }
        // Either the result was in the cache or the force flag is set to true, we need to compile
        try {
            result = compiler.compute(lessResourceReference, includeSkinStyle, useVelocity, true, skin);
        } catch (LESSCompilerException e) {
            logger.error("Error during the compilation of the resource [{}].", lessResourceReference, e);
            // We must cache the result, even if the compilation have failed, to prevent re-compiling again and
            // again (the compilation will still fail until the LESS resource is updated so it useless to retry).
            result = exceptionAsResult(e);
        } finally {
            // Put the result in the cache
            cache.set(lessResourceReference, skinReference, colorThemeReference, result);
        }
    }
    return cloneResult(result);
}
Also used : ColorThemeReference(org.xwiki.lesscss.internal.colortheme.ColorThemeReference) DefaultLESSCompiler(org.xwiki.lesscss.internal.compiler.DefaultLESSCompiler) SkinReference(org.xwiki.lesscss.internal.skin.SkinReference) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException)

Example 4 with LESSCompilerException

use of org.xwiki.lesscss.compiler.LESSCompilerException in project xwiki-platform by xwiki.

the class CachedLESSCompilerTest method computeSkinFileWhenException.

@Test
public void computeSkinFileWhenException() throws Exception {
    // Mocks
    LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
    when(resource.getContent(eq("skin"))).thenReturn("Some LESS content");
    when(xwiki.evaluateVelocity(eq("Some LESS content"), eq("SomeContextDocument"))).thenReturn("Some Velocity-rendered LESS content");
    Less4jException lessCompilerException = mock(Less4jException.class);
    when(less4jCompiler.compile(eq("Some Velocity-rendered LESS content"), eq("skin"), eq(false))).thenThrow(lessCompilerException);
    // Tests
    LESSCompilerException caughtException = null;
    try {
        mocker.getComponentUnderTest().compute(resource, false, true, true, "skin");
    } catch (LESSCompilerException e) {
        caughtException = e;
    }
    // Verify
    assertNotNull(caughtException);
    assertEquals(lessCompilerException, caughtException.getCause());
    assertEquals("Failed to compile the resource [" + resource.toString() + "] with LESS.", caughtException.getMessage());
}
Also used : LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) Less4jException(com.github.sommeri.less4j.Less4jException) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) Test(org.junit.Test)

Example 5 with LESSCompilerException

use of org.xwiki.lesscss.compiler.LESSCompilerException in project xwiki-platform by xwiki.

the class DefaultLESSCompilerTest method compileWhenError.

@Test
public void compileWhenError() throws Exception {
    // Mocks
    LESSCompilerException expectedException = new LESSCompilerException("an exception");
    when(cachedLESSCompiler.compute(any(LESSResourceReference.class), anyBoolean(), anyBoolean(), anyBoolean(), any())).thenThrow(expectedException);
    // Test
    String result = mocker.getComponentUnderTest().compile(lessResourceReference, false, false, false);
    // Asserts
    assertTrue(StringUtils.startsWith(result, "/* org.xwiki.lesscss.compiler.LESSCompilerException: an exception"));
    assertTrue(StringUtils.endsWith(result, "*/"));
    verify(cache).set(eq(lessResourceReference), eq(skinReference), eq(colorThemeReference), eq(result));
    verify(mocker.getMockedLogger()).error(eq("Error during the compilation of the resource [{}]."), eq(lessResourceReference), eq(expectedException));
}
Also used : LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) Test(org.junit.Test)

Aggregations

LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)16 Test (org.junit.Test)9 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 XWikiException (com.xpn.xwiki.XWikiException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 WikiReference (org.xwiki.model.reference.WikiReference)3 Less4jException (com.github.sommeri.less4j.Less4jException)2 XWiki (com.xpn.xwiki.XWiki)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 StringWriter (java.io.StringWriter)2 ColorTheme (org.xwiki.lesscss.internal.colortheme.ColorTheme)2 ColorThemeReference (org.xwiki.lesscss.internal.colortheme.ColorThemeReference)2 SkinReference (org.xwiki.lesscss.internal.skin.SkinReference)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 VelocityContext (org.apache.velocity.VelocityContext)1 LESSCompiler (org.xwiki.lesscss.compiler.LESSCompiler)1 DefaultLESSCompiler (org.xwiki.lesscss.internal.compiler.DefaultLESSCompiler)1 LESSSkinFileResourceReference (org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference)1 LESSResourceReferenceFactory (org.xwiki.lesscss.resources.LESSResourceReferenceFactory)1