You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2008/10/19 17:31:29 UTC

svn commit: r706032 - in /poi/branches/ooxml/src/ooxml/java/org/apache/poi: POIXMLDocument.java POIXMLDocumentPart.java xslf/XSLFSlideShow.java xssf/usermodel/XSSFWorkbook.java xwpf/XWPFDocument.java

Author: yegor
Date: Sun Oct 19 08:31:28 2008
New Revision: 706032

URL: http://svn.apache.org/viewvc?rev=706032&view=rev
Log:
cleanup of POIXMLDocument constructors and related sub-classes

Modified:
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocument.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xslf/XSLFSlideShow.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocument.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocument.java?rev=706032&r1=706031&r2=706032&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocument.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocument.java Sun Oct 19 08:31:28 2008
@@ -28,7 +28,7 @@
 import org.openxml4j.opc.*;
 import org.openxml4j.opc.Package;
 
-public class POIXMLDocument extends POIXMLDocumentPart{
+public abstract class POIXMLDocument extends POIXMLDocumentPart{
 
     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";
@@ -43,33 +43,26 @@
     /** The OPC Package */
     private Package pkg;
 
-    /** The OPC core Package Part */
-    private PackagePart corePart;
-
     /**
      * The properties of the OPC package, opened as needed
      */
     private POIXMLProperties properties;
 
-    /**
-     * The embedded OLE2 files in the OPC package
-     */
-    protected List<PackagePart> embedds;
-
     protected POIXMLDocument() {
         super(null, null);
-        embedds = new LinkedList<PackagePart>();
+        try {
+            Package pkg = newPackage();
+            initialize(pkg);
+        } catch (IOException e){
+            throw new POIXMLException(e);
+        }
     }
 
     protected POIXMLDocument(Package pkg) throws IOException {
-        this();
+        super(null, null);
         initialize(pkg);
     }
 
-    protected POIXMLDocument(String path) throws IOException {
-           this(openPackage(path));
-    }
-
     /**
      * Wrapper to open a package, returning an IOException
      *  in the event of a problem.
@@ -83,7 +76,7 @@
         }
     }
 
-    protected void initialize(Package pkg) throws IOException {
+    private void initialize(Package pkg) throws IOException {
         try {
             this.pkg = pkg;
 
@@ -91,10 +84,11 @@
                     PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
 
             // Get core part
-            this.corePart = super.packagePart = this.pkg.getPart(coreDocRelationship);
+            this.packagePart = this.pkg.getPart(coreDocRelationship);
+            this.packageRel = coreDocRelationship;
 
             // Verify it's there
-            if(corePart == null) {
+            if(this.packagePart == null) {
                 throw new IllegalArgumentException("No core part found for this document! Nothing with " + coreDocRelationship.getRelationshipType() + " present as a relation.");
             }
         } catch (OpenXML4JException e) {
@@ -102,12 +96,16 @@
         }
     }
 
+    protected Package newPackage() throws IOException {
+        throw new POIXMLException("Must be overridden");
+    }
+
     public Package getPackage() {
         return this.pkg;
     }
 
     protected PackagePart getCorePart() {
-        return this.corePart;
+        return this.packagePart;
     }
 
     /**
@@ -128,7 +126,7 @@
      * @return The target part
      * @throws InvalidFormatException
      */
-    public static PackagePart getTargetPart(Package pkg, PackageRelationship rel) throws InvalidFormatException {
+    protected static PackagePart getTargetPart(Package pkg, PackageRelationship rel) throws InvalidFormatException {
         PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
         PackagePart part = pkg.getPart(relName);
         if (part == null) {
@@ -138,33 +136,13 @@
     }
 
     /**
-     * Fetches the (single) PackagePart which is defined as
-     *  the supplied relation content type of the base
-     *  package/container, or null if none found.
-     * @param relationType The relation content type to search for
-     * @throws IllegalArgumentException If we find more than one part of that type
-     */
-    protected PackagePart getSinglePartByRelationType(String relationType) throws IllegalArgumentException, OpenXML4JException {
-        PackageRelationshipCollection rels =
-            pkg.getRelationshipsByType(relationType);
-        if(rels.size() == 0) {
-            return null;
-        }
-        if(rels.size() > 1) {
-            throw new IllegalArgumentException("Found " + rels.size() + " relations for the type " + relationType + ", should only ever be one!");
-        }
-        PackageRelationship rel = rels.getRelationship(0);
-        return getTargetPart(rel);
-    }
-
-    /**
      * Retrieves all the PackageParts which are defined as
      *  relationships of the base document with the
      *  specified content type.
      */
     protected PackagePart[] getRelatedByType(String contentType) throws InvalidFormatException {
         PackageRelationshipCollection partsC =
-            getCorePart().getRelationshipsByType(contentType);
+            getPackagePart().getRelationshipsByType(contentType);
 
         PackagePart[] parts = new PackagePart[partsC.size()];
         int count = 0;
@@ -224,8 +202,6 @@
     /**
      * Get the document's embedded files.
      */
-    public List<PackagePart> getAllEmbedds() throws OpenXML4JException
-    {
-        return embedds;
-    }
+    public abstract List<PackagePart> getAllEmbedds() throws OpenXML4JException;
+
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java?rev=706032&r1=706031&r2=706032&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java Sun Oct 19 08:31:28 2008
@@ -213,14 +213,14 @@
     /**
      * Fired when a new package part is created
      */
-    public void onDocumentCreate(){
+    protected void onDocumentCreate(){
 
     }
 
     /**
      * Fired when a package part is read
      */
-    public void onDocumentRead(){
+    protected void onDocumentRead(){
 
     }
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xslf/XSLFSlideShow.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xslf/XSLFSlideShow.java?rev=706032&r1=706031&r2=706032&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xslf/XSLFSlideShow.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xslf/XSLFSlideShow.java Sun Oct 19 08:31:28 2008
@@ -17,6 +17,8 @@
 package org.apache.poi.xslf;
 
 import java.io.IOException;
+import java.util.List;
+import java.util.LinkedList;
 
 import org.apache.poi.POIXMLDocument;
 import org.apache.xmlbeans.XmlException;
@@ -61,13 +63,18 @@
 	public static final String COMMENT_RELATION_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
 
 	private PresentationDocument presentationDoc;
-	
+    /**
+     * The embedded OLE2 files in the OPC package
+     */
+    private List<PackagePart> embedds;
+
 	public XSLFSlideShow(Package container) throws OpenXML4JException, IOException, XmlException {
 		super(container);
 		
 		presentationDoc =
 			PresentationDocument.Factory.parse(getCorePart().getInputStream());
 		
+        embedds = new LinkedList<PackagePart>();
 		for (CTSlideIdListEntry ctSlide : getSlideReferences().getSldIdArray()) {
 	          PackagePart slidePart =
 	                getTargetPart(getCorePart().getRelationship(ctSlide.getId2()));
@@ -224,4 +231,12 @@
 			throw new IllegalStateException(e);
 		}
 	}
+
+    /**
+     * Get the document's embedded files.
+     */
+    public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
+        return embedds;
+    }
+
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=706032&r1=706031&r2=706032&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java Sun Oct 19 08:31:28 2008
@@ -38,6 +38,7 @@
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
 import org.openxml4j.exceptions.OpenXML4JException;
+import org.openxml4j.exceptions.InvalidFormatException;
 import org.openxml4j.opc.*;
 import org.openxml4j.opc.Package;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
@@ -99,15 +100,16 @@
     private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class);
 
     /**
+     * The embedded OLE2 files in the OPC package
+     */
+    private List<PackagePart> embedds;
+
+    /**
      * Create a new SpreadsheetML workbook.
      */
     public XSSFWorkbook() {
         super();
-        try {
-            newWorkbook();
-        }catch (Exception e){
-            throw new POIXMLException(e);
-        }
+        onDocumentCreate();
     }
 
     /**
@@ -126,37 +128,38 @@
      * @param pkg the OpenXML4J <code>Package</code> object.
      */
     public XSSFWorkbook(Package pkg) throws IOException {
-        super();
+        super(ensureWriteAccess(pkg));
+        onDocumentRead();
+    }
+
+    /**
+     * YK: current implementation of OpenXML4J is funny.
+     * Packages opened by Package.open(InputStream is) are read-only,
+     * there is no way to change or even save such an instance in a OutputStream.
+     * The workaround is to create a copy via a temp file
+     */
+    private static Package ensureWriteAccess(Package pkg) throws IOException {
         if(pkg.getPackageAccess() == PackageAccess.READ){
             //YK: current implementation of OpenXML4J is funny.
-            //Packages opened by Package.open(InputStream is) are read-only,
-            //there is no way to change or even save such an instance in a OutputStream.
-            //The workaround is to create a copy via a temp file
             try {
-                Package tmp = PackageHelper.clone(pkg);
-                initialize(tmp);
+                return PackageHelper.clone(pkg);
             } catch (OpenXML4JException e){
                 throw new POIXMLException(e);
             }
-        } else {
-            initialize(pkg);
         }
+        return pkg;
     }
 
     /**
      * Initialize this workbook from the specified Package
      */
     @Override
-    protected void initialize(Package pkg) throws IOException {
-        super.initialize(pkg);
-
+    protected void onDocumentRead() {
         try {
             //build the POIXMLDocumentPart tree, this workbook is the root
             read(XSSFFactory.getInstance());
 
-            PackagePart corePart = getCorePart();
-
-            WorkbookDocument doc = WorkbookDocument.Factory.parse(corePart.getInputStream());
+            WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream());
             this.workbook = doc.getWorkbook();
 
             HashMap<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
@@ -167,8 +170,10 @@
                     shIdMap.put(p.getPackageRelationship().getId(), (XSSFSheet)p);
                 }
             }
+
             // Load individual sheets
             sheets = new LinkedList<XSSFSheet>();
+            embedds = new LinkedList<PackagePart>();
             for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
                 String id = ctSheet.getId();
                 XSSFSheet sh = shIdMap.get(id);
@@ -215,22 +220,8 @@
         }
     }
 
-    /**
-     * Create a new SpreadsheetML OOXML package and setup the default minimal content
-     */
-    protected void newWorkbook() throws IOException, OpenXML4JException{
-        Package pkg = Package.create(PackageHelper.createTempFile());
-        // Main part
-        PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
-        // Create main part relationship
-        pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
-        // Create main document part
-        pkg.createPart(corePartName, XSSFRelation.WORKBOOK.getContentType());
-
-        pkg.getPackageProperties().setCreatorProperty("Apache POI");
-
-        super.initialize(pkg);
-
+    @Override
+    protected void onDocumentCreate() {
         workbook = CTWorkbook.Factory.newInstance();
         CTBookViews bvs = workbook.addNewBookViews();
         CTBookView bv = bvs.addNewWorkbookView();
@@ -242,6 +233,28 @@
 
         namedRanges = new LinkedList<XSSFName>();
         sheets = new LinkedList<XSSFSheet>();
+        embedds = new LinkedList<PackagePart>();
+    }
+
+    /**
+     * Create a new SpreadsheetML package and setup the default minimal content
+     */
+    protected Package newPackage() throws IOException {
+        try {
+            Package pkg = Package.create(PackageHelper.createTempFile());
+            // Main part
+            PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
+            // Create main part relationship
+            pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
+            // Create main document part
+            pkg.createPart(corePartName, XSSFRelation.WORKBOOK.getContentType());
+
+            pkg.getPackageProperties().setCreatorProperty("Apache POI");
+
+            return pkg;
+        } catch (InvalidFormatException e){
+            throw new POIXMLException(e);
+        }
     }
 
     /**
@@ -709,7 +722,7 @@
      *  macro enabled workbook (.xlsm)?
      */
     public boolean isMacroEnabled() {
-        return getCorePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
+        return getPackagePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
     }
 
     public void removeName(int index) {
@@ -1154,4 +1167,11 @@
         CTWorkbookPr workbookPr = workbook.getWorkbookPr();
         return workbookPr != null && workbookPr.getDate1904();
     }
+
+    /**
+     * Get the document's embedded files.
+     */
+    public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
+        return embedds;
+    }
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java?rev=706032&r1=706031&r2=706032&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java Sun Oct 19 08:31:28 2008
@@ -71,7 +71,11 @@
 	protected List<XWPFHyperlink> hyperlinks;
 	protected List<XWPFParagraph> paragraphs;
 	protected List<XWPFTable> tables;
-	
+    /**
+     * The embedded OLE2 files in the OPC package
+     */
+    private List<PackagePart> embedds;
+
 	/** Handles the joy of different headers/footers for different pages */
 	private XWPFHeaderFooterPolicy headerFooterPolicy;
 	
@@ -239,5 +243,13 @@
 	{
 		return getCorePart().getRelationshipsByType(COMMENT_RELATION_TYPE);
 	}
+
+    /**
+     * Get the document's embedded files.
+     */
+    public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
+        return embedds;
+    }
+
 }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org