Example usage for org.apache.poi.xwpf.usermodel XWPFDocument XWPFDocument

List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument XWPFDocument

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFDocument XWPFDocument.

Prototype

public XWPFDocument(InputStream is) throws IOException 

Source Link

Usage

From source file:org.obeonetwork.m2doc.generator.test.TemplateProcessorTest.java

License:Open Source License

/**
 * Tests a gd:if with <code>false</code> expression evaluation and with an
 * else./*from   w ww. j  av  a 2 s. c  om*/
 * 
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 */
@Test
public void testEmptyParagraphsProcessing()
        throws InvalidFormatException, IOException, DocumentParserException {
    FileInputStream is = new FileInputStream("templates/testEmptyParagraphs.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    Map<String, Object> definitions = new HashMap<String, Object>();
    definitions.put("author", "Moi");
    XWPFDocument destinationDoc = createDestinationDocument("templates/testEmptyParagraphs.docx");
    TemplateProcessor processor = new TemplateProcessor(definitions, "", env, destinationDoc);
    processor.doSwitch(template);
    assertEquals(5, destinationDoc.getParagraphs().size());
    assertEquals("Paragraph1 Moi", destinationDoc.getParagraphs().get(0).getText());
    assertEquals("", destinationDoc.getParagraphs().get(1).getText());
    assertEquals("Paragraph2", destinationDoc.getParagraphs().get(2).getText());
    assertEquals("", destinationDoc.getParagraphs().get(3).getText());
    assertEquals("Paragraph3 Moi", destinationDoc.getParagraphs().get(4).getText());
}

From source file:org.obeonetwork.m2doc.generator.test.TemplateProcessorUserDocRemplaceTest.java

License:Open Source License

/**
 * Load doc from path./*from w  w  w.  java  2 s .co  m*/
 * 
 * @param docPath
 *            resultPath
 * @return document
 * @throws FileNotFoundException
 *             FileNotFoundException
 * @throws InvalidFormatException
 *             InvalidFormatException
 * @throws IOException
 *             IOException
 */
private XWPFDocument loadDoc(String docPath) throws FileNotFoundException, InvalidFormatException, IOException {
    FileInputStream is = new FileInputStream(docPath);
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    return document;
}

From source file:org.obeonetwork.m2doc.generator.test.TemplateRuntimeErrorTests.java

License:Open Source License

/**
 * Tests processing of a variable reference where the variable is unknown.
 * // w  w w.j a  va2 s. com
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 */
@Test
public void testUnknownVarRefProcessing() throws InvalidFormatException, IOException, DocumentParserException {
    FileInputStream is = new FileInputStream("templates/testVar.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    Map<String, Object> definitions = new HashMap<String, Object>();
    XWPFDocument destinationDoc = createDestinationDocument("templates/testVar.docx");
    TemplateProcessor processor = new TemplateProcessor(definitions, "", env, destinationDoc);
    processor.doSwitch(template);
    // scan the destination document
    assertEquals(2, destinationDoc.getParagraphs().size());
    System.out.println(destinationDoc.getParagraphs().get(0).getText());
    assertEquals(
            "Template de test pour les balises de rfrence  une variable\u00a0: Couldn't find the x variable",
            destinationDoc.getParagraphs().get(0).getText());
    XWPFRun run = destinationDoc.getParagraphs().get(0).getRuns()
            .get(destinationDoc.getParagraphs().get(0).getRuns().size() - 1);
    assertEquals("FF0000", run.getColor());
    assertNotNull(run.getCTR().getRPr().getB());

    assertEquals("Fin du gabarit", destinationDoc.getParagraphs().get(1).getText());
}

From source file:org.obeonetwork.m2doc.generator.test.TemplateRuntimeErrorTests.java

License:Open Source License

/**
 * Tests processing of a query where the evaluation results in an error.
 * //  w ww .j  av a 2s  .  com
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 */
@Test
public void testQueryEvaluationErrorProcessing()
        throws InvalidFormatException, IOException, DocumentParserException {
    FileInputStream is = new FileInputStream("templates/testAQL.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    Map<String, Object> definitions = new HashMap<String, Object>();
    XWPFDocument destinationDoc = createDestinationDocument("templates/testAQL.docx");
    TemplateProcessor processor = new TemplateProcessor(definitions, "", env, destinationDoc);
    processor.doSwitch(template);
    // scan the destination document
    assertEquals(4, destinationDoc.getParagraphs().size());
    System.out.println(destinationDoc.getParagraphs().get(0).getText());
    assertEquals("Template de test pour les balises de query aql\u00a0: Couldn't find the self variable",
            destinationDoc.getParagraphs().get(0).getText());
    assertEquals(
            "Attempt to access feature (name) on a non ModelObject value (org.eclipse.acceleo.query.runtime.impl.Nothing).",
            destinationDoc.getParagraphs().get(1).getText());
    XWPFRun run = destinationDoc.getParagraphs().get(0).getRuns()
            .get(destinationDoc.getParagraphs().get(0).getRuns().size() - 1);
    assertEquals("FF0000", run.getColor());
    assertNotNull(run.getCTR().getRPr().getB());
    assertEquals("Fin du gabarit", destinationDoc.getParagraphs().get(2).getText());
    assertEquals("", destinationDoc.getParagraphs().get(3).getText());
}

From source file:org.obeonetwork.m2doc.generator.test.TemplateValidationGeneratorTest.java

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an info.
 * /* w w w . j av  a 2 s.  com*/
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testInfoGeneration()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    IQueryEnvironment queryEnvironment = org.eclipse.acceleo.query.runtime.Query
            .newEnvironmentWithDefaultServices(null);
    final File tempFile = File.createTempFile("testParsingErrorSimpleTag", ".docx");

    try (DocumentTemplate template = M2DocUtils
            .parse(URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), queryEnvironment)) {
        final XWPFRun location = ((XWPFParagraph) template.getDocument().getBodyElements().get(0)).getRuns()
                .get(0);
        template.getBody().getValidationMessages().add(new TemplateValidationMessage(
                ValidationMessageLevel.INFO, "XXXXXXXXXXXXXXXXXXXXXXXX", location));
        M2DocUtils.serializeValidatedDocumentTemplate(template, URI.createFileURI(tempFile.getAbsolutePath()));
    }
    assertTrue(new File(tempFile.getAbsolutePath()).exists());

    try (FileInputStream resIs = new FileInputStream(tempFile.getAbsolutePath());
            OPCPackage resOPackage = OPCPackage.open(resIs);
            XWPFDocument resDocument = new XWPFDocument(resOPackage);) {

        final XWPFRun messageRun = M2DocTestUtils.getRunContaining(resDocument, "XXXXXXXXXXXXXXXXXXXXXXXX");

        assertNotNull(messageRun);
        assertEquals("XXXXXXXXXXXXXXXXXXXXXXXX", messageRun.text());
        assertEquals("0000FF", messageRun.getColor());
    }

    tempFile.delete();
}

From source file:org.obeonetwork.m2doc.generator.test.TemplateValidationGeneratorTest.java

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an warning.
 * //  w  w w .  ja  v a2  s . co  m
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testWarningGeneration()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    IQueryEnvironment queryEnvironment = org.eclipse.acceleo.query.runtime.Query
            .newEnvironmentWithDefaultServices(null);
    final File tempFile = File.createTempFile("testParsingErrorSimpleTag", ".docx");

    try (DocumentTemplate template = M2DocUtils
            .parse(URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), queryEnvironment)) {
        final XWPFRun location = ((XWPFParagraph) template.getDocument().getBodyElements().get(0)).getRuns()
                .get(0);
        template.getBody().getValidationMessages().add(new TemplateValidationMessage(
                ValidationMessageLevel.WARNING, "XXXXXXXXXXXXXXXXXXXXXXXX", location));
        M2DocUtils.serializeValidatedDocumentTemplate(template, URI.createFileURI(tempFile.getAbsolutePath()));
    }
    assertTrue(new File(tempFile.getAbsolutePath()).exists());

    try (FileInputStream resIs = new FileInputStream(tempFile.getAbsolutePath());
            OPCPackage resOPackage = OPCPackage.open(resIs);
            XWPFDocument resDocument = new XWPFDocument(resOPackage);) {

        final XWPFRun messageRun = M2DocTestUtils.getRunContaining(resDocument, "XXXXXXXXXXXXXXXXXXXXXXXX");

        assertNotNull(messageRun);
        assertEquals("XXXXXXXXXXXXXXXXXXXXXXXX", messageRun.text());
        assertEquals("FFA500", messageRun.getColor());
    }

    tempFile.delete();
}

From source file:org.obeonetwork.m2doc.generator.test.TemplateValidationGeneratorTest.java

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an error.
 * /*from  w w  w.j  a v  a  2  s  .  com*/
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testErrorGeneration()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    IQueryEnvironment queryEnvironment = org.eclipse.acceleo.query.runtime.Query
            .newEnvironmentWithDefaultServices(null);
    final File tempFile = File.createTempFile("testParsingErrorSimpleTag", ".docx");

    try (DocumentTemplate template = M2DocUtils
            .parse(URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), queryEnvironment)) {
        final XWPFRun location = ((XWPFParagraph) template.getDocument().getBodyElements().get(0)).getRuns()
                .get(0);
        template.getBody().getValidationMessages().add(new TemplateValidationMessage(
                ValidationMessageLevel.ERROR, "XXXXXXXXXXXXXXXXXXXXXXXX", location));
        M2DocUtils.serializeValidatedDocumentTemplate(template, URI.createFileURI(tempFile.getAbsolutePath()));
    }
    assertTrue(new File(tempFile.getAbsolutePath()).exists());

    try (FileInputStream resIs = new FileInputStream(tempFile.getAbsolutePath());
            OPCPackage resOPackage = OPCPackage.open(resIs);
            XWPFDocument resDocument = new XWPFDocument(resOPackage);) {

        final XWPFRun messageRun = M2DocTestUtils.getRunContaining(resDocument, "XXXXXXXXXXXXXXXXXXXXXXXX");

        assertNotNull(messageRun);
        assertEquals("XXXXXXXXXXXXXXXXXXXXXXXX", messageRun.text());
        assertEquals("FF0000", messageRun.getColor());
    }

    tempFile.delete();
}

From source file:org.obeonetwork.m2doc.generator.test.TemplateValidationGeneratorTest.java

License:Open Source License

/**
 * Ensure that the validation generation produces a document with errors in the good order.
 * /*from  w  ww  .  ja  v a 2s.c  o  m*/
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testErrorGenerationOrder()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    IQueryEnvironment queryEnvironment = org.eclipse.acceleo.query.runtime.Query
            .newEnvironmentWithDefaultServices(null);
    final File tempFile = File.createTempFile("testParsingErrorSimpleTag", ".docx");

    try (DocumentTemplate template = M2DocUtils
            .parse(URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), queryEnvironment)) {
        final XWPFRun location = ((XWPFParagraph) template.getDocument().getBodyElements().get(0)).getRuns()
                .get(0);
        template.getBody().getValidationMessages()
                .add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, "AAAA", location));
        template.getBody().getValidationMessages()
                .add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, "BBBB", location));
        template.getBody().getValidationMessages()
                .add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, "CCCC", location));
        template.getBody().getValidationMessages()
                .add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, "DDDD", location));
        M2DocUtils.serializeValidatedDocumentTemplate(template, URI.createFileURI(tempFile.getAbsolutePath()));
    }
    assertTrue(new File(tempFile.getAbsolutePath()).exists());

    try (FileInputStream resIs = new FileInputStream(tempFile.getAbsolutePath());
            OPCPackage resOPackage = OPCPackage.open(resIs);
            XWPFDocument resDocument = new XWPFDocument(resOPackage);) {

        final XWPFRun messageARun = M2DocTestUtils.getRunContaining(resDocument, "AAAA");
        final XWPFRun messageBRun = M2DocTestUtils.getRunContaining(resDocument, "BBBB");
        final XWPFRun messageCRun = M2DocTestUtils.getRunContaining(resDocument, "CCCC");
        final XWPFRun messageDRun = M2DocTestUtils.getRunContaining(resDocument, "DDDD");

        assertNotNull(messageARun);
        assertEquals("AAAA", messageARun.text());
        assertEquals("FF0000", messageARun.getColor());

        assertNotNull(messageBRun);
        assertEquals("BBBB", messageBRun.text());
        assertEquals("FF0000", messageBRun.getColor());

        assertNotNull(messageCRun);
        assertEquals("CCCC", messageCRun.text());
        assertEquals("FF0000", messageCRun.getColor());

        assertNotNull(messageDRun);
        assertEquals("DDDD", messageDRun.text());
        assertEquals("FF0000", messageDRun.getColor());

        final int indexA = ((XWPFParagraph) messageARun.getParent()).getRuns().indexOf(messageARun);
        final int indexB = ((XWPFParagraph) messageBRun.getParent()).getRuns().indexOf(messageBRun);
        final int indexC = ((XWPFParagraph) messageCRun.getParent()).getRuns().indexOf(messageCRun);
        final int indexD = ((XWPFParagraph) messageDRun.getParent()).getRuns().indexOf(messageDRun);

        assertTrue(indexA < indexB);
        assertTrue(indexB < indexC);
        assertTrue(indexC < indexD);
    }

    tempFile.delete();
}

From source file:org.obeonetwork.m2doc.generator.test.VariousTest.java

License:Open Source License

@Test
public void testStaticFragmentWithFieldProcessing()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/test2.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    // insert some static content.
    document.createParagraph().createRun().setText("static part that will not contain any link");
    CTBookmark bookmark = document.getDocument().getBody().addNewBookmarkStart();
    bookmark.setName("bookmark1");
    bookmark.setId(new BigInteger("66"));
    document.createParagraph().createRun().setText("bookmarked part");
    CTMarkupRange range = document.getDocument().getBody().addNewBookmarkEnd();
    range.setId(new BigInteger("66"));
    document.createParagraph().createRun().setText("another static part that will not contain any link");
    // save the document in another file
    FileOutputStream fos = new FileOutputStream("results/bookmarkTest.docx");
    document.write(fos);/* w  w  w  .jav a2s.c o  m*/
    document.close();
    fos.close();
}

From source file:org.obeonetwork.m2doc.generator.test.VariousTest.java

License:Open Source License

@Test
public void testPropertiesAccess()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/propertiesTest.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    CustomProperties props = document.getProperties().getCustomProperties();
    List<CTProperty> properties = props.getUnderlyingProperties().getPropertyList();
    for (CTProperty property : properties) {
        // TODO Finish this.
    }//from   w w  w.ja v  a  2  s.co m
}