Search in sources :

Example 1 with TargetDocumentDescriptor

use of org.xwiki.officeimporter.splitter.TargetDocumentDescriptor in project xwiki-platform by xwiki.

the class DefaultXDOMOfficeDocumentSplitterTest method testDocumentSplitting.

/**
 * Test basic document splitting.
 *
 * @throws Exception if it fails to parse the wiki syntax or if it fails to split the document
 */
@Test
public void testDocumentSplitting() throws Exception {
    // Create xwiki/2.0 document.
    StringBuffer buffer = new StringBuffer();
    buffer.append("=Heading1=").append('\n');
    buffer.append("Content").append('\n');
    buffer.append("==Heading11==").append('\n');
    buffer.append("Content").append('\n');
    buffer.append("==Heading12==").append('\n');
    buffer.append("Content").append('\n');
    buffer.append("=Heading2=").append('\n');
    buffer.append("Content").append('\n');
    XDOM xdom = xwikiSyntaxParser.parse(new StringReader(buffer.toString()));
    // Create xdom office document.
    XDOMOfficeDocument officeDocument = new XDOMOfficeDocument(xdom, new HashMap<String, byte[]>(), getComponentManager());
    final DocumentReference baseDocument = new DocumentReference("xwiki", "Test", "Test");
    // Add expectations to mock document name serializer.
    getMockery().checking(new Expectations() {

        {
            allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(baseDocument);
            will(returnValue("Test.Test"));
            allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(new DocumentReference("xwiki", "Test", "Heading1"));
            will(returnValue("Test.Heading1"));
            allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(new DocumentReference("xwiki", "Test", "Heading11"));
            will(returnValue("Test.Heading11"));
            allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(new DocumentReference("xwiki", "Test", "Heading12"));
            will(returnValue("Test.Heading12"));
            allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(new DocumentReference("xwiki", "Test", "Heading2"));
            will(returnValue("Test.Heading2"));
        }
    });
    // Add expectations to mock document name factory.
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("Test.Test");
            will(returnValue(new DocumentReference("xwiki", "Test", "Test")));
            allowing(mockDocumentReferenceResolver).resolve("Test.Heading1");
            will(returnValue(new DocumentReference("xwiki", "Test", "Heading1")));
            allowing(mockDocumentReferenceResolver).resolve("Test.Heading11");
            will(returnValue(new DocumentReference("xwiki", "Test", "Heading11")));
            allowing(mockDocumentReferenceResolver).resolve("Test.Heading12");
            will(returnValue(new DocumentReference("xwiki", "Test", "Heading12")));
            allowing(mockDocumentReferenceResolver).resolve("Test.Heading2");
            will(returnValue(new DocumentReference("xwiki", "Test", "Heading2")));
        }
    });
    // Add expectations to mock document access bridge.
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).exists("Test.Heading1");
            will(returnValue(false));
            allowing(mockDocumentAccessBridge).exists("Test.Heading11");
            will(returnValue(false));
            allowing(mockDocumentAccessBridge).exists("Test.Heading12");
            will(returnValue(false));
            allowing(mockDocumentAccessBridge).exists("Test.Heading2");
            will(returnValue(false));
        }
    });
    // Perform the split operation.
    Map<TargetDocumentDescriptor, XDOMOfficeDocument> result = officeDocumentSplitter.split(officeDocument, new int[] { 1, 2, 3, 4, 5, 6 }, "headingNames", baseDocument);
    // There should be five XDOM office documents.
    Assert.assertEquals(5, result.size());
}
Also used : Expectations(org.jmock.Expectations) XDOM(org.xwiki.rendering.block.XDOM) TargetDocumentDescriptor(org.xwiki.officeimporter.splitter.TargetDocumentDescriptor) StringReader(java.io.StringReader) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) AbstractOfficeImporterTest(org.xwiki.officeimporter.internal.AbstractOfficeImporterTest) Test(org.junit.Test)

Example 2 with TargetDocumentDescriptor

use of org.xwiki.officeimporter.splitter.TargetDocumentDescriptor 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)

Aggregations

DocumentReference (org.xwiki.model.reference.DocumentReference)2 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)2 TargetDocumentDescriptor (org.xwiki.officeimporter.splitter.TargetDocumentDescriptor)2 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Expectations (org.jmock.Expectations)1 Test (org.junit.Test)1 AbstractOfficeImporterTest (org.xwiki.officeimporter.internal.AbstractOfficeImporterTest)1 WikiDocument (org.xwiki.refactoring.WikiDocument)1 HeadingLevelSplittingCriterion (org.xwiki.refactoring.splitter.criterion.HeadingLevelSplittingCriterion)1 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)1 NamingCriterion (org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion)1 XDOM (org.xwiki.rendering.block.XDOM)1