From df2b666ebdb5452ad622a6e4f9f02cffff0dd982 Mon Sep 17 00:00:00 2001 From: Ugo Cei Date: Mon, 4 Feb 2008 16:34:44 +0000 Subject: [PATCH] Introduce POIXMLDocument as common base class for all OOXML document types. git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@618319 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/POIXMLDocument.java | 65 +++++++++++++++++++ .../poi/xssf/usermodel/XSSFWorkbook.java | 23 ++----- 2 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/POIXMLDocument.java diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/POIXMLDocument.java new file mode 100644 index 000000000..8b4dc84d9 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/POIXMLDocument.java @@ -0,0 +1,65 @@ +/* ==================================================================== + 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; + +import java.io.IOException; + +import org.openxml4j.exceptions.InvalidFormatException; +import org.openxml4j.exceptions.OpenXML4JException; +import org.openxml4j.opc.Package; +import org.openxml4j.opc.PackagePart; +import org.openxml4j.opc.PackageRelationship; +import org.openxml4j.opc.PackageRelationshipTypes; + + +public abstract class POIXMLDocument { + + public static final String CORE_PROPERTIES_REL_TYPE = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"; + + public static final String EXTENDED_PROPERTIES_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"; + + /** The OPC Package */ + private Package pkg; + + /** The OPC core Package Part */ + private PackagePart corePart; + + protected POIXMLDocument() {} + + protected POIXMLDocument(String path) throws IOException { + try { + this.pkg = Package.open(path); + PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType( + PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0); + + // Get core part + this.corePart = this.pkg.getPart(coreDocRelationship); + } catch (InvalidFormatException e) { + throw new IOException(e.toString()); + } catch (OpenXML4JException e) { + throw new IOException(e.toString()); + } + } + + protected Package getPackage() { + return this.pkg; + } + + protected PackagePart getCorePart() { + return this.corePart; + } +} 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 a26ca9c5f..5d827eca4 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -17,7 +17,6 @@ package org.apache.poi.xssf.usermodel; -import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.LinkedList; @@ -25,6 +24,7 @@ import java.util.List; import javax.xml.namespace.QName; +import org.apache.poi.POIXMLDocument; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Font; @@ -36,11 +36,9 @@ import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; import org.openxml4j.exceptions.InvalidFormatException; -import org.openxml4j.exceptions.OpenXML4JException; import org.openxml4j.opc.Package; import org.openxml4j.opc.PackagePart; import org.openxml4j.opc.PackagePartName; -import org.openxml4j.opc.PackageRelationship; import org.openxml4j.opc.PackageRelationshipTypes; import org.openxml4j.opc.PackagingURIHelper; import org.openxml4j.opc.TargetMode; @@ -52,14 +50,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument; -public class XSSFWorkbook implements Workbook { +public class XSSFWorkbook extends POIXMLDocument implements Workbook { private CTWorkbook workbook; private List sheets = new LinkedList(); - - /** The OPC Package */ - private Package pkg; public XSSFWorkbook() { this.workbook = CTWorkbook.Factory.newInstance(); @@ -70,20 +65,10 @@ public class XSSFWorkbook implements Workbook { } public XSSFWorkbook(String path) throws IOException { + super(path); try { - this.pkg = Package.open(path); - PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType( - PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0); - - // Get core part - PackagePart corePart = this.pkg.getPart(coreDocRelationship); - WorkbookDocument doc = WorkbookDocument.Factory.parse(corePart.getInputStream()); + WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream()); this.workbook = doc.getWorkbook(); - - } catch (InvalidFormatException e) { - throw new IOException(e.toString()); - } catch (OpenXML4JException e) { - throw new IOException(e.toString()); } catch (XmlException e) { throw new IOException(e.toString()); }