Search in sources :

Example 6 with MacroBlock

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

the class DefaultLinkRefactoringTest method renameLinksFromLinksAndMacros.

@Test
public void renameLinksFromLinksAndMacros() throws Exception {
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument document = mock(XWikiDocument.class);
    when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
    when(document.getDocumentReference()).thenReturn(documentReference);
    when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
    // From a terminal document to another terminal document.
    DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B");
    DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
    XDOM xdom = mock(XDOM.class);
    when(document.getXDOM()).thenReturn(xdom);
    Map<String, String> includeParameters = new HashMap<String, String>();
    includeParameters.put("reference", "A.B");
    MacroBlock includeMacroBlock = new MacroBlock("include", includeParameters, false);
    ResourceReference resourceReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
    LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), resourceReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(includeMacroBlock, documentLinkBlock));
    when(this.resourceReferenceResolver.resolve(resourceReference, null, documentReference)).thenReturn(oldLinkTarget);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
    this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
    assertEquals("X.Y", includeMacroBlock.getParameter("reference"));
    assertEquals("X.Y", documentLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
    verifyDocumentSave(document, "Renamed back-links.", false);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 7 with MacroBlock

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

the class ClassLoadingTest method registerComponents.

@Override
protected void registerComponents() throws Exception {
    super.registerComponents();
    this.mockSetup = new ScriptMockSetup(getComponentManager());
    this.macro = getComponentManager().getInstance(Macro.class, "groovy");
    this.context = new MacroTransformationContext();
    // The script macro checks the current block (which is a macro block) to see what engine to use
    this.context.setCurrentMacroBlock(new MacroBlock("groovy", Collections.<String, String>emptyMap(), false));
    // Set the syntax since the script macro needs it to parse the script result using that syntax
    this.context.setSyntax(Syntax.XWIKI_2_0);
}
Also used : ScriptMockSetup(org.xwiki.rendering.macro.script.ScriptMockSetup) Macro(org.xwiki.rendering.macro.Macro) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 8 with MacroBlock

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

the class SecurityTest method executeGroovyMacro.

private void executeGroovyMacro(String script, boolean restricted) throws Exception {
    Macro macro = getComponentManager().getInstance(Macro.class, "groovy");
    JSR223ScriptMacroParameters parameters = new JSR223ScriptMacroParameters();
    MacroTransformationContext context = new MacroTransformationContext();
    context.getTransformationContext().setRestricted(restricted);
    context.setSyntax(Syntax.XWIKI_2_1);
    // The script macro checks the current block (which is a macro block) to see what engine to use.
    context.setCurrentMacroBlock(new MacroBlock("groovy", Collections.<String, String>emptyMap(), false));
    macro.execute(parameters, script, context);
}
Also used : Macro(org.xwiki.rendering.macro.Macro) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) JSR223ScriptMacroParameters(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 9 with MacroBlock

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

the class AbstractScriptMacro method parseScriptResult.

/**
 * Convert script result as a {@link Block} list.
 *
 * @param content the script result to parse.
 * @param parameters the macro parameters.
 * @param context the context of the macro transformation.
 * @return the {@link Block}s.
 * @throws MacroExecutionException Failed to find source parser.
 * @since 2.1M1
 */
protected List<Block> parseScriptResult(String content, P parameters, MacroTransformationContext context) throws MacroExecutionException {
    List<Block> result;
    if (parameters.isWiki()) {
        result = parseSourceSyntax(content, context);
    } else {
        try {
            result = this.plainTextParser.parse(new StringReader(content)).getChildren();
        } catch (ParseException e) {
            // String.
            throw new MacroExecutionException("Failed to parse link label as plain text", e);
        }
    }
    // 3) If in inline mode remove any top level paragraph
    if (context.isInline()) {
        this.parserUtils.removeTopLevelParagraph(result);
        // TODO: use inline parser instead
        if (!result.isEmpty() && result.get(0) instanceof MacroBlock && !((MacroBlock) result.get(0)).isInline()) {
            MacroBlock macro = (MacroBlock) result.get(0);
            result.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true));
        }
    }
    return result;
}
Also used : StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) ParseException(org.xwiki.rendering.parser.ParseException) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 10 with MacroBlock

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

the class NestedScriptMacroValidatorTest method buildContext.

/**
 * Build a chain of nested macros ({@link MacroMarkerBlock} blocks) and put them into a macro transformation
 * context. The chain will be the only child of a top level XDOM, the last child will be the current
 * {@link MacroBlock}.
 *
 * @param chain list of nested macro names (starting with parent)
 * @return an initialized macro transformation context
 */
private MacroTransformationContext buildContext(String... chain) {
    MacroTransformationContext context = new MacroTransformationContext();
    if (chain == null || chain.length < 1) {
        context.setXDOM(new XDOM(new LinkedList<Block>()));
        return context;
    }
    Map<String, String> parameters = new HashMap<String, String>();
    MacroBlock child = new MacroBlock(chain[chain.length - 1], parameters, false);
    Block current = child;
    for (int i = chain.length - 2; i >= 0; i--) {
        Block parent = new MacroMarkerBlock(chain[i], parameters, Collections.singletonList(current), false);
        current = parent;
    }
    XDOM root = new XDOM(Collections.singletonList(current));
    context.setXDOM(root);
    context.setCurrentMacroBlock(child);
    return context;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) LinkedList(java.util.LinkedList) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Aggregations

MacroBlock (org.xwiki.rendering.block.MacroBlock)30 Test (org.junit.Test)14 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)13 XDOM (org.xwiki.rendering.block.XDOM)12 DocumentReference (org.xwiki.model.reference.DocumentReference)9 Block (org.xwiki.rendering.block.Block)8 LinkBlock (org.xwiki.rendering.block.LinkBlock)7 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 HashMap (java.util.HashMap)5 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)5 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)4 MetaData (org.xwiki.rendering.listener.MetaData)4 ContextMacroParameters (org.xwiki.rendering.macro.context.ContextMacroParameters)4 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)3 WordBlock (org.xwiki.rendering.block.WordBlock)3 MacroContentParser (org.xwiki.rendering.macro.MacroContentParser)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ToString (org.suigeneris.jrcs.util.ToString)2