Search in sources :

Example 1 with LESSResourceReference

use of org.xwiki.lesscss.resources.LESSResourceReference 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 LESSResourceReference

use of org.xwiki.lesscss.resources.LESSResourceReference in project xwiki-platform by xwiki.

the class SSXListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XWikiDocument document = (XWikiDocument) source;
    String currentWiki = wikiDescriptorManager.getCurrentWikiId();
    DocumentReference ssxDocRef = new DocumentReference(currentWiki, "XWiki", "StyleSheetExtension");
    List<BaseObject> ssxObjects = document.getXObjects(ssxDocRef);
    if (ssxObjects != null && !ssxObjects.isEmpty()) {
        for (BaseObject obj : ssxObjects) {
            if (obj == null) {
                continue;
            }
            if ("LESS".equals(obj.getStringValue("contentType"))) {
                ObjectPropertyReference objectPropertyReference = new ObjectPropertyReference("code", new BaseObjectReference(ssxDocRef, obj.getNumber(), document.getDocumentReference()));
                LESSResourceReference lessResourceReference = lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference);
                lessResourcesCache.clearFromLESSResource(lessResourceReference);
                colorThemeCache.clearFromLESSResource(lessResourceReference);
            }
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) BaseObjectReference(com.xpn.xwiki.objects.BaseObjectReference) LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with LESSResourceReference

use of org.xwiki.lesscss.resources.LESSResourceReference in project xwiki-platform by xwiki.

the class CachedLESSCompilerTest method computeSkinFileWithoutLESS.

@Test
public void computeSkinFileWithoutLESS() throws Exception {
    // Mocks
    LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
    when(resource.getContent(eq("skin2"))).thenReturn("Some LESS content");
    when(xwiki.evaluateVelocity(eq("Some LESS content"), eq("SomeContextDocument"))).thenReturn("Some Velocity-rendered LESS content");
    // Tests
    assertEquals("Some Velocity-rendered LESS content", mocker.getComponentUnderTest().compute(resource, false, true, false, "skin2"));
    // Verify that the LESS compiler is never called
    verifyZeroInteractions(less4jCompiler);
}
Also used : LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) Test(org.junit.Test)

Example 4 with LESSResourceReference

use of org.xwiki.lesscss.resources.LESSResourceReference in project xwiki-platform by xwiki.

the class CachedLESSCompilerTest method computeSkinFile.

@Test
public void computeSkinFile() throws Exception {
    // Mocks
    LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
    when(resource.getContent(eq("skin2"))).thenReturn("Some LESS content");
    when(xwiki.evaluateVelocity(eq("Some LESS content"), eq("SomeContextDocument"))).thenReturn("Some Velocity-rendered LESS content");
    when(less4jCompiler.compile(eq("Some Velocity-rendered LESS content"), eq("skin2"), eq(false))).thenReturn("output");
    // Tests
    assertEquals("output", mocker.getComponentUnderTest().compute(resource, false, true, true, "skin2"));
    // Verify
    verify(xcontext, times(1)).put("skin", "skin2");
    verify(xcontext, times(1)).put("skin", "skin");
}
Also used : LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) Test(org.junit.Test)

Example 5 with LESSResourceReference

use of org.xwiki.lesscss.resources.LESSResourceReference 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)

Aggregations

LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)14 Test (org.junit.Test)12 LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)5 BaseObject (com.xpn.xwiki.objects.BaseObject)2 ColorThemeReference (org.xwiki.lesscss.internal.colortheme.ColorThemeReference)2 SkinReference (org.xwiki.lesscss.internal.skin.SkinReference)2 ObjectPropertyReference (org.xwiki.model.reference.ObjectPropertyReference)2 Less4jException (com.github.sommeri.less4j.Less4jException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObjectReference (com.xpn.xwiki.objects.BaseObjectReference)1 StringWriter (java.io.StringWriter)1 VelocityContext (org.apache.velocity.VelocityContext)1 LESSCompiler (org.xwiki.lesscss.compiler.LESSCompiler)1 LESSResourceReferenceFactory (org.xwiki.lesscss.resources.LESSResourceReferenceFactory)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 VelocityManager (org.xwiki.velocity.VelocityManager)1 XWikiVelocityException (org.xwiki.velocity.XWikiVelocityException)1