use of com.enonic.xp.portal.macro.MacroProcessor 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());
}
use of com.enonic.xp.portal.macro.MacroProcessor 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);
}
}
use of com.enonic.xp.portal.macro.MacroProcessor in project xp by enonic.
the class MacroInstructionTest method testInstructionMacroMultiValue.
@Test
public void testInstructionMacroMultiValue() throws Exception {
MacroKey key = MacroKey.from("myapp:mymacro");
MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).build();
when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
MacroProcessor macro = (ctx) -> PortalResponse.create().body(ctx.getName() + ": param1=" + ctx.getParameter("param1").get(0) + ", body=" + ctx.getBody()).build();
when(macroProcessorFactory.fromScript(any())).thenReturn(macro);
String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"mymacro\" param1=\"value1\" param1=\"value2\" param2=\"other\" _body=\"body\"").getAsString();
assertEquals("mymacro: param1=value1, body=body", outputHtml);
}
use of com.enonic.xp.portal.macro.MacroProcessor in project xp by enonic.
the class MacroInstructionTest method testInstructionMacro.
@Test
public void testInstructionMacro() throws Exception {
MacroKey key = MacroKey.from("myapp:mymacro");
MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).build();
when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
MacroProcessor macro = (ctx) -> PortalResponse.create().body(ctx.getName() + ": param1=" + ctx.getParameter("param1").get(0) + ", body=" + ctx.getBody()).build();
when(macroProcessorFactory.fromScript(any())).thenReturn(macro);
String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"mymacro\" param1=\"value1\" _body=\"body\"").getAsString();
assertEquals("mymacro: param1=value1, body=body", outputHtml);
}
use of com.enonic.xp.portal.macro.MacroProcessor in project xp by enonic.
the class MacroInstructionTest method testInstructionSystemMacro.
@Test
public void testInstructionSystemMacro() throws Exception {
MacroKey key = MacroKey.from(ApplicationKey.SYSTEM, "mymacro");
MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).build();
when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
when(macroDescriptorService.getByApplication(any())).thenReturn(MacroDescriptors.empty());
MacroProcessor macro = (ctx) -> PortalResponse.create().body(ctx.getName() + ": param1=" + ctx.getParam("param1") + ", body=" + ctx.getBody()).build();
when(macroProcessorFactory.fromScript(any())).thenReturn(macro);
String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"mymacro\" param1=\"value1\" _body=\"body\"").getAsString();
assertEquals("mymacro: param1=value1, body=body", outputHtml);
}
Aggregations