Search in sources :

Example 11 with DocumentResourceReference

use of org.xwiki.rendering.listener.reference.DocumentResourceReference in project xwiki-platform by xwiki.

the class DefaultDocumentSplitter method createLink.

/**
 * Creates a {@link LinkBlock} suitable to be placed in the parent document.
 *
 * @param block the {@link Block} that has just been split into a separate document.
 * @param target name of the target wiki document.
 * @return a {@link LinkBlock} representing the link from the parent document to new document.
 */
private LinkBlock createLink(Block block, String target) {
    Block firstBlock = block.getChildren().get(0);
    if (firstBlock instanceof HeaderBlock) {
        DocumentResourceReference reference = new DocumentResourceReference(target);
        // Clone the header block and remove any unwanted stuff
        Block clonedHeaderBlock = firstBlock.clone(new BlockFilter() {

            @Override
            public List<Block> filter(Block block) {
                List<Block> blocks = new ArrayList<Block>();
                if (block instanceof WordBlock || block instanceof SpaceBlock || block instanceof SpecialSymbolBlock) {
                    blocks.add(block);
                }
                return blocks;
            }
        });
        return new LinkBlock(clonedHeaderBlock.getChildren(), reference, false);
    } else if (firstBlock instanceof SectionBlock) {
        return createLink(firstBlock, target);
    } else {
        throw new IllegalArgumentException("A SectionBlock should either begin with a HeaderBlock or another SectionBlock.");
    }
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) NewLineBlock(org.xwiki.rendering.block.NewLineBlock) IdBlock(org.xwiki.rendering.block.IdBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) ArrayList(java.util.ArrayList) List(java.util.List) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) SectionBlock(org.xwiki.rendering.block.SectionBlock) BlockFilter(org.xwiki.rendering.block.BlockFilter)

Aggregations

DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)11 Test (org.junit.Test)9 DocumentReference (org.xwiki.model.reference.DocumentReference)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)6 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)6 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 SpaceReference (org.xwiki.model.reference.SpaceReference)6 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)5 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Expectations (org.jmock.Expectations)1 Block (org.xwiki.rendering.block.Block)1 BlockFilter (org.xwiki.rendering.block.BlockFilter)1 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)1 IdBlock (org.xwiki.rendering.block.IdBlock)1 LinkBlock (org.xwiki.rendering.block.LinkBlock)1 NewLineBlock (org.xwiki.rendering.block.NewLineBlock)1 SectionBlock (org.xwiki.rendering.block.SectionBlock)1