Search in sources :

Example 1 with WikiDocument

use of org.xwiki.refactoring.WikiDocument in project xwiki-platform by xwiki.

the class DefaultDocumentSplitter method split.

/**
 * A recursive method for traversing the xdom of the root document and splitting it into sub documents.
 *
 * @param parentDoc the parent {@link WikiDocument} under which the given list of children reside.
 * @param children current list of blocks being traversed.
 * @param depth the depth from the root xdom to current list of children.
 * @param result space for storing the resulting documents.
 * @param splittingCriterion the {@link SplittingCriterion}.
 * @param namingCriterion the {@link NamingCriterion}.
 */
private void split(WikiDocument parentDoc, List<Block> children, int depth, List<WikiDocument> result, SplittingCriterion splittingCriterion, NamingCriterion namingCriterion) {
    ListIterator<Block> it = children.listIterator();
    while (it.hasNext()) {
        Block block = it.next();
        if (splittingCriterion.shouldSplit(block, depth)) {
            // Split a new document and add it to the results list.
            XDOM xdom = new XDOM(block.getChildren());
            String newDocumentName = namingCriterion.getDocumentName(xdom);
            WikiDocument newDoc = new WikiDocument(newDocumentName, xdom, parentDoc);
            result.add(newDoc);
            // Remove the original block from the parent document.
            it.remove();
            // Place a link from the parent to child.
            it.add(new NewLineBlock());
            it.add(createLink(block, newDocumentName));
            // Check whether this node should be further traversed.
            if (splittingCriterion.shouldIterate(block, depth)) {
                split(newDoc, newDoc.getXdom().getChildren(), depth + 1, result, splittingCriterion, namingCriterion);
            }
        } else if (splittingCriterion.shouldIterate(block, depth)) {
            split(parentDoc, block.getChildren(), depth + 1, result, splittingCriterion, namingCriterion);
        }
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) 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) WikiDocument(org.xwiki.refactoring.WikiDocument) NewLineBlock(org.xwiki.rendering.block.NewLineBlock)

Example 2 with WikiDocument

use of org.xwiki.refactoring.WikiDocument in project xwiki-platform by xwiki.

the class DefaultXDOMOfficeDocumentSplitter method split.

@Override
public Map<TargetDocumentDescriptor, XDOMOfficeDocument> split(XDOMOfficeDocument officeDocument, int[] headingLevelsToSplit, String namingCriterionHint, DocumentReference baseDocumentReference) throws OfficeImporterException {
    // TODO: This code needs to be refactored along with the xwiki-refactoring module code.
    String strBaseDoc = this.entityReferenceSerializer.serialize(baseDocumentReference);
    Map<TargetDocumentDescriptor, XDOMOfficeDocument> result = new HashMap<TargetDocumentDescriptor, XDOMOfficeDocument>();
    // Create splitting and naming criterion for refactoring.
    SplittingCriterion splittingCriterion = new HeadingLevelSplittingCriterion(headingLevelsToSplit);
    NamingCriterion namingCriterion = DocumentSplitterUtils.getNamingCriterion(namingCriterionHint, strBaseDoc, this.docBridge, this.plainTextRenderer);
    // Create the root document required by refactoring module.
    WikiDocument rootDoc = new WikiDocument(strBaseDoc, officeDocument.getContentDocument(), null);
    List<WikiDocument> documents = this.documentSplitter.split(rootDoc, splittingCriterion, namingCriterion);
    for (WikiDocument doc : documents) {
        // Initialize a target page descriptor.
        DocumentReference targetReference = this.currentMixedDocumentReferenceResolver.resolve(doc.getFullName());
        TargetDocumentDescriptor targetDocumentDescriptor = new TargetDocumentDescriptor(targetReference, this.componentManager);
        if (doc.getParent() != null) {
            DocumentReference targetParent = this.currentMixedDocumentReferenceResolver.resolve(doc.getParent().getFullName());
            targetDocumentDescriptor.setParentReference(targetParent);
        }
        // Rewire artifacts.
        Map<String, byte[]> artifacts = DocumentSplitterUtils.relocateArtifacts(doc, officeDocument);
        // Create the resulting XDOMOfficeDocument.
        XDOMOfficeDocument splitDocument = new XDOMOfficeDocument(doc.getXdom(), artifacts, this.componentManager);
        result.put(targetDocumentDescriptor, splitDocument);
    }
    return result;
}
Also used : TargetDocumentDescriptor(org.xwiki.officeimporter.splitter.TargetDocumentDescriptor) HashMap(java.util.HashMap) NamingCriterion(org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion) WikiDocument(org.xwiki.refactoring.WikiDocument) HeadingLevelSplittingCriterion(org.xwiki.refactoring.splitter.criterion.HeadingLevelSplittingCriterion) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) HeadingLevelSplittingCriterion(org.xwiki.refactoring.splitter.criterion.HeadingLevelSplittingCriterion) SplittingCriterion(org.xwiki.refactoring.splitter.criterion.SplittingCriterion)

Example 3 with WikiDocument

use of org.xwiki.refactoring.WikiDocument 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 4 with WikiDocument

use of org.xwiki.refactoring.WikiDocument 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)

Aggregations

WikiDocument (org.xwiki.refactoring.WikiDocument)4 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)3 NamingCriterion (org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion)3 Block (org.xwiki.rendering.block.Block)3 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)3 IdBlock (org.xwiki.rendering.block.IdBlock)3 LinkBlock (org.xwiki.rendering.block.LinkBlock)3 SectionBlock (org.xwiki.rendering.block.SectionBlock)3 WordBlock (org.xwiki.rendering.block.WordBlock)3 XDOM (org.xwiki.rendering.block.XDOM)3 Test (org.junit.Test)2 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 HashMap (java.util.HashMap)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)1 TargetDocumentDescriptor (org.xwiki.officeimporter.splitter.TargetDocumentDescriptor)1 HeadingLevelSplittingCriterion (org.xwiki.refactoring.splitter.criterion.HeadingLevelSplittingCriterion)1 NewLineBlock (org.xwiki.rendering.block.NewLineBlock)1 SpaceBlock (org.xwiki.rendering.block.SpaceBlock)1