Search in sources :

Example 11 with HeaderBlock

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

the class XWikiDocument method updateDocumentSection.

/**
 * Update a section content in document.
 *
 * @param sectionNumber the index (starting at 1) of the section in the list of all sections in the document.
 * @param newSectionContent the new section content.
 * @return the new document content.
 * @throws XWikiException error when updating content
 */
public String updateDocumentSection(int sectionNumber, String newSectionContent) throws XWikiException {
    String content;
    if (is10Syntax()) {
        content = updateDocumentSection10(sectionNumber, newSectionContent);
    } else {
        // Get the current section block
        HeaderBlock header = getFilteredHeaders().get(sectionNumber - 1);
        XDOM xdom = (XDOM) header.getRoot();
        // newSectionContent -> Blocks
        List<Block> blocks = parseContent(newSectionContent).getChildren();
        int sectionLevel = header.getLevel().getAsInt();
        for (int level = 1; level < sectionLevel && blocks.size() == 1 && blocks.get(0) instanceof SectionBlock; ++level) {
            blocks = blocks.get(0).getChildren();
        }
        // replace old current SectionBlock with new Blocks
        Block section = header.getSection();
        section.getParent().replaceChild(blocks, section);
        // render back XDOM to document's content syntax
        content = renderXDOM(xdom, getSyntax());
    }
    return content;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) 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) SectionBlock(org.xwiki.rendering.block.SectionBlock)

Aggregations

HeaderBlock (org.xwiki.rendering.block.HeaderBlock)11 Block (org.xwiki.rendering.block.Block)9 XDOM (org.xwiki.rendering.block.XDOM)8 SectionBlock (org.xwiki.rendering.block.SectionBlock)7 LinkBlock (org.xwiki.rendering.block.LinkBlock)5 ArrayList (java.util.ArrayList)4 WordBlock (org.xwiki.rendering.block.WordBlock)4 ToString (org.suigeneris.jrcs.util.ToString)3 IdBlock (org.xwiki.rendering.block.IdBlock)3 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)3 List (java.util.List)2 Test (org.junit.Test)2 WikiDocument (org.xwiki.refactoring.WikiDocument)2 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)2 NamingCriterion (org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion)2 BlockFilter (org.xwiki.rendering.block.BlockFilter)2 MacroBlock (org.xwiki.rendering.block.MacroBlock)2 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)2 SpaceBlock (org.xwiki.rendering.block.SpaceBlock)2 SpecialSymbolBlock (org.xwiki.rendering.block.SpecialSymbolBlock)2