use of org.xwiki.rendering.block.MacroBlock in project xwiki-platform by xwiki.
the class DefaultLinkedResourceHelper method setResourceReferenceString.
@Override
public void setResourceReferenceString(Block block, String newReferenceString) {
if (block instanceof LinkBlock) {
LinkBlock linkBlock = (LinkBlock) block;
ResourceReference linkReference = linkBlock.getReference();
linkReference.setReference(newReferenceString);
} else if (block instanceof MacroBlock) {
if (StringUtils.isNotBlank(block.getParameter(DOCUMENT_MACRO_PARAMETER))) {
// Backwards compatibility check.
block.setParameter(DOCUMENT_MACRO_PARAMETER, newReferenceString);
} else {
block.setParameter(REFERENCE_MACRO_PARAMETER, newReferenceString);
}
}
}
use of org.xwiki.rendering.block.MacroBlock in project xwiki-platform by xwiki.
the class DisplayMacroTest method createMacroTransformationContext.
private MacroTransformationContext createMacroTransformationContext(String documentName, boolean isInline) {
MacroTransformationContext context = new MacroTransformationContext();
MacroBlock displayMacro = new MacroBlock("display", Collections.singletonMap("reference", documentName), isInline);
context.setCurrentMacroBlock(displayMacro);
return context;
}
use of org.xwiki.rendering.block.MacroBlock in project xwiki-platform by xwiki.
the class XWikiDocument method getIncludedPagesInternal.
private List<String> getIncludedPagesInternal(XWikiContext context) {
if (is10Syntax()) {
return getIncludedPagesForXWiki10Syntax(getContent(), context);
} else {
// Find all include macros listed on the page
XDOM dom = getXDOM();
List<String> result = new ArrayList<String>();
List<MacroBlock> macroBlocks = dom.getBlocks(new ClassBlockMatcher(MacroBlock.class), Block.Axes.DESCENDANT);
for (MacroBlock macroBlock : macroBlocks) {
if (macroBlock.getId().equalsIgnoreCase("include") || macroBlock.getId().equalsIgnoreCase("display")) {
String documentName = macroBlock.getParameters().get("reference");
if (StringUtils.isEmpty(documentName)) {
documentName = macroBlock.getParameters().get("document");
if (StringUtils.isEmpty(documentName)) {
continue;
}
}
DocumentReference documentReference = getExplicitDocumentReferenceResolver().resolve(documentName, getDocumentReference());
if (this.getDocumentReference().equals(documentReference)) {
// Skip auto-includes since they are not allowed anyway.
continue;
}
documentName = LOCAL_REFERENCE_SERIALIZER.serialize(documentReference);
result.add(documentName);
} else if (macroBlock.getId().equalsIgnoreCase("velocity") && !StringUtils.isEmpty(macroBlock.getContent())) {
// Try to find matching content inside each velocity macro
result.addAll(getIncludedPagesForXWiki10Syntax(macroBlock.getContent(), context));
}
}
return result;
}
}
use of org.xwiki.rendering.block.MacroBlock in project xwiki-platform by xwiki.
the class XWikiDocument method getDefaultEditModeInternal.
private String getDefaultEditModeInternal(XWikiContext context) throws XWikiException {
String editModeProperty = "defaultEditMode";
DocumentReference editModeClass = getCurrentReferenceDocumentReferenceResolver().resolve(XWikiConstant.EDIT_MODE_CLASS);
// check if the current document has any edit mode class object attached to it, and read the edit mode from it
BaseObject editModeObject = this.getXObject(editModeClass);
if (editModeObject != null) {
String defaultEditMode = editModeObject.getStringValue(editModeProperty);
if (StringUtils.isEmpty(defaultEditMode)) {
return "edit";
} else {
return defaultEditMode;
}
}
// otherwise look for included documents
com.xpn.xwiki.XWiki xwiki = context.getWiki();
if (is10Syntax()) {
if (getContent().indexOf("includeForm(") != -1) {
return "inline";
}
} else {
// its own name since it's a deployment time concern.
for (Block macroBlock : getXDOM().getBlocks(new MacroBlockMatcher("include"), Axes.CHILD)) {
// Find the document reference to include by checking the macro's "reference" parameter.
// For backward-compatibility we also check for a "document" parameter since this is the parameter name
// that was used prior to XWiki 3.4M1 when the "reference" one was introduced and thus when the
// "document" one was deprecated.
String includedDocumentReference = macroBlock.getParameter("reference");
if (includedDocumentReference == null) {
includedDocumentReference = macroBlock.getParameter("document");
}
if (includedDocumentReference != null) {
// Resolve the document name into a valid Reference
DocumentReference documentReference = getCurrentMixedDocumentReferenceResolver().resolve(includedDocumentReference);
XWikiDocument includedDocument = xwiki.getDocument(documentReference, context);
if (!includedDocument.isNew()) {
// get the edit mode object, first the new class and then the deprecated class if new class
// is not found
editModeObject = includedDocument.getXObject(editModeClass);
if (editModeObject == null) {
editModeObject = includedDocument.getXObject(SHEETCLASS_REFERENCE);
}
if (editModeObject != null) {
// Use the user-defined default edit mode if set.
String defaultEditMode = editModeObject.getStringValue(editModeProperty);
if (StringUtils.isBlank(defaultEditMode)) {
// and inline only if the object is sheetclass
return "inline";
} else {
return defaultEditMode;
}
}
}
}
}
}
return "edit";
}
use of org.xwiki.rendering.block.MacroBlock in project xwiki-platform by xwiki.
the class DefaultLinkRefactoringTest method renameLinksFromMacros.
@Test
public void renameLinksFromMacros() 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 includeMacroBlock1 = new MacroBlock("include", includeParameters, false);
Map<String, String> includeOldParameters = new HashMap<String, String>();
includeOldParameters.put("document", "A.B");
MacroBlock includeMacroBlock2 = new MacroBlock("include", includeOldParameters, false);
Map<String, String> displayParameters = new HashMap<String, String>();
displayParameters.put("reference", "A.B");
MacroBlock displayMacroBlock = new MacroBlock("display", displayParameters, false);
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(includeMacroBlock1, includeMacroBlock2, displayMacroBlock));
ResourceReference macroResourceReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
when(this.resourceReferenceResolver.resolve(macroResourceReference, 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", includeMacroBlock1.getParameter("reference"));
assertEquals("X.Y", includeMacroBlock2.getParameter("document"));
assertEquals("X.Y", displayMacroBlock.getParameter("reference"));
verifyDocumentSave(document, "Renamed back-links.", false);
}
Aggregations