Search in sources :

Example 1 with MacroBlock

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);
        }
    }
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 2 with MacroBlock

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;
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 3 with MacroBlock

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;
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) ArrayList(java.util.ArrayList) ToString(org.suigeneris.jrcs.util.ToString) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 4 with MacroBlock

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";
}
Also used : XWiki(com.xpn.xwiki.XWiki) MacroBlockMatcher(org.xwiki.rendering.block.match.MacroBlockMatcher) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ToString(org.suigeneris.jrcs.util.ToString) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 5 with MacroBlock

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);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

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