org.apache.poi.xwpf.usermodel.IBodyElement Java Examples

The following examples show how to use org.apache.poi.xwpf.usermodel.IBodyElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Updates bookmarks for the given output {@link XWPFTable} according to its input {@link XWPFTable}.
 * 
 * @param bookmarkManager
 *            the {@link BookmarkManager}
 * @param outputTable
 *            the output {@link XWPFTable}
 * @param inputTable
 *            the input {@link XWPFTable}
 */
private void updateBookmarks(BookmarkManager bookmarkManager, final XWPFTable outputTable,
        final XWPFTable inputTable) {
    final List<XWPFTableRow> inputRows = inputTable.getRows();
    final List<XWPFTableRow> outputRows = outputTable.getRows();
    for (int rowIndex = 0; rowIndex < inputRows.size(); rowIndex++) {
        final XWPFTableRow inputRow = inputRows.get(rowIndex);
        final XWPFTableRow outputRow = outputRows.get(rowIndex);
        final List<XWPFTableCell> inputCells = inputRow.getTableCells();
        final List<XWPFTableCell> outputCells = outputRow.getTableCells();
        for (int cellIndex = 0; cellIndex < inputCells.size(); cellIndex++) {
            final XWPFTableCell inputCell = inputCells.get(cellIndex);
            final XWPFTableCell outputCell = outputCells.get(cellIndex);
            final List<IBodyElement> inputBodyElements = inputCell.getBodyElements();
            final List<IBodyElement> outputBodyElements = outputCell.getBodyElements();
            for (int bodyElementIndex = 0; bodyElementIndex < inputBodyElements.size(); bodyElementIndex++) {
                final IBodyElement inputBodyElement = inputBodyElements.get(bodyElementIndex);
                if (inputBodyElement instanceof XWPFParagraph) {
                    final IBodyElement outputBodyElement = outputBodyElements.get(bodyElementIndex);
                    updateBookmarks(bookmarkManager, ((XWPFParagraph) outputBodyElement).getCTP(),
                            ((XWPFParagraph) inputBodyElement).getCTP());
                }
            }
        }
    }
}
 
Example #2
Source File: AbstractBodyParser.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets the {@link CTSdtBlock} corresponding to the given {@link ContentControl}.
 * 
 * @param body
 *            the {@link IBody}
 * @param sdt
 *            the {@link XWPFSDT}
 * @return the {@link CTSdtBlock} corresponding to the given {@link ContentControl}
 */
public static CTSdtBlock getCTSdtBlock(IBody body, XWPFSDT sdt) {
    final CTSdtBlock res;

    int sdtIndex = -1;
    for (IBodyElement element : body.getBodyElements()) {
        if (element instanceof XWPFSDT) {
            sdtIndex++;
            if (element == sdt) {
                break;
            }
        }
    }

    if (sdtIndex > -1) {
        res = getCTSdtBlock(body, sdtIndex);
    } else {
        res = null;
    }

    return res;
}
 
Example #3
Source File: HeaderFooterBodyContainer.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public int getPosOfParagraphCTP(CTP startCtp) {
    IBodyElement current;
    List<IBodyElement> bodyElements = headerFooter.getBodyElements();
    for (int i = 0; i < bodyElements.size(); i++) {
        current = bodyElements.get(i);
        if (current.getElementType() == BodyElementType.PARAGRAPH) {
            if (((XWPFParagraph) current).getCTP().equals(startCtp)) {
                return i;
            }
        }
    }
    return -1;
}
 
Example #4
Source File: TemplateResolver.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public List<MetaTemplate> resolveBodyElements(List<IBodyElement> bodyElements) {
    List<MetaTemplate> metaTemplates = new ArrayList<>();
    if (null == bodyElements) return metaTemplates;

    // current iterable templates state
    Deque<BlockTemplate> stack = new LinkedList<BlockTemplate>();

    for (IBodyElement element : bodyElements) {
        if (element == null) continue;
        if (element.getElementType() == BodyElementType.PARAGRAPH) {
            XWPFParagraph paragraph = (XWPFParagraph) element;
            RunningRunParagraph runningRun = new RunningRunParagraph(paragraph, templatePattern);
            List<XWPFRun> refactorRuns = runningRun.refactorRun();
            if (null == refactorRuns) continue;
            Collections.reverse(refactorRuns);
            resolveXWPFRuns(refactorRuns, metaTemplates, stack);
        } else if (element.getElementType() == BodyElementType.TABLE) {
            XWPFTable table = (XWPFTable) element;
            List<XWPFTableRow> rows = table.getRows();
            if (null == rows) continue;
            for (XWPFTableRow row : rows) {
                List<XWPFTableCell> cells = row.getTableCells();
                if (null == cells) continue;
                cells.forEach(cell -> {
                    List<MetaTemplate> visitBodyElements = resolveBodyElements(cell.getBodyElements());
                    if (stack.isEmpty()) {
                        metaTemplates.addAll(visitBodyElements);
                    } else {
                        stack.peek().getTemplates().addAll(visitBodyElements);
                    }
                });
            }
        }
    }

    checkStack(stack);
    return metaTemplates;
}
 
Example #5
Source File: CellBodyContainer.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public void updateBodyElements(IBodyElement insertNewParagraph, IBodyElement copy) {
    int pos = -1;
    List<IBodyElement> bodyElements = getBodyElements();
    for (int i = 0; i < bodyElements.size(); i++) {
        if (bodyElements.get(i) == insertNewParagraph) {
            pos = i;
        }
    }
    if (-1 != pos) bodyElements.set(pos, copy);

}
 
Example #6
Source File: CellBodyContainer.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public int getPosOfParagraphCTP(CTP startCtp) {
    IBodyElement current;
    List<IBodyElement> bodyElements = cell.getBodyElements();
    for (int i = 0; i < bodyElements.size(); i++) {
        current = bodyElements.get(i);
        if (current.getElementType() == BodyElementType.PARAGRAPH) {
            if (((XWPFParagraph) current).getCTP().equals(startCtp)) { return i; }
        }
    }
    return -1;
}
 
Example #7
Source File: HeaderFooterBodyContainer.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public void updateBodyElements(IBodyElement insertNewParagraph, IBodyElement copy) {
    int pos = -1;
    List<IBodyElement> bodyElements = getBodyElements();
    for (int i = 0; i < bodyElements.size(); i++) {
        if (bodyElements.get(i) == insertNewParagraph) {
            pos = i;
        }
    }
    if (-1 != pos) bodyElements.set(pos, copy);

}
 
Example #8
Source File: HeaderFooterBodyContainer.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public void removeBodyElement(int pos) {
    List<IBodyElement> bodyElements = getBodyElements();
    if (pos >= 0 && pos < bodyElements.size()) {
        BodyElementType type = bodyElements.get(pos).getElementType();
        if (type == BodyElementType.TABLE) {
            headerFooter.removeTable((XWPFTable) bodyElements.get(pos));
        }
        if (type == BodyElementType.PARAGRAPH) {
            headerFooter.removeParagraph((XWPFParagraph) bodyElements.get(pos));
        }
    }
}
 
Example #9
Source File: NiceXWPFDocument.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public void updateBodyElements(IBodyElement insertNewParagraph, IBodyElement copy) {
    int pos = -1;
    for (int i = 0; i < bodyElements.size(); i++) {
        if (bodyElements.get(i) == insertNewParagraph) {
            pos =  i;
        }
    }
    if (-1 != pos) bodyElements.set(pos, copy);
    
}
 
Example #10
Source File: NiceXWPFDocument.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public int getPosOfTableCTTbl(CTTbl bodyObj) {
    IBodyElement current;
    for (int i = 0; i < bodyElements.size(); i++) {
        current = bodyElements.get(i);
        if (current.getElementType() == BodyElementType.TABLE) {
            if (((XWPFTable)current).getCTTbl().equals(bodyObj)) {
                return i;
            }
        }
    }
    return -1;
}
 
Example #11
Source File: NiceXWPFDocument.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public int getPosOfParagraphCTP(CTP bodyObj) {
    IBodyElement current;
    for (int i = 0; i < bodyElements.size(); i++) {
        current = bodyElements.get(i);
        if (current.getElementType() == BodyElementType.PARAGRAPH) {
            if (((XWPFParagraph)current).getCTP().equals(bodyObj)) {
                return i;
            }
        }
    }
    return -1;
}
 
Example #12
Source File: POIServices.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Marks the {@link CTSimpleField} of the given {@link IBody} as
 * {@link CTSimpleField#setDirty(org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff.Enum) dirty}.
 * 
 * @param body
 *            the {@link IBody}
 */
public void markFieldsAsDirty(IBody body) {
    if (body instanceof XWPFDocument) {
        final XWPFDocument document = (XWPFDocument) body;
        for (XWPFHeader header : document.getHeaderList()) {
            markFieldsAsDirty(header);
        }
        for (XWPFFooter footer : document.getFooterList()) {
            markFieldsAsDirty(footer);
        }
    }
    for (IBodyElement element : body.getBodyElements()) {
        markFieldsAsDirty(element);
    }
}
 
Example #13
Source File: BodyElementIterator.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Create a new iterator for a single {@link IBodyElement}.
 * 
 * @param bodyElement
 *            the {@link IBodyElement}
 */
public BodyElementIterator(IBodyElement bodyElement) {
    final List<IBodyElement> list = new ArrayList<IBodyElement>();

    list.add(bodyElement);

    this.iterator = list.iterator();
}
 
Example #14
Source File: TokenIterator.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Put the iterator in a state where it is finished or there's a token to
 * consume in the currentIterator.
 */
private void moveToNextToken() {
    if (tokenIterator == null || !tokenIterator.hasNext()) {
        while (elementIterator.hasNext() && (tokenIterator == null || !tokenIterator.hasNext())) {
            final IBodyElement element = elementIterator.next();
            switch (element.getElementType()) {
                case PARAGRAPH:
                    // create an empty run if there's no run in the paragraph.
                    // this eases the processing of documents. The processing is based on runs and a paragraph that has no run in it
                    // won't
                    // be seen by the generator and, as a consequence, won't be inserted as a static part in the result.
                    XWPFParagraph paragraph = (XWPFParagraph) element;
                    if (paragraph.getRuns().size() == 0) {
                        paragraph.createRun().setText("");
                    }
                    tokenIterator = new RunIterator(((XWPFParagraph) element).getRuns());
                    break;

                case TABLE:
                    tokenIterator = new BodyElementIterator(element);
                    break;

                case CONTENTCONTROL:
                    tokenIterator = new BodyElementIterator(element);
                    break;

                default:
                    throw new UnsupportedOperationException(
                            "Unsupported type of body element : " + element.getElementType());
            }
        }
    }
}
 
Example #15
Source File: DocumentBodyContainer.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@Override
public List<IBodyElement> getBodyElements() {
    return doc.getBodyElements();
}
 
Example #16
Source File: CellBodyContainnerTest.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@Test
void testGetBodyElements() {
    List<IBodyElement> bodyElements = container.getBodyElements();
    assertEquals(bodyElements.size(), 6);
}
 
Example #17
Source File: HeaderFooterBodyContainnerTest.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@Test
void testGetBodyElements() {
    List<IBodyElement> bodyElements = container.getBodyElements();
    assertEquals(bodyElements.size(), 6);
}
 
Example #18
Source File: DocumentBodyContainnerTest.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@Test
void testGetBodyElements() {
    List<IBodyElement> bodyElements = container.getBodyElements();
    assertEquals(bodyElements.size(), 6);
}
 
Example #19
Source File: CellBodyContainer.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<IBodyElement> getBodyElements() {
    return (List<IBodyElement>) ReflectionUtils.getValue("bodyElements", cell);
}
 
Example #20
Source File: HeaderFooterBodyContainer.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<IBodyElement> getBodyElements() {
    return (List<IBodyElement>) ReflectionUtils.getValue("bodyElements", headerFooter);
}
 
Example #21
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Copies the given {@link IBody} to the given {@link XWPFParagraph}.
 * 
 * @param outputParagraph
 *            the output {@link XWPFParagraph}
 * @param body
 *            the {@link IBody}
 * @param bookmarkManager
 *            the {@link BookmarkManager} to update or <code>null</code>
 * @return the new current {@link XWPFParagraph}
 * @throws Exception
 *             if something goes wrong
 */
public XWPFParagraph copyBody(XWPFParagraph outputParagraph, IBody body, BookmarkManager bookmarkManager)
        throws Exception {
    XWPFParagraph res = null;

    if (body.getBodyElements().isEmpty()) {
        res = outputParagraph;
    } else {
        final IBody outputBody = outputParagraph.getBody();
        boolean inline;
        if (!(outputBody instanceof XWPFTableCell) && outputParagraph.getRuns().size() == 1
            && outputParagraph.getRuns().get(0).getText(0).length() == 0) {
            removeParagraph(outputBody, outputParagraph);
            inline = false;
        } else {
            inline = true;
        }

        final Map<String, String> inputRelationIdToOutputMap = new HashMap<>();
        final Map<URI, URI> inputPartURIToOutputPartURI = new HashMap<>();
        for (IBodyElement element : body.getBodyElements()) {
            if (element instanceof XWPFParagraph) {
                final XWPFParagraph inputParagraph = (XWPFParagraph) element;
                if (inline) {
                    // inline the all paragraph
                    copyParagraphFragment(inputRelationIdToOutputMap, inputPartURIToOutputPartURI, outputParagraph,
                            inputParagraph, null, null);
                    res = outputParagraph;
                } else {
                    res = copyParagraph(outputBody, inputRelationIdToOutputMap, inputPartURIToOutputPartURI,
                            inputParagraph, bookmarkManager);
                }
            } else if (element instanceof XWPFTable) {
                final XWPFTable inputTable = (XWPFTable) element;
                copyTable(outputBody, inputRelationIdToOutputMap, inputPartURIToOutputPartURI, inputTable,
                        bookmarkManager);
                res = null;
            } else if (element instanceof XWPFSDT) {
                copyCTSdtBlock(outputBody, AbstractBodyParser.getCTSdtBlock(outputBody, (XWPFSDT) element));
                res = null;
            } else {
                throw new IllegalStateException("unknown body element.");
            }
            inline = false;
        }

        if (res != null && outputBody instanceof XWPFTableCell && res.getRuns().isEmpty()) {
            res.createRun();
        }
    }

    return res;
}
 
Example #22
Source File: NumberingContinue.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
public static NumberingContinue of(BodyContainer bodyContainer, int start, int end, IterableTemplate iterable) {
    if (start + 1 >= end) return new NumberingContinue();

    final List<IBodyElement> elements = bodyContainer.getBodyElements().subList(start + 1, end);
    if (elements.isEmpty()) return new NumberingContinue();

    CTNumPr first = null;
    int firstPos = -1;
    for (IBodyElement element : elements) {
        if (element.getElementType() == BodyElementType.PARAGRAPH) {
            XWPFParagraph paragraph = (XWPFParagraph) element;
            CTP ctp = paragraph.getCTP();
            if (ctp.getPPr() != null && ctp.getPPr().getNumPr() != null) {
                CTNumPr numPr = ctp.getPPr().getNumPr();
                // find first
                if (null == first) {
                    first = numPr;
                    firstPos = bodyContainer.getPosOfParagraphCTP(ctp);
                } else {
                    // first is not unique
                    if ((Objects.equals(numPr.getIlvl().getVal(), first.getIlvl().getVal())
                            && Objects.equals(numPr.getNumId().getVal(), first.getNumId().getVal()))) {
                        first = null;
                        break;
                    }
                }
            }
        }
    }
    if (null == first) return new NumberingContinue();

    // the first is unique, if first inside other iterable section
    List<MetaTemplate> templates = iterable.getTemplates();
    for (MetaTemplate template : templates) {
        if (template instanceof IterableTemplate) {
            CTP startCtp = ((XWPFParagraph) ((IterableTemplate) template).getStartRun().getParent()).getCTP();
            CTP endCtp = ((XWPFParagraph) ((IterableTemplate) template).getEndRun().getParent()).getCTP();

            int startPos = bodyContainer.getPosOfParagraphCTP(startCtp);
            if (startPos >= firstPos) break;

            int endPos = bodyContainer.getPosOfParagraphCTP(endCtp);
            if (firstPos > startPos && firstPos < endPos) { return new NumberingContinue(); }
        }
    }
    return new NumberingContinue(first.getNumId().getVal());
}
 
Example #23
Source File: DocumentBodyContainer.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@Override
public void updateBodyElements(IBodyElement insertNewTbl, IBodyElement copy) {
    doc.updateBodyElements(insertNewTbl, copy);
}
 
Example #24
Source File: ParsingToken.java    From M2Doc with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Gets the {@link IBodyElement}.
 * 
 * @return the {@link IBodyElement} if the {@link #getKind() kind} is not {@link ParsingTokenKind#RUN}, <code>null</code> otherwise
 */
public IBodyElement getBodyElement() {
    return bodyElement;
}
 
Example #25
Source File: ParsingToken.java    From M2Doc with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new parsing token that corresponds to a {@link IBodyElement}.
 * 
 * @param bodyElement
 *            the {@link IBodyElement}
 */
public ParsingToken(IBodyElement bodyElement) {
    this.bodyElement = bodyElement;
    run = null;
    kind = ParsingTokenKind.getParsingTokenKind(bodyElement.getElementType());
}
 
Example #26
Source File: Resolver.java    From poi-tl with Apache License 2.0 2 votes vote down vote up
/**
 * resolve body elements
 * 
 * @param bodyElements
 * @return
 */
List<MetaTemplate> resolveBodyElements(List<IBodyElement> bodyElements);
 
Example #27
Source File: BodyContainer.java    From poi-tl with Apache License 2.0 votes vote down vote up
void updateBodyElements(IBodyElement insertNewParagraph, IBodyElement copy); 
Example #28
Source File: BodyContainer.java    From poi-tl with Apache License 2.0 votes vote down vote up
List<IBodyElement> getBodyElements();