diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/StylesSource.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/StylesSource.java new file mode 100644 index 000000000..2b21ecaac --- /dev/null +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/StylesSource.java @@ -0,0 +1,22 @@ +/* ==================================================================== + 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.ss.usermodel; + +public interface StylesSource { + public String getNumberFormatAt(long idx); + public long putNumberFormat(String fmt); +} diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 5606dbd61..f96df8e72 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -20,9 +20,12 @@ package org.apache.poi.xssf.model; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Enumeration; +import java.util.Hashtable; import java.util.LinkedList; +import java.util.Map.Entry; -import org.apache.poi.ss.usermodel.SharedStringSource; +import org.apache.poi.ss.usermodel.StylesSource; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlOptions; import org.openxml4j.opc.PackagePart; @@ -43,8 +46,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;; * * @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $ */ -public class StylesTable { - private final LinkedList numberFormats = new LinkedList(); +public class StylesTable implements StylesSource { + private final Hashtable numberFormats = new Hashtable(); private final LinkedList fonts = new LinkedList(); private final LinkedList fills = new LinkedList(); private final LinkedList borders = new LinkedList(); @@ -80,7 +83,7 @@ public class StylesTable { // Grab all the different bits we care about for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) { - numberFormats.add(nfmt.getFormatCode()); + numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode()); } for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) { fonts.add(font); @@ -96,17 +99,56 @@ public class StylesTable { } } - public String getNumberFormatAt(int idx) { + public String getNumberFormatAt(long idx) { return numberFormats.get(idx); } - public synchronized int putNumberFormat(String fmt) { - if (numberFormats.contains(fmt)) { - return numberFormats.indexOf(fmt); + public synchronized long putNumberFormat(String fmt) { + if (numberFormats.containsValue(fmt)) { + // Find the key, and return that + for(Enumeration keys = numberFormats.keys(); keys.hasMoreElements();) { + Long key = keys.nextElement(); + if(numberFormats.get(key).equals(fmt)) { + return key; + } + } + throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!"); } - numberFormats.add(fmt); - return numberFormats.size() - 1; + + // Find a spare key, and add that + long newKey = 1; + while(numberFormats.containsKey(newKey)) { + newKey++; + } + numberFormats.put(newKey, fmt); + return newKey; } + + /** + * For unit testing only + */ + public int _getNumberFormatSize() { + return numberFormats.size(); + } + /** + * For unit testing only + */ + public int _getFontsSize() { + return fonts.size(); + } + /** + * For unit testing only + */ + public int _getFillsSize() { + return fills.size(); + } + /** + * For unit testing only + */ + public int _getBordersSize() { + return borders.size(); + } + /** * Save this table to its own PackagePart. @@ -139,16 +181,21 @@ public class StylesTable { // Formats CTNumFmts formats = CTNumFmts.Factory.newInstance(); formats.setCount(numberFormats.size()); - for (String fmt : numberFormats) { - formats.addNewNumFmt().setFormatCode(fmt); + for (Entry fmt : numberFormats.entrySet()) { + CTNumFmt ctFmt = formats.addNewNumFmt(); + ctFmt.setNumFmtId(fmt.getKey()); + ctFmt.setFormatCode(fmt.getValue()); } doc.getStyleSheet().setNumFmts(formats); // Fonts + // TODO // Fills + // TODO // Borders + // TODO // Save doc.save(out); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 1c90affd1..4d3a943b3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -35,10 +35,12 @@ import org.apache.poi.ss.usermodel.Palette; import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.SharedStringSource; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.StylesSource; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; import org.apache.poi.xssf.model.SharedStringsTable; +import org.apache.poi.xssf.model.StylesTable; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; @@ -69,6 +71,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { private static final String SHARED_STRINGS_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"; + private static final String STYLES_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"; + private static final String DRAWING_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"; private static final String IMAGE_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"; @@ -78,6 +82,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { private List sheets = new LinkedList(); private SharedStringSource sharedStringSource; + private StylesSource stylesSource; private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class); @@ -97,14 +102,27 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { try { WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream()); this.workbook = doc.getWorkbook(); + + PackageRelationshipCollection prc; + Iterator it; + // Load shared strings - PackageRelationshipCollection prc = getCorePart().getRelationshipsByType(SHARED_STRINGS_RELATIONSHIP); - Iterator it = prc.iterator(); + prc = getCorePart().getRelationshipsByType(SHARED_STRINGS_RELATIONSHIP); + it = prc.iterator(); if (it.hasNext()) { PackageRelationship rel = it.next(); PackagePart part = getTargetPart(rel); this.sharedStringSource = new SharedStringsTable(part); } + // Load styles source + prc = getCorePart().getRelationshipsByType(STYLES_RELATIONSHIP); + it = prc.iterator(); + if (it.hasNext()) { + PackageRelationship rel = it.next(); + PackagePart part = getTargetPart(rel); + this.stylesSource = new StylesTable(part); + } + // Load individual sheets for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) { PackagePart part = getPackagePart(ctSheet); @@ -345,8 +363,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } public String getSSTString(int index) { - // TODO Auto-generated method stub - return null; + return getSharedStringSource().getSharedStringAt(index); } public short getSelectedTab() { @@ -513,6 +530,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { out.close(); } + // TODO - shared strings source + // TODO - styles source + pkg.close(); } catch (InvalidFormatException e) { @@ -529,10 +549,16 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { public SharedStringSource getSharedStringSource() { return this.sharedStringSource; } - protected void setSharedStringSource(SharedStringSource sharedStringSource) { this.sharedStringSource = sharedStringSource; } + + public StylesSource getStylesSource() { + return this.stylesSource; + } + protected void setStylesSource(StylesSource stylesSource) { + this.stylesSource = stylesSource; + } public CreationHelper getCreationHelper() { return new XSSFCreationHelper(this); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index 0c8aa6ba6..56b1b170b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -281,6 +281,10 @@ public class TestXSSFCell extends TestCase { assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell()); } + + public void testCellFormatting() { + + } private XSSFRow createParentObjects() { XSSFWorkbook wb = new XSSFWorkbook(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 11e7ad523..eea58b89e 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -24,6 +24,8 @@ import java.io.OutputStream; import junit.framework.TestCase; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.StylesSource; +import org.apache.poi.xssf.model.StylesTable; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; @@ -130,4 +132,40 @@ public class TestXSSFWorkbook extends TestCase { workbook.write(out); out.close(); } + + public void testExisting() throws Exception { + File xml = new File( + System.getProperty("HSSF.testdata.path") + + File.separator + "Formatting.xlsx" + ); + assertTrue(xml.exists()); + + XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + assertNotNull(workbook.getSharedStringSource()); + assertNotNull(workbook.getStylesSource()); + } + + public void testStyles() throws Exception { + File xml = new File( + System.getProperty("HSSF.testdata.path") + + File.separator + "Formatting.xlsx" + ); + assertTrue(xml.exists()); + + XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + + StylesSource ss = workbook.getStylesSource(); + assertNotNull(ss); + assertTrue(ss instanceof StylesTable); + StylesTable st = (StylesTable)ss; + + // Has 8 number formats + assertEquals(8, st._getNumberFormatSize()); + // Has 2 fonts + assertEquals(2, st._getFontsSize()); + // Has 2 fills + assertEquals(2, st._getFillsSize()); + // Has 1 border + assertEquals(1, st._getBordersSize()); + } }