]> source.dussan.org Git - poi.git/commitdiff
#61940 - Replace ClassID statics with enum
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 31 Dec 2017 00:13:38 +0000 (00:13 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 31 Dec 2017 00:13:38 +0000 (00:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1819705 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hpsf/ClassID.java
src/java/org/apache/poi/hpsf/ClassIDPredefined.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java

index ec9c35555e637361839a0a45359270ede0aab261..47eb272daa1d75dce2a31f9946da1989497f8e96 100644 (file)
@@ -30,43 +30,60 @@ import org.apache.poi.util.HexDump;
  * The ClassID (or CLSID) is a UUID - see RFC 4122 
  */
 public class ClassID {
-    public static final ClassID OLE10_PACKAGE  = new ClassID("{0003000C-0000-0000-C000-000000000046}");
-    public static final ClassID PPT_SHOW       = new ClassID("{64818D10-4F9B-11CF-86EA-00AA00B929E8}");
-    public static final ClassID XLS_WORKBOOK   = new ClassID("{00020841-0000-0000-C000-000000000046}");
-    public static final ClassID TXT_ONLY       = new ClassID("{5e941d80-bf96-11cd-b579-08002b30bfeb}");
-
-    // Excel V3
-    public static final ClassID EXCEL_V3       = new ClassID("{00030000-0000-0000-C000-000000000046}");
-    public static final ClassID EXCEL_V3_CHART = new ClassID("{00030001-0000-0000-C000-000000000046}");
-    public static final ClassID EXCEL_V3_MACRO = new ClassID("{00030002-0000-0000-C000-000000000046}");
-    // Excel V5
-    public static final ClassID EXCEL95        = new ClassID("{00020810-0000-0000-C000-000000000046}");
-    public static final ClassID EXCEL95_CHART  = new ClassID("{00020811-0000-0000-C000-000000000046}");
-    // Excel V8
-    public static final ClassID EXCEL97        = new ClassID("{00020820-0000-0000-C000-000000000046}");
-    public static final ClassID EXCEL97_CHART  = new ClassID("{00020821-0000-0000-C000-000000000046}");
-    // Excel V11
-    public static final ClassID EXCEL2003      = new ClassID("{00020812-0000-0000-C000-000000000046}");
-    // Excel V12
-    public static final ClassID EXCEL2007      = new ClassID("{00020830-0000-0000-C000-000000000046}");
-    public static final ClassID EXCEL2007_MACRO= new ClassID("{00020832-0000-0000-C000-000000000046}");
-    public static final ClassID EXCEL2007_XLSB = new ClassID("{00020833-0000-0000-C000-000000000046}");
-    // Excel V14
-    public static final ClassID EXCEL2010      = new ClassID("{00024500-0000-0000-C000-000000000046}");
-    public static final ClassID EXCEL2010_CHART= new ClassID("{00024505-0014-0000-C000-000000000046}");
-    public static final ClassID EXCEL2010_ODS  = new ClassID("{EABCECDB-CC1C-4A6F-B4E3-7F888A5ADFC8}");
-    
-    public static final ClassID WORD97         = new ClassID("{00020906-0000-0000-C000-000000000046}");
-    public static final ClassID WORD95         = new ClassID("{00020900-0000-0000-C000-000000000046}");
-    public static final ClassID WORD2007       = new ClassID("{F4754C9B-64F5-4B40-8AF4-679732AC0607}");
-    public static final ClassID WORD2007_MACRO = new ClassID("{18A06B6B-2F3F-4E2B-A611-52BE631B2D22}");
-    
-    public static final ClassID POWERPOINT97   = new ClassID("{64818D10-4F9B-11CF-86EA-00AA00B929E8}");
-    public static final ClassID POWERPOINT95   = new ClassID("{EA7BAE70-FB3B-11CD-A903-00AA00510EA3}");
-    public static final ClassID POWERPOINT2007 = new ClassID("{CF4F55F4-8F87-4D47-80BB-5808164BB3F8}");
-    public static final ClassID POWERPOINT2007_MACRO = new ClassID("{DC020317-E6E2-4A62-B9FA-B3EFE16626F4}");
-    
-    public static final ClassID EQUATION30     = new ClassID("{0002CE02-0000-0000-C000-000000000046}");
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID OLE10_PACKAGE  = ClassIDPredefined.OLE_V1_PACKAGE.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID PPT_SHOW       = ClassIDPredefined.POWERPOINT_V8.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID XLS_WORKBOOK   = ClassIDPredefined.EXCEL_V7_WORKBOOK.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID TXT_ONLY       = ClassIDPredefined.TXT_ONLY.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL_V3       = ClassIDPredefined.EXCEL_V3.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL_V3_CHART = ClassIDPredefined.EXCEL_V3_CHART.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL_V3_MACRO = ClassIDPredefined.EXCEL_V3_MACRO.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL95        = ClassIDPredefined.EXCEL_V7.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL95_CHART  = ClassIDPredefined.EXCEL_V7_CHART.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL97        = ClassIDPredefined.EXCEL_V8.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL97_CHART  = ClassIDPredefined.EXCEL_V8_CHART.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL2003      = ClassIDPredefined.EXCEL_V11.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL2007      = ClassIDPredefined.EXCEL_V12.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL2007_MACRO= ClassIDPredefined.EXCEL_V12_MACRO.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL2007_XLSB = ClassIDPredefined.EXCEL_V12_XLSB.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL2010      = ClassIDPredefined.EXCEL_V14.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL2010_CHART= ClassIDPredefined.EXCEL_V14_CHART.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EXCEL2010_ODS  = ClassIDPredefined.EXCEL_V14_ODS.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID WORD95         = ClassIDPredefined.WORD_V7.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID WORD97         = ClassIDPredefined.WORD_V8.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID WORD2007       = ClassIDPredefined.WORD_V12.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID WORD2007_MACRO = ClassIDPredefined.WORD_V12_MACRO.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID POWERPOINT97   = ClassIDPredefined.POWERPOINT_V8.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID POWERPOINT95   = ClassIDPredefined.POWERPOINT_V7.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID POWERPOINT2007 = ClassIDPredefined.POWERPOINT_V12.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID POWERPOINT2007_MACRO = ClassIDPredefined.POWERPOINT_V12_MACRO.getClassID();
+    /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
+    public static final ClassID EQUATION30     = ClassIDPredefined.EQUATION_V3.getClassID();
        
     /** The number of bytes occupied by this object in the byte stream. */
     public static final int LENGTH = 16;
diff --git a/src/java/org/apache/poi/hpsf/ClassIDPredefined.java b/src/java/org/apache/poi/hpsf/ClassIDPredefined.java
new file mode 100644 (file)
index 0000000..bf85c24
--- /dev/null
@@ -0,0 +1,134 @@
+/* ====================================================================
+   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.hpsf;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public enum ClassIDPredefined {
+    /** OLE 1.0 package manager */
+    OLE_V1_PACKAGE       ("{0003000C-0000-0000-C000-000000000046}", ".bin", null),
+    /** Excel V3 - document */
+    EXCEL_V3             ("{00030000-0000-0000-C000-000000000046}", ".xls", "application/vnd.ms-excel"),
+    /** Excel V3 - chart */
+    EXCEL_V3_CHART       ("{00030001-0000-0000-C000-000000000046}", null, null),
+    /** Excel V3 - macro */
+    EXCEL_V3_MACRO       ("{00030002-0000-0000-C000-000000000046}", null, null),
+    /** Excel V7 / 95 - document */
+    EXCEL_V7             ("{00020810-0000-0000-C000-000000000046}", ".xls", "application/vnd.ms-excel"),
+    /** Excel V7 / 95 - workbook */
+    EXCEL_V7_WORKBOOK    ("{00020841-0000-0000-C000-000000000046}", null, null),
+    /** Excel V7 / 95 - chart */
+    EXCEL_V7_CHART       ("{00020811-0000-0000-C000-000000000046}", null, null),
+    /** Excel V8 / 97 - document */
+    EXCEL_V8             ("{00020820-0000-0000-C000-000000000046}", ".xls", "application/vnd.ms-excel"),
+    /** Excel V8 / 97 - chart */
+    EXCEL_V8_CHART       ("{00020821-0000-0000-C000-000000000046}", null, null),
+    /** Excel V11 / 2003 - document */
+    EXCEL_V11            ("{00020812-0000-0000-C000-000000000046}", ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
+    /** Excel V12 / 2007 - document */
+    EXCEL_V12            ("{00020830-0000-0000-C000-000000000046}", ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
+    /** Excel V12 / 2007 - macro */
+    EXCEL_V12_MACRO      ("{00020832-0000-0000-C000-000000000046}", ".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"),
+    /** Excel V12 / 2007 - xlsb document */
+    EXCEL_V12_XLSB       ("{00020833-0000-0000-C000-000000000046}", ".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12"),
+    /* Excel V14 / 2010 - document */
+    EXCEL_V14            ("{00024500-0000-0000-C000-000000000046}", ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
+    /* Excel V14 / 2010 - workbook */
+    EXCEL_V14_WORKBOOK   ("{000208D5-0000-0000-C000-000000000046}", null, null),
+    /* Excel V14 / 2010 - chart */
+    EXCEL_V14_CHART      ("{00024505-0014-0000-C000-000000000046}", null, null),
+    /** Excel V14 / 2010 - OpenDocument spreadsheet */
+    EXCEL_V14_ODS        ("{EABCECDB-CC1C-4A6F-B4E3-7F888A5ADFC8}", ".ods", "application/vnd.oasis.opendocument.spreadsheet"),
+    /** Word V7 / 95 - document */
+    WORD_V7              ("{00020900-0000-0000-C000-000000000046}", ".doc", "application/msword"),
+    /** Word V8 / 97 - document */
+    WORD_V8              ("{00020906-0000-0000-C000-000000000046}", ".doc", "application/msword"),
+    /** Word V12 / 2007 - document */
+    WORD_V12             ("{F4754C9B-64F5-4B40-8AF4-679732AC0607}", ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
+    /** Word V12 / 2007 - macro */
+    WORD_V12_MACRO       ("{18A06B6B-2F3F-4E2B-A611-52BE631B2D22}", ".docm", "application/vnd.ms-word.document.macroEnabled.12"),
+    /** Powerpoint V7 / 95 - document */
+    POWERPOINT_V7        ("{EA7BAE70-FB3B-11CD-A903-00AA00510EA3}", ".ppt", "application/vnd.ms-powerpoint"),
+    /** Powerpoint V7 / 95 - slide */
+    POWERPOINT_V7_SLIDE  ("{EA7BAE71-FB3B-11CD-A903-00AA00510EA3}", null, null),
+    /** Powerpoint V8 / 97 - document */
+    POWERPOINT_V8        ("{64818D10-4F9B-11CF-86EA-00AA00B929E8}", ".ppt", "application/vnd.ms-powerpoint"),
+    /** Powerpoint V8 / 97 - template */
+    POWERPOINT_V8_TPL    ("{64818D11-4F9B-11CF-86EA-00AA00B929E8}", ".pot", "application/vnd.ms-powerpoint"),
+    /** Powerpoint V12 / 2007 - document */
+    POWERPOINT_V12       ("{CF4F55F4-8F87-4D47-80BB-5808164BB3F8}", ".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"),
+    /** Powerpoint V12 / 2007 - macro */
+    POWERPOINT_V12_MACRO ("{DC020317-E6E2-4A62-B9FA-B3EFE16626F4}", ".pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12"),
+    /** Publisher V12 */
+    PUBLISHER_V12        ("{0002123D-0000-0000-C000-000000000046}", ".pub", "application/x-mspublisher"),
+    /** Visio 2000 (V6) / 2002 (V10) - drawing */
+    VISIO_V10            ("{00021A14-0000-0000-C000-000000000046}", ".vsd", "application/vnd.visio"),
+    /** Equation Editor 3.0 */
+    EQUATION_V3          ("{0002CE02-0000-0000-C000-000000000046}", null, null),
+    /** AcroExch.Document */
+    PDF                  ("{B801CA65-A1FC-11D0-85AD-444553540000}", ".pdf", "application/pdf"),
+    /** Plain Text Persistent Handler **/
+    TXT_ONLY             ("{5e941d80-bf96-11cd-b579-08002b30bfeb}", ".txt", "text/plain")
+    ;
+    
+    private static final Map<String,ClassIDPredefined> LOOKUP = new HashMap<>();
+
+    static {
+        for (ClassIDPredefined p : values()) {
+            LOOKUP.put(p.externalForm, p);
+        }
+    }
+    
+    private final String externalForm;
+    private ClassID classId;
+    private final String fileExtension;
+    private final String contentType;
+    
+    ClassIDPredefined(final String externalForm, final String fileExtension, final String contentType) {
+        this.externalForm = externalForm;
+        this.fileExtension = fileExtension;
+        this.contentType = contentType;
+    }
+    
+    public ClassID getClassID() {
+        synchronized (this) {
+            // TODO: init classId directly in the constructor when old statics have been removed from ClassID
+            if (classId == null) {
+                classId = new ClassID(externalForm);
+            }
+        }
+        return classId;
+    }
+
+    public String getFileExtension() {
+        return fileExtension;
+    }
+
+    public String getContentType() {
+        return contentType;
+    }
+    
+    public static ClassIDPredefined lookup(final String externalForm) {
+        return LOOKUP.get(externalForm);
+    }
+
+    public static ClassIDPredefined lookup(final ClassID classID) {
+        return (classID == null) ? null : LOOKUP.get(classID.toString());
+    }
+}
index 3ae745388814bd801762d4339e05fc5c535b8890..8ea6df28a64e643a3c578bc492168a6fb0d67488 100644 (file)
@@ -29,6 +29,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.poi.hpsf.ClassID;
+import org.apache.poi.hpsf.ClassIDPredefined;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.DocumentInputStream;
 import org.apache.poi.poifs.filesystem.Entry;
@@ -184,7 +185,7 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
         @Override
         public boolean canExtract(DirectoryNode dn) {
             ClassID clsId = dn.getStorageClsid();
-            return ClassID.OLE10_PACKAGE.equals(clsId);
+            return ClassIDPredefined.lookup(clsId) == ClassIDPredefined.OLE_V1_PACKAGE;
         }
 
         @Override
@@ -271,33 +272,19 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
         @Override
         public EmbeddedData extract(DirectoryNode dn) throws IOException {
 
-            ClassID clsId = dn.getStorageClsid();
+            ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid());
 
-            String contentType, ext;
-            if (ClassID.WORD2007.equals(clsId)) {
-                ext = ".docx";
-                contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
-            } else if (ClassID.WORD2007_MACRO.equals(clsId)) {
-                ext = ".docm";
-                contentType = "application/vnd.ms-word.document.macroEnabled.12";
-            } else if (ClassID.EXCEL2007.equals(clsId) || ClassID.EXCEL2003.equals(clsId) || ClassID.EXCEL2010.equals(clsId)) {
-                ext = ".xlsx";
-                contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
-            } else if (ClassID.EXCEL2007_MACRO.equals(clsId)) {
-                ext = ".xlsm";
-                contentType = "application/vnd.ms-excel.sheet.macroEnabled.12";
-            } else if (ClassID.EXCEL2007_XLSB.equals(clsId)) {
-                ext = ".xlsb";
-                contentType = "application/vnd.ms-excel.sheet.binary.macroEnabled.12";
-            } else if (ClassID.POWERPOINT2007.equals(clsId)) {
-                ext = ".pptx";
-                contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
-            } else if (ClassID.POWERPOINT2007_MACRO.equals(clsId)) {
-                ext = ".ppsm";
-                contentType = "application/vnd.ms-powerpoint.slideshow.macroEnabled.12";
-            } else {
-                ext = ".zip";
+            String contentType = null;
+            String ext = null;
+            
+            if (clsId != null) {
+                contentType = clsId.getContentType();
+                ext = clsId.getFileExtension();
+            }
+            
+            if (contentType == null || ext == null) {
                 contentType = "application/zip";
+                ext = ".zip";
             }
 
             DocumentInputStream dis = dn.createDocumentInputStream("package");
@@ -315,16 +302,16 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
         }
         
         protected boolean canExtractExcel(DirectoryNode dn) {
-            ClassID clsId = dn.getStorageClsid();
-            return (ClassID.EXCEL95.equals(clsId)
-                || ClassID.EXCEL97.equals(clsId)
+            ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid());
+            return (ClassIDPredefined.EXCEL_V7 == clsId
+                || ClassIDPredefined.EXCEL_V8 == clsId
                 || dn.hasEntry("Workbook") /*...*/);
         }
 
         protected boolean canExtractWord(DirectoryNode dn) {
-            ClassID clsId = dn.getStorageClsid();
-            return (ClassID.WORD95.equals(clsId)
-                || ClassID.WORD97.equals(clsId)
+            ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid());
+            return (ClassIDPredefined.WORD_V7 == clsId
+                || ClassIDPredefined.WORD_V8 == clsId
                 || dn.hasEntry("WordDocument"));
         }
         
index bb8e2be2b3e792891d78d8bb1598d28d47133a16..78ec93481b8d452b802be4cd470e7c7c99a345cd 100644 (file)
@@ -47,7 +47,7 @@ import org.apache.poi.POIXMLDocument;
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
 import org.apache.poi.POIXMLProperties;
-import org.apache.poi.hpsf.ClassID;
+import org.apache.poi.hpsf.ClassIDPredefined;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
@@ -2373,7 +2373,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
             try (POIFSFileSystem poifs = new POIFSFileSystem()) {
                 DirectoryNode root = poifs.getRoot();
                 root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
-                root.setStorageClsid(ClassID.OLE10_PACKAGE);
+                root.setStorageClsid(ClassIDPredefined.OLE_V1_PACKAGE.getClassID());
 
                 // TODO: generate CombObj stream