Search in sources :

Example 6 with ClassBlockMatcher

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

the class DocumentSplitterUtils method relocateArtifacts.

/**
 * Move artifacts (i.e. embedded images) from the original office document to a specific wiki document corresponding
 * to a section. Only the artifacts from that section are moved.
 *
 * @param sectionDoc the newly created wiki document corresponding to a section of the original office document
 * @param officeDocument the office document being splitted into wiki documents
 * @return the relocated artifacts
 */
public static Map<String, byte[]> relocateArtifacts(WikiDocument sectionDoc, XDOMOfficeDocument officeDocument) {
    Map<String, byte[]> artifacts = new HashMap<String, byte[]>();
    List<ImageBlock> imageBlocks = sectionDoc.getXdom().getBlocks(new ClassBlockMatcher(ImageBlock.class), Axes.DESCENDANT);
    for (ImageBlock imageBlock : imageBlocks) {
        String imageReference = imageBlock.getReference().getReference();
        artifacts.put(imageReference, officeDocument.getArtifacts().remove(imageReference));
    }
    return artifacts;
}
Also used : ImageBlock(org.xwiki.rendering.block.ImageBlock) HashMap(java.util.HashMap) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher)

Example 7 with ClassBlockMatcher

use of org.xwiki.rendering.block.match.ClassBlockMatcher 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 8 with ClassBlockMatcher

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

the class DocumentTitleDisplayer method extractTitleFromContent.

@Override
protected XDOM extractTitleFromContent(DocumentModelBridge document, DocumentDisplayerParameters parameters) {
    // Note: Ideally we should apply transformations on the document's returned XDOM here since macros could
    // generate headings for example or some other transformations could modify headings. However we don't do this
    // at the moment since it would be too costly to do so. In the future we will even probably remove the feature
    // of generating the title from the content.
    List<HeaderBlock> blocks = document.getXDOM().getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT);
    if (!blocks.isEmpty()) {
        HeaderBlock heading = blocks.get(0);
        // Check the heading depth after which we should return null if no heading was found.
        if (heading.getLevel().getAsInt() <= displayConfiguration.getTitleHeadingDepth()) {
            XDOM headingXDOM = new XDOM(Collections.<Block>singletonList(heading));
            try {
                TransformationContext txContext = new TransformationContext(headingXDOM, document.getSyntax(), parameters.isTransformationContextRestricted());
                txContext.setTargetSyntax(parameters.getTargetSyntax());
                transformationManager.performTransformations(headingXDOM, txContext);
                Block headingBlock = headingXDOM.getChildren().size() > 0 ? headingXDOM.getChildren().get(0) : null;
                if (headingBlock instanceof HeaderBlock) {
                    return new XDOM(headingBlock.getChildren());
                }
            } catch (TransformationException e) {
                getLogger().warn("Failed to extract title from document content.");
            }
        }
    }
    return null;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) TransformationContext(org.xwiki.rendering.transformation.TransformationContext)

Example 9 with ClassBlockMatcher

use of org.xwiki.rendering.block.match.ClassBlockMatcher 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)

Example 10 with ClassBlockMatcher

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

the class ColumnsLayoutManager method layoutContainer.

@Override
public void layoutContainer(Block container) {
    List<GroupBlock> innerGroups = container.getBlocks(new ClassBlockMatcher(GroupBlock.class), Block.Axes.CHILD);
    // FIXME Should we cry and throw an exception if ever we meet something else than a group right under
    // the container macro, or automatically put it in a group maybe ?
    int count = innerGroups.size();
    if (innerGroups.size() == 0) {
        // nothing inside, nothing to layout
        return;
    }
    Map<String, Object> skinxParams = new HashMap<String, Object>();
    skinxParams.put("forceSkinAction", true);
    ssfx.use("uicomponents/container/columns.css", skinxParams);
    // add styles to all columns inside
    for (int i = 0; i < count; i++) {
        GroupBlock column = innerGroups.get(i);
        String classValue = "column";
        if (i == 0) {
            // we're at the first element in the list, put a marker. Don't need it to do standard columns layout,
            // but maybe somebody needs it for customization...
            classValue = classValue + " first-column";
        }
        if (i == count - 1) {
            // we're at the last element in the list, put a marker
            classValue = classValue + " last-column";
        }
        String oldClass = column.getParameter(CLASS_ATTRIBUTE);
        column.setParameter(CLASS_ATTRIBUTE, (StringUtils.isEmpty(oldClass) ? classValue : oldClass + " " + classValue));
    }
    // finally, clear the floats introduced by the columns
    Map<String, String> clearFloatsParams = new HashMap<String, String>();
    clearFloatsParams.put(CLASS_ATTRIBUTE, "clearfloats");
    String oldClass = container.getParameter(CLASS_ATTRIBUTE);
    String newClass = "container-columns container-columns-" + count;
    container.setParameter(CLASS_ATTRIBUTE, StringUtils.isEmpty(oldClass) ? newClass : oldClass + " " + newClass);
    container.addChild(new GroupBlock(clearFloatsParams));
}
Also used : HashMap(java.util.HashMap) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) GroupBlock(org.xwiki.rendering.block.GroupBlock)

Aggregations

ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)16 XDOM (org.xwiki.rendering.block.XDOM)10 Block (org.xwiki.rendering.block.Block)6 LinkBlock (org.xwiki.rendering.block.LinkBlock)5 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)5 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)4 ImageBlock (org.xwiki.rendering.block.ImageBlock)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)3 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)3 File (java.io.File)2 InputStream (java.io.InputStream)2 ToString (org.suigeneris.jrcs.util.ToString)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)2 WikiDocument (org.xwiki.refactoring.WikiDocument)2 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)2