#ODFTOOLKIT-456# Patch by Siddharth Prasad - Missing support for getting active sheet in Spreadsheets
git-svn-id: https://svn.apache.org/repos/asf/incubator/odf/trunk@1799582 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/simple/src/main/java/org/odftoolkit/simple/SpreadsheetDocument.java b/simple/src/main/java/org/odftoolkit/simple/SpreadsheetDocument.java
index 550c3a2..8fb64de 100644
--- a/simple/src/main/java/org/odftoolkit/simple/SpreadsheetDocument.java
+++ b/simple/src/main/java/org/odftoolkit/simple/SpreadsheetDocument.java
@@ -31,6 +31,7 @@
import java.util.logging.Logger;
import javax.xml.xpath.XPathConstants;
import org.odftoolkit.odfdom.dom.OdfContentDom;
+import org.odftoolkit.odfdom.dom.OdfSettingsDom;
import org.odftoolkit.odfdom.dom.element.draw.DrawFrameElement;
import org.odftoolkit.odfdom.dom.element.office.OfficeSpreadsheetElement;
import org.odftoolkit.odfdom.dom.element.table.TableTableCellElement;
@@ -267,6 +268,32 @@
return getTableByName(name);
}
+
+ /**
+ * Retrieves the active sheet of this document.
+ *
+ * @return the active sheet of this document. If active sheet doesn't exist, it returns <code>null</code>.
+ */
+ public Table getActiveSheet() {
+ try {
+ OdfSettingsDom settingsDom = getSettingsDom();
+ String activeSheetName = (String) settingsDom.getXPath().evaluate(
+ "/office:document-settings/office:settings/config:config-item-set/config:config-item-map-indexed"
+ + "/config:config-item-map-entry/config:config-item[@config:name='ActiveTable']",
+ settingsDom,
+ XPathConstants.STRING
+ );
+ Table table = getTableByName(activeSheetName);
+ if(table == null){
+ table = getSheetByIndex(0);
+ }
+ return table;
+ } catch (Exception e) {
+ Logger.getLogger(SpreadsheetDocument.class.getName()).log(Level.SEVERE, null, e);
+ }
+ return null;
+ }
+
/**
* Adds a new blank sheet with the specified <code>name</code> to this
* document.
diff --git a/simple/src/test/java/org/odftoolkit/simple/SpreadsheetTest.java b/simple/src/test/java/org/odftoolkit/simple/SpreadsheetTest.java
index 4bec0ba..e327258 100644
--- a/simple/src/test/java/org/odftoolkit/simple/SpreadsheetTest.java
+++ b/simple/src/test/java/org/odftoolkit/simple/SpreadsheetTest.java
@@ -1,119 +1,131 @@
-/*
-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.odftoolkit.simple;
-
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import junit.framework.Assert;
-
-import org.junit.Test;
-import org.odftoolkit.simple.table.Table;
-import org.odftoolkit.simple.utils.ResourceUtilities;
-
-public class SpreadsheetTest {
-
- private final static String TEST_FILE_NAME = "TestSpreadsheetTable.ods";
-
- @Test
- public void testGetSheetCount() {
- try {
- SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
- .getTestResourceAsStream(TEST_FILE_NAME));
- Assert.assertEquals(3, document.getSheetCount());
- } catch (Exception e) {
- Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testGetSheetByIndexOrName() {
- try {
- SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
- .getTestResourceAsStream(TEST_FILE_NAME));
- Table table1 = document.getSheetByName("Sheet2");
- Table table2 = document.getSheetByIndex(1);
- Assert.assertEquals("Sheet2", table1.getTableName());
- Assert.assertEquals("Sheet2", table2.getTableName());
- } catch (Exception e) {
- Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testInsertSheet() {
- try {
- SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
- .getTestResourceAsStream(TEST_FILE_NAME));
- int oldCount = document.getSheetCount();
- Table table = document.insertSheet(0);
- Assert.assertFalse("Sheet1".equals(table.getTableName()));
- int newCount = document.getSheetCount();
- Assert.assertEquals(1, newCount - oldCount);
- table = document.insertSheet(document.getSheetByName("Sheet1"), 2);
- Assert.assertEquals(table.getTableName(), document.getSheetByIndex(2).getTableName());
- document.save(ResourceUtilities.newTestOutputFile("Output_"+TEST_FILE_NAME));
-
- //data table from difference document.
- document = SpreadsheetDocument.loadDocument(ResourceUtilities.getTestResourceAsStream(TEST_FILE_NAME));
- Table sheet1 = document.getSheetByName("Sheet1");
- SpreadsheetDocument document1 = SpreadsheetDocument.newSpreadsheetDocument();
- table = document1.insertSheet(sheet1, 0);
- Assert.assertEquals(sheet1.getCellByPosition("E3").getDisplayText(), table.getCellByPosition("E3").getDisplayText());
- document1.save(ResourceUtilities.newTestOutputFile("Output2_"+TEST_FILE_NAME));
- } catch (Exception e) {
- Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
- Assert.fail(e.getMessage());
- }
- }
-
- @Test
- public void testAppendAndRemoveSheet() {
- try {
- SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
- .getTestResourceAsStream(TEST_FILE_NAME));
- Table table = document.appendSheet("Sheet4");
- Assert.assertEquals("Sheet4", table.getTableName());
- table = document.appendSheet(document.getSheetByName("Sheet1"), "Sheet5");
- Assert.assertEquals("Sheet5", table.getTableName());
- document.save(ResourceUtilities.newTestOutputFile("Output_"+TEST_FILE_NAME));
-
- //reload
- document = SpreadsheetDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Output_"+TEST_FILE_NAME));
- document.removeSheet(4);
- Assert.assertNull(document.getSheetByName("Sheet5"));
- document.removeSheet(3);
- Assert.assertNull(document.getSheetByName("Sheet4"));
-
- //data table from difference document.
- document = SpreadsheetDocument.loadDocument(ResourceUtilities.getTestResourceAsStream(TEST_FILE_NAME));
- Table sheet1 = document.getSheetByName("Sheet1");
- SpreadsheetDocument document1 = SpreadsheetDocument.newSpreadsheetDocument();
- table = document1.appendSheet(sheet1, "SheetA");
- Assert.assertEquals(sheet1.getCellByPosition("E3").getDisplayText(), table.getCellByPosition("E3").getDisplayText());
- document1.save(ResourceUtilities.newTestOutputFile("Output2_"+TEST_FILE_NAME));
- } catch (Exception e) {
- Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
- Assert.fail(e.getMessage());
- }
- }
-}
+/*
+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.odftoolkit.simple;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import junit.framework.Assert;
+import org.junit.Test;
+import org.odftoolkit.simple.table.Table;
+import org.odftoolkit.simple.utils.ResourceUtilities;
+
+public class SpreadsheetTest {
+
+ private final static String TEST_FILE_NAME = "TestSpreadsheetTable.ods";
+
+ @Test
+ public void testGetSheetCount() {
+ try {
+ SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
+ .getTestResourceAsStream(TEST_FILE_NAME));
+ Assert.assertEquals(3, document.getSheetCount());
+ } catch (Exception e) {
+ Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
+ Assert.fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void testGetSheetByIndexOrName() {
+ try {
+ SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
+ .getTestResourceAsStream(TEST_FILE_NAME));
+ Table table1 = document.getSheetByName("Sheet2");
+ Table table2 = document.getSheetByIndex(1);
+ Assert.assertEquals("Sheet2", table1.getTableName());
+ Assert.assertEquals("Sheet2", table2.getTableName());
+ } catch (Exception e) {
+ Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
+ Assert.fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void testGetActiveSheet() {
+ try {
+ SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
+ .getTestResourceAsStream(TEST_FILE_NAME));
+ Table table = document.getActiveSheet();
+ Assert.assertEquals("Sheet2", table.getTableName());
+ } catch (Exception e) {
+ e.printStackTrace();
+ Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
+ Assert.fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void testInsertSheet() {
+ try {
+ SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
+ .getTestResourceAsStream(TEST_FILE_NAME));
+ int oldCount = document.getSheetCount();
+ Table table = document.insertSheet(0);
+ Assert.assertFalse("Sheet1".equals(table.getTableName()));
+ int newCount = document.getSheetCount();
+ Assert.assertEquals(1, newCount - oldCount);
+ table = document.insertSheet(document.getSheetByName("Sheet1"), 2);
+ Assert.assertEquals(table.getTableName(), document.getSheetByIndex(2).getTableName());
+ document.save(ResourceUtilities.newTestOutputFile("Output_"+TEST_FILE_NAME));
+
+ //data table from difference document.
+ document = SpreadsheetDocument.loadDocument(ResourceUtilities.getTestResourceAsStream(TEST_FILE_NAME));
+ Table sheet1 = document.getSheetByName("Sheet1");
+ SpreadsheetDocument document1 = SpreadsheetDocument.newSpreadsheetDocument();
+ table = document1.insertSheet(sheet1, 0);
+ Assert.assertEquals(sheet1.getCellByPosition("E3").getDisplayText(), table.getCellByPosition("E3").getDisplayText());
+ document1.save(ResourceUtilities.newTestOutputFile("Output2_"+TEST_FILE_NAME));
+ } catch (Exception e) {
+ Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
+ Assert.fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void testAppendAndRemoveSheet() {
+ try {
+ SpreadsheetDocument document = SpreadsheetDocument.loadDocument(ResourceUtilities
+ .getTestResourceAsStream(TEST_FILE_NAME));
+ Table table = document.appendSheet("Sheet4");
+ Assert.assertEquals("Sheet4", table.getTableName());
+ table = document.appendSheet(document.getSheetByName("Sheet1"), "Sheet5");
+ Assert.assertEquals("Sheet5", table.getTableName());
+ document.save(ResourceUtilities.newTestOutputFile("Output_"+TEST_FILE_NAME));
+
+ //reload
+ document = SpreadsheetDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Output_"+TEST_FILE_NAME));
+ document.removeSheet(4);
+ Assert.assertNull(document.getSheetByName("Sheet5"));
+ document.removeSheet(3);
+ Assert.assertNull(document.getSheetByName("Sheet4"));
+
+ //data table from difference document.
+ document = SpreadsheetDocument.loadDocument(ResourceUtilities.getTestResourceAsStream(TEST_FILE_NAME));
+ Table sheet1 = document.getSheetByName("Sheet1");
+ SpreadsheetDocument document1 = SpreadsheetDocument.newSpreadsheetDocument();
+ table = document1.appendSheet(sheet1, "SheetA");
+ Assert.assertEquals(sheet1.getCellByPosition("E3").getDisplayText(), table.getCellByPosition("E3").getDisplayText());
+ document1.save(ResourceUtilities.newTestOutputFile("Output2_"+TEST_FILE_NAME));
+ } catch (Exception e) {
+ Logger.getLogger(SpreadsheetTest.class.getName()).log(Level.SEVERE, null, e);
+ Assert.fail(e.getMessage());
+ }
+ }
+}
diff --git a/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java b/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
index 58b2ea2..c83d9ac 100644
--- a/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
+++ b/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
@@ -1,4 +1,4 @@
-/*
+/*
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
@@ -29,9 +29,7 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
-
import junit.framework.Assert;
-
import org.junit.Before;
import org.junit.Test;
import org.odftoolkit.odfdom.dom.OdfContentDom;
@@ -44,6 +42,7 @@
import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeAutomaticStyles;
import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
import org.odftoolkit.odfdom.type.Color;
+import org.odftoolkit.simple.Document;
import org.odftoolkit.simple.SpreadsheetDocument;
import org.odftoolkit.simple.TextDocument;
import org.odftoolkit.simple.style.Border;
@@ -52,6 +51,7 @@
import org.odftoolkit.simple.style.StyleTypeDefinitions.CellBordersType;
import org.odftoolkit.simple.style.StyleTypeDefinitions.FontStyle;
import org.odftoolkit.simple.style.StyleTypeDefinitions.HorizontalAlignmentType;
+import org.odftoolkit.simple.style.StyleTypeDefinitions.SupportedLinearMeasure;
import org.odftoolkit.simple.style.StyleTypeDefinitions.TextLinePosition;
import org.odftoolkit.simple.style.StyleTypeDefinitions.VerticalAlignmentType;
import org.odftoolkit.simple.text.Paragraph;
@@ -63,8 +63,6 @@
import org.odftoolkit.simple.utils.ResourceUtilities;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
-import org.odftoolkit.simple.Document;
-import org.odftoolkit.simple.style.StyleTypeDefinitions.SupportedLinearMeasure;
public class TableCellTest {
@@ -74,7 +72,7 @@
final static String SAMPLE_STYLE_SPREADSHEET = "TestSpreadsheetStyleTable";
final static String SAMPLE_TEXT = "TestTextTable";
static final String filename = "testGetCellAt.ods";
-
+
SpreadsheetDocument odsdoc, odsstyle;
TextDocument odtdoc;
Table odsTable, odtTable;
@@ -303,7 +301,7 @@
Cell dcell = table.getCellByPosition(3, 2);
format = dcell.getFormatString();
- Assert.assertEquals("MMM d, yy", format);
+ Assert.assertEquals("d. MMM yy", format);
dcell.setFormatString("yyyy-MM-dd");
displayvalue = dcell.getDisplayText();
@@ -581,7 +579,7 @@
fcell = table.getCellByPosition(columnindex, rowindex);
Assert.assertEquals(0, fcell.getDateValue().compareTo(expectedCalendar));
}
-
+
@Test
public void testDateTimeValue() {
int rowindex = 7, columnindex = 7;
@@ -603,7 +601,7 @@
fcell = table.getCellByPosition(columnindex, rowindex);
Assert.assertEquals(expectedCalendar, fcell.getDateTimeValue());
}
-
+
@Test
public void testGetFromEmptyDateValue() throws Exception {
@@ -612,7 +610,7 @@
cell.setValueType(OfficeValueTypeAttribute.Value.DATE.toString());
Assert.assertNull(cell.getDateValue());
}
-
+
@Test
public void testGetFromEmptyTimeValue() throws Exception {
Table table = odsdoc.getTableByName("Sheet1");
@@ -1349,10 +1347,10 @@
final SpreadsheetDocument doc = SpreadsheetDocument.newSpreadsheetDocument();
final Table table = doc.addTable();
final String contentStr = "This is a long text content.";
-
+
// basically the default font is of Arial type, so assume the default family
final String nfFamilyName = "Arial";
-
+
if (isFontAvailable(nfFamilyName)) {
checkCellWidth(table.getCellByPosition(0, 0), null, 44.0977, contentStr);
}
@@ -1476,8 +1474,8 @@
}
Assert.assertEquals(node, null);
}
-
-
+
+
@Test
public void testSetHorizontalAlignment() {
try {
@@ -1487,17 +1485,17 @@
String right = cell1.getHorizontalAlignment();
Assert.assertEquals(null, right);
/**
- *The parameter can be "center", "end", "justify", "left", "right", or
- * "start". Actually, "left" will be interpreted as "start", while "right"
- * will be interpreted as "end". If argument is null, the explicit
- * horizontal alignment setting is removed.
+ *The parameter can be "center", "end", "justify", "left", "right", or
+ * "start". Actually, "left" will be interpreted as "start", while "right"
+ * will be interpreted as "end". If argument is null, the explicit
+ * horizontal alignment setting is removed.
*/
cell1.setHorizontalAlignment("left");
-
+
//validate
String left = cell1.getHorizontalAlignment();
Assert.assertEquals("start", left);
-
+
//save
//odsdoc.save(ResourceUtilities.newTestOutputFile("testTableCell.ods"));
@@ -1505,10 +1503,10 @@
LOGGER.log(Level.SEVERE, e.getMessage(), e);
Assert.fail(e.getMessage());
}
-
+
}
-
-
+
+
@Test
public void testGetCountry() {
try {
@@ -1516,7 +1514,7 @@
borderbase.setLineStyle(StyleTypeDefinitions.LineType.DOUBLE);
SpreadsheetDocument doc = SpreadsheetDocument.newSpreadsheetDocument();
Table table = doc.getTableByName("Sheet1");
-
+
Cell cell = table.getCellByPosition(2, 2);
CellStyleHandler cellHandler = cell.getStyleHandler();
@@ -1524,17 +1522,17 @@
//validate
String country = cellHandler.getCountry(Document.ScriptType.CJK);
Assert.assertEquals("English", country);
-
+
//save
doc.save(ResourceUtilities.newTestOutputFile("testSupportedLinearMeasure.ods"));
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
Assert.fail(e.getMessage());
}
-
+
}
-
-
+
+
@Test
public void testGetLanguage() {
try {
@@ -1542,7 +1540,7 @@
borderbase.setLineStyle(StyleTypeDefinitions.LineType.DOUBLE);
SpreadsheetDocument doc = SpreadsheetDocument.newSpreadsheetDocument();
Table table = doc.getTableByName("Sheet1");
-
+
Cell cell = table.getCellByPosition(2, 2);
CellStyleHandler cellHandler = cell.getStyleHandler();
@@ -1551,17 +1549,17 @@
String language = cellHandler.getLanguage(Document.ScriptType.WESTERN);
System.out.println(language);
Assert.assertEquals("English", language);
-
+
//save
doc.save(ResourceUtilities.newTestOutputFile("testSupportedLinearMeasure.ods"));
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
Assert.fail(e.getMessage());
}
-
+
}
-
-
+
+
@Test
public void testSetColor() {
try {
@@ -1569,7 +1567,7 @@
borderbase.setLineStyle(StyleTypeDefinitions.LineType.DOUBLE);
SpreadsheetDocument doc = SpreadsheetDocument.newSpreadsheetDocument();
Table table = doc.getTableByName("Sheet1");
-
+
Cell cell = table.getCellByPosition(2, 2);
CellStyleHandler cellHandler = cell.getStyleHandler();
@@ -1577,17 +1575,17 @@
//validate
Color red = cellHandler.getBackgroundColor();
Assert.assertEquals(Color.RED.toString(), red.toString());
-
+
//save
doc.save(ResourceUtilities.newTestOutputFile("testSupportedLinearMeasure.ods"));
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
Assert.fail(e.getMessage());
}
-
+
}
-
-
+
+
@Test
public void testSetBorder() {
Border borderbase = new Border(new Color("#00ccff"), 0.0701, 0.0008, 0.0346, SupportedLinearMeasure.IN);
@@ -1595,12 +1593,12 @@
SpreadsheetDocument doc = SpreadsheetDocument.loadDocument(ResourceUtilities
.getTestResourceAsStream(filename));
Table table = doc.getTableByName("A");
-
+
borderbase.setLinearMeasure(StyleTypeDefinitions.SupportedLinearMeasure.CM);
-
+
Cell cell = table.getCellByPosition("A14");
cell.setBorders(CellBordersType.LEFT, borderbase);
-
+
CellStyleHandler cellHandler = cell.getStyleHandler();
Border border = cellHandler.getBorder(CellBordersType.LEFT);
@@ -1612,9 +1610,9 @@
LOGGER.log(Level.SEVERE, e.getMessage(), e);
Assert.fail();
}
-
+
}
-
+
@Test
public void testIsTextWrapped() {
Border borderbase = new Border(new Color("#00ccff"), 0.0701, 0.0008, 0.0346, SupportedLinearMeasure.CM);
@@ -1629,13 +1627,13 @@
CellStyleHandler cellHandler = cell.getStyleHandler();
cellHandler.setTextWrapped(true);
Assert.assertTrue(cellHandler.isTextWrapped());
-
+
//save
//doc.save(ResourceUtilities.newTestOutputFile("testSetWidth.ods"));
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
Assert.fail();
}
-
+
}
}
diff --git a/simple/src/test/resources/TestSpreadsheetTable.ods b/simple/src/test/resources/TestSpreadsheetTable.ods
index fe1a184..157b729 100644
--- a/simple/src/test/resources/TestSpreadsheetTable.ods
+++ b/simple/src/test/resources/TestSpreadsheetTable.ods
Binary files differ