Search in sources :

Example 1 with XWikiVelocityException

use of org.xwiki.velocity.XWikiVelocityException 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 XWikiVelocityException

use of org.xwiki.velocity.XWikiVelocityException in project xwiki-platform by xwiki.

the class DefaultVelocityManager method getVelocityEngine.

/**
 * @return the Velocity Engine corresponding to the current execution context. More specifically returns the
 *         Velocity Engine for the current skin since each skin has its own Velocity Engine so that each skin can
 *         have global velocimacros defined
 * @throws XWikiVelocityException in case of an error while creating a Velocity Engine
 */
@Override
public VelocityEngine getVelocityEngine() throws XWikiVelocityException {
    // Note: For improved performance we cache the Velocity Engines in order not to
    // recreate them all the time. The key we use is the location to the skin's macro.vm
    // file since caching on the skin would create more Engines than needed (some skins
    // don't have a macros.vm file and some skins inherit from others).
    // Create a Velocity context using the Velocity Manager associated to the current skin's
    // macros.vm
    // Get the location of the skin's macros.vm file
    XWikiContext xcontext = this.xcontextProvider.get();
    final Template template;
    if (xcontext != null && xcontext.getWiki() != null) {
        template = getVelocityEngineMacrosTemplate();
    } else {
        template = null;
    }
    String cacheKey = template != null ? template.getId() : "default";
    // Get the Velocity Engine to use
    VelocityEngine velocityEngine = this.velocityFactory.getVelocityEngine(cacheKey);
    if (velocityEngine == null) {
        // created only when a new skin is created and not be on the main execution path.
        synchronized (this) {
            velocityEngine = this.velocityFactory.getVelocityEngine(cacheKey);
            if (velocityEngine == null) {
                // Gather the global Velocity macros that we want to have. These are skin dependent.
                Properties properties = new Properties();
                // Loader
                if (!this.velocityConfiguration.getProperties().containsKey(RESOURCE_LOADER)) {
                    properties.setProperty(RESOURCE_LOADER, "xwiki");
                    properties.setProperty(RESOURCE_LOADER_CLASS, XWikiWebappResourceLoader.class.getName());
                }
                if (xcontext != null && xcontext.getWiki() != null) {
                    // Note: if you don't want any template to be used set the property named
                    // xwiki.render.velocity.macrolist to an empty string value.
                    String macroList = xcontext.getWiki().Param("xwiki.render.velocity.macrolist");
                    if (macroList == null) {
                        macroList = "/templates/macros.vm";
                    }
                    properties.put(RuntimeConstants.VM_LIBRARY, macroList);
                }
                velocityEngine = this.velocityFactory.createVelocityEngine(cacheKey, properties);
                if (template != null) {
                    // template by default
                    try {
                        final VelocityEngine finalVelocityEngine = velocityEngine;
                        this.authorExecutor.call(() -> {
                            finalVelocityEngine.evaluate(new VelocityContext(), NullWriter.NULL_WRITER, "", template.getContent().getContent());
                            return null;
                        }, template.getContent().getAuthorReference());
                    } catch (Exception e) {
                        this.logger.error("Failed to evaluate macros templates [{}]", template.getPath(), e);
                    }
                }
            }
        }
    }
    return velocityEngine;
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) VelocityContext(org.apache.velocity.VelocityContext) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) XWikiWebappResourceLoader(org.xwiki.velocity.XWikiWebappResourceLoader) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) InitializationException(org.xwiki.component.phase.InitializationException) Template(org.xwiki.template.Template)

Example 3 with XWikiVelocityException

use of org.xwiki.velocity.XWikiVelocityException in project xwiki-platform by xwiki.

the class VelocityRendererTest method renderWithException.

@Test
public void renderWithException() throws Exception {
    // Mocks
    Exception exception = new XWikiVelocityException("exception");
    when(velocityManager.getVelocityEngine()).thenThrow(exception);
    // Test
    IconException caughtException = null;
    try {
        mocker.getComponentUnderTest().render("myCode");
    } catch (IconException e) {
        caughtException = e;
    }
    // Verify
    assertNotNull(caughtException);
    assertEquals("Failed to render the icon.", caughtException.getMessage());
    assertEquals(exception, caughtException.getCause());
}
Also used : XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) IconException(org.xwiki.icon.IconException) IconException(org.xwiki.icon.IconException) Test(org.junit.Test)

Example 4 with XWikiVelocityException

use of org.xwiki.velocity.XWikiVelocityException in project xwiki-platform by xwiki.

the class PdfExportImpl method getPDFTemplateProperty.

/**
 * Extract XSLT file content using the following algorithm:
 * <ul>
 *   <li>Check if a query string named {@code pdftemplate} exists and if so use its value as the reference to
 *       a document containing a XWiki.PDFClass xobject from which to extract the XSLT data. If not defined
 *       (or if empty) then use the current document as the document having the XWiki.PDFClass xobject.</li>
 *   <li>Read the value of the xproperty named after the passed {@code propertyName} parameter. If the document
 *       or the property don't exist, then return an empty String. Otherwise execute Velocity on the xproperty's
 *       value and return this.</li>
 * </ul>
 *
 * @param propertyName the xproperty containing the XSLT to return
 * @param context the current request context
 * @return the content of the xproperty, velocity-parsed, or an empty string if there's no such property
 */
private String getPDFTemplateProperty(String propertyName, XWikiContext context) {
    String pdftemplate = context.getRequest().getParameter("pdftemplate");
    DocumentReference templateReference;
    DocumentReference classReference;
    if (StringUtils.isNotEmpty(pdftemplate)) {
        templateReference = referenceResolver.resolve(pdftemplate);
        classReference = new DocumentReference(templateReference.getWikiReference().getName(), "XWiki", "PDFClass");
    } else {
        templateReference = dab.getCurrentDocumentReference();
        String currentWiki = dab.getCurrentDocumentReference().getRoot().getName();
        classReference = new DocumentReference(currentWiki, "XWiki", "PDFClass");
    }
    String result = (String) dab.getProperty(templateReference, classReference, propertyName);
    if (StringUtils.isBlank(result)) {
        return "";
    }
    String templateName = referenceSerializer.serialize(templateReference);
    try {
        StringWriter writer = new StringWriter();
        VelocityContext vcontext = velocityManager.getVelocityContext();
        velocityManager.getVelocityEngine().evaluate(vcontext, writer, templateName, result);
        result = writer.toString();
    } catch (XWikiVelocityException e) {
        LOGGER.warn("Error applying Velocity to the [{}] property of the [{}] document. Using the property's value " + "without applying Velocity.", propertyName, templateName, ExceptionUtils.getRootCauseMessage(e));
    }
    return result;
}
Also used : XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 5 with XWikiVelocityException

use of org.xwiki.velocity.XWikiVelocityException in project xwiki-platform by xwiki.

the class DefaultOldRendering method parseContent.

@Override
public String parseContent(String content, XWikiContext xcontext) {
    try {
        if (StringUtils.isNotEmpty(content)) {
            VelocityManager velocityManager = this.velocityManagerProvider.get();
            VelocityContext velocityContext = velocityManager.getVelocityContext();
            VelocityEngine velocityEngine = velocityManager.getVelocityEngine();
            StringWriter writer = new StringWriter();
            velocityEngine.evaluate(velocityContext, writer, xcontext.getDoc().getPrefixedFullName(), content);
            return writer.toString();
        }
    } catch (XWikiVelocityException e) {
        this.logger.error("Faield to parse content [" + content + "]", e);
    }
    return "";
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext)

Aggregations

XWikiVelocityException (org.xwiki.velocity.XWikiVelocityException)10 VelocityContext (org.apache.velocity.VelocityContext)8 StringWriter (java.io.StringWriter)7 VelocityEngine (org.xwiki.velocity.VelocityEngine)4 Properties (java.util.Properties)2 Test (org.junit.Test)2 InitializationException (org.xwiki.component.phase.InitializationException)2 IconException (org.xwiki.icon.IconException)2 VelocityManager (org.xwiki.velocity.VelocityManager)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LESSCompiler (org.xwiki.lesscss.compiler.LESSCompiler)1 LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)1 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)1 LESSResourceReferenceFactory (org.xwiki.lesscss.resources.LESSResourceReferenceFactory)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 ObjectPropertyReference (org.xwiki.model.reference.ObjectPropertyReference)1