Table extends Component { public Row appendRow() { Node childNode = mTableElement.getLastChild(); Node positionNode = null; TableTableRowElement refRowElement = null; TableTableRowElement newRow = null; if (childNode instanceof TableNamedExpressionsElement) { childNode = childNode.getPreviousSibling(); positionNode = childNode; } if (childNode instanceof TableTableRowElement) { refRowElement = (TableTableRowElement) childNode; } int columnCount = getColumnCount(); if (refRowElement == null) { newRow = createDefaultRow(columnCount, true); mTableElement.appendChild(newRow); } else { newRow = (TableTableRowElement) OdfXMLFactory.newOdfElement((OdfFileDom) mTableElement.getOwnerDocument(), OdfName.newName(OdfDocumentNamespace.TABLE, "table-row")); TableTableCellElementBase cellElement = (TableTableCellElementBase) refRowElement.getFirstChild(); int i = 1; while (cellElement != null && i <= columnCount) { String tableNameSpace = OdfDocumentNamespace.TABLE.getUri(); if (cellElement instanceof TableCoveredTableCellElement) { TableCoveredTableCellElement coveredCellEle = (TableCoveredTableCellElement) cellElement; TableTableRowElement aRowEle = (TableTableRowElement) (coveredCellEle.getParentNode() .getPreviousSibling()); while (aRowEle != null) { TableTableCellElementBase coverCellEle = (TableTableCellElementBase) (aRowEle.getFirstChild()); int j = coverCellEle.getTableNumberColumnsRepeatedAttribute(); while (j < i) { coverCellEle = (TableTableCellElementBase) (coverCellEle.getNextSibling()); if (coverCellEle instanceof TableTableCellElement) { j += (coverCellEle.getTableNumberColumnsRepeatedAttribute() * (((TableTableCellElement) coverCellEle) .getTableNumberColumnsSpannedAttribute())); } else { j += coverCellEle.getTableNumberColumnsRepeatedAttribute(); } } if (coverCellEle instanceof TableTableCellElement) { TableTableCellElement newCellEle = (TableTableCellElement) (coverCellEle.cloneNode(true)); cleanCell(newCellEle); newCellEle.removeAttributeNS(tableNameSpace, "number-rows-spanned"); newRow.appendChild(newCellEle); if (mIsSpreadsheet) { int columnsSpannedNumber = newCellEle.getTableNumberColumnsSpannedAttribute(); newCellEle.removeAttributeNS(tableNameSpace, "number-columns-spanned"); int newColumnRepeatedNumber = newCellEle.getTableNumberColumnsRepeatedAttribute() * columnsSpannedNumber; if (newColumnRepeatedNumber > 1) { newCellEle.setTableNumberColumnsRepeatedAttribute(newColumnRepeatedNumber); } else { newCellEle.removeAttributeNS(tableNameSpace, "number-columns-repeated"); } int tempi = newColumnRepeatedNumber; while (tempi > 0) { int iColumnRepeatedNumber = cellElement.getTableNumberColumnsRepeatedAttribute(); if (iColumnRepeatedNumber > tempi) { if (cellElement instanceof TableCoveredTableCellElement) { cellElement.setTableNumberColumnsRepeatedAttribute(iColumnRepeatedNumber - tempi); TableTableCellElementBase newCoveredCellEle = (TableTableCellElementBase) cellElement .cloneNode(true); cleanCell(newCoveredCellEle); if (tempi > 1) { newCoveredCellEle.setTableNumberColumnsRepeatedAttribute(tempi); } else { newCoveredCellEle.removeAttributeNS( OdfDocumentNamespace.TABLE.getUri(), "number-columns-repeated"); } refRowElement.insertBefore(newCoveredCellEle, cellElement); cellElement = newCoveredCellEle; } } tempi = tempi - cellElement.getTableNumberColumnsRepeatedAttribute(); i = i + cellElement.getTableNumberColumnsRepeatedAttribute(); if (!(cellElement instanceof TableCoveredTableCellElement) && (tempi > 0)){ Logger.getLogger(Table.class.getName()).log(Level.FINE, "Not covered cell was ignored"); } cellElement = (TableTableCellElementBase) (cellElement.getNextSibling()); } } else { cellElement = (TableTableCellElementBase) cellElement.getNextSibling(); i += cellElement.getTableNumberColumnsRepeatedAttribute(); int newColumnSpanNumber = newCellEle.getTableNumberColumnsSpannedAttribute(); while ((cellElement != null) && (cellElement instanceof TableCoveredTableCellElement) && (newColumnSpanNumber > 1)) { TableCoveredTableCellElement newCoveredCellElement = (TableCoveredTableCellElement) cellElement .cloneNode(true); cleanCell(newCoveredCellElement); newRow.appendChild(newCoveredCellElement); i += cellElement.getTableNumberColumnsRepeatedAttribute(); cellElement = (TableTableCellElementBase) cellElement.getNextSibling(); newColumnSpanNumber--; } } break; } Node preNode = aRowEle.getPreviousSibling(); if (preNode instanceof TableTableRowElement) { aRowEle = (TableTableRowElement) preNode; } else { aRowEle = (TableTableRowElement) (preNode.getLastChild()); } } } else { TableTableCellElement newCellEle = (TableTableCellElement) cellElement.cloneNode(true); cleanCell(newCellEle); newRow.appendChild(newCellEle); Integer tableNumberColumnsRepeated = newCellEle.getTableNumberColumnsRepeatedAttribute(); Integer tableNumberColumnsSpanned = newCellEle.getTableNumberColumnsSpannedAttribute(); i += tableNumberColumnsRepeated * tableNumberColumnsSpanned; cellElement = (TableTableCellElementBase) cellElement.getNextSibling(); if (tableNumberColumnsSpanned > 1) { int j = 1; if (mIsSpreadsheet) { newCellEle.removeAttributeNS(tableNameSpace, "number-columns-spanned"); int newColumnRepeatedNumber = tableNumberColumnsRepeated * tableNumberColumnsSpanned; if (newColumnRepeatedNumber > 1) { newCellEle.setTableNumberColumnsRepeatedAttribute(newColumnRepeatedNumber); } else { newCellEle.removeAttributeNS(tableNameSpace, "number-columns-repeated"); } int tempi = newColumnRepeatedNumber; while (tempi > 1) { int iColumnRepeatedNumber = cellElement.getTableNumberColumnsRepeatedAttribute(); if (iColumnRepeatedNumber > tempi + 1) { if (cellElement instanceof TableCoveredTableCellElement) { cellElement.setTableNumberColumnsRepeatedAttribute(iColumnRepeatedNumber - tempi + 1); TableTableCellElementBase newCoveredCellEle = (TableTableCellElementBase) cellElement .cloneNode(true); cleanCell(newCoveredCellEle); newCoveredCellEle.setTableNumberColumnsRepeatedAttribute(tempi - 1); refRowElement.insertBefore(newCoveredCellEle, cellElement); cellElement = newCoveredCellEle; } } tempi = tempi - cellElement.getTableNumberColumnsRepeatedAttribute(); if (!(cellElement instanceof TableCoveredTableCellElement) && (tempi > 1)){ Logger.getLogger(Table.class.getName()).log(Level.FINE, "Not covered cell was ignored"); } cellElement = (TableTableCellElementBase) (cellElement.getNextSibling()); } } else { while ((j < tableNumberColumnsSpanned) && (cellElement != null)) { int iColumnRepeatedNumber = cellElement.getTableNumberColumnsRepeatedAttribute(); if (iColumnRepeatedNumber > tableNumberColumnsSpanned - j) { if (cellElement instanceof TableCoveredTableCellElement) { cellElement.setTableNumberColumnsRepeatedAttribute(iColumnRepeatedNumber - tableNumberColumnsSpanned + j); TableTableCellElementBase newCoveredCellEle = (TableTableCellElementBase) cellElement .cloneNode(true); cleanCell(newCoveredCellEle); newCoveredCellEle .setTableNumberColumnsRepeatedAttribute(tableNumberColumnsSpanned - j); refRowElement.insertBefore(newCoveredCellEle, cellElement); cellElement = newCoveredCellEle; } } TableTableCellElementBase newCoveredCellEle = (TableTableCellElementBase) cellElement .cloneNode(true); cleanCell(newCoveredCellEle); newRow.appendChild(newCoveredCellEle); j += newCoveredCellEle.getTableNumberColumnsRepeatedAttribute(); cellElement = (TableTableCellElementBase) cellElement.getNextSibling(); } } } } } if (positionNode == null) { mTableElement.appendChild(newRow); } else { mTableElement.insertBefore(newRow, positionNode); } } return getRowInstance(newRow, 0); } private  Table(TableContainer container, TableTableElement table); static Table getInstance(TableTableElement element); static Table newTable(TableContainer tableContainer); static Table newTable(TableContainer tableContainer, int numRows, int numCols); static Table newTable(TableContainer tableContainer, int numRows, int numCols, 
			double marginLeft, double marginRight); static Table newTable(TableContainer tableContainer, int numRows, int numCols, int headerRowNumber,
			int headerColumnNumber); static Table newTable(TableContainer tableContainer, String[] rowLabel, String[] columnLabel, double[][] data); static Table newTable(TableContainer tableContainer, String[] rowLabel, String[] columnLabel, String[][] data); double getWidth(); void setWidth(double width); void applyStyle(TableTemplate template); int getRowCount(); int getColumnCount(); Row appendRow(); List<Row> appendRows(int rowCount); Column appendColumn(); List<Column> appendColumns(int columnCount); TableTableElement getOdfElement(); List<Column> insertColumnsBefore(int index, int columnCount); void removeColumnsByIndex(int startIndex, int deleteColCount); List<Row> insertRowsBefore(int index, int rowCount); List<Column> getColumnList(); Iterator<Column> getColumnIterator(); List<Row> getRowList(); Iterator<Row> getRowIterator(); Column getColumnByIndex(int index); Row getRowByIndex(int index); void removeRowsByIndex(int startIndex, int deleteRowCount); void remove(); int getHeaderRowCount(); int getHeaderColumnCount(); String getTableName(); void setTableName(String tableName); boolean isProtected(); void setProtected(boolean isProtected); boolean isCellStyleInheritance(); void setCellStyleInheritance(boolean isEnabled); boolean isUseRepeat(); void setUseRepeat(boolean isSingle); CellRange getCellRangeByPosition(int startCol, int startRow, int endCol, int endRow); CellRange getCellRangeByPosition(String startAddress, String endAddress); CellRange getCellRangeByName(String name); Cell getCellByPosition(int colIndex, int rowIndex); Cell getCellByPosition(String address); void setVerticalMargin(double spaceTop, double spaceBottom); DefaultStyleHandler getStyleHandler();  }
@Test public void testAppendRow() { OdfFileDom dom; try { SpreadsheetDocument odsDoc = SpreadsheetDocument.loadDocument(ResourceUtilities .getTestResourceAsStream("TestODSAppendRow.ods")); dom = odsDoc.getContentDom(); NodeList tablelist = dom.getElementsByTagNameNS(OdfDocumentNamespace.TABLE.getUri(), "table"); for (int i = 0; i < tablelist.getLength(); i++) { mOdsTable = (TableTableElement) tablelist.item(i); testAppendRow(mOdsTable); } odsDoc.save(ResourceUtilities.newTestOutputFile("TestODSAppendRowOutput.ods")); TextDocument odtDoc = TextDocument.loadDocument(ResourceUtilities .getTestResourceAsStream("TestODTAppendRow.odt")); dom = odtDoc.getContentDom(); tablelist = dom.getElementsByTagNameNS(OdfDocumentNamespace.TABLE.getUri(), "table"); for (int i = 0; i < tablelist.getLength(); i++) { mOdtTable = (TableTableElement) tablelist.item(i); testAppendRow(mOdtTable); } odtDoc.save(ResourceUtilities.newTestOutputFile("TestODTAppendRowOutput.odt")); } catch (Exception e) { Logger.getLogger(TableTest.class.getName()).log(Level.SEVERE, null, e); Assert.fail(e.getMessage()); } }