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.TemplateParsingValidatorTest.java

License:Open Source License

/**
 * Tests that parsing errors from AQL template tag (conditional here) are placed next to the start tag.
 * The tested tag is <{m:wrong->.}ajout de value1{m:endif}>
 * The expected tag is : <{m:wrong->.} Expression wrong->. is invalid ajout de value1{m:endif}>
 * After the run with the end '}' char, the following runs must be present :
 * A run must contains blanks char./*from   ww  w  .ja v a2 s.com*/
 * The next one must contains the error message.
 * The next one must contains blank char and the next one the static content of the conditional.
 * 
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testErrorInStartTag()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/testParsingErrorStartTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    TemplateValidationGenerator validator = new TemplateValidationGenerator();
    validator.doSwitch(template);
    createDestinationDocument(document, "results/testParsingErrorStartTag.docx");

    // scan the destination document
    assertEquals(2, document.getParagraphs().size());
    assertEquals(16, document.getParagraphs().get(0).getRuns().size());
    assertEquals(1, document.getParagraphs().get(1).getRuns().size());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(5).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(6).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(6).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(6).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(7).getCTR().getRPr().getHighlight().getVal());
    assertEquals("Expression \"wrong->.\" is invalid: missing collection service call",
            document.getParagraphs().get(0).getRuns().get(7).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(7).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(7).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(7).getCTR().getRPr().getHighlight().getVal());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(8).getText(0));
    assertEquals("ajout de value1", document.getParagraphs().get(0).getRuns().get(9).getText(0));
    assertEquals("Unexpected tag m:endif at this location",
            document.getParagraphs().get(0).getRuns().get(13).getText(0));
}

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

License:Open Source License

/**
 * Tests that parsing errors from AQL template tag (conditional here) are placed next to the start tag.
 * The tested tag is <{m:diagram provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}Some text>
 * The expected tag is : <{m:diagram provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}<---The image
 * tag is referencing an unknown diagram provider : 'noExistingProvider' Some text>
 * After the run with the end '}' char, the following runs must be present :
 * A run must contains blanks char./* w w w.j av a 2 s .  co  m*/
 * The next one must contains the error message.
 * The next one is a blank separator.
 * The next one must contains the other error message.
 * The next one must contains blank char and the next one the static content after the tag in the original template.
 * 
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testErrorInSimpleTag()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/testParsingErrorSimpleTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    TemplateValidationGenerator validator = new TemplateValidationGenerator();
    validator.doSwitch(template);
    createDestinationDocument(document, "results/testParsingErrorSimpleTag.docx");
    // scan the destination document
    assertEquals(2, document.getParagraphs().size());
    assertEquals(11, document.getParagraphs().get(0).getRuns().size());
    assertEquals(1, document.getParagraphs().get(1).getRuns().size());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(2).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(3).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(3).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(3).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(5).getCTR().getRPr().getHighlight().getVal());
    assertEquals("The image tag is referencing an unknown diagram provider : 'noExistingProvider'",
            document.getParagraphs().get(0).getRuns().get(5).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(5).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(5).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(5).getCTR().getRPr().getHighlight().getVal());
    assertEquals("Some text", document.getParagraphs().get(0).getRuns().get(10).getText(0));
}

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

License:Open Source License

/**
 * Tests that parsing errors from AQL template tag (conditional here) are placed next to the start tag.
 * The tested tag is <{m:diagram provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}Some text>
 * The expected tag is : <{m:diagram provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}<---The image
 * tag is referencing an unknown diagram provider : 'noExistingProvider' <---The start of an option's key has been read but the end of
 * it and the value were missing : ' title="representationTitle"'. Some text>
 * After the run with the end '}' char, the following runs must be present :
 * A run must contains blanks char.//from   w  w w.j ava  2 s.c  om
 * The next one must contains the error message.
 * The next one is a blank separator.
 * The next one must contains the other error message.
 * The next one must contains blank char and the next one the static content after the tag in the original template.
 * 
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testMultiErrorInSimpleTag()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/testMultiParsingErrorSimpleTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    TemplateValidationGenerator validator = new TemplateValidationGenerator();
    validator.doSwitch(template);
    createDestinationDocument(document, "results/testMultiParsingErrorSimpleTag.docx");
    // scan the destination document
    assertEquals(1, document.getParagraphs().size());
    assertEquals(14, document.getParagraphs().get(0).getRuns().size());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(2).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(3).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(3).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(3).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(3).getCTR().getRPr().getHighlight().getVal());
    assertEquals("The image tag is referencing an unknown diagram provider : 'noExistingProvider'",
            document.getParagraphs().get(0).getRuns().get(6).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(6).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(6).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(6).getCTR().getRPr().getHighlight().getVal());

    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(10).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(11).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(11).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(11).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(11).getCTR().getRPr().getHighlight().getVal());
    assertEquals(
            "The start of an option's key has been read but the end of it and the value were missing : ' title=\"representationTitle\"'.",
            document.getParagraphs().get(0).getRuns().get(12).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(12).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(12).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(11).getCTR().getRPr().getHighlight().getVal());

    assertEquals("Some text", document.getParagraphs().get(0).getRuns().get(13).getText(0));
}

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

License:Open Source License

/**
 * Tests that parsing errors from AQL template tag (conditional here) are placed next to the start tag.
 * The tested tag is <{m:diagram provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}Some text>
 * The expected tag is : <{m:diagram provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}<---The image
 * tag is referencing an unknown diagram provider : 'noExistingProvider' Some text>
 * After the run with the end '}' char, the following runs must be present :
 * A run must contains blanks char./*from w w  w  .  j  a  v  a  2  s.  c  o m*/
 * The next one must contains the error message.
 * The next one is a blank separator.
 * The next one must contains the other error message.
 * The next one must contains blank char and the next one the static content after the tag in the original template.
 * 
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testErrorInEndTag()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/testParsingErrorEndTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    TemplateValidationGenerator validator = new TemplateValidationGenerator();
    validator.doSwitch(template);
    createDestinationDocument(document, "results/testParsingErrorEndTag.docx");
    // scan the destination document
    assertEquals(1, document.getParagraphs().size());
    assertEquals(24, document.getParagraphs().get(0).getRuns().size());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(9).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(10).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(10).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(10).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(10).getCTR().getRPr().getHighlight().getVal());
    assertEquals("Unexpected tag m:endlet at this location",
            document.getParagraphs().get(0).getRuns().get(11).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(11).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(11).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(11).getCTR().getRPr().getHighlight().getVal());

    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(10).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(10).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(10).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(10).getCTR().getRPr().getHighlight().getVal());
    assertEquals("Unexpected tag m:endlet at this location",
            document.getParagraphs().get(0).getRuns().get(11).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(11).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(11).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(11).getCTR().getRPr().getHighlight().getVal());

    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(15).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(16).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(16).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(16).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(16).getCTR().getRPr().getHighlight().getVal());
    assertEquals("gd:elseif, gd:else or gd:endif expected here.",
            document.getParagraphs().get(0).getRuns().get(17).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(17).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(17).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(17).getCTR().getRPr().getHighlight().getVal());

    assertEquals("Some", document.getParagraphs().get(0).getRuns().get(18).getText(0));
    assertEquals(" t", document.getParagraphs().get(0).getRuns().get(19).getText(0));
    assertEquals("ext", document.getParagraphs().get(0).getRuns().get(20).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(22).getText(0));
    assertEquals("Unexpected tag EOF at this location",
            document.getParagraphs().get(0).getRuns().get(23).getText(0));
}

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

License:Open Source License

/**
 * Tests that parsing errors from AQL template tag (conditional here) are placed next to the start tag when no following text exists.
 * The tested tag is <{m:diagram provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}>
 * The expected tag is : <{m:diagram provider:"noExistingProvider" width:"500" height:"500" title="representationTitle"}<---The image
 * tag is referencing an unknown diagram provider : 'noExistingProvider' >
 * After the run with the end '}' char, the following runs must be present :
 * A run must contains blanks char.//from  w  ww  .ja  va 2  s . com
 * The next one must contains the error message.
 * The next one is a blank separator.
 * The next one must contains the other error message.
 * The next one must contains blank char and the next one the static content after the tag in the original template.
 * 
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 * @throws DocumentGenerationException
 */
@Test
public void testErrorInSimpleTagWithoutFollowing()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/testParsingErrorSimpleTagWithoutFollowingText.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    BodyParser parser = new BodyParser(document, env);
    Template template = parser.parseTemplate();
    TemplateValidationGenerator validator = new TemplateValidationGenerator();
    validator.doSwitch(template);
    createDestinationDocument(document, "results/testParsingErrorSimpleTagWithoutFollowingText.docx");
    // scan the destination document
    assertEquals(1, document.getParagraphs().size());
    assertEquals(11, document.getParagraphs().get(0).getRuns().size());
    assertEquals("    ", document.getParagraphs().get(0).getRuns().get(2).getText(0));
    assertEquals("<---", document.getParagraphs().get(0).getRuns().get(3).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(3).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(3).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(5).getCTR().getRPr().getHighlight().getVal());
    assertEquals("The image tag is referencing an unknown diagram provider : 'noExistingProvider'",
            document.getParagraphs().get(0).getRuns().get(5).getText(0));
    assertEquals("FF0000", document.getParagraphs().get(0).getRuns().get(5).getColor());
    assertEquals(16, document.getParagraphs().get(0).getRuns().get(5).getFontSize());
    assertEquals(STHighlightColor.LIGHT_GRAY,
            document.getParagraphs().get(0).getRuns().get(5).getCTR().getRPr().getHighlight().getVal());
}

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

License:Open Source License

private XWPFDocument createDestinationDocument(String inputDocumentFileName)
        throws IOException, InvalidFormatException {
    FileInputStream is = new FileInputStream(inputDocumentFileName);
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    int size = document.getBodyElements().size();
    for (int i = 0; i < size; i++) {
        document.removeBodyElement(0);/*from   w  ww  .j  ava  2  s. co m*/
    }
    return document;
}

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

License:Open Source License

/**
 * Test the replacement of a variable in a doc.
 * /*ww  w. j  a v  a2s .  co m*/
 * @throws InvalidFormatException
 * @throws IOException
 * @throws DocumentParserException
 */
@Test
public void testVarRefProcessing() 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>();
    definitions.put("x", "valueofx");
    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: valueofx",
            destinationDoc.getParagraphs().get(0).getText());
    assertEquals("Fin du gabarit", destinationDoc.getParagraphs().get(1).getText());
}

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

License:Open Source License

@Test
public void testVarRefStyledProcessing() throws InvalidFormatException, IOException, DocumentParserException {
    FileInputStream is = new FileInputStream("templates/testVarStyle.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("x", "valueofx");
    XWPFDocument destinationDoc = createDestinationDocument("templates/testVarStyle.docx");
    TemplateProcessor processor = new TemplateProcessor(definitions, "", env, destinationDoc);
    processor.doSwitch(template);/*from   w w  w .j  av a 2  s.co  m*/
    assertEquals("Template de test pour les balises de rfrence  une variable\u00a0: valueofx",
            destinationDoc.getParagraphs().get(0).getText());
    XWPFParagraph paragraph = destinationDoc.getParagraphs().get(0);
    XWPFRun run = paragraph.getRuns().get(paragraph.getRuns().size() - 1);
    assertEquals("E36C0A", run.getColor());
    assertNotNull(run.getCTR().getRPr().getI());
    assertNotNull(run.getCTR().getRPr().getB());
}

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

License:Open Source License

@Test
public void testQueryProcessing() 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>();
    definitions.put("self", EcorePackage.eINSTANCE);
    XWPFDocument destinationDoc = createDestinationDocument("templates/testAQL.docx");
    TemplateProcessor processor = new TemplateProcessor(definitions, "", env, destinationDoc);
    processor.doSwitch(template);/*from w  ww.  ja v a2s.c  o  m*/
    // scan the destination document
    assertEquals(3, destinationDoc.getParagraphs().size());
    System.out.println(destinationDoc.getParagraphs().get(0).getText());
    assertEquals("Template de test pour les balises de query aql\u00a0: ecore",
            destinationDoc.getParagraphs().get(0).getText());
    assertEquals("Fin du gabarit", destinationDoc.getParagraphs().get(1).getText());
    assertEquals("", destinationDoc.getParagraphs().get(2).getText());
}

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

License:Open Source License

@Test
public void testVarQueryStyledProcessing() throws InvalidFormatException, IOException, DocumentParserException {
    FileInputStream is = new FileInputStream("templates/testVarStyle.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("x", "valueofx");
    XWPFDocument destinationDoc = createDestinationDocument("templates/testVarStyle.docx");
    TemplateProcessor processor = new TemplateProcessor(definitions, "", env, destinationDoc);
    processor.doSwitch(template);/*  ww  w .  j  av a2s  .  co  m*/
    assertEquals("Template de test pour les balises de rfrence  une variable\u00a0: valueofx",
            destinationDoc.getParagraphs().get(0).getText());
    XWPFParagraph paragraph = destinationDoc.getParagraphs().get(0);
    XWPFRun run = paragraph.getRuns().get(paragraph.getRuns().size() - 1);
    assertEquals("E36C0A", run.getColor());
    assertNotNull(run.getCTR().getRPr().getI());
    assertNotNull(run.getCTR().getRPr().getB());
}