Search in sources :

Example 6 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class MacroBlockDumper method dump.

/**
 * {@inheritDoc}
 *
 * The dump contains all the macro parameters and the content of the macro.
 * The source MetaData is also included if available.
 */
@Override
public void dump(OutputStream out, Block block) throws IOException {
    if (block instanceof MacroBlock) {
        MacroBlock b = (MacroBlock) block;
        dump(out, b.getId(), b.getParameters(), b.getContent());
    } else if (block instanceof MacroMarkerBlock) {
        MacroMarkerBlock b = (MacroMarkerBlock) block;
        dump(out, b.getId(), b.getParameters(), b.getContent());
    } else {
        throw new IllegalArgumentException("Unsupported block [" + block.getClass().getName() + "].");
    }
    String source = getSourceReference(block);
    if (source != null) {
        out.write(toBytes(source));
    }
    out.write(0x00);
}
Also used : MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 7 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class MacroBlockDumperTest method testDumpMacroMarkerBlockWithoutSource.

@Test
public void testDumpMacroMarkerBlockWithoutSource() throws Exception {
    byte[] dump1 = dumper.dump(new MacroBlock("macro", params, "content", false));
    byte[] dump2 = dumper.dump(new MacroMarkerBlock("macro", params, "content", Collections.<Block>emptyList(), false));
    assertThat(dump1, equalTo(dump2));
    params.put("param", "value");
    params.put("param2", "value2");
    dump1 = dumper.dump(new MacroBlock("macro", params, "content", false));
    dump2 = dumper.dump(new MacroMarkerBlock("macro", params, "content", Collections.<Block>emptyList(), false));
    assertThat(dump1, equalTo(dump2));
}
Also used : MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 8 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class EditableGadgetRenderer method getGadgetEditMetadata.

/**
 * @param gadget the gadget to decorate
 * @return the block containing the metadata that will allow clients to edit this gadget
 */
protected Block getGadgetEditMetadata(Gadget gadget) {
    GroupBlock metadataBlock = new GroupBlock();
    metadataBlock.setParameter(CLASS, METADATA);
    // look at the content of the gadget and store whether it's a macro or not
    boolean isMacro = gadget.getContent().size() == 1 && gadget.getContent().get(0) instanceof MacroMarkerBlock;
    GroupBlock isMacroBlock = new GroupBlock();
    isMacroBlock.setParameter(CLASS, "isMacro");
    isMacroBlock.addChild(new WordBlock(Boolean.toString(isMacro)));
    metadataBlock.addChild(isMacroBlock);
    if (isMacro) {
        // render the annotated macro call in the page, to be able to edit it. Only the macro call comments will be
        // rendered, since transformations are not ran, so there is no content in the macro. But annotation is
        // enough.
        GroupBlock renderedContentBlock = new GroupBlock();
        renderedContentBlock.setParameter(CLASS, "content");
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer gadgetContentRenderer = getGadgetContentRenderer();
        gadgetContentRenderer.render(gadget.getContent(), printer);
        RawBlock rawBlock = new RawBlock(printer.toString(), getRawBlockSyntax(gadgetContentRenderer));
        renderedContentBlock.addChild(rawBlock);
        // render the title in the page as well, to be edited as source
        GroupBlock gadgetTitleBlock = new GroupBlock();
        gadgetTitleBlock.setParameter(CLASS, "title");
        // even if it's not a word, it's fine since it will be rendered in one piece
        gadgetTitleBlock.addChild(new WordBlock(gadget.getTitleSource()));
        metadataBlock.addChild(renderedContentBlock);
        metadataBlock.addChild(gadgetTitleBlock);
    }
    return metadataBlock;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WordBlock(org.xwiki.rendering.block.WordBlock) RawBlock(org.xwiki.rendering.block.RawBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 9 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class IntegrationTest method initialize.

@RenderingTestSuite.Initialized
@SuppressWarnings("unchecked")
public void initialize(final MockingComponentManager componentManager) throws Exception {
    Mockery mockery = new JUnit4Mockery();
    // Since we have a dependency on XWiki Platform Oldcore the Context Component Manager will be found and the
    // test will try to look up the Dashboard macro in the User and Wiki Component Manager and thus need a Current
    // User and a Current Wiki. It's easier for this test to simply unregister the Context Component Manager rather
    // than have to provide mocks for them.
    componentManager.unregisterComponent(ComponentManager.class, "context");
    final SkinExtension mockSsfx = componentManager.registerMockComponent(mockery, SkinExtension.class, "ssfx", "ssfxMock");
    final SkinExtension mockJsfx = componentManager.registerMockComponent(mockery, SkinExtension.class, "jsfx", "jsfxMock");
    mockery.checking(new Expectations() {

        {
            allowing(mockSsfx).use(with("uicomponents/container/columns.css"), with(any(Map.class)));
            allowing(mockSsfx).use(with("uicomponents/dashboard/dashboard.css"), with(any(Map.class)));
            allowing(mockJsfx).use(with("js/scriptaculous/dragdrop.js"));
            allowing(mockJsfx).use(with("js/scriptaculous/effects.js"));
            allowing(mockJsfx).use(with("uicomponents/dashboard/dashboard.js"), with(any(Map.class)));
        }
    });
    final GadgetSource mockGadgetSource = componentManager.registerMockComponent(mockery, GadgetSource.class);
    mockery.checking(new Expectations() {

        {
            // Mock gadget for macrodashboard_nested_velocity.test
            allowing(mockGadgetSource).getGadgets(with("nested_velocity"), with(any(MacroTransformationContext.class)));
            will(returnValue(Arrays.asList(new Gadget("0", Arrays.asList(new WordBlock("title")), Arrays.asList(new MacroMarkerBlock("velocity", Collections.emptyMap(), "someVelocityCodeHere", Collections.singletonList(new WordBlock("someVelocityOutputHere")), true)), "1,1"))));
            // Mock gadget for macrodashboard1.test
            allowing(mockGadgetSource).getGadgets(with(aNull(String.class)), with(any(MacroTransformationContext.class)));
            will(returnValue(Arrays.asList(new Gadget("0", Arrays.<Block>asList(new WordBlock("title")), Arrays.<Block>asList(new WordBlock("content")), "1,1"))));
            allowing(mockGadgetSource).getDashboardSourceMetadata(with(AnyOf.anyOf(aNull(String.class), any(String.class))), with(any(MacroTransformationContext.class)));
            will(returnValue(Collections.<Block>emptyList()));
            allowing(mockGadgetSource).isEditing();
            // return true on is editing, to take as many paths possible
            will(returnValue(true));
        }
    });
    // Mock VelocityManager used in macrodashboard_nested_velocity.test because we do not have an XWikiContext
    // instance in the ExecutionContext.
    final VelocityManager mockVelocityManager = componentManager.registerMockComponentWithId(mockery, VelocityManager.class, "velocityManagerMock");
    mockery.checking(new Expectations() {

        {
            allowing(mockVelocityManager).getVelocityContext();
            will(returnValue(new VelocityContext()));
            allowing(mockVelocityManager).getCurrentVelocityContext();
            will(returnValue(new VelocityContext()));
            allowing(mockVelocityManager).getVelocityEngine();
            will(doAll(new CustomAction("mockGetVelocityEngine") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    VelocityEngine velocityEngine = componentManager.getInstance(VelocityEngine.class);
                    Properties properties = new Properties();
                    properties.setProperty("resource.loader", "file");
                    velocityEngine.initialize(properties);
                    return velocityEngine;
                }
            }));
            allowing(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
            will(doAll(new CustomAction("mockEvaluate") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    VelocityEngine velocityEngine = mockVelocityManager.getVelocityEngine();
                    return velocityEngine.evaluate(mockVelocityManager.getVelocityContext(), (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
                }
            }));
        }
    });
    componentManager.registerMockComponent(mockery, ContextualAuthorizationManager.class);
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) SkinExtension(org.xwiki.skinx.SkinExtension) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) VelocityContext(org.apache.velocity.VelocityContext) WordBlock(org.xwiki.rendering.block.WordBlock) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Reader(java.io.Reader) Properties(java.util.Properties) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) VelocityManager(org.xwiki.velocity.VelocityManager) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Map(java.util.Map) Writer(java.io.Writer)

Example 10 with MacroMarkerBlock

use of org.xwiki.rendering.block.MacroMarkerBlock in project xwiki-platform by xwiki.

the class NestedScriptMacroValidatorListener method check.

@Override
protected void check(CancelableEvent event, MacroTransformationContext context, ScriptMacroParameters parameters) {
    // Traverse the XDOM tree up to the root
    if (context.getCurrentMacroBlock() != null) {
        MacroMarkerBlock parent = context.getCurrentMacroBlock().getFirstBlock(new ClassBlockMatcher(MacroMarkerBlock.class), Block.Axes.ANCESTOR);
        while (parent != null) {
            String parentId = parent.getId();
            try {
                Macro<?> macro = this.macroManager.getMacro(new MacroId(parentId));
                if (macro instanceof ScriptMacro) {
                    // Find the
                    event.cancel(String.format("Nested scripts are not allowed. Current Script Macro [%s] " + "(source [%s]) is executed inside Script Macro [%s] (source [%s])", context.getCurrentMacroBlock().getId(), extractSourceContentReference(context.getCurrentMacroBlock()), parentId, extractSourceContentReference(parent)));
                } else if (macro instanceof NestedScriptMacroEnabled) {
                    // This macro has the right to produce script macro whatever the parent.
                    return;
                } else if ("include".equals(parentId)) {
                    // use NestedScriptMacroEnabled, we should maybe find something more generic
                    return;
                }
            } catch (MacroLookupException exception) {
            // Shouldn't happen, the parent macro was already successfully executed earlier
            }
            parent = parent.getFirstBlock(new ClassBlockMatcher(MacroMarkerBlock.class), Block.Axes.ANCESTOR);
        }
    }
}
Also used : ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroId(org.xwiki.rendering.macro.MacroId) ScriptMacro(org.xwiki.rendering.macro.script.ScriptMacro) MacroLookupException(org.xwiki.rendering.macro.MacroLookupException)

Aggregations

MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)12 Block (org.xwiki.rendering.block.Block)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 Test (org.junit.Test)5 WordBlock (org.xwiki.rendering.block.WordBlock)4 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)4 XDOM (org.xwiki.rendering.block.XDOM)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)3 HashMap (java.util.HashMap)2 Expectations (org.jmock.Expectations)2 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)2 Reader (java.io.Reader)1 Writer (java.io.Writer)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 VelocityContext (org.apache.velocity.VelocityContext)1 Mockery (org.jmock.Mockery)1 Invocation (org.jmock.api.Invocation)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1