/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.xwpf.usermodel; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import java.util.EnumMap; import java.util.HashMap; import java.util.List; import java.util.function.Consumer; import java.util.function.Function; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.util.Internal; import org.apache.poi.util.NotImplemented; import org.apache.poi.util.Removal; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblCellMar; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; /** *
Sketch of XWPFTable class. Only table's text is being hold.
*Specifies the contents of a table present in the document. A table is a set * of paragraphs (and other block-level content) arranged in rows and columns.
*/ @SuppressWarnings("WeakerAccess") public class XWPFTable implements IBodyElement, ISDTContents { public static final String REGEX_PERCENTAGE = "[0-9]+(\\.[0-9]+)?%"; public static final String DEFAULT_PERCENTAGE_WIDTH = "100%"; static final String NS_OOXML_WP_MAIN = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; public static final String REGEX_WIDTH_VALUE = "auto|[0-9]+|" + REGEX_PERCENTAGE; // Create a map from this XWPF-level enum to the STBorder.Enum values public enum XWPFBorderType { NIL, NONE, SINGLE, THICK, DOUBLE, DOTTED, DASHED, DOT_DASH, DOT_DOT_DASH, TRIPLE, THIN_THICK_SMALL_GAP, THICK_THIN_SMALL_GAP, THIN_THICK_THIN_SMALL_GAP, THIN_THICK_MEDIUM_GAP, THICK_THIN_MEDIUM_GAP, THIN_THICK_THIN_MEDIUM_GAP, THIN_THICK_LARGE_GAP, THICK_THIN_LARGE_GAP, THIN_THICK_THIN_LARGE_GAP, WAVE, DOUBLE_WAVE, DASH_SMALL_GAP, DASH_DOT_STROKED, THREE_D_EMBOSS, THREE_D_ENGRAVE, OUTSET, INSET } private enum Border { INSIDE_V, INSIDE_H, LEFT, TOP, BOTTOM, RIGHT } private static final EnumMap* To get all text within a table, see XWPFWordExtractor's appendTableText * as an example. * * @return text */ public String getText() { return text.toString(); } /** * This method has existed since 2008 without an implementation. * It will be removed unless an implementation is provided. * @deprecated 4.0.0 due to lack of implementation. */ @Deprecated @Removal @NotImplemented public void addNewRowBetween(int start, int end) { throw new UnsupportedOperationException("XWPFTable#addNewRowBetween(int, int) not implemented"); } /** * add a new column for each row in this table */ public void addNewCol() { if (ctTbl.sizeOfTrArray() == 0) { createRow(); } for (int i = 0; i < ctTbl.sizeOfTrArray(); i++) { XWPFTableRow tabRow = new XWPFTableRow(ctTbl.getTrArray(i), this); tabRow.createCell(); } } /** * create a new XWPFTableRow object with as many cells as the number of columns defined in that moment * * @return tableRow */ public XWPFTableRow createRow() { int sizeCol = ctTbl.sizeOfTrArray() > 0 ? ctTbl.getTrArray(0) .sizeOfTcArray() : 0; XWPFTableRow tabRow = new XWPFTableRow(ctTbl.addNewTr(), this); addColumn(tabRow, sizeCol); tableRows.add(tabRow); return tabRow; } /** * @param pos - index of the row * @return the row at the position specified or null if no rows is defined or if the position is greather than the max size of rows array */ public XWPFTableRow getRow(int pos) { if (pos >= 0 && pos < ctTbl.sizeOfTrArray()) { //return new XWPFTableRow(ctTbl.getTrArray(pos)); return getRows().get(pos); } return null; } /** * Get the width value as an integer. *
If the width type is AUTO, DXA, or NIL, the value is 20ths of a point. If * the width type is PCT, the value is the percentage times 50 (e.g., 2500 for 50%).
* @return width value as an integer */ public int getWidth() { CTTblPr tblPr = getTblPr(); return tblPr.isSetTblW() ? tblPr.getTblW().getW().intValue() : -1; } /** * Set the width in 20ths of a point (twips). * @param width Width value (20ths of a point) */ public void setWidth(int width) { CTTblPr tblPr = getTblPr(); CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); tblWidth.setW(new BigInteger(Integer.toString(width))); tblWidth.setType(STTblWidth.DXA); } /** * @return number of rows in table */ public int getNumberOfRows() { return ctTbl.sizeOfTrArray(); } /** * Returns CTTblPr object for table. Creates it if it does not exist. */ private CTTblPr getTblPr() { return getTblPr(true); } /** * Returns CTTblPr object for table. If force parameter is true, will * create the element if necessary. If force parameter is false, returns * null when CTTblPr element is missing. * * @param force - force creation of CTTblPr element if necessary */ private CTTblPr getTblPr(boolean force) { return (ctTbl.getTblPr() != null) ? ctTbl.getTblPr() : (force ? ctTbl.addNewTblPr() : null); } /** * Return CTTblBorders object for table. If force parameter is true, will * create the element if necessary. If force parameter is false, returns * null when CTTblBorders element is missing. * * @param force - force creation of CTTblBorders element if necessary */ private CTTblBorders getTblBorders(boolean force) { CTTblPr tblPr = getTblPr(force); return tblPr == null ? null : tblPr.isSetTblBorders() ? tblPr.getTblBorders() : force ? tblPr.addNewTblBorders() : null; } /** * Return CTBorder object for given border. If force parameter is true, * will create the element if necessary. If force parameter is false, returns * null when the border element is missing. * * @param force - force creation of border if necessary. */ private CTBorder getTblBorder(boolean force, Border border) { final FunctionIf the width type is DXA or AUTO, then the value will always have * a fractional part of zero (because these values are really integers). * If the with type is percentage, then value may have a non-zero fractional * part. * * @return Width value as a double-precision decimal. * @since 4.0.0 */ public double getWidthDecimal() { return getWidthDecimal(getTblPr().getTblW()); } /** * Get the width as a decimal value. *
This method is also used by table cells. * @param ctWidth Width value to evaluate. * @return Width value as a decimal * @since 4.0.0 */ protected static double getWidthDecimal(CTTblWidth ctWidth) { double result = 0.0; STTblWidth.Enum typeValue = ctWidth.getType(); if (typeValue == STTblWidth.DXA || typeValue == STTblWidth.AUTO || typeValue == STTblWidth.NIL) { result = 0.0 + ctWidth.getW().intValue(); } else if (typeValue == STTblWidth.PCT) { // Percentage values are stored as integers that are 50 times // percentage. result = ctWidth.getW().intValue() / 50.0; } else { // Should never get here } return result; } /** * Get the width type for the table, as an {@link STTblWidth.Enum} value. * A table width can be specified as an absolute measurement (an integer * number of twips), a percentage, or the value "AUTO". * * @return The width type. * @since 4.0.0 */ public TableWidthType getWidthType() { return getWidthType(getTblPr().getTblW()); } /** * Get the width type from the width value * @param ctWidth CTTblWidth to evalute * @return The table width type * @since 4.0.0 */ protected static TableWidthType getWidthType(CTTblWidth ctWidth) { STTblWidth.Enum typeValue = ctWidth.getType(); if (typeValue == null) { typeValue = STTblWidth.NIL; ctWidth.setType(typeValue); } switch (typeValue.intValue()) { case STTblWidth.INT_NIL: return TableWidthType.NIL; case STTblWidth.INT_AUTO: return TableWidthType.AUTO; case STTblWidth.INT_DXA: return TableWidthType.DXA; case STTblWidth.INT_PCT: return TableWidthType.PCT; default: // Should never get here return TableWidthType.AUTO; } } /** * Set the width to the value "auto", an integer value (20ths of a point), or a percentage ("nn.nn%"). * * @param widthValue String matching one of "auto", [0-9]+, or [0-9]+(\.[0-9]+)%. * @since 4.0.0 */ public void setWidth(String widthValue) { setWidthValue(widthValue, getTblPr().getTblW()); } /** * Set the width value from a string * @param widthValue The width value string * @param ctWidth CTTblWidth to set the value on. */ protected static void setWidthValue(String widthValue, CTTblWidth ctWidth) { if (!widthValue.matches(REGEX_WIDTH_VALUE)) { throw new RuntimeException("Table width value \"" + widthValue + "\" " + "must match regular expression \"" + REGEX_WIDTH_VALUE + "\"."); } if (widthValue.matches("auto")) { ctWidth.setType(STTblWidth.AUTO); ctWidth.setW(BigInteger.ZERO); } else if (widthValue.matches(REGEX_PERCENTAGE)) { setWidthPercentage(ctWidth, widthValue); } else { // Must be an integer ctWidth.setW(new BigInteger(widthValue)); ctWidth.setType(STTblWidth.DXA); } } /** * Set the underlying table width value to a percentage value. * @param ctWidth The CTTblWidth to set the value on * @param widthValue String width value in form "33.3%" or an integer that is 50 times desired percentage value (e.g, * 2500 for 50%) * @since 4.0.0 */ protected static void setWidthPercentage(CTTblWidth ctWidth, String widthValue) { ctWidth.setType(STTblWidth.PCT); if (widthValue.matches(REGEX_PERCENTAGE)) { String numberPart = widthValue.substring(0, widthValue.length() - 1); double percentage = Double.parseDouble(numberPart) * 50; long intValue = Math.round(percentage); ctWidth.setW(BigInteger.valueOf(intValue)); } else if (widthValue.matches("[0-9]+")) { ctWidth.setW(new BigInteger(widthValue)); } else { throw new RuntimeException("setWidthPercentage(): Width value must be a percentage (\"33.3%\" or an integer, was \"" + widthValue + "\""); } } /** * Set the width value type for the table. *
If the width type is changed from the current type and the currently-set value * is not consistent with the new width type, the value is reset to the default value * for the specified width type.
* * @param widthType Width type * @since 4.0.0 */ public void setWidthType(TableWidthType widthType) { setWidthType(widthType, getTblPr().getTblW()); } /** * Set the width type if different from current width type * @param widthType The new width type * @param ctWidth CTTblWidth to set the type on * @since 4.0.0 */ protected static void setWidthType(TableWidthType widthType, CTTblWidth ctWidth) { TableWidthType currentType = getWidthType(ctWidth); if (!currentType.equals(widthType)) { STTblWidth.Enum stWidthType = widthType.getStWidthType(); ctWidth.setType(stWidthType); switch (stWidthType.intValue()) { case STTblWidth.INT_PCT: setWidthPercentage(ctWidth, DEFAULT_PERCENTAGE_WIDTH); break; default: ctWidth.setW(BigInteger.ZERO); } } } }