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;
}
Aggregations