use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.
the class MacroInstruction method resolveMacroDescriptor.
private MacroDescriptor resolveMacroDescriptor(final SiteConfigs siteConfigs, final String macroName) {
// Searches for the macro in the applications associated to the site
MacroDescriptor macroDescriptor = siteConfigs.stream().map(siteConfig -> MacroKey.from(siteConfig.getApplicationKey(), macroName)).map(macroDescriptorService::getByKey).filter(Objects::nonNull).findFirst().orElse(null);
if (macroDescriptor == null) {
macroDescriptor = resolveMacroDescriptorCaseInsensitive(siteConfigs, macroName);
}
// If there is no corresponding macro
if (macroDescriptor == null) {
// Searches in the builtin macros
final MacroKey macroKey = MacroKey.from(ApplicationKey.SYSTEM, macroName);
macroDescriptor = macroDescriptorService.getByKey(macroKey);
}
return macroDescriptor;
}
use of com.enonic.xp.macro.MacroDescriptor 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.macro.MacroDescriptor in project xp by enonic.
the class MacroDescriptorServiceTest method testGetBySystemKey.
@Test
public void testGetBySystemKey() throws Exception {
final MacroKey macroKey = MacroKey.from(ApplicationKey.SYSTEM, "disable");
final MacroDescriptor descriptor = this.service.getByKey(macroKey);
assertNotNull(descriptor);
assertTrue(descriptor.getKey().equals(macroKey));
assertEquals("Disable macros", descriptor.getDisplayName());
assertEquals("Contents of this macro will not be formatted", descriptor.getDescription());
assertNotNull(descriptor.getForm());
}
use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.
the class MacroDescriptorServiceTest method testGetByKey.
@Test
public void testGetByKey() throws Exception {
final MacroKey macroKey = MacroKey.from(ApplicationKey.from("myapp1"), "macro1");
final MacroDescriptor descriptor = this.service.getByKey(macroKey);
assertNotNull(descriptor);
assertTrue(descriptor.getKey().equals(macroKey));
}
use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.
the class MacroDescriptorServiceTest method testIconAdded.
@Test
public void testIconAdded() throws Exception {
final MacroKey macroKey = MacroKey.from(ApplicationKey.SYSTEM, "disable");
final MacroDescriptor descriptor = this.service.getByKey(macroKey);
assertNotNull(descriptor);
assertTrue(descriptor.getKey().equals(macroKey));
assertNotNull(descriptor.getIcon());
}
Aggregations