Search in sources :

Example 1 with WordBlock

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

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

the class HelloWorldUIExtension method execute.

@Override
public Block execute() {
    List<Block> blocks = new ArrayList<Block>();
    blocks.add(new WordBlock("HelloWorld"));
    return new CompositeBlock(blocks);
}
Also used : WordBlock(org.xwiki.rendering.block.WordBlock) ArrayList(java.util.ArrayList) Block(org.xwiki.rendering.block.Block) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) WordBlock(org.xwiki.rendering.block.WordBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock)

Example 3 with WordBlock

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

the class DefaultGadgetSource method getDashboardSourceMetadata.

@Override
public List<Block> getDashboardSourceMetadata(String source, MacroTransformationContext context) {
    DocumentReference sourceDoc = getSourceDocumentReference(source);
    String classParameterName = "class";
    GroupBlock metadataContainer = new GroupBlock();
    metadataContainer.setParameter(classParameterName, DashboardMacro.METADATA);
    // generate anchors for the urls
    XWikiContext xContext = getXWikiContext();
    String editURL = xContext.getWiki().getURL(sourceDoc, "save", "", "", xContext);
    LinkBlock editURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(editURL, ResourceType.URL), false);
    editURLBlock.setParameter(classParameterName, DashboardMacro.EDIT_URL);
    metadataContainer.addChild(editURLBlock);
    String removeURL = xContext.getWiki().getURL(sourceDoc, "objectremove", "", "", xContext);
    LinkBlock removeURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(removeURL, ResourceType.URL), false);
    removeURLBlock.setParameter(classParameterName, DashboardMacro.REMOVE_URL);
    metadataContainer.addChild(removeURLBlock);
    String addURL = xContext.getWiki().getURL(sourceDoc, "objectadd", "", "", xContext);
    LinkBlock addURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(addURL, ResourceType.URL), false);
    addURLBlock.setParameter(classParameterName, DashboardMacro.ADD_URL);
    metadataContainer.addChild(addURLBlock);
    // and create divs for the source metadata
    GroupBlock sourcePageBlock = new GroupBlock();
    sourcePageBlock.addChild(new WordBlock(sourceDoc.getName()));
    sourcePageBlock.setParameter(classParameterName, DashboardMacro.SOURCE_PAGE);
    metadataContainer.addChild(sourcePageBlock);
    GroupBlock sourceSpaceBlock = new GroupBlock();
    // Extract the full Space Reference (in order to support Nested Spaces) and set it in the XDOM
    sourceSpaceBlock.addChild(new WordBlock(this.localReferenceSerializer.serialize(sourceDoc.getLastSpaceReference())));
    sourceSpaceBlock.setParameter(classParameterName, DashboardMacro.SOURCE_SPACE);
    metadataContainer.addChild(sourceSpaceBlock);
    GroupBlock sourceWikiBlock = new GroupBlock();
    sourceWikiBlock.addChild(new WordBlock(sourceDoc.getWikiReference().getName()));
    sourceWikiBlock.setParameter(classParameterName, DashboardMacro.SOURCE_WIKI);
    metadataContainer.addChild(sourceWikiBlock);
    String sourceURL = xContext.getWiki().getURL(sourceDoc, "view", "", "", xContext);
    LinkBlock sourceURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(sourceURL, ResourceType.URL), false);
    sourceURLBlock.setParameter(classParameterName, DashboardMacro.SOURCE_URL);
    metadataContainer.addChild(sourceURLBlock);
    return Collections.<Block>singletonList(metadataContainer);
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) WordBlock(org.xwiki.rendering.block.WordBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) XWikiContext(com.xpn.xwiki.XWikiContext) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) WordBlock(org.xwiki.rendering.block.WordBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with WordBlock

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

the class DocumentTitleDisplayerTest method fallbackOnSpaceNameWhenSpaceHomePageTitleIsEmpty.

@Test
public void fallbackOnSpaceNameWhenSpaceHomePageTitleIsEmpty() throws Exception {
    EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
    when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(new EntityReference("Page", EntityType.DOCUMENT));
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    when(document.getDocumentReference()).thenReturn(new DocumentReference("wiki", Arrays.asList("Space"), "Page"));
    XDOM titleXDOM = new XDOM(Arrays.asList(new WordBlock("Space")));
    Parser plainTextParser = this.mocker.getInstance(Parser.class, "plain/1.0");
    when(plainTextParser.parse(any(StringReader.class))).thenReturn(titleXDOM);
    DocumentDisplayerParameters params = new DocumentDisplayerParameters();
    params.setTitleDisplayed(true);
    assertSame(titleXDOM, this.mocker.getComponentUnderTest().display(document, params));
    ArgumentCaptor<Reader> argument = ArgumentCaptor.forClass(Reader.class);
    verify(plainTextParser).parse(argument.capture());
    assertEquals("Space", IOUtils.toString(argument.getValue()));
}
Also used : EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) WordBlock(org.xwiki.rendering.block.WordBlock) EntityReference(org.xwiki.model.reference.EntityReference) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) DocumentReference(org.xwiki.model.reference.DocumentReference) Parser(org.xwiki.rendering.parser.Parser) Test(org.junit.Test)

Example 5 with WordBlock

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

the class MessageToolTranslationMessageParserTest method messageWithChoiceSyntax.

@Test
public void messageWithChoiceSyntax() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("{0,choice,0#choice1|0<choice2}");
    Assert.assertEquals("{0,choice,0#choice1|0<choice2}", translationMessage.getRawSource());
    Assert.assertEquals(new WordBlock("choice1"), translationMessage.render(null, null, 0));
    Assert.assertEquals(new WordBlock("choice2"), translationMessage.render(null, null, 42));
}
Also used : WordBlock(org.xwiki.rendering.block.WordBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Aggregations

WordBlock (org.xwiki.rendering.block.WordBlock)24 Test (org.junit.Test)13 Block (org.xwiki.rendering.block.Block)13 XDOM (org.xwiki.rendering.block.XDOM)8 DocumentReference (org.xwiki.model.reference.DocumentReference)6 TranslationMessage (org.xwiki.localization.message.TranslationMessage)5 ArrayList (java.util.ArrayList)4 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)4 LinkBlock (org.xwiki.rendering.block.LinkBlock)4 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)4 SpecialSymbolBlock (org.xwiki.rendering.block.SpecialSymbolBlock)4 EntityReference (org.xwiki.model.reference.EntityReference)3 IdBlock (org.xwiki.rendering.block.IdBlock)3 SectionBlock (org.xwiki.rendering.block.SectionBlock)3 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 List (java.util.List)2 Expectations (org.jmock.Expectations)2 Mockery (org.jmock.Mockery)2