]> source.dussan.org Git - poi.git/commitdiff
refactored POIXMLFactory and related classes; simplified XSSFRelation - now it's...
authorYegor Kozlov <yegor@apache.org>
Sun, 12 Oct 2008 13:10:30 +0000 (13:10 +0000)
committerYegor Kozlov <yegor@apache.org>
Sun, 12 Oct 2008 13:10:30 +0000 (13:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@703814 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java
src/ooxml/java/org/apache/poi/POIXMLDocument.java
src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
src/ooxml/java/org/apache/poi/POIXMLFactory.java
src/ooxml/java/org/apache/poi/POIXMLRelation.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFactory.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPictureData.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java

index 2ed1c3c7b1b24bafc32d70003226b364bcadbe8a..91d0cc9851b43a1b1b66cd5ca6f358e1c59a7fc6 100644 (file)
@@ -374,8 +374,6 @@ public interface Workbook {
      */
     void removeName(String name);
 
-    Palette getCustomPalette();
-
     /**
      * Adds a picture to the workbook.
      *
index 74be9909ca86f35caf6392fce86111fbe8629a5f..806fd6381ece099f0bd0e167eb22d6b79d353c08 100644 (file)
@@ -22,7 +22,6 @@ import java.util.List;
 
 import org.apache.poi.poifs.common.POIFSConstants;
 import org.apache.poi.util.IOUtils;
-import org.apache.poi.util.PackageHelper;
 import org.apache.xmlbeans.XmlException;
 import org.openxml4j.exceptions.InvalidFormatException;
 import org.openxml4j.exceptions.OpenXML4JException;
index 979caeea30609e508beb475be001198121de185f..e809a7729df06d0bd2aff75e2ed0e6d5a352223a 100755 (executable)
@@ -143,43 +143,35 @@ public class POIXMLDocumentPart {
      * Create a new child POIXMLDocumentPart\r
      *\r
      * @param descriptor the part descriptor\r
-     * @param cls the Class object identifying the type of instance to create\r
+     * @param factory the factory that will create an instance of the requested relation\r
      * @return the created child POIXMLDocumentPart\r
      */\r
-    protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls){\r
-        return createRelationship(descriptor, cls, -1);\r
+    protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory){\r
+        return createRelationship(descriptor, factory, -1, false);\r
     }\r
 \r
-    /**\r
-     * Create a new child POIXMLDocumentPart\r
-     *\r
-     * @param descriptor the part descriptor\r
-     * @param cls the Class object identifying the type of instance to create\r
-     * @param idx part number\r
-     * @return the created child POIXMLDocumentPart\r
-     */\r
-    protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls, int idx){\r
-        return createRelationship(descriptor, cls, idx, false);\r
+    protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx){\r
+        return createRelationship(descriptor, factory, idx, false);\r
     }\r
 \r
     /**\r
      * Create a new child POIXMLDocumentPart\r
      *\r
      * @param descriptor the part descriptor\r
-     * @param cls the Class object identifying the type of instance to create\r
+     * @param factory the factory that will create an instance of the requested relation\r
      * @param idx part number\r
-     * @param norel if true, then no relationship is added. \r
+     * @param noRelation if true, then no relationship is added.\r
      * @return the created child POIXMLDocumentPart\r
      */\r
-    protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls, int idx, boolean norel){\r
+    protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx, boolean noRelation){\r
         try {\r
 \r
             PackagePartName ppName = PackagingURIHelper.createPartName(descriptor.getFileName(idx));\r
             PackageRelationship rel = null;\r
-            if(!norel) rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation());\r
+            if(!noRelation) rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation());\r
 \r
             PackagePart part = packagePart.getPackage().createPart(ppName, descriptor.getContentType());\r
-            POIXMLDocumentPart doc = cls.newInstance();\r
+            POIXMLDocumentPart doc = factory.newDocumentPart(descriptor);\r
             doc.packageRel = rel;\r
             doc.packagePart = part;\r
             doc.parent = this;\r
@@ -206,7 +198,7 @@ public class POIXMLDocumentPart {
                     logger.log(POILogger.ERROR, "Skipped invalid entry " + rel.getTargetURI());\r
                     continue;\r
                 }\r
-                POIXMLDocumentPart childPart = factory.create(rel, p);\r
+                POIXMLDocumentPart childPart = factory.createDocumentPart(rel, p);\r
                 childPart.parent = this;\r
                 addRelation(childPart);\r
 \r
index 6909cb9c31d847fd046e31069534703cf9eb9b07..4b922dd14603186e04ff08b86da1ea7f17f04c08 100755 (executable)
@@ -25,16 +25,24 @@ import org.openxml4j.opc.PackagePart;
  *\r
  * @author Yegor Kozlov\r
  */\r
-public class POIXMLFactory {\r
+public abstract class POIXMLFactory {\r
 \r
     /**\r
-     * Creates a new instance of a {@link POIXMLDocumentPart}\r
+     * Create a POIXMLDocumentPart from existing package part and relation. This method is called\r
+     * from {@link POIXMLDocumentPart#read(POIXMLFactory)} when parsing a document\r
      *\r
      * @param rel   the package part relationship\r
      * @param part  the PackagePart representing the created instance\r
      * @return A new instance of a POIXMLDocumentPart.\r
      */\r
-     public POIXMLDocumentPart create(PackageRelationship rel, PackagePart part){\r
-        return new POIXMLDocumentPart(part, rel);\r
-    }\r
+     public abstract POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part);\r
+\r
+    /**\r
+     * Create a new POIXMLDocumentPart using the supplied descriptor. This method is used when adding new parts\r
+     * to a document, for example, when adding a sheet to a workbook, slide to a presentation, etc.\r
+     *\r
+     * @param descriptor  described the object to create\r
+     * @return A new instance of a POIXMLDocumentPart.\r
+     */\r
+     public abstract POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor);\r
 }\r
index fb001efdcc160b7fb83c0d36e7ffbb632094be49..a11c49ca18c92b7dfa54f049e9afc0b457b32396 100755 (executable)
@@ -21,7 +21,7 @@ package org.apache.poi;
  *\r
  * @author Yegor Kozlov\r
  */\r
-public class POIXMLRelation {\r
+public abstract class POIXMLRelation {\r
 \r
     /**\r
      * Describes the content stored in a part.\r
@@ -38,19 +38,36 @@ public class POIXMLRelation {
      */\r
     protected String _defaultName;\r
 \r
+    /**\r
+     * Defines what object is used to construct instances of this relationship\r
+     */\r
+    private Class<? extends POIXMLDocumentPart> _cls;\r
+\r
     /**\r
      * Instantiates a POIXMLRelation.\r
      *\r
      * @param type content type\r
      * @param rel  relationship\r
      * @param defaultName default item name\r
+     * @param cls defines what object is used to construct instances of this relationship\r
      */\r
-    public POIXMLRelation(String type, String rel, String defaultName) {\r
+    public POIXMLRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {\r
         _type = type;\r
         _relation = rel;\r
         _defaultName = defaultName;\r
+        _cls = cls;\r
     }\r
 \r
+    /**\r
+     * Instantiates a POIXMLRelation.\r
+     *\r
+     * @param type content type\r
+     * @param rel  relationship\r
+     * @param defaultName default item name\r
+     */\r
+    public POIXMLRelation(String type, String rel, String defaultName) {\r
+        this(type, rel, defaultName, null);\r
+    }\r
     /**\r
      * Return the content type. Content types define a media type, a subtype, and an\r
      * optional set of parameters, as defined in RFC 2616.\r
@@ -93,4 +110,13 @@ public class POIXMLRelation {
         }\r
         return _defaultName.replace("#", Integer.toString(index));\r
     }\r
+\r
+    /**\r
+     * Return type of the obejct used to construct instances of this relationship\r
+     *\r
+     * @return the class of the object used to construct instances of this relation\r
+     */\r
+    public Class<? extends POIXMLDocumentPart> getRelationClass(){\r
+        return _cls;\r
+    }\r
 }\r
index 52b8d63f9a82fceb27eaf8880f097f24543d9062..0900183e853d64a7d17370767f91ddeb92110a11 100755 (executable)
@@ -19,14 +19,12 @@ package org.apache.poi.xssf.usermodel;
 import org.apache.poi.POIXMLDocumentPart;\r
 import org.apache.poi.POIXMLFactory;\r
 import org.apache.poi.POIXMLException;\r
-import org.apache.poi.xssf.model.SharedStringsTable;\r
-import org.apache.poi.xssf.model.StylesTable;\r
-import org.apache.poi.xssf.model.CommentsTable;\r
+import org.apache.poi.POIXMLRelation;\r
+import org.apache.poi.util.POILogger;\r
+import org.apache.poi.util.POILogFactory;\r
 import org.openxml4j.opc.PackageRelationship;\r
 import org.openxml4j.opc.PackagePart;\r
 \r
-import java.util.Map;\r
-import java.util.HashMap;\r
 import java.lang.reflect.Constructor;\r
 \r
 /**\r
@@ -35,25 +33,40 @@ import java.lang.reflect.Constructor;
  * @author Yegor Kozlov\r
  */\r
 public class XSSFFactory extends POIXMLFactory  {\r
-    protected static Map<String, Class> parts = new HashMap<String, Class>();\r
-    static {\r
-        parts.put(XSSFRelation.WORKSHEET.getRelation(), XSSFSheet.class);\r
-        parts.put(XSSFRelation.SHARED_STRINGS.getRelation(), SharedStringsTable.class);\r
-        parts.put(XSSFRelation.STYLES.getRelation(), StylesTable.class);\r
-        parts.put(XSSFRelation.SHEET_COMMENTS.getRelation(), CommentsTable.class);\r
-        parts.put(XSSFRelation.DRAWINGS.getRelation(), XSSFDrawing.class);\r
-        parts.put(XSSFRelation.IMAGES.getRelation(), XSSFPictureData.class);\r
+    private static POILogger logger = POILogFactory.getLogger(XSSFFactory.class);\r
+\r
+    private XSSFFactory(){\r
+\r
+    }\r
+\r
+    public static XSSFFactory getInstance(){\r
+        return new XSSFFactory();\r
     }\r
 \r
-    public POIXMLDocumentPart create(PackageRelationship rel, PackagePart p){\r
-        Class cls = parts.get(rel.getRelationshipType());\r
-        if(cls == null) return super.create(rel, p);\r
+    public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){\r
+        XSSFRelation descriptor = XSSFRelation.getInstance(rel.getRelationshipType());\r
+        if(descriptor == null || descriptor.getRelationClass() == null){\r
+            logger.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());\r
+            return new POIXMLDocumentPart(part, rel);\r
+        }\r
 \r
         try {\r
+            Class cls = descriptor.getRelationClass();\r
             Constructor<? extends POIXMLDocumentPart> constructor = cls.getConstructor(PackagePart.class, PackageRelationship.class);\r
-            return constructor.newInstance(p, rel);\r
+            return constructor.newInstance(part, rel);\r
+        } catch (Exception e){\r
+            throw new POIXMLException(e);\r
+        }\r
+    }\r
+\r
+    public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){\r
+        try {\r
+            Class cls = descriptor.getRelationClass();\r
+            Constructor<? extends POIXMLDocumentPart> constructor = cls.getConstructor();\r
+            return constructor.newInstance();\r
         } catch (Exception e){\r
             throw new POIXMLException(e);\r
         }\r
     }\r
+\r
 }\r
index fdc49fcaca55a8da2ed2aa5c6703928a3d35226d..eebd5d6d4b962a862aac4525291c20d3baadecb7 100644 (file)
@@ -39,12 +39,12 @@ public class XSSFPictureData extends POIXMLDocumentPart implements PictureData {
     protected static final POIXMLRelation[] RELATIONS;
     static {
         RELATIONS = new POIXMLRelation[8];
-        RELATIONS[Workbook.PICTURE_TYPE_EMF] = new POIXMLRelation("image/x-emf", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.emf");
-        RELATIONS[Workbook.PICTURE_TYPE_WMF] = new POIXMLRelation("image/x-wmf", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.wmf");
-        RELATIONS[Workbook.PICTURE_TYPE_PICT] = new POIXMLRelation("image/pict", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.pict");
-        RELATIONS[Workbook.PICTURE_TYPE_JPEG] = new POIXMLRelation("image/jpeg", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.jpeg");
-        RELATIONS[Workbook.PICTURE_TYPE_PNG] = new POIXMLRelation("image/png", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.png");
-        RELATIONS[Workbook.PICTURE_TYPE_DIB] = new POIXMLRelation("image/dib", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.dib");
+        RELATIONS[Workbook.PICTURE_TYPE_EMF] = XSSFRelation.IMAGE_EMF;
+        RELATIONS[Workbook.PICTURE_TYPE_WMF] = XSSFRelation.IMAGE_WMF;
+        RELATIONS[Workbook.PICTURE_TYPE_PICT] = XSSFRelation.IMAGE_PICT;
+        RELATIONS[Workbook.PICTURE_TYPE_JPEG] = XSSFRelation.IMAGE_JPEG;
+        RELATIONS[Workbook.PICTURE_TYPE_PNG] = XSSFRelation.IMAGE_PNG;
+        RELATIONS[Workbook.PICTURE_TYPE_DIB] = XSSFRelation.IMAGE_DIB;
     }
 
     /**
index 53197200b1c43ee9741eba7d0372a6e8c2b4fc56..ef9bf7fbb16789b0f4a514439a52fb6b14206ecb 100644 (file)
@@ -18,39 +18,37 @@ package org.apache.poi.xssf.usermodel;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
 import java.util.Iterator;
-import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 
 import org.apache.poi.POIXMLDocument;
 import org.apache.poi.POIXMLRelation;
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.xssf.model.StylesTable;
+import org.apache.poi.xssf.model.SharedStringsTable;
+import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
-import org.apache.poi.xssf.model.BinaryPart;
-import org.apache.poi.xssf.model.CommentsTable;
-import org.apache.poi.xssf.model.Control;
-import org.apache.poi.xssf.model.Drawing;
-import org.apache.poi.xssf.model.SharedStringsTable;
-import org.apache.poi.xssf.model.StylesTable;
-import org.apache.poi.xssf.model.ThemeTable;
-import org.apache.poi.xssf.model.XSSFChildContainingModel;
-import org.apache.poi.xssf.model.XSSFModel;
-import org.apache.poi.xssf.model.XSSFWritableModel;
 import org.openxml4j.exceptions.InvalidFormatException;
 import org.openxml4j.opc.PackagePart;
 import org.openxml4j.opc.PackagePartName;
 import org.openxml4j.opc.PackageRelationship;
 import org.openxml4j.opc.PackageRelationshipCollection;
 import org.openxml4j.opc.PackagingURIHelper;
-import org.openxml4j.opc.TargetMode;
 
 /**
  * 
  */
-public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
+public final class XSSFRelation extends POIXMLRelation {
+
+    private static POILogger log = POILogFactory.getLogger(XSSFRelation.class);
+
+    /**
+     * A map to lookup POIXMLRelation by its relation type
+     */
+    protected static Map<String, XSSFRelation> _table = new HashMap<String, XSSFRelation>();
+
 
        public static final XSSFRelation WORKBOOK = new XSSFRelation(
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
@@ -68,15 +66,15 @@ public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
                        "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
                        "/xl/worksheets/sheet#.xml",
-                       null
+                       XSSFSheet.class
        );
-       public static final XSSFRelation<SharedStringsTable> SHARED_STRINGS = create(
+       public static final XSSFRelation SHARED_STRINGS = new XSSFRelation(
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
                        "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
                        "/xl/sharedStrings.xml",
                        SharedStringsTable.class
        );
-       public static final XSSFRelation<StylesTable> STYLES = create(
+       public static final XSSFRelation STYLES = new XSSFRelation(
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
                    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
                    "/xl/styles.xml",
@@ -86,22 +84,58 @@ public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
                        "application/vnd.openxmlformats-officedocument.drawing+xml",
                        "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing",
                        "/xl/drawings/drawing#.xml",
-                       null
+                       XSSFDrawing.class
        );
-       public static final XSSFRelation<Drawing> VML_DRAWINGS = create(
+       public static final XSSFRelation VML_DRAWINGS = new XSSFRelation(
                        "application/vnd.openxmlformats-officedocument.vmlDrawing",
                        "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
                        "/xl/drawings/vmlDrawing#.vml",
-                       Drawing.class
+                       null
        );
     public static final XSSFRelation IMAGES = new XSSFRelation(
-            //client will substitute $type and $ext with the appropriate values depending on the passed data
-            "image/$type",
+            null,
                "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-               "/xl/media/image#.$ext",
-               null
+               null,
+               XSSFPictureData.class
     );
-       public static final XSSFRelation<CommentsTable> SHEET_COMMENTS = create(
+    public static final XSSFRelation IMAGE_EMF = new XSSFRelation(
+            "image/x-emf",
+               "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+               "/xl/media/image#.emf",
+               XSSFPictureData.class
+    );
+    public static final XSSFRelation IMAGE_WMF = new XSSFRelation(
+            "image/x-wmf",
+               "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+               "/xl/media/image#.wmf",
+               XSSFPictureData.class
+    );
+    public static final XSSFRelation IMAGE_PICT = new XSSFRelation(
+            "image/pict",
+               "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+               "/xl/media/image#.pict",
+               XSSFPictureData.class
+    );
+    public static final XSSFRelation IMAGE_JPEG = new XSSFRelation(
+            "image/jpeg",
+               "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+               "/xl/media/image#.jpeg",
+               XSSFPictureData.class
+    );
+    public static final XSSFRelation IMAGE_PNG = new XSSFRelation(
+            "image/png",
+               "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+               "/xl/media/image#.png",
+               XSSFPictureData.class
+    );
+    public static final XSSFRelation IMAGE_DIB = new XSSFRelation(
+            "image/dib",
+               "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+               "/xl/media/image#.dib",
+               XSSFPictureData.class
+    );
+
+  public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation(
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
                    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
                    "/xl/comments#.xml",
@@ -113,111 +147,54 @@ public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
                    null,
                    null
        );
-       public static final XSSFRelation<BinaryPart> OLEEMBEDDINGS = create(
+       public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation(
                null,
                POIXMLDocument.OLE_OBJECT_REL_TYPE,
                null,
-               BinaryPart.class
+               null
        );
-       public static final XSSFRelation<BinaryPart> PACKEMBEDDINGS = create(
+       public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation(
             null,
             POIXMLDocument.PACK_OBJECT_REL_TYPE,
             null,
-            BinaryPart.class
+            null
     );
 
-       public static final XSSFRelation<BinaryPart> VBA_MACROS = create(
+       public static final XSSFRelation VBA_MACROS = new XSSFRelation(
             "application/vnd.ms-office.vbaProject",
             "http://schemas.microsoft.com/office/2006/relationships/vbaProject",
             "/xl/vbaProject.bin",
-               BinaryPart.class
+               null
     );
-       public static final XSSFRelation<Control> ACTIVEX_CONTROLS = create(
+       public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation(
                        "application/vnd.ms-office.activeX+xml",
                        "http://schemas.openxmlformats.org/officeDocument/2006/relationships/control",
                        "/xl/activeX/activeX#.xml",
-                       Control.class
+                       null
        );
-       public static final XSSFRelation<BinaryPart> ACTIVEX_BINS = create(
+       public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation(
                        "application/vnd.ms-office.activeX",
                        "http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary",
                        "/xl/activeX/activeX#.bin",
-               BinaryPart.class
+               null
        );
-    public static final XSSFRelation<ThemeTable> THEME = create(
+    public static final XSSFRelation THEME = new XSSFRelation(
             "application/vnd.openxmlformats-officedocument.theme+xml",
             "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
             "/xl/theme/theme#.xml",
-            ThemeTable.class
+            null
     );
        
        
-    private static POILogger log = POILogFactory.getLogger(XSSFRelation.class);
-    
-    private static <R extends XSSFModel> XSSFRelation<R> create(String type, String rel, String defaultName, Class<R> cls) {
-       return new XSSFRelation<R>(type, rel, defaultName, cls);
-    }
-   
-       private Constructor<W> _constructor;
-       private final boolean _constructorTakesTwoArgs;
-       
-       private XSSFRelation(String type, String rel, String defaultName, Class<W> cls) {
-               super(type, rel, defaultName);
 
-               if (cls == null) {
-                       _constructor = null;
-                       _constructorTakesTwoArgs = false;
-               } else {
-                       Constructor<W> c;
-                       boolean twoArg;
-                       
-                       // Find the right constructor 
-                       try {
-                               c = cls.getConstructor(InputStream.class, String.class);
-                               twoArg = true;
-                       } catch(NoSuchMethodException e) {
-                               try {
-                                       c = cls.getConstructor(InputStream.class);
-                                       twoArg = false;
-                               } catch(NoSuchMethodException e2) {
-                                       throw new RuntimeException(e2);
-                               }
-                       }
-                       _constructor = c;
-                       _constructorTakesTwoArgs = twoArg;
-               }
-       }
+       private XSSFRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
+        super(type, rel, defaultName, cls);
 
-       /**
-        * Does one of these exist for the given core
-        *  package part?
-        */
-       public boolean exists(PackagePart corePart) throws InvalidFormatException {
-               if(corePart == null) {
-                       // new file, can't exist
-                       return false;
-               }
-               
-        PackageRelationshipCollection prc =
-               corePart.getRelationshipsByType(_relation);
-        Iterator<PackageRelationship> it = prc.iterator();
-        return it.hasNext();
-       }
-       
-       /**
-        * Returns the filename for the nth one of these, 
-        *  eg /xl/comments4.xml
-        */
-       public String getFileName(int index) {
-               if(_defaultName.indexOf("#") == -1) {
-                       // Generic filename in all cases
-                       return getDefaultFileName();
-               }
-               return _defaultName.replace("#", Integer.toString(index));
-       }
+        if(cls != null && !_table.containsKey(rel)) _table.put(rel, this);
+    }
 
-       /**
-        * Fetches the InputStream to read the contents, based
+    /**
+        *  Fetches the InputStream to read the contents, based
         *  of the specified core part, for which we are defined
         *  as a suitable relationship
         */
@@ -235,150 +212,16 @@ public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
                return null;
         }
        }
-       
-       /**
-        * Loads all the XSSFModels of this type which are
-        *  defined as relationships of the given parent part
-        */
-       public List<W> loadAll(PackagePart parentPart) throws Exception {
-               List<W> found = new ArrayList<W>();
-               for(PackageRelationship rel : parentPart.getRelationshipsByType(_relation)) {
-                       PackagePart part = XSSFWorkbook.getTargetPart(parentPart.getPackage(), rel);
-                       found.add(create(part, rel));
-               }
-               return found;
-       }
-       
-       /**
-        * Load a single Model, which is defined as a suitable
-        *  relationship from the specified core (parent) 
-        *  package part.
-        */
-       public W load(PackagePart corePart) throws Exception {
-        PackageRelationshipCollection prc =
-               corePart.getRelationshipsByType(_relation);
-        Iterator<PackageRelationship> it = prc.iterator();
-        if(it.hasNext()) {
-            PackageRelationship rel = it.next();
-            PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
-            PackagePart part = corePart.getPackage().getPart(relName);
-            return create(part, rel);
-        } else {
-               log.log(POILogger.WARN, "No part " + _defaultName + " found");
-               return null;
-        }
-       }
-       
-       /**
-        * Does the actual Model creation
-        */
-       private W create(PackagePart thisPart, PackageRelationship rel) 
-                       throws IOException, InvalidFormatException {
-               
-               if (_constructor == null) {
-                       throw new IllegalStateException("Model class not set");
-               }
-               // Instantiate, if we can
-               InputStream inp = thisPart.getInputStream();
-               if (inp == null) {
-                       return null; // TODO - is this valid?
-               }
-               Object[] args;
-               if (_constructorTakesTwoArgs) {
-                       args = new Object[] { inp, rel.getId(), };
-               } else {
-                       args = new Object[] { inp, };
-               }
-               W result;
-        try {
-               try {
-                               result = _constructor.newInstance(args);
-                       } catch (IllegalArgumentException e) {
-                               throw new RuntimeException(e);
-                       } catch (InstantiationException e) {
-                               throw new RuntimeException(e);
-                       } catch (IllegalAccessException e) {
-                               throw new RuntimeException(e);
-                       } catch (InvocationTargetException e) {
-                               Throwable t = e.getTargetException();
-                               if (t instanceof IOException) {
-                                       throw (IOException)t;
-                               }
-                               if (t instanceof RuntimeException) {
-                                       throw (RuntimeException)t;
-                               }
-                               throw new RuntimeException(t);
-                       }
-        } finally {
-               inp.close();
-        }
-        
-               // Do children, if required
-               if(result instanceof XSSFChildContainingModel) {
-                       XSSFChildContainingModel ccm = 
-                               (XSSFChildContainingModel)result;
-                       for(String relType : ccm.getChildrenRelationshipTypes()) {
-                               for(PackageRelationship cRel : thisPart.getRelationshipsByType(relType)) {
-                                       PackagePart childPart = XSSFWorkbook.getTargetPart(thisPart.getPackage(), cRel);
-                                       ccm.generateChild(childPart, cRel.getId());
-                               }
-                       }
-               }
-               
-               
-        return result;
-       }
-       
-       /**
-        * Save, with the default name
-        * @return The internal reference ID it was saved at, normally then used as an r:id
-        */
-       protected String save(XSSFWritableModel model, PackagePart corePart) throws IOException {
-               return save(model, corePart, _defaultName);
-       }
-       /**
-        * Save, with the name generated by the given index
-        * @return The internal reference ID it was saved at, normally then used as an r:id
-        */
-       protected String save(XSSFWritableModel model, PackagePart corePart, int index) throws IOException {
-               return save(model, corePart, getFileName(index));
-       }
-       /**
-        * Save, with the specified name
-        * @return The internal reference ID it was saved at, normally then used as an r:id
-        */
-       protected String save(XSSFWritableModel model, PackagePart corePart, String name) throws IOException {
-        PackagePartName ppName = null;
-        try {
-               ppName = PackagingURIHelper.createPartName(name);
-        } catch(InvalidFormatException e) {
-               throw new IllegalStateException("Can't create part with name " + name + " for " + model, e);
-        }
-        PackageRelationship rel =
-               corePart.addRelationship(ppName, TargetMode.INTERNAL, _relation);
-        PackagePart part = corePart.getPackage().createPart(ppName, _type);
-        
-        OutputStream out = part.getOutputStream();
-        model.writeTo(out);
-        out.close();
-        
-               // Do children, if required
-               if(model instanceof XSSFChildContainingModel) {
-                       XSSFChildContainingModel ccm = 
-                               (XSSFChildContainingModel)model;
-                       // Loop over each child, writing it out
-                       int numChildren = ccm.getNumberOfChildren();
-                       for(int i=0; i<numChildren; i++) {
-                               XSSFChildContainingModel.WritableChild child =
-                                       ccm.getChildForWriting(i);
-                               child.getRelation().save(
-                                               child.getModel(),
-                                               part, 
-                                               (i+1)
-                               );
-                       }
-               }
-        
-        return rel.getId();
-       }
+
+
+    /**
+     * Get POIXMLRelation by relation type
+     *
+     * @param rel relation type, for example,
+     *    <code>http://schemas.openxmlformats.org/officeDocument/2006/relationships/image</code>
+     * @return registered POIXMLRelation or null if not found
+     */
+    public static XSSFRelation getInstance(String rel){
+        return _table.get(rel);
+    }
 }
index 2055894a8cac70999f0f9a898d1a3334523b464a..127e43851050bf9c00632b3dfbef3772e1eabbaa 100644 (file)
@@ -22,13 +22,11 @@ import java.io.OutputStream;
 import java.util.*;
 import javax.xml.namespace.QName;
 
-import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
 import org.apache.poi.hssf.util.PaneInformation;
 import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.CommentsSource;
 import org.apache.poi.ss.usermodel.Footer;
 import org.apache.poi.ss.usermodel.Header;
-import org.apache.poi.ss.usermodel.PrintSetup;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.util.CellRangeAddress;
@@ -71,10 +69,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
     protected CommentsSource sheetComments;
     protected CTMergeCells ctMergeCells;
 
-
-    protected List<Control> controls;
-
-
     public static final short LeftMargin = 0;
     public static final short RightMargin = 1;
     public static final short TopMargin = 2;
@@ -167,7 +161,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
 
     public List<Control> getControls()
     {
-        return controls;
+        return null;
     }
 
     /**
@@ -267,7 +261,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
         if(ctDrawing == null) {
             //drawingNumber = #drawings.size() + 1
             int drawingNumber = getPackagePart().getPackage().getPartsByRelationshipType(XSSFRelation.DRAWINGS.getRelation()).size() + 1;
-            drawing = (XSSFDrawing)createRelationship(XSSFRelation.DRAWINGS, XSSFDrawing.class, drawingNumber);
+            drawing = (XSSFDrawing)createRelationship(XSSFRelation.DRAWINGS, XSSFFactory.getInstance(), drawingNumber);
             String relId = drawing.getPackageRelationship().getId();
 
             //add CT_Drawing element which indicates that this sheet contains drawing components built on the drawingML platform.
@@ -632,7 +626,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
     public short getLeftCol() {
         String cellRef = worksheet.getSheetViews().getSheetViewArray(0).getTopLeftCell();
         CellReference cellReference = new CellReference(cellRef);
-        return (short)cellReference.getCol();
+        return cellReference.getCol();
     }
 
     public double getMargin(short margin) {
@@ -1220,8 +1214,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
      * @see #setZoom(int)
      */
     public void setZoom(int numerator, int denominator) {
-        Float result = new Float(numerator)/new Float(denominator)*100;
-        setZoom(result.intValue());
+        int zoom = 100*numerator/denominator;
+        setZoom(zoom);
     }
 
     /**
@@ -1368,7 +1362,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
 
     public void ungroupRow(int fromRow, int toRow) {
         for(int i=fromRow;i<=toRow;i++){
-            XSSFRow xrow=(XSSFRow)getRow(i-1);
+            XSSFRow xrow=getRow(i-1);
             if(xrow!=null){
                 CTRow ctrow=xrow.getCTRow();
                 short outlinelevel=ctrow.getOutlineLevel();
@@ -1384,12 +1378,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
 
     private void setSheetFormatPrOutlineLevelRow(){
         short maxLevelRow=getMaxOutlineLevelRows();
-        getSheetTypeSheetFormatPr().setOutlineLevelRow((short)(maxLevelRow));
+        getSheetTypeSheetFormatPr().setOutlineLevelRow(maxLevelRow);
     }
 
     private void setSheetFormatPrOutlineLevelCol(){
         short maxLevelCol=getMaxOutlineLevelCols();
-        getSheetTypeSheetFormatPr().setOutlineLevelCol((short)(maxLevelCol));
+        getSheetTypeSheetFormatPr().setOutlineLevelCol(maxLevelCol);
     }
 
     protected CTSheetViews getSheetTypeSheetViews() {
@@ -1519,7 +1513,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
 
     private CommentsSource getComments() {
         if (sheetComments == null) {
-            sheetComments = (CommentsTable)createRelationship(XSSFRelation.SHEET_COMMENTS, CommentsTable.class, (int)sheet.getSheetId());
+            sheetComments = (CommentsTable)createRelationship(XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), (int)sheet.getSheetId());
         }
         return sheetComments;
     }
index bcc61660f6f8e8efa85add1629fb3b4191f69207..8a10a266cebbbacf2c4c89d15236e6e459c4dbd1 100644 (file)
@@ -19,14 +19,10 @@ package org.apache.poi.xssf.usermodel;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.InputStream;
-import java.io.ByteArrayInputStream;
 import java.util.*;
 import javax.xml.namespace.QName;
 import org.apache.poi.POIXMLDocument;
 import org.apache.poi.POIXMLDocumentPart;
-import org.apache.poi.ss.usermodel.Palette;
-import org.apache.poi.ss.usermodel.PictureData;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -34,12 +30,10 @@ import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.util.PackageHelper;
-import org.apache.poi.util.IOUtils;
 import org.apache.poi.xssf.model.*;
 import org.apache.poi.POIXMLException;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
-import org.openxml4j.exceptions.InvalidFormatException;
 import org.openxml4j.exceptions.OpenXML4JException;
 import org.openxml4j.opc.*;
 import org.openxml4j.opc.Package;
@@ -131,7 +125,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
     public XSSFWorkbook(Package pkg) throws IOException {
         super();
         if(pkg.getPackageAccess() == PackageAccess.READ){
-            //current implementation of OpenXML4J is funny.
+            //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
@@ -155,7 +149,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
 
         try {
             //build the POIXMLDocumentPart tree, this workbook is the root
-            read(new XSSFFactory());
+            read(XSSFFactory.getInstance());
 
             PackagePart corePart = getCorePart();
 
@@ -202,7 +196,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
 
             if(sharedStringSource == null) {
                 //Create SST if it is missing
-                sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, SharedStringsTable.class);
+                sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
             }
 
             // Process the named ranges
@@ -240,8 +234,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
         bv.setActiveTab(0);
         workbook.addNewSheets();
 
-        sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, SharedStringsTable.class);
-        stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, StylesTable.class);
+        sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
+        stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
 
         namedRanges = new LinkedList<XSSFName>();
         sheets = new LinkedList<XSSFSheet>();
@@ -273,7 +267,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
      */
     public int addPicture(byte[] pictureData, int format) {
         int imageNumber = getAllPictures().size() + 1;
-        XSSFPictureData img = (XSSFPictureData)createRelationship(XSSFPictureData.RELATIONS[format], XSSFPictureData.class, imageNumber, true);
+        XSSFPictureData img = (XSSFPictureData)createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true);
         try {
             OutputStream out = img.getPackagePart().getOutputStream();
             out.write(pictureData);
@@ -392,7 +386,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
                throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
 
         int sheetNumber = getNumberOfSheets() + 1;
-        XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFSheet.class, sheetNumber);
+        XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber);
 
         CTSheet sheet = addSheet(sheetname);
         wrapper.sheet = sheet;
@@ -496,11 +490,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
         return stylesSource.getStyleAt(idx);
     }
 
-    public Palette getCustomPalette() {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
     /**
      * Get the font at the given index number
      *
@@ -508,7 +497,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
      * @return XSSFFont at the index
      */
     public XSSFFont getFontAt(short idx) {
-        return (XSSFFont)stylesSource.getFontAt(idx);
+        return stylesSource.getFontAt(idx);
     }
 
     /**