Search in sources :

Example 1 with TemplateManager

use of org.xwiki.template.TemplateManager in project xwiki-platform by xwiki.

the class DefaultNotificationFilterDisplayerTest method setUp.

@Before
public void setUp() throws Exception {
    templateManager = mocker.registerMockComponent(TemplateManager.class);
    ScriptContextManager scriptContextManager = mocker.registerMockComponent(ScriptContextManager.class);
    when(scriptContextManager.getCurrentScriptContext()).thenReturn(mock(ScriptContext.class));
}
Also used : TemplateManager(org.xwiki.template.TemplateManager) ScriptContext(javax.script.ScriptContext) ScriptContextManager(org.xwiki.script.ScriptContextManager) Before(org.junit.Before)

Example 2 with TemplateManager

use of org.xwiki.template.TemplateManager in project xwiki-platform by xwiki.

the class PropertyClass method getDefaultCustomDisplayer.

/**
 * Method to find the default custom displayer to use for a specific Property Class.
 *
 * @param propertyClassName the type of the property; this is defined in each subclass, such as {@code boolean},
 *            {@code string} or {@code dblist}
 * @param context the current request context
 * @return An identifier for the location of a custom displayer. This can be {@code class} if there's custom display
 *         code specified in the class itself, {@code page:currentwiki:XWiki.BooleanDisplayer} if such a document
 *         exists in the current wiki, {@code page:xwiki:XWiki.StringDisplayer} if such a document exists in the
 *         main wiki, or {@code template:displayer_boolean.vm} if a template on the filesystem or in the current
 *         skin exists.
 */
protected String getDefaultCustomDisplayer(String propertyClassName, XWikiContext context) {
    LOGGER.debug("Looking up default custom displayer for property class name [{}]", propertyClassName);
    try {
        // First look into the current wiki
        String pageName = StringUtils.capitalize(propertyClassName) + "Displayer";
        DocumentReference reference = new DocumentReference(context.getWikiId(), "XWiki", pageName);
        if (context.getWiki().exists(reference, context)) {
            LOGGER.debug("Found default custom displayer for property class name in local wiki: [{}]", pageName);
            return DOCUMENT_DISPLAYER_IDENTIFIER_PREFIX + "XWiki." + pageName;
        }
        // Look in the main wiki
        if (!context.isMainWiki()) {
            reference = new DocumentReference(context.getMainXWiki(), "XWiki", pageName);
            if (context.getWiki().exists(reference, context)) {
                LOGGER.debug("Found default custom displayer for property class name in main wiki: [{}]", pageName);
                return DOCUMENT_DISPLAYER_IDENTIFIER_PREFIX + context.getMainXWiki() + ":XWiki." + pageName;
            }
        }
        // Look in templates
        String templateName = "displayer_" + propertyClassName + ".vm";
        TemplateManager templateManager = Utils.getComponent(TemplateManager.class);
        Template existingTemplate = templateManager.getTemplate(templateName);
        if (existingTemplate != null) {
            LOGGER.debug("Found default custom displayer for property class name as template: [{}]", templateName);
            return TEMPLATE_DISPLAYER_IDENTIFIER_PREFIX + templateName;
        }
    } catch (Throwable e) {
        // If we fail we consider there is no custom displayer
        LOGGER.error("Error finding if property [{}] has a custom displayer. " + "Considering that there's no custom displayer.", propertyClassName, e);
    }
    return null;
}
Also used : TemplateManager(org.xwiki.template.TemplateManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Template(org.xwiki.template.Template)

Aggregations

TemplateManager (org.xwiki.template.TemplateManager)2 ScriptContext (javax.script.ScriptContext)1 Before (org.junit.Before)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 ScriptContextManager (org.xwiki.script.ScriptContextManager)1 Template (org.xwiki.template.Template)1