Example usage for org.apache.poi.xwpf.usermodel XWPFRun text

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun text

Introduction

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

Prototype

@Override
public String text() 

Source Link

Document

Returns the string version of the text, with tabs and carriage returns in place of their xml equivalents.

Usage

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 w  w  .j a  va2s .  c  om
 * @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.test.M2DocTestUtils.java

License:Open Source License

/**
 * Gets the {@link XWPFRun} containing the given text in the given {@link XWPFDocument}.
 * /*from   ww w. ja v  a2 s .com*/
 * @param document
 *            the {@link XWPFDocument}
 * @param text
 *            the {@link XWPFRun}
 * @return the {@link XWPFRun} containing the given text in the given {@link XWPFDocument} if any, <code>null</code> otherwise
 */
public static XWPFRun getRunContaining(XWPFDocument document, String text) {
    XWPFRun res = null;

    for (XWPFParagraph paragraph : document.getParagraphs()) {
        for (XWPFRun run : paragraph.getRuns()) {
            if (run.text().contains(text)) {
                res = run;
                break;
            }
        }
    }

    return res;
}

From source file:org.obeonetwork.m2doc.test.TemplateAstSerializer.java

License:Open Source License

@Override
public Void caseStaticFragment(StaticFragment staticFragment) {
    for (XWPFRun run : staticFragment.getRuns()) {
        builder.append(run.text().replaceAll("\r\n", "\n" + indentation).replaceAll("\r", "\n" + indentation));
    }//from   w  w  w.j a va 2 s .c om
    return null;
}

From source file:org.obeonetwork.m2doc.tests.generator.TemplateValidationGeneratorTests.java

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an info.
 * /*from ww w. jav  a2 s  .  c o m*/
 * @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,
            this.getClass().getClassLoader())) {
        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.tests.generator.TemplateValidationGeneratorTests.java

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an warning.
 * //from  ww w.  j  a  va  2  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,
            this.getClass().getClassLoader())) {
        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.tests.generator.TemplateValidationGeneratorTests.java

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an error.
 * //from  w  w  w.  j a v a 2s. 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,
            this.getClass().getClassLoader())) {
        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.tests.generator.TemplateValidationGeneratorTests.java

License:Open Source License

/**
 * Ensure that the validation generation produces a document with errors in the good order.
 * // w  w  w . j  a  va2s  .  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,
            this.getClass().getClassLoader())) {
        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();
}