}
private void updateTableCells(OdfDocument odfdoc, String tablename,
double[] values, String[] labels) {
int i = 0, j = 0;
TableTableRowElement td;
TableTableCellElement cell;
OdfFileDom dom;
NodeList tableList;
TableTableElement myTable;
NodeList lst;
OdfTextParagraph p;
OfficeSpreadsheetElement spreadsheet = null;
try {
dom = odfdoc.getContentDom();
tableList = dom.getElementsByTagNameNS(
OdfDocumentNamespace.TABLE.getUri(), "table");
spreadsheet = (OfficeSpreadsheetElement) dom.getElementsByTagNameNS(
OdfDocumentNamespace.OFFICE.getUri(), "spreadsheet").item(0);
i = 0;
if (tableList.getLength() > 0) {
for (; i < tableList.getLength(); i++) {
String currentname = ((TableTableElement) tableList.item(i)).getTableNameAttribute();
if (currentname == null) {
currentname = "";
}
if (currentname.equalsIgnoreCase(tablename)) {
break;
}
}
}
if (i < tableList.getLength()) //table with the specific table name is found
{
myTable = (TableTableElement) tableList.item(i);
} else { //table with the specific table name is not found. Create table
myTable = dom.newOdfElement(TableTableElement.class);
myTable.setTableNameAttribute(tablename);
spreadsheet.appendChild(myTable);
}
lst = myTable.getElementsByTagNameNS(OdfDocumentNamespace.TABLE.getUri(), "table-row");
if (lst.getLength() == 0) { //the first table row is not existed. Create table row
td = dom.newOdfElement(TableTableRowElement.class);
cell = dom.newOdfElement(TableTableCellElement.class);
p = dom.newOdfElement(OdfTextParagraph.class);
if (tablename.startsWith("Memory")) {
p.setTextContent("memory(b)");
} else {
p.setTextContent("time(ms)");
}
td.appendChild(cell);
cell.appendChild(p);
myTable.appendChild(td);
} else {
td = (TableTableRowElement) lst.item(0); //the first table row is existed.
}
cell = dom.newOdfElement(TableTableCellElement.class);
td.appendChild(cell);
p = dom.newOdfElement(OdfTextParagraph.class);
p.setTextContent(testTag);
cell.appendChild(p);
for (i = 1; i < values.length + 1; i++) {
if (i < lst.getLength()) { //table row is existed
td = (TableTableRowElement) lst.item(i);
} else { //table row is not existed.
td = dom.newOdfElement(TableTableRowElement.class);
myTable.appendChild(td);
//append first cell with labels
cell = dom.newOdfElement(TableTableCellElement.class);
td.appendChild(cell);
p = dom.newOdfElement(OdfTextParagraph.class);
p.setTextContent(labels[j]);
cell.appendChild(p);
}
cell = dom.newOdfElement(TableTableCellElement.class);
cell.setOfficeValueTypeAttribute("float");
cell.setOfficeValueAttribute(new Double(values[j]));
p = dom.newOdfElement(OdfTextParagraph.class);
p.setTextContent(values[j] + "");
cell.appendChild(p);
td.appendChild(cell);
j++;
}
} catch (Exception e) {
LOG.log(Level.SEVERE, null, e);
}