@Test public void testDocumentNamesGeneration() throws Exception { XDOM xdom = xwikiParser.parse(new StringReader("=Heading=")); BlockRenderer plainSyntaxBlockRenderer = getComponentManager().getInstance(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString()); NamingCriterion namingCriterion = new HeadingNameNamingCriterion("Test.Test", docBridge, plainSyntaxBlockRenderer, false); Block sectionBlock = xdom.getChildren().get(0); Assert.assertEquals("Test.Heading", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren()))); Assert.assertEquals("Test.Heading-1", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren()))); xdom = xwikiParser.parse(new StringReader("= This-Very.Weird:Heading! =")); sectionBlock = xdom.getChildren().get(0); Assert.assertEquals("Test.This-Very-Weird-Heading!", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren()))); xdom = xwikiParser.parse(new StringReader("= This?Is@A/Very#Weird~Heading =")); sectionBlock = xdom.getChildren().get(0); Assert.assertEquals("Test.ThisIsAVeryWeirdHeading", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren()))); xdom = xwikiParser.parse(new StringReader("=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=")); sectionBlock = xdom.getChildren().get(0); Assert.assertEquals(255, namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren())).length()); Assert.assertEquals("Test.Test-1", namingCriterion.getDocumentName(xdom)); xdom = xwikiParser.parse(new StringReader("= =")); sectionBlock = xdom.getChildren().get(0); Assert.assertEquals("Test.Test-2", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren()))); }
@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) { 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()); } } if (null == documentName || documentName.equals("")) { documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc); } else if (prependBasePageName) { documentName = prefix + basePageName + INDEX_SEPERATOR + documentName; } else { documentName = prefix + documentName; } int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255; if (documentName.length() > maxWidth) { documentName = documentName.substring(0, maxWidth); } String newDocumentName = documentName; int localIndex = 0; while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) { newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex); } documentNames.add(newDocumentName); return newDocumentName; }
HeadingNameNamingCriterion implements NamingCriterion { @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) { 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()); } } if (null == documentName || documentName.equals("")) { documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc); } else if (prependBasePageName) { documentName = prefix + basePageName + INDEX_SEPERATOR + documentName; } else { documentName = prefix + documentName; } int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255; if (documentName.length() > maxWidth) { documentName = documentName.substring(0, maxWidth); } String newDocumentName = documentName; int localIndex = 0; while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) { newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex); } documentNames.add(newDocumentName); return newDocumentName; } }
HeadingNameNamingCriterion implements NamingCriterion { @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) { 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()); } } if (null == documentName || documentName.equals("")) { documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc); } else if (prependBasePageName) { documentName = prefix + basePageName + INDEX_SEPERATOR + documentName; } else { documentName = prefix + documentName; } int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255; if (documentName.length() > maxWidth) { documentName = documentName.substring(0, maxWidth); } String newDocumentName = documentName; int localIndex = 0; while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) { newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex); } documentNames.add(newDocumentName); return newDocumentName; } HeadingNameNamingCriterion(String baseDocumentName, DocumentAccessBridge docBridge,
        BlockRenderer plainSyntaxRenderer, boolean prependBasePageName); }
HeadingNameNamingCriterion implements NamingCriterion { @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) { 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()); } } if (null == documentName || documentName.equals("")) { documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc); } else if (prependBasePageName) { documentName = prefix + basePageName + INDEX_SEPERATOR + documentName; } else { documentName = prefix + documentName; } int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255; if (documentName.length() > maxWidth) { documentName = documentName.substring(0, maxWidth); } String newDocumentName = documentName; int localIndex = 0; while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) { newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex); } documentNames.add(newDocumentName); return newDocumentName; } HeadingNameNamingCriterion(String baseDocumentName, DocumentAccessBridge docBridge,
        BlockRenderer plainSyntaxRenderer, boolean prependBasePageName); @Override String getDocumentName(XDOM newDoc); }
HeadingNameNamingCriterion implements NamingCriterion { @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) { 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()); } } if (null == documentName || documentName.equals("")) { documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc); } else if (prependBasePageName) { documentName = prefix + basePageName + INDEX_SEPERATOR + documentName; } else { documentName = prefix + documentName; } int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255; if (documentName.length() > maxWidth) { documentName = documentName.substring(0, maxWidth); } String newDocumentName = documentName; int localIndex = 0; while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) { newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex); } documentNames.add(newDocumentName); return newDocumentName; } HeadingNameNamingCriterion(String baseDocumentName, DocumentAccessBridge docBridge,
        BlockRenderer plainSyntaxRenderer, boolean prependBasePageName); @Override String getDocumentName(XDOM newDoc);  }