Search in sources :

Example 1 with LESSResourceReferenceFactory

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

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

the class DefaultLESSColorThemeConverterTest method setUp.

@Before
public void setUp() throws Exception {
    cache = mocker.getInstance(ColorThemeCache.class);
    currentColorThemeGetter = mocker.getInstance(CurrentColorThemeGetter.class);
    skinReferenceFactory = mocker.getInstance(SkinReferenceFactory.class);
    colorThemeReferenceFactory = mocker.getInstance(ColorThemeReferenceFactory.class);
    lessResourceReferenceFactory = mocker.getInstance(LESSResourceReferenceFactory.class);
    xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    xcontext = mock(XWikiContext.class);
    when(xcontextProvider.get()).thenReturn(xcontext);
    xwiki = mock(XWiki.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    when(xwiki.getSkin(xcontext)).thenReturn("skin");
    when(currentColorThemeGetter.getCurrentColorTheme(true, "default")).thenReturn("colorTheme");
    when(skinReferenceFactory.createReference("skin")).thenReturn(new FSSkinReference("skin"));
    when(colorThemeReferenceFactory.createReference("colorTheme")).thenReturn(new NamedColorThemeReference("colorTheme"));
    when(cache.getMutex(eq(new LESSSkinFileResourceReference("file", null, null)), eq(new FSSkinReference("skin")), eq(new NamedColorThemeReference("colorTheme")))).thenReturn("mutex");
}
Also used : CurrentColorThemeGetter(org.xwiki.lesscss.internal.colortheme.CurrentColorThemeGetter) LESSResourceReferenceFactory(org.xwiki.lesscss.resources.LESSResourceReferenceFactory) ColorThemeReferenceFactory(org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory) NamedColorThemeReference(org.xwiki.lesscss.internal.colortheme.NamedColorThemeReference) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) FSSkinReference(org.xwiki.lesscss.internal.skin.FSSkinReference) ColorThemeCache(org.xwiki.lesscss.internal.cache.ColorThemeCache) SkinReferenceFactory(org.xwiki.lesscss.internal.skin.SkinReferenceFactory) LESSSkinFileResourceReference(org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference) Before(org.junit.Before)

Aggregations

LESSResourceReferenceFactory (org.xwiki.lesscss.resources.LESSResourceReferenceFactory)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 StringWriter (java.io.StringWriter)1 VelocityContext (org.apache.velocity.VelocityContext)1 Before (org.junit.Before)1 LESSCompiler (org.xwiki.lesscss.compiler.LESSCompiler)1 LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)1 ColorThemeCache (org.xwiki.lesscss.internal.cache.ColorThemeCache)1 ColorThemeReferenceFactory (org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory)1 CurrentColorThemeGetter (org.xwiki.lesscss.internal.colortheme.CurrentColorThemeGetter)1 NamedColorThemeReference (org.xwiki.lesscss.internal.colortheme.NamedColorThemeReference)1 LESSSkinFileResourceReference (org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference)1 FSSkinReference (org.xwiki.lesscss.internal.skin.FSSkinReference)1 SkinReferenceFactory (org.xwiki.lesscss.internal.skin.SkinReferenceFactory)1 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)1 ObjectPropertyReference (org.xwiki.model.reference.ObjectPropertyReference)1 VelocityManager (org.xwiki.velocity.VelocityManager)1 XWikiVelocityException (org.xwiki.velocity.XWikiVelocityException)1