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);
}
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);
}
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);
}
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;
}
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;
}
Aggregations