Search in sources :

Example 11 with ClassBlockMatcher

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

the class DocumentTableBlockDataSource method getTableBlock.

@Override
protected TableBlock getTableBlock(String macroContent, MacroTransformationContext context) throws MacroExecutionException {
    XDOM xdom = computeXDOM(context);
    // Find the correct table block.
    List<TableBlock> tableBlocks = xdom.getBlocks(new ClassBlockMatcher(TableBlock.class), Block.Axes.DESCENDANT);
    TableBlock result = null;
    this.logger.debug("Table id is [{}], there are [{}] tables in the document [{}]", new Object[] { this.tableId, tableBlocks.size(), this.documentReference });
    if (null != tableId) {
        for (TableBlock tableBlock : tableBlocks) {
            String id = tableBlock.getParameter("id");
            if (null != id && id.equals(this.tableId)) {
                result = tableBlock;
                break;
            }
        }
    } else {
        result = (tableBlocks.size() > 0) ? tableBlocks.get(0) : null;
    }
    if (null == result) {
        throw new MacroExecutionException("Unable to find a matching data table.");
    }
    return result;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) TableBlock(org.xwiki.rendering.block.TableBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 12 with ClassBlockMatcher

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

the class MacroContentTableBlockDataSource method getTableBlock.

@Override
protected TableBlock getTableBlock(String macroContent, MacroTransformationContext context) throws MacroExecutionException {
    // Since we are using an inline source the macro content cannot be empty/null.
    if (StringUtils.isEmpty(macroContent)) {
        throw new MacroExecutionException("A Chart Macro using an inline source must have a data table defined in " + "its content.");
    }
    // Parse the macro content into an XDOM.
    XDOM xdom = this.macroContentParser.parse(macroContent, context, true, false);
    // Take the first TableBlock found in the macro content.
    List<TableBlock> tableBlocks = xdom.getBlocks(new ClassBlockMatcher(TableBlock.class), Block.Axes.DESCENDANT);
    if (tableBlocks.size() == 0) {
        throw new MacroExecutionException("Unable to locate a suitable data table.");
    }
    return tableBlocks.get(0);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) TableBlock(org.xwiki.rendering.block.TableBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 13 with ClassBlockMatcher

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

the class DefaultDocumentSplitterTest method updateAnchors.

@Test
public void updateAnchors() throws Exception {
    SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
    when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(false, true, true);
    when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(false, false, false);
    NamingCriterion namingCriterion = mock(NamingCriterion.class);
    when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
    // [[link>>||anchor="chapter1"]]
    // = {{id name="chapter1"}}Chapter 1 =
    // = Chapter 2 ==
    // [[link>>path:#chapter1]]
    ResourceReference reference = new ResourceReference("", ResourceType.DOCUMENT);
    reference.setParameter("anchor", "chapter1");
    LinkBlock link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
    ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(link));
    HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new IdBlock("chapter1"), new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
    SectionBlock section1 = new SectionBlock(Arrays.<Block>asList(header));
    header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 2")), HeaderLevel.LEVEL1);
    reference = new ResourceReference("#chapter1", ResourceType.PATH);
    link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
    SectionBlock section2 = new SectionBlock(Arrays.<Block>asList(header, new ParagraphBlock(Arrays.<Block>asList(link))));
    XDOM xdom = new XDOM(Arrays.<Block>asList(paragraph, section1, section2));
    WikiDocument document = new WikiDocument("Space.Page", xdom, null);
    List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
    ClassBlockMatcher linkMatcher = new ClassBlockMatcher(LinkBlock.class);
    ResourceReference updatedReference = document.getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
    assertEquals("chapter1", updatedReference.getParameter("anchor"));
    assertEquals(result.get(1).getFullName(), updatedReference.getReference());
    updatedReference = result.get(2).getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
    assertEquals(ResourceType.DOCUMENT, updatedReference.getType());
    assertEquals("chapter1", updatedReference.getParameter("anchor"));
    assertEquals(result.get(1).getFullName(), updatedReference.getReference());
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) WikiDocument(org.xwiki.refactoring.WikiDocument) IdBlock(org.xwiki.rendering.block.IdBlock) NamingCriterion(org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion) LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) LinkBlock(org.xwiki.rendering.block.LinkBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) IdBlock(org.xwiki.rendering.block.IdBlock) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) SectionBlock(org.xwiki.rendering.block.SectionBlock) SplittingCriterion(org.xwiki.refactoring.splitter.criterion.SplittingCriterion) Test(org.junit.Test)

Example 14 with ClassBlockMatcher

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

the class DefaultDocumentSplitterTest method split.

@Test
public void split() throws Exception {
    SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
    when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(true, false, false, true);
    when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(true, false, false, false);
    NamingCriterion namingCriterion = mock(NamingCriterion.class);
    when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
    // = Chapter 1 =
    // Once upon a time..
    // == Section 1.1 ==
    // In a kingdom far away..
    HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Section 1.1")), HeaderLevel.LEVEL2);
    ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("In a kingdom far away..")));
    SectionBlock subSection = new SectionBlock(Arrays.<Block>asList(header, paragraph));
    header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
    paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("Once upon a time..")));
    SectionBlock section = new SectionBlock(Arrays.<Block>asList(header, paragraph, subSection));
    XDOM xdom = new XDOM(Arrays.<Block>asList(section));
    WikiDocument document = new WikiDocument("Space.Page", xdom, null);
    List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
    assertEquals(3, result.size());
    assertSame(document, result.get(0));
    assertEquals("Child1", result.get(1).getFullName());
    assertSame(document, result.get(1).getParent());
    assertEquals("Child2", result.get(2).getFullName());
    assertSame(result.get(1), result.get(2).getParent());
    ClassBlockMatcher headerMatcher = new ClassBlockMatcher(HeaderBlock.class);
    assertTrue(document.getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).isEmpty());
    assertEquals(1, result.get(1).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
    assertEquals(1, result.get(2).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) NamingCriterion(org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) LinkBlock(org.xwiki.rendering.block.LinkBlock) WordBlock(org.xwiki.rendering.block.WordBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) IdBlock(org.xwiki.rendering.block.IdBlock) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) WikiDocument(org.xwiki.refactoring.WikiDocument) SectionBlock(org.xwiki.rendering.block.SectionBlock) SplittingCriterion(org.xwiki.refactoring.splitter.criterion.SplittingCriterion) Test(org.junit.Test)

Example 15 with ClassBlockMatcher

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

the class DefaultDocumentSplitter method updateAnchors.

/**
 * @param document the document whose anchors to update
 * @param fragments see {@link #collectDocumentFragments(List)}
 */
private void updateAnchors(WikiDocument document, Map<String, String> fragments) {
    for (LinkBlock linkBlock : document.getXdom().<LinkBlock>getBlocks(new ClassBlockMatcher(LinkBlock.class), Axes.DESCENDANT)) {
        ResourceReference reference = linkBlock.getReference();
        ResourceType resoureceType = reference.getType();
        String fragment = null;
        if ((ResourceType.DOCUMENT.equals(resoureceType) || ResourceType.SPACE.equals(resoureceType)) && StringUtils.isEmpty(reference.getReference())) {
            fragment = reference.getParameter(ANCHOR_PARAMETER);
        } else if (StringUtils.startsWith(reference.getReference(), "#") && (ResourceType.PATH.equals(resoureceType) || ResourceType.URL.equals(resoureceType))) {
            fragment = reference.getReference().substring(1);
        }
        String targetDocument = fragments.get(fragment);
        if (targetDocument != null && !targetDocument.equals(document.getFullName())) {
            // The fragment has been moved so we need to update the link.
            reference.setType(ResourceType.DOCUMENT);
            reference.setReference(targetDocument);
            reference.setParameter(ANCHOR_PARAMETER, fragment);
        }
    }
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference)

Aggregations

ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)16 XDOM (org.xwiki.rendering.block.XDOM)10 Block (org.xwiki.rendering.block.Block)6 LinkBlock (org.xwiki.rendering.block.LinkBlock)5 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)5 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)4 ImageBlock (org.xwiki.rendering.block.ImageBlock)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)3 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)3 File (java.io.File)2 InputStream (java.io.InputStream)2 ToString (org.suigeneris.jrcs.util.ToString)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)2 WikiDocument (org.xwiki.refactoring.WikiDocument)2 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)2