Search in sources :

Example 1 with MacroContext

use of com.enonic.xp.portal.macro.MacroContext in project xp by enonic.

the class DisableMacroProcessorTest method testProcess.

@Test
public void testProcess() {
    final MacroProcessor processor = new DisableMacroProcessor();
    final MacroContext macroContext1 = MacroContext.create().name("name").body("here is macro: [macro]body[/macro]").build();
    assertEquals("here is macro: [macro]body[/macro]", processor.process(macroContext1).getBody());
    final MacroContext macroContext2 = MacroContext.create().name("name").body("<tagWithMacro>here is macro: [macro]body[/macro]</tagWithMacro>").build();
    assertEquals("<tagWithMacro>here is macro: [macro]body[/macro]</tagWithMacro>", processor.process(macroContext2).getBody());
}
Also used : MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) MacroContext(com.enonic.xp.portal.macro.MacroContext) Test(org.junit.jupiter.api.Test)

Example 2 with MacroContext

use of com.enonic.xp.portal.macro.MacroContext in project xp by enonic.

the class MacroInstruction method evaluate.

@Override
public PortalResponse evaluate(final PortalRequest portalRequest, final String instruction) {
    if (!Instruction.isInstruction(instruction, "MACRO")) {
        return null;
    }
    // parse instruction
    final Instruction macroInstruction;
    try {
        macroInstruction = new InstructionParser().parse(instruction);
    } catch (RenderException e) {
        return null;
    }
    final String macroName = macroInstruction.attribute(MACRO_NAME);
    if (macroName == null) {
        return null;
    }
    // resolve macro processor
    final Site site = portalRequest.getSite();
    if (site == null) {
        throw new RenderException("Macro controller script could not be resolved, context site could not be found.");
    }
    final MacroDescriptor macroDescriptor = resolveMacroDescriptor(site.getSiteConfigs(), macroName);
    if (macroDescriptor == null) {
        final String editModeMacro = toMacroInstruction(macroInstruction);
        return PortalResponse.create().body(editModeMacro).build();
    }
    final MacroProcessor macroProcessor = resolveMacroProcessor(macroDescriptor);
    if (macroProcessor == null) {
        throw new RenderException("Macro controller not found: " + macroName);
    }
    // execute macro
    final MacroContext context = createContext(macroInstruction, macroDescriptor, portalRequest);
    final ApplicationKey previousAppKey = portalRequest.getApplicationKey();
    try {
        portalRequest.setApplicationKey(macroDescriptor.getKey().getApplicationKey());
        return macroProcessor.process(context);
    } finally {
        portalRequest.setApplicationKey(previousAppKey);
    }
}
Also used : Site(com.enonic.xp.site.Site) ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) MacroContext(com.enonic.xp.portal.macro.MacroContext) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction)

Example 3 with MacroContext

use of com.enonic.xp.portal.macro.MacroContext in project xp by enonic.

the class MacroInstruction method createContext.

private MacroContext createContext(final Instruction macroInstruction, final MacroDescriptor macroDescriptor, final PortalRequest request) {
    final Form macroForm = macroDescriptor.getForm();
    final Map<String, String> paramCaseTranslator = new HashMap<>(macroForm.size());
    for (FormItem formItem : macroForm) {
        final String name = formItem.getName();
        paramCaseTranslator.put(name.toLowerCase(), name);
    }
    final MacroContext.Builder context = MacroContext.create().name(macroDescriptor.getName());
    for (String name : macroInstruction.attributeNames()) {
        if (name.equalsIgnoreCase(MACRO_BODY) || name.equalsIgnoreCase(MACRO_NAME) || name.equalsIgnoreCase(MACRO_DOCUMENT)) {
            continue;
        }
        String contextParamName = name;
        if (macroForm.getFormItems().getItemByName(name) == null) {
            final String normalizedName = paramCaseTranslator.get(name.toLowerCase());
            if (normalizedName != null) {
                contextParamName = normalizedName;
            }
        }
        for (String attribute : macroInstruction.attributes(name)) {
            context.param(contextParamName, attribute);
        }
    }
    context.body(macroInstruction.attribute(MACRO_BODY));
    context.request(request);
    final String documentRef = macroInstruction.attribute(MACRO_DOCUMENT);
    final String document = (String) ContextAccessor.current().getLocalScope().getAttribute(documentRef);
    context.document(document);
    return context.build();
}
Also used : Form(com.enonic.xp.form.Form) HashMap(java.util.HashMap) FormItem(com.enonic.xp.form.FormItem) MacroContext(com.enonic.xp.portal.macro.MacroContext)

Aggregations

MacroContext (com.enonic.xp.portal.macro.MacroContext)3 MacroProcessor (com.enonic.xp.portal.macro.MacroProcessor)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 Form (com.enonic.xp.form.Form)1 FormItem (com.enonic.xp.form.FormItem)1 MacroDescriptor (com.enonic.xp.macro.MacroDescriptor)1 RenderException (com.enonic.xp.portal.impl.rendering.RenderException)1 PostProcessInstruction (com.enonic.xp.portal.postprocess.PostProcessInstruction)1 Site (com.enonic.xp.site.Site)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1