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

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

Introduction

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

Prototype

public IRunBody getParent() 

Source Link

Document

Get the currently referenced paragraph/SDT object

Usage

From source file:com.deepoove.poi.NiceXWPFDocument.java

License:Apache License

/**
 * ???/*  w ww.  ja  va2  s.c  o  m*/
 * 
 * @param run
 * @param row
 * @param col
 * @return
 */
public XWPFTable insertNewTable(XWPFRun run, int row, int col) {
    XmlCursor cursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor();

    // XmlCursor cursor = run.getCTR().newCursor();
    if (isCursorInBody(cursor)) {
        String uri = CTTbl.type.getName().getNamespaceURI();
        String localPart = "tbl";
        cursor.beginElement(localPart, uri);
        cursor.toParent();

        CTTbl t = (CTTbl) cursor.getObject();
        XWPFTable newT = new XWPFTable(t, this, row, col);
        XmlObject o = null;
        while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if (!(o instanceof CTTbl)) {
            tables.add(0, newT);
        } else {
            int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
            tables.add(pos, newT);
        }
        int i = 0;
        XmlCursor tableCursor = t.newCursor();
        try {
            cursor.toCursor(tableCursor);
            while (cursor.toPrevSibling()) {
                o = cursor.getObject();
                if (o instanceof CTP || o instanceof CTTbl)
                    i++;
            }
            bodyElements.add(i, newT);
            cursor.toCursor(tableCursor);
            cursor.toEndToken();
            return newT;
        } finally {
            tableCursor.dispose();
        }
    }
    return null;
}

From source file:com.deepoove.poi.policy.DynamicTableRenderPolicy.java

License:Apache License

@Override
public void render(ElementTemplate runTemplateP, Object data, XWPFTemplate template) {
    NiceXWPFDocument doc = template.getXWPFDocument();
    RunTemplate runTemplate = (RunTemplate) runTemplateP;
    XWPFRun run = runTemplate.getRun();
    try {/*from   ww  w.j a  va 2s.  c  om*/
        XmlCursor newCursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor();
        newCursor.toParent();
        //if (newCursor.getObject() instanceof CTTc) 
        newCursor.toParent();
        newCursor.toParent();
        XmlObject object = newCursor.getObject();
        XWPFTable table = doc.getTable((CTTbl) object);
        render(table, data);
    } catch (Exception e) {
        logger.error("dynamic table error:" + e.getMessage());
    }
}

From source file:fr.opensagres.poi.xwpf.converter.xhtml.internal.XHTMLMapper.java

License:Open Source License

@Override
protected void visitRun(XWPFRun run, boolean pageNumber, String url, Object paragraphContainer)
        throws Exception {
    if (run.getParent() instanceof XWPFParagraph) {
        this.currentParagraph = (XWPFParagraph) run.getParent();
    }//  w w w  . jav a  2 s .  co  m

    XWPFParagraph paragraph = run.getParagraph();
    // 1) create attributes

    // 1.1) Create "class" attributes.
    this.currentRunAttributes = createClassAttribute(paragraph.getStyleID());

    // 1.2) Create "style" attributes.
    CTRPr rPr = run.getCTR().getRPr();
    CSSStyle cssStyle = getStylesDocument().createCSSStyle(rPr);
    if (cssStyle != null) {
        cssStyle.addProperty(CSSStylePropertyConstants.WHITE_SPACE, "pre-wrap");
    }
    this.currentRunAttributes = createStyleAttribute(cssStyle, currentRunAttributes);

    if (url != null) {
        // url is not null, generate a HTML a.
        AttributesImpl hyperlinkAttributes = new AttributesImpl();
        SAXHelper.addAttrValue(hyperlinkAttributes, HREF_ATTR, url);
        startElement(A_ELEMENT, hyperlinkAttributes);
    }

    super.visitRun(run, pageNumber, url, paragraphContainer);

    if (url != null) {
        // url is not null, close the HTML a.
        // TODO : for the moment generate space to be ensure that a has some content.
        characters(" ");
        endElement(A_ELEMENT);
    }
    this.currentRunAttributes = null;
    this.currentParagraph = null;
}

From source file:org.obeonetwork.m2doc.generator.BookmarkManager.java

License:Open Source License

/**
 * Starts a bookmark in the given {@link CTBody} with the given name.
 * /*from  w w  w .j  av a 2  s  .c om*/
 * @param paragraph
 *            the current {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 */
public void startBookmark(XWPFParagraph paragraph, String name) {
    if (bookmarks.containsKey(name)) {
        M2DocUtils.appendMessageRun(paragraph, ValidationMessageLevel.ERROR,
                "Can't start duplicated bookmark " + name);
    } else {
        final CTBookmark bookmark = paragraph.getCTP().addNewBookmarkStart();
        // we create a new run for future error messages.
        messagePositions.put(bookmark, paragraph.createRun());
        bookmark.setName(name);
        final BigInteger id = getRandomID();
        bookmark.setId(id);
        bookmarks.put(name, bookmark);
        startedBookmarks.put(name, bookmark);
        Set<CTText> pendingRefs = pendingReferences.remove(name);
        if (pendingRefs != null) {
            for (CTText pendingRef : pendingRefs) {
                // we remove the created for error messages.
                final XWPFRun run = messagePositions.get(pendingRef);
                final IRunBody parent = run.getParent();
                if (parent instanceof XWPFParagraph) {
                    ((XWPFParagraph) parent).removeRun(((XWPFParagraph) parent).getRuns().indexOf(run));
                } else {
                    throw new IllegalStateException("this should not happend");
                }
                pendingRef.setStringValue(String.format(REF_TAG, bookmark.getName()));
            }
        }
    }
}

From source file:org.obeonetwork.m2doc.generator.BookmarkManager.java

License:Open Source License

/**
 * Ends the bookmark with the given name.
 * /*from www . j  av a2  s  . com*/
 * @param paragraph
 *            the current {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 */
public void endBookmark(XWPFParagraph paragraph, String name) {
    final CTBookmark bookmark = startedBookmarks.remove(name);
    if (bookmark != null) {
        final CTMarkupRange range = paragraph.getCTP().addNewBookmarkEnd();
        range.setId(bookmarks.get(name).getId());
        // we remove the created for error messages.
        final XWPFRun run = messagePositions.get(bookmark);
        final IRunBody parent = run.getParent();
        if (parent instanceof XWPFParagraph) {
            ((XWPFParagraph) parent).removeRun(((XWPFParagraph) parent).getRuns().indexOf(run));
        } else {
            throw new IllegalStateException("this should not happend");
        }
    } else if (bookmarks.containsKey(name)) {
        M2DocUtils.appendMessageRun(paragraph, ValidationMessageLevel.ERROR,
                "Can't can't end already closed bookmark " + name);
    } else {
        M2DocUtils.appendMessageRun(paragraph, ValidationMessageLevel.ERROR,
                "Can't can't end not existing bookmark " + name);
    }
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts a run in the generated document. The new run is a copy from the specified run.
 * //from   w w w  . ja  va  2 s .co  m
 * @param srcRun
 *            the run to copy
 * @return the inserted run.
 */
private XWPFRun insertRun(XWPFRun srcRun) {
    if (srcRun.getParent() != currentTemplateParagraph || forceNewParagraph) {
        createNewParagraph((XWPFParagraph) srcRun.getParent());
        forceNewParagraph = false;
    }
    XWPFRun newRun = null;
    if (srcRun instanceof XWPFHyperlinkRun) {
        // Hyperlinks meta information is saved in the paragraph and not in the run. So we have to update the paragrapah with a copy of
        // the hyperlink to insert.
        CTHyperlink newHyperlink = currentGeneratedParagraph.getCTP().addNewHyperlink();
        newHyperlink.set(((XWPFHyperlinkRun) srcRun).getCTHyperlink());

        newRun = new XWPFHyperlinkRun(newHyperlink, srcRun.getCTR(), srcRun.getParent());
        currentGeneratedParagraph.addRun(newRun);
    } else {
        newRun = currentGeneratedParagraph.createRun();
        newRun.getCTR().set(srcRun.getCTR());
    }
    return newRun;
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts a run in the generated document and set it's text to the specified replacement. The new run is a copy from the specified run.
 * /* w w w . j  a  va 2s  . co  m*/
 * @param srcRun
 *            the run to copy
 * @param replacement
 *            the text to set
 * @return the inserted run
 */
private XWPFRun insertFieldRunReplacement(XWPFRun srcRun, String replacement) {
    if (srcRun.getParent() != currentTemplateParagraph || forceNewParagraph) {
        createNewParagraph((XWPFParagraph) srcRun.getParent());
        forceNewParagraph = false;
    }
    return insertString(srcRun, replacement);
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Insert the given {@link MPagination}.
 * /*  ww  w.j a  v a  2s . c  o  m*/
 * @param run
 *            the {@link XWPFRun} to insert to
 * @param mPagination
 *            the {@link MPagination}
 */
private void insertMPagination(XWPFRun run, MPagination mPagination) {
    switch (mPagination) {
    case newColumn:
        run.addBreak(BreakType.COLUMN);
        break;

    case newParagraph:
        createNewParagraph((XWPFParagraph) run.getParent());
        break;

    case newPage:
        run.addBreak(BreakType.PAGE);
        break;

    case newTableOfContent:
        CTP ctP = currentGeneratedParagraph.getCTP();
        CTSimpleField toc = ctP.addNewFldSimple();
        toc.setInstr("TOC \\h");
        toc.setDirty(STOnOff.TRUE);
        break;

    case newTextWrapping:
        run.addBreak(BreakType.TEXT_WRAPPING);
        break;

    default:
        throw new IllegalStateException("Not supported MPagination.");
    }
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts the given {@link MTable}.//from w w w .j  a va2  s .  c  om
 * 
 * @param run
 *            the {@link XWPFRun} to insert to
 * @param table
 *            the {@link MTable} to insert
 */
private void insertMTable(XWPFRun run, MTable table) {
    final XWPFTable docTable;
    if (generatedDocument instanceof XWPFDocument) {
        if (table.getLabel() != null) {
            XWPFRun captionRun;
            captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        docTable = ((XWPFDocument) generatedDocument).createTable();
    } else if (generatedDocument instanceof XWPFHeaderFooter) {
        final XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) generatedDocument;
        final int index = headerFooter._getHdrFtr().getTblArray().length;
        final CTTbl cttbl = headerFooter._getHdrFtr().insertNewTbl(index);
        docTable = new XWPFTable(cttbl, headerFooter);
        headerFooter.insertTable(index, docTable);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tcell = (XWPFTableCell) generatedDocument;
        if (table.getLabel() != null) {
            final XWPFRun captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        CTTbl ctTbl = tcell.getCTTc().addNewTbl();
        docTable = new XWPFTable(ctTbl, tcell);
        int tableRank = tcell.getTables().size();
        tcell.insertTable(tableRank, docTable);
        // A paragraph is mandatory at the end of a cell, so let's always add one.
        tcell.addParagraph();
    } else {
        docTable = null;
        M2DocUtils.appendMessageRun((XWPFParagraph) run.getParent(), ValidationMessageLevel.ERROR,
                "m:table can't be inserted here.");
    }

    if (docTable != null) {
        fillTable(docTable, table);
    }
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Closes the generation of the given {@link Repetition}.
 * if the end of {@link Repetition} lies on a distinct paragraph, insert a new
 * paragraph there to take this into account.
 * //w  ww.ja v a2s  . c om
 * @param repetition
 *            {@link Repetition} to close
 */
private void closingRepretition(Repetition repetition) {
    int bodySize = repetition.getBody().getStatements().size();
    if (bodySize > 0 && repetition.getBody().getStatements().get(bodySize - 1).getRuns().size() > 0) {
        IConstruct lastBodyPart = repetition.getBody().getStatements().get(bodySize - 1);
        int runNumber = lastBodyPart.getRuns().size();
        XWPFRun lastRun = lastBodyPart.getRuns().get(runNumber - 1);
        int closingRunNumber = repetition.getClosingRuns().size();
        if (closingRunNumber > 0 && repetition.getClosingRuns().get(0).getParent() != lastRun.getParent()) {
            forceNewParagraph = true;
        }
    }
}