Search in sources :

Example 11 with XDOMOfficeDocument

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());
}
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 12 with XDOMOfficeDocument

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());
}
Also used : Expectations(org.jmock.Expectations) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) AbstractOfficeImporterTest(org.xwiki.officeimporter.internal.AbstractOfficeImporterTest) Test(org.junit.Test)

Example 13 with XDOMOfficeDocument

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());
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

Example 14 with XDOMOfficeDocument

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);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) ParseException(org.xwiki.rendering.parser.ParseException) XHTMLOfficeDocument(org.xwiki.officeimporter.document.XHTMLOfficeDocument) Document(org.w3c.dom.Document) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

Example 15 with XDOMOfficeDocument

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;
}
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

XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)16 DocumentReference (org.xwiki.model.reference.DocumentReference)10 Test (org.junit.Test)9 XDOM (org.xwiki.rendering.block.XDOM)9 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 File (java.io.File)3 HashMap (java.util.HashMap)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 Expectations (org.jmock.Expectations)2 Document (org.w3c.dom.Document)2 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)2 AbstractOfficeImporterTest (org.xwiki.officeimporter.internal.AbstractOfficeImporterTest)2 TargetDocumentDescriptor (org.xwiki.officeimporter.splitter.TargetDocumentDescriptor)2 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 Reader (java.io.Reader)1 Type (java.lang.reflect.Type)1