use of org.xwiki.rendering.block.SpaceBlock in project xwiki-platform by xwiki.
the class HeadingNameNamingCriterion method getDocumentName.
@Override
public String getDocumentName(XDOM newDoc) {
String documentName = null;
String prefix = spaceName + ".";
if (newDoc.getChildren().size() > 0) {
Block firstChild = newDoc.getChildren().get(0);
if (firstChild instanceof HeaderBlock) {
// Clone the header block and remove any unwanted stuff
Block clonedHeaderBlock = firstChild.clone(new BlockFilter() {
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;
}
});
XDOM xdom = new XDOM(clonedHeaderBlock.getChildren());
WikiPrinter printer = new DefaultWikiPrinter();
this.plainSyntaxRenderer.render(xdom, printer);
documentName = cleanPageName(printer.toString());
}
}
// Fall back if necessary.
if (null == documentName || documentName.equals("")) {
documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc);
} else if (prependBasePageName) {
documentName = prefix + basePageName + INDEX_SEPERATOR + documentName;
} else {
documentName = prefix + documentName;
}
// Truncate long document names.
int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255;
if (documentName.length() > maxWidth) {
documentName = documentName.substring(0, maxWidth);
}
// Resolve any name clashes.
String newDocumentName = documentName;
int localIndex = 0;
while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) {
// Append a trailing local index if the page already exists
newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex);
}
// Add the newly generated document name into the pool of generated document names.
documentNames.add(newDocumentName);
return newDocumentName;
}
use of org.xwiki.rendering.block.SpaceBlock 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.");
}
}
Aggregations