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());
}
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;
}
Aggregations