From: Nick Burch Date: Sun, 16 Mar 2008 18:57:15 +0000 (+0000) Subject: Move the SharedStringsTable to a more generic package, and make a start on the styles... X-Git-Tag: REL_3_5_BETA2~186 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ef20df0bd11d8884193661d41d054511f4179bb1;p=poi.git Move the SharedStringsTable to a more generic package, and make a start on the styles table git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@637624 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java new file mode 100644 index 0000000000..fd43647cd1 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java @@ -0,0 +1,126 @@ +/* ==================================================================== + 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.xssf.model; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.LinkedList; + +import org.apache.poi.ss.usermodel.SharedStringSource; +import org.apache.xmlbeans.XmlException; +import org.apache.xmlbeans.XmlOptions; +import org.openxml4j.opc.PackagePart; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.SstDocument; + + +/** + * Table of strings shared across all sheets in a workbook. + * + * FIXME: I don't like having a dependency on PackagePart (from OpenXML4J) in model classes. + * I'd rather let Workbook keep track of all part-document relationships and keep all other + * classes clean. -- Ugo + * + * @version $Id$ + */ +public class SharedStringsTable implements SharedStringSource { + + private final LinkedList strings = new LinkedList(); + + private PackagePart part; + + /** + * Create a new SharedStringsTable by reading it from a PackagePart. + * + * @param part The PackagePart to read. + * @throws IOException if an error occurs while reading. + */ + public SharedStringsTable(PackagePart part) throws IOException { + this.part = part; + InputStream is = part.getInputStream(); + try { + readFrom(is); + } finally { + if (is != null) is.close(); + } + } + + /** + * Read this shared strings table from an XML file. + * + * @param is The input stream containing the XML document. + * @throws IOException if an error occurs while reading. + */ + public void readFrom(InputStream is) throws IOException { + try { + SstDocument doc = SstDocument.Factory.parse(is); + for (CTRst rst : doc.getSst().getSiArray()) { + strings.add(rst.getT()); + } + } catch (XmlException e) { + throw new IOException(e.getLocalizedMessage()); + } + } + + public String getSharedStringAt(int idx) { + return strings.get(idx); + } + + public synchronized int putSharedString(String s) { + if (strings.contains(s)) { + return strings.indexOf(s); + } + strings.add(s); + return strings.size() - 1; + } + + /** + * Save this table to its own PackagePart. + * + * @throws IOException if an error occurs while writing. + */ + public void save() throws IOException { + OutputStream out = this.part.getOutputStream(); + try { + writeTo(out); + } finally { + out.close(); + } + } + + /** + * Write this table out as XML. + * + * @param out The stream to write to. + * @throws IOException if an error occurs while writing. + */ + public void writeTo(OutputStream out) throws IOException { + XmlOptions options = new XmlOptions(); + options.setSaveOuter(); + SstDocument doc = SstDocument.Factory.newInstance(options); + CTSst sst = doc.addNewSst(); + sst.setCount(strings.size()); + sst.setUniqueCount(strings.size()); + for (String s : strings) { + sst.addNewSi().setT(s); + } + doc.save(out); + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java new file mode 100644 index 0000000000..5606dbd61c --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -0,0 +1,156 @@ +/* ==================================================================== + 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.xssf.model; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.LinkedList; + +import org.apache.poi.ss.usermodel.SharedStringSource; +import org.apache.xmlbeans.XmlException; +import org.apache.xmlbeans.XmlOptions; +import org.openxml4j.opc.PackagePart; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmt; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmts; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;; + + +/** + * Table of styles shared across all sheets in a workbook. + * + * FIXME: I don't like having a dependency on PackagePart (from OpenXML4J) in model classes. + * I'd rather let Workbook keep track of all part-document relationships and keep all other + * classes clean. -- Ugo + * + * @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $ + */ +public class StylesTable { + private final LinkedList numberFormats = new LinkedList(); + private final LinkedList fonts = new LinkedList(); + private final LinkedList fills = new LinkedList(); + private final LinkedList borders = new LinkedList(); + + private PackagePart part; + private StyleSheetDocument doc; + + /** + * Create a new StylesTable by reading it from a PackagePart. + * + * @param part The PackagePart to read. + * @throws IOException if an error occurs while reading. + */ + public StylesTable(PackagePart part) throws IOException { + this.part = part; + InputStream is = part.getInputStream(); + try { + readFrom(is); + } finally { + if (is != null) is.close(); + } + } + + /** + * Read this shared styles table from an XML file. + * + * @param is The input stream containing the XML document. + * @throws IOException if an error occurs while reading. + */ + public void readFrom(InputStream is) throws IOException { + try { + doc = StyleSheetDocument.Factory.parse(is); + + // Grab all the different bits we care about + for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) { + numberFormats.add(nfmt.getFormatCode()); + } + for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) { + fonts.add(font); + } + for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) { + fills.add(fill); + } + for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) { + borders.add(border); + } + } catch (XmlException e) { + throw new IOException(e.getLocalizedMessage()); + } + } + + public String getNumberFormatAt(int idx) { + return numberFormats.get(idx); + } + + public synchronized int putNumberFormat(String fmt) { + if (numberFormats.contains(fmt)) { + return numberFormats.indexOf(fmt); + } + numberFormats.add(fmt); + return numberFormats.size() - 1; + } + + /** + * Save this table to its own PackagePart. + * + * @throws IOException if an error occurs while writing. + */ + public void save() throws IOException { + OutputStream out = this.part.getOutputStream(); + try { + writeTo(out); + } finally { + out.close(); + } + } + + /** + * Write this table out as XML. + * + * @param out The stream to write to. + * @throws IOException if an error occurs while writing. + */ + public void writeTo(OutputStream out) throws IOException { + XmlOptions options = new XmlOptions(); + options.setSaveOuter(); + + // Work on the current one + // Need to do this, as we don't handle + // all the possible entries yet + + // Formats + CTNumFmts formats = CTNumFmts.Factory.newInstance(); + formats.setCount(numberFormats.size()); + for (String fmt : numberFormats) { + formats.addNewNumFmt().setFormatCode(fmt); + } + doc.getStyleSheet().setNumFmts(formats); + + // Fonts + + // Fills + + // Borders + + // Save + doc.save(out); + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/strings/SharedStringsTable.java b/src/ooxml/java/org/apache/poi/xssf/strings/SharedStringsTable.java deleted file mode 100644 index 924b10f3e8..0000000000 --- a/src/ooxml/java/org/apache/poi/xssf/strings/SharedStringsTable.java +++ /dev/null @@ -1,126 +0,0 @@ -/* ==================================================================== - 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.xssf.strings; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.LinkedList; - -import org.apache.poi.ss.usermodel.SharedStringSource; -import org.apache.xmlbeans.XmlException; -import org.apache.xmlbeans.XmlOptions; -import org.openxml4j.opc.PackagePart; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.SstDocument; - - -/** - * Table of strings shared across all sheets in a workbook. - * - * FIXME: I don't like having a dependency on PackagePart (from OpenXML4J) in model classes. - * I'd rather let Workbook keep track of all part-document relationships and keep all other - * classes clean. -- Ugo - * - * @version $Id$ - */ -public class SharedStringsTable implements SharedStringSource { - - private final LinkedList strings = new LinkedList(); - - private PackagePart part; - - /** - * Create a new SharedStringsTable by reading it from a PackagePart. - * - * @param part The PackagePart to read. - * @throws IOException if an error occurs while reading. - */ - public SharedStringsTable(PackagePart part) throws IOException { - this.part = part; - InputStream is = part.getInputStream(); - try { - readFrom(is); - } finally { - if (is != null) is.close(); - } - } - - /** - * Read this shared strings table from an XML file. - * - * @param is The input stream containing the XML document. - * @throws IOException if an error occurs while reading. - */ - public void readFrom(InputStream is) throws IOException { - try { - SstDocument doc = SstDocument.Factory.parse(is); - for (CTRst rst : doc.getSst().getSiArray()) { - strings.add(rst.getT()); - } - } catch (XmlException e) { - throw new IOException(e.getLocalizedMessage()); - } - } - - public String getSharedStringAt(int idx) { - return strings.get(idx); - } - - public synchronized int putSharedString(String s) { - if (strings.contains(s)) { - return strings.indexOf(s); - } - strings.add(s); - return strings.size() - 1; - } - - /** - * Save this table to its own PackagePart. - * - * @throws IOException if an error occurs while writing. - */ - public void save() throws IOException { - OutputStream out = this.part.getOutputStream(); - try { - writeTo(out); - } finally { - out.close(); - } - } - - /** - * Write this table out as XML. - * - * @param out The stream to write to. - * @throws IOException if an error occurs while writing. - */ - public void writeTo(OutputStream out) throws IOException { - XmlOptions options = new XmlOptions(); - options.setSaveOuter(); - SstDocument doc = SstDocument.Factory.newInstance(options); - CTSst sst = doc.addNewSst(); - sst.setCount(strings.size()); - sst.setUniqueCount(strings.size()); - for (String s : strings) { - sst.addNewSi().setT(s); - } - 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 e3dd080620..1c90affd17 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -38,7 +38,7 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; -import org.apache.poi.xssf.strings.SharedStringsTable; +import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions;