use of org.xwiki.officeimporter.document.XDOMOfficeDocument 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.document.XDOMOfficeDocument in project xwiki-platform by xwiki.
the class DefaultXDOMOfficeDocumentBuilderTest method testXDOMOfficeDocumentBuilding.
/**
* Test {@link OfficeDocument} building.
*/
@Test
public void testXDOMOfficeDocumentBuilding() throws Exception {
// Create & register a mock document converter to by-pass the office server.
final InputStream mockOfficeFileStream = new ByteArrayInputStream(new byte[1024]);
final Map<String, InputStream> mockInput = new HashMap<String, InputStream>();
mockInput.put(INPUT_FILE_NAME, mockOfficeFileStream);
final Map<String, byte[]> mockOutput = new HashMap<String, byte[]>();
mockOutput.put(OUTPUT_FILE_NAME, "<html><head><title></tile></head><body><p><strong>Hello There</strong></p></body></html>".getBytes());
final OfficeConverter mockDocumentConverter = getMockery().mock(OfficeConverter.class);
final DocumentReference documentReference = new DocumentReference("xwiki", "Main", "Test");
getMockery().checking(new Expectations() {
{
oneOf(mockOfficeServer).getConverter();
will(returnValue(mockDocumentConverter));
allowing(mockDocumentConverter).convert(mockInput, INPUT_FILE_NAME, OUTPUT_FILE_NAME);
will(returnValue(mockOutput));
allowing(mockDocumentReferenceResolver).resolve("xwiki:Main.Test");
will(returnValue(documentReference));
allowing(mockDefaultStringEntityReferenceSerializer).serialize(documentReference);
will(returnValue("xwiki:Main.Test"));
}
});
XDOMOfficeDocument document = xdomOfficeDocumentBuilder.build(mockOfficeFileStream, INPUT_FILE_NAME, documentReference, true);
assertEquals("xwiki:Main.Test", document.getContentDocument().getMetaData().getMetaData(MetaData.BASE));
assertEquals("**Hello There**", document.getContentAsString());
assertEquals(0, document.getArtifacts().size());
}
use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.
the class DefaultPresentationBuilder method build.
@Override
public XDOMOfficeDocument build(InputStream officeFileStream, String officeFileName, DocumentReference documentReference) throws OfficeImporterException {
// Accents seems to cause issues in some conditions
// See https://jira.xwiki.org/browse/XWIKI-14692
String cleanedOfficeFileName = StringUtils.stripAccents(officeFileName);
// Invoke the office document converter.
Map<String, byte[]> artifacts = importPresentation(officeFileStream, cleanedOfficeFileName);
// Create presentation HTML.
String html = buildPresentationHTML(artifacts, StringUtils.substringBeforeLast(cleanedOfficeFileName, "."));
// Clear and adjust presentation HTML (slide image URLs are updated to point to the corresponding attachments).
html = cleanPresentationHTML(html, documentReference);
// Create the XDOM.
XDOM xdom = buildPresentationXDOM(html, documentReference);
return new XDOMOfficeDocument(xdom, artifacts, this.contextComponentManagerProvider.get());
}
use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.
the class DefaultXDOMOfficeDocumentBuilder method build.
@Override
public XDOMOfficeDocument build(XHTMLOfficeDocument xhtmlOfficeDocument) throws OfficeImporterException {
Document xhtmlDoc = xhtmlOfficeDocument.getContentDocument();
HTMLUtils.stripHTMLEnvelope(xhtmlDoc);
XDOM xdom = null;
try {
xdom = this.xHtmlParser.parse(new StringReader(HTMLUtils.toString(xhtmlDoc)));
} catch (ParseException ex) {
throw new OfficeImporterException("Error: Could not parse xhtml office content.", ex);
}
return new XDOMOfficeDocument(xdom, xhtmlOfficeDocument.getArtifacts(), this.componentManager);
}
use of org.xwiki.officeimporter.document.XDOMOfficeDocument 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