Table extends Component { private Row insertRowBefore(Row refRow, Row positionRow, int columnCount) { TableTableRowElement aRow = (TableTableRowElement) OdfXMLFactory.newOdfElement((OdfFileDom) mTableElement .getOwnerDocument(), OdfName.newName(OdfDocumentNamespace.TABLE, "table-row")); int coveredLength = 0, coveredHeigth = 0; for (int i = 0; i < columnCount;) { Cell refCell = refRow.getCellByIndex(i); int columnsRepeatedNumber = refCell.getColumnsRepeatedNumber(); if (!refCell.isCoveredElement()) { TableTableCellElement aCellEle = (TableTableCellElement) refCell.getOdfElement(); coveredHeigth = aCellEle.getTableNumberRowsSpannedAttribute(); if (coveredHeigth == 1) { TableTableCellElement newCellEle = (TableTableCellElement) aCellEle.cloneNode(true); cleanCell(newCellEle); aRow.appendChild(newCellEle); } else { aCellEle.setTableNumberRowsSpannedAttribute(coveredHeigth + 1); TableCoveredTableCellElement newCellEle = (TableCoveredTableCellElement) OdfXMLFactory .newOdfElement((OdfFileDom) mTableElement.getOwnerDocument(), OdfName.newName( OdfDocumentNamespace.TABLE, "covered-table-cell")); if (columnsRepeatedNumber > 1) { newCellEle.setTableNumberColumnsRepeatedAttribute(columnsRepeatedNumber); } else { newCellEle.removeAttributeNS(OdfDocumentNamespace.TABLE.getUri(), "number-columns-repeated"); } aRow.appendChild(newCellEle); } coveredLength = aCellEle.getTableNumberColumnsSpannedAttribute() - columnsRepeatedNumber; i = i + columnsRepeatedNumber; } else { TableCoveredTableCellElement aCellEle = (TableCoveredTableCellElement) refCell.getOdfElement(); if (coveredLength >= 1) { TableCoveredTableCellElement newCellEle = (TableCoveredTableCellElement) aCellEle.cloneNode(true); aRow.appendChild(newCellEle); coveredLength -= newCellEle.getTableNumberColumnsRepeatedAttribute(); } else { TableTableCellElement coveredCell = (TableTableCellElement) refCell.getCoverCell().getOdfElement(); TableTableCellElement newCellEle = (TableTableCellElement) coveredCell.cloneNode(true); cleanCell(newCellEle); newCellEle.removeAttributeNS(OdfDocumentNamespace.TABLE.getUri(), "number-rows-spanned"); aRow.appendChild(newCellEle); coveredLength = coveredCell.getTableNumberColumnsSpannedAttribute() - columnsRepeatedNumber; } i = i + columnsRepeatedNumber; } } if (positionRow == null) { mTableElement.appendChild(aRow); } else { mTableElement.insertBefore(aRow, positionRow.getOdfElement()); } return getRowInstance(aRow, 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 testInsertRowBefore() { mOdtDoc = loadODTDocument(mOdtTestFileName + ".odt"); Table table2 = mOdtDoc.getTableByName("Table2"); Row row = table2.getRowByIndex(0); int originalRowCount = table2.getRowCount(); List<Row> newRows = table2.insertRowsBefore(0, 2); Row newRow1 = table2.getRowByIndex(0); Row newRow2 = table2.getRowByIndex(0); Assert.assertEquals(newRow1.getCellCount(), newRows.get(0).getCellCount()); Assert.assertEquals(newRow2.getCellCount(), newRows.get(1).getCellCount()); Assert.assertEquals(row, table2.getRowByIndex(2)); saveodt(mOdtTestFileName + "Out.odt"); mOdtDoc = loadODTDocument(mOdtTestFileName + "Out.odt"); Table newTable = mOdtDoc.getTableByName("Table2"); Assert.assertEquals(originalRowCount + 2, newTable.getRowCount()); }