Search in sources :

Example 1 with SpecialSymbolBlock

use of org.xwiki.rendering.block.SpecialSymbolBlock 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;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) ArrayList(java.util.ArrayList) List(java.util.List) BlockFilter(org.xwiki.rendering.block.BlockFilter)

Example 2 with SpecialSymbolBlock

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

the class MessageToolTranslationMessageParserTest method messageWithApostrophe.

@Test
public void messageWithApostrophe() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("'");
    Assert.assertEquals("'", translationMessage.getRawSource());
    Assert.assertEquals(new SpecialSymbolBlock('\''), translationMessage.render(null, null));
}
Also used : SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 3 with SpecialSymbolBlock

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

the class MessageToolTranslationMessageParserTest method messageWithEscapedParameter.

@Test
public void messageWithEscapedParameter() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("'{0}");
    Assert.assertEquals("'{0}", translationMessage.getRawSource());
    Assert.assertEquals(new CompositeBlock(Arrays.<Block>asList(new SpecialSymbolBlock('{'), new WordBlock("0"), new SpecialSymbolBlock('}'))), translationMessage.render(null, null, "word"));
}
Also used : SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 4 with SpecialSymbolBlock

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

the class MessageToolTranslationMessageParserTest method messageWithExpectedParameter.

@Test
public void messageWithExpectedParameter() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("{0}");
    Assert.assertEquals("{0}", translationMessage.getRawSource());
    Assert.assertEquals(new CompositeBlock(Arrays.<Block>asList(new SpecialSymbolBlock('{'), new WordBlock("0"), new SpecialSymbolBlock('}'))), translationMessage.render(null, null));
}
Also used : SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 5 with SpecialSymbolBlock

use of org.xwiki.rendering.block.SpecialSymbolBlock 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

SpecialSymbolBlock (org.xwiki.rendering.block.SpecialSymbolBlock)5 Block (org.xwiki.rendering.block.Block)4 WordBlock (org.xwiki.rendering.block.WordBlock)4 Test (org.junit.Test)3 TranslationMessage (org.xwiki.localization.message.TranslationMessage)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BlockFilter (org.xwiki.rendering.block.BlockFilter)2 CompositeBlock (org.xwiki.rendering.block.CompositeBlock)2 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)2 SpaceBlock (org.xwiki.rendering.block.SpaceBlock)2 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 XDOM (org.xwiki.rendering.block.XDOM)1 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)1 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)1 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)1