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:DocxProcess.DocxTemplateReplacer.java

private TreeMap<Integer, XWPFRun> getPosToRuns(XWPFParagraph paragraph) {
    int pos = 0;//from w w w. ja v  a 2s  . co  m
    TreeMap<Integer, XWPFRun> map = new TreeMap<Integer, XWPFRun>();
    for (XWPFRun run : paragraph.getRuns()) {
        String runText = run.text();
        if (runText != null && runText.length() > 0) {
            for (int i = 0; i < runText.length(); i++) {
                map.put(pos + i, run);
            }
            pos += runText.length();
        }

    }
    return map;
}

From source file:offishell.msoffice.WordTestHelper.java

License:MIT License

/**
 * <p>/*  w w w  .  j a va  2 s . c  o m*/
 * Assertion helper.
 * </p>
 * 
 * @param para
 * @param value
 */
public default void checkRun(XWPFRun run, Assertion value) {
    assert value.of(run.text());
    assert value.of(run.getFontFamily());
    assert value.of(run.getFontSize());
    assert value.of(run.isBold());
    assert value.of(run.isCapitalized());
    assert value.of(run.isDoubleStrikeThrough());
    assert value.of(run.isEmbossed());
    assert value.of(run.isHighlighted());
    assert value.of(run.isImprinted());
}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>/*from  ww  w .j  a  v  a 2  s.co  m*/
 * Helper method to clone {@link XWPFRun}.
 * </p>
 * 
 * @param in
 * @param out
 * @param model
 */
public static void copy(XWPFRun in, XWPFRun out, UnaryOperator<String> converter) {
    // copy
    out.setBold(in.isBold());
    out.setCapitalized(in.isCapitalized());
    out.setCharacterSpacing(in.getCharacterSpacing());
    out.setColor(in.getColor());
    out.setDoubleStrikethrough(in.isDoubleStrikeThrough());
    out.setEmbossed(in.isEmbossed());
    out.setFontFamily(in.getFontFamily());
    out.setFontSize(in.getFontSize());
    out.setImprinted(in.isImprinted());
    out.setItalic(in.isItalic());
    out.setKerning(in.getKerning());
    out.setShadow(in.isShadowed());
    out.setSmallCaps(in.isSmallCaps());
    out.setStrikeThrough(in.isStrikeThrough());
    out.setVerticalAlignment(out.getVerticalAlignment().toString());
    out.setTextPosition(in.getTextPosition());
    out.setUnderline(in.getUnderline());

    // copy context
    CTR inCTR = in.getCTR();
    CTRPr inPR = inCTR.getRPr();
    CTR outCTR = out.getCTR();
    CTRPr outPR = outCTR.isSetRPr() ? outCTR.getRPr() : outCTR.addNewRPr();
    outPR.set(inCTR.getRPr());
    out.setVerticalAlignment(
            inPR == null || inPR.getVertAlign() == null ? "baseline" : inPR.getVertAlign().toString());

    // // copy tab
    // CTEmpty[] tabs = inCTR.getTabArray();
    //
    // if (tabs.length != 0) {
    // out.addTab();
    // }
    outCTR.setAnnotationRefArray(inCTR.getAnnotationRefList().toArray(CTEmpty[]::new));
    outCTR.setBrArray(inCTR.getBrList().toArray(CTBr[]::new));
    outCTR.setCommentReferenceArray(inCTR.getCommentReferenceList().toArray(CTMarkup[]::new));
    outCTR.setContinuationSeparatorArray(inCTR.getContinuationSeparatorList().toArray(CTEmpty[]::new));
    outCTR.setCrArray(inCTR.getCrList().toArray(CTEmpty[]::new));
    outCTR.setDelInstrTextArray(inCTR.getDelInstrTextList().toArray(CTText[]::new));
    outCTR.setDrawingArray(inCTR.getDrawingList().toArray(CTDrawing[]::new));
    outCTR.setEndnoteRefArray(inCTR.getEndnoteRefList().toArray(CTEmpty[]::new));
    outCTR.setFldCharArray(inCTR.getFldCharList().toArray(CTFldChar[]::new));
    outCTR.setFootnoteRefArray(inCTR.getFootnoteRefList().toArray(CTEmpty[]::new));
    outCTR.setInstrTextArray(inCTR.getInstrTextList().toArray(CTText[]::new));
    outCTR.setLastRenderedPageBreakArray(inCTR.getLastRenderedPageBreakList().toArray(CTEmpty[]::new));
    outCTR.setObjectArray(inCTR.getObjectList().toArray(CTObject[]::new));
    outCTR.setPictArray(inCTR.getPictList().toArray(CTPicture[]::new));
    outCTR.setPtabArray(inCTR.getPtabList().toArray(CTPTab[]::new));
    outCTR.setSymArray(inCTR.getSymList().toArray(CTSym[]::new));
    outCTR.setTabArray(inCTR.getTabList().toArray(CTEmpty[]::new));

    // copy image
    for (XWPFPicture inPicture : in.getEmbeddedPictures()) {
        try {
            XWPFPictureData inData = inPicture.getPictureData();
            String outId = out.getDocument().addPictureData(new ByteArrayInputStream(inData.getData()),
                    inData.getPictureType());

            select(CTBlip.class, outCTR).to(blip -> blip.setEmbed(outId));
        } catch (Exception e) {
            throw I.quiet(e);
        }
    }

    // copy text
    write(out, converter.apply(in.text()));
}

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

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an info.
 * //from   w ww.  ja v 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);
    FileInputStream is = new FileInputStream("templates/testParsingErrorSimpleTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    DocumentParser parser = new DocumentParser(document, queryEnvironment);
    DocumentTemplate template = parser.parseDocument();
    final XWPFRun location = ((XWPFParagraph) template.getDocument().getBodyElements().get(0)).getRuns().get(0);
    template.getBody().getValidationMessages().add(
            new TemplateValidationMessage(ValidationMessageLevel.INFO, "XXXXXXXXXXXXXXXXXXXXXXXX", location));
    TemplateGenerator generator = new TemplateGenerator("results/testParsingErrorSimpleTag.docx", template);
    generator.generate();
    assertTrue(new File("results/testParsingErrorSimpleTag.docx").exists());

    FileInputStream resIs = new FileInputStream("results/testParsingErrorSimpleTag.docx");
    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());
}

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

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an warning.
 * /* w ww  . j  ava2 s  .  com*/
 * @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);
    FileInputStream is = new FileInputStream("templates/testParsingErrorSimpleTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    DocumentParser parser = new DocumentParser(document, queryEnvironment);
    DocumentTemplate template = parser.parseDocument();
    final XWPFRun location = ((XWPFParagraph) template.getDocument().getBodyElements().get(0)).getRuns().get(0);
    template.getBody().getValidationMessages().add(new TemplateValidationMessage(ValidationMessageLevel.WARNING,
            "XXXXXXXXXXXXXXXXXXXXXXXX", location));
    TemplateGenerator generator = new TemplateGenerator("results/testParsingErrorSimpleTag.docx", template);
    generator.generate();
    assertTrue(new File("results/testParsingErrorSimpleTag.docx").exists());

    FileInputStream resIs = new FileInputStream("results/testParsingErrorSimpleTag.docx");
    OPCPackage resOPackage = OPCPackage.open(resIs);
    XWPFDocument resDocument = new XWPFDocument(resOPackage);

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

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

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

License:Open Source License

/**
 * Ensure that the validation generation produces a document with an error.
 * //from   w w w. j  ava 2s  .  co m
 * @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);
    FileInputStream is = new FileInputStream("templates/testParsingErrorSimpleTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    DocumentParser parser = new DocumentParser(document, queryEnvironment);
    DocumentTemplate template = parser.parseDocument();
    final XWPFRun location = ((XWPFParagraph) template.getDocument().getBodyElements().get(0)).getRuns().get(0);
    template.getBody().getValidationMessages().add(
            new TemplateValidationMessage(ValidationMessageLevel.ERROR, "XXXXXXXXXXXXXXXXXXXXXXXX", location));
    TemplateGenerator generator = new TemplateGenerator("results/testParsingErrorSimpleTag.docx", template);
    generator.generate();
    assertTrue(new File("results/testParsingErrorSimpleTag.docx").exists());

    FileInputStream resIs = new FileInputStream("results/testParsingErrorSimpleTag.docx");
    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());
}

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

License:Open Source License

/**
 * Ensure that the validation generation produces a document with errors in the good order.
 * //  w  w  w. ja  v a 2s.co 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);
    FileInputStream is = new FileInputStream("templates/testParsingErrorSimpleTag.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    DocumentParser parser = new DocumentParser(document, queryEnvironment);
    DocumentTemplate template = parser.parseDocument();
    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));
    TemplateGenerator generator = new TemplateGenerator("results/testParsingErrorSimpleTag.docx", template);
    generator.generate();
    assertTrue(new File("results/testParsingErrorSimpleTag.docx").exists());

    FileInputStream resIs = new FileInputStream("results/testParsingErrorSimpleTag.docx");
    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);
}

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.
 * //from w  w  w . j a v  a  2 s.  co 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)) {
        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.
 * /*from   w w w . j av  a 2  s .c om*/
 * @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.
 * //ww w  .j  a va 2 s  . c  om
 * @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();
}