From ef20df0bd11d8884193661d41d054511f4179bb1 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 16 Mar 2008 18:57:15 +0000 Subject: [PATCH] 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 --- .../SharedStringsTable.java | 2 +- .../apache/poi/xssf/model/StylesTable.java | 156 ++++++++++++++++++ .../poi/xssf/usermodel/XSSFWorkbook.java | 2 +- 3 files changed, 158 insertions(+), 2 deletions(-) rename src/ooxml/java/org/apache/poi/xssf/{strings => model}/SharedStringsTable.java (99%) create mode 100644 src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java diff --git a/src/ooxml/java/org/apache/poi/xssf/strings/SharedStringsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java similarity index 99% rename from src/ooxml/java/org/apache/poi/xssf/strings/SharedStringsTable.java rename to src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java index 924b10f3e8..fd43647cd1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/strings/SharedStringsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java @@ -15,7 +15,7 @@ limitations under the License. ==================================================================== */ -package org.apache.poi.xssf.strings; +package org.apache.poi.xssf.model; import java.io.IOException; import java.io.InputStream; 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/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; -- 2.39.5