Example usage for org.apache.poi.xwpf.usermodel XWPFTable getStyleID

List of usage examples for org.apache.poi.xwpf.usermodel XWPFTable getStyleID

Introduction

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

Prototype

public String getStyleID() 

Source Link

Document

get the StyleID of the table

Usage

From source file:fr.opensagres.poi.xwpf.converter.core.styles.AbstractValueProvider.java

License:Open Source License

public Value getValueFromStyles(XWPFElement element, XWPFStylesDocument stylesDocument) {

    // 1) At first get from cache or compute the default value
    String key = getKey(element, stylesDocument, null, null);
    // search from the cache
    Object defaultValue = stylesDocument.getValue(key);
    if (defaultValue == null) {
        // compute the default value and cache it
        defaultValue = getDefaultValue(element, stylesDocument);
        if (defaultValue == null) {
            defaultValue = XWPFStylesDocument.EMPTY_VALUE;
        }/*  www .j a v  a 2 s  .c o m*/
        updateValueCache(stylesDocument, key, defaultValue);
    }

    // 2) Search value from the linked style
    Object result = getValueFromStyleIds(element, stylesDocument, defaultValue);
    if (result != null) {
        return getValueOrNull(result);
    }
    // 3) Search if the XWPF element (paragraph or run) belongs to table which is styled.
    XWPFTableCell cell = getParentTableCell(element);
    if (cell != null) {
        XWPFTable table = cell.getTableRow().getTable();
        String tableStyleID = table.getStyleID();
        if (StringUtils.isNotEmpty(tableStyleID)) {
            // the current XWPFElement paragraph, run, etc belongs to a cell of a table which is styled.

            // 1) search styles from <w:style w:type="table" w:styleId="XXX"><w:tblStylePr
            // w:type="firstRow">, <w:tblStylePr w:type="lastRow">, etc
            TableCellInfo cellInfo = stylesDocument.getTableCellInfo(cell);
            result = getValueFromTableStyleId(element, stylesDocument, tableStyleID, cellInfo);
            if (result != null) {
                return getValueOrNull(result);
            }
            // no styles founded, search from the <w:style w:type="table" w:styleId="XXXX">
            result = getValueFromStyleId(element, stylesDocument, tableStyleID, defaultValue);
            if (result != null) {
                return getValueOrNull(result);
            }
        }
    }
    updateValueCache(stylesDocument, key, defaultValue);
    return getValueOrNull(defaultValue);
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.table.AbstractTableValueProvider.java

License:Open Source License

@Override
protected String[] getStyleID(XWPFTable table) {
    return new String[] { table.getStyleID() };
}

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

License:Open Source License

@Override
protected Object startVisitTable(XWPFTable table, float[] colWidths, Object tableContainer) throws Exception {
    // 1) create attributes
    // 1.1) Create class attributes.
    AttributesImpl attributes = createClassAttribute(table.getStyleID());

    // 1.2) Create "style" attributes.
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    CSSStyle cssStyle = getStylesDocument().createCSSStyle(tblPr);
    if (cssStyle != null) {
        cssStyle.addProperty(CSSStylePropertyConstants.BORDER_COLLAPSE,
                CSSStylePropertyConstants.BORDER_COLLAPSE_COLLAPSE);
    }/*from  w  ww .  j a va2 s .  c  o  m*/
    attributes = createStyleAttribute(cssStyle, attributes);

    // 2) create element
    startElement(TABLE_ELEMENT, attributes);
    return null;
}

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

License:Open Source License

@Override
protected void startVisitTableRow(XWPFTableRow row, Object tableContainer, int rowIndex, boolean headerRow)
        throws Exception {

    // 1) create attributes
    // Create class attributes.
    XWPFTable table = row.getTable();
    AttributesImpl attributes = createClassAttribute(table.getStyleID());

    // 2) create element
    if (headerRow) {
        startElement(TH_ELEMENT, attributes);
    } else {//  www. jav  a 2s .com
        startElement(TR_ELEMENT, attributes);
    }
}

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

License:Open Source License

@Override
protected Object startVisitTableCell(XWPFTableCell cell, Object tableContainer, boolean firstRow,
        boolean lastRow, boolean firstCell, boolean lastCell, List<XWPFTableCell> vMergeCells)
        throws Exception {
    // 1) create attributes
    // 1.1) Create class attributes.
    XWPFTableRow row = cell.getTableRow();
    XWPFTable table = row.getTable();
    AttributesImpl attributes = createClassAttribute(table.getStyleID());

    // 1.2) Create "style" attributes.
    CTTcPr tcPr = cell.getCTTc().getTcPr();
    CSSStyle cssStyle = getStylesDocument().createCSSStyle(tcPr);
    //At lease support solid borders for now
    if (cssStyle != null) {
        TableCellBorder border = getStylesDocument().getTableBorder(table, BorderSide.TOP);
        if (border != null) {
            String style = border.getBorderSize() + "px solid "
                    + StringUtils.toHexString(border.getBorderColor());
            cssStyle.addProperty(CSSStylePropertyConstants.BORDER_TOP, style);
        }/*ww  w.j  a  v a 2s. c  o  m*/

        border = getStylesDocument().getTableBorder(table, BorderSide.BOTTOM);
        if (border != null) {
            String style = border.getBorderSize() + "px solid "
                    + StringUtils.toHexString(border.getBorderColor());
            cssStyle.addProperty(CSSStylePropertyConstants.BORDER_BOTTOM, style);
        }

        border = getStylesDocument().getTableBorder(table, BorderSide.LEFT);
        if (border != null) {
            String style = border.getBorderSize() + "px solid "
                    + StringUtils.toHexString(border.getBorderColor());
            cssStyle.addProperty(CSSStylePropertyConstants.BORDER_LEFT, style);
        }

        border = getStylesDocument().getTableBorder(table, BorderSide.RIGHT);
        if (border != null) {
            String style = border.getBorderSize() + "px solid "
                    + StringUtils.toHexString(border.getBorderColor());
            cssStyle.addProperty(CSSStylePropertyConstants.BORDER_RIGHT, style);
        }
    }
    attributes = createStyleAttribute(cssStyle, attributes);

    // colspan attribute
    BigInteger gridSpan = stylesDocument.getTableCellGridSpan(cell);
    if (gridSpan != null) {
        attributes = SAXHelper.addAttrValue(attributes, COLSPAN_ATTR, gridSpan.intValue());
    }

    if (vMergeCells != null) {
        attributes = SAXHelper.addAttrValue(attributes, ROWSPAN_ATTR, vMergeCells.size());
    }

    // 2) create element
    startElement(TD_ELEMENT, attributes);

    return null;
}

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

License:Open Source License

/**
 * Copy Table Style./*  www .  j a v  a2  s  .  co m*/
 * 
 * @param inputTable
 *            input Table
 * @param outputDoc
 *            outputDoc where copy style
 * @throws IOException
 *             if the copy fails
 */
private static void copyTableStyle(XWPFTable inputTable, XWPFDocument outputDoc) throws IOException {
    try (XWPFDocument inputDoc = inputTable.getBody().getXWPFDocument();) {
        XWPFStyle style = inputDoc.getStyles().getStyle(inputTable.getStyleID());
        if (outputDoc == null || style == null) {
            return;
        }

        if (outputDoc.getStyles() == null) {
            outputDoc.createStyles();
        }

        List<XWPFStyle> usedStyleList = inputDoc.getStyles().getUsedStyleList(style);
        for (XWPFStyle xwpfStyle : usedStyleList) {
            outputDoc.getStyles().addStyle(xwpfStyle);
        }
    }
}