#61940 - Replace ClassID statics with enum

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1819705 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2017-12-31 00:13:38 +00:00
parent 4073f568cd
commit 7d3876cf12
4 changed files with 209 additions and 71 deletions

View File

@ -30,43 +30,60 @@ import org.apache.poi.util.HexDump;
* The ClassID (or CLSID) is a UUID - see RFC 4122 * The ClassID (or CLSID) is a UUID - see RFC 4122
*/ */
public class ClassID { public class ClassID {
public static final ClassID OLE10_PACKAGE = new ClassID("{0003000C-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID PPT_SHOW = new ClassID("{64818D10-4F9B-11CF-86EA-00AA00B929E8}"); public static final ClassID OLE10_PACKAGE = ClassIDPredefined.OLE_V1_PACKAGE.getClassID();
public static final ClassID XLS_WORKBOOK = new ClassID("{00020841-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID TXT_ONLY = new ClassID("{5e941d80-bf96-11cd-b579-08002b30bfeb}"); public static final ClassID PPT_SHOW = ClassIDPredefined.POWERPOINT_V8.getClassID();
/** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
// Excel V3 public static final ClassID XLS_WORKBOOK = ClassIDPredefined.EXCEL_V7_WORKBOOK.getClassID();
public static final ClassID EXCEL_V3 = new ClassID("{00030000-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID EXCEL_V3_CHART = new ClassID("{00030001-0000-0000-C000-000000000046}"); public static final ClassID TXT_ONLY = ClassIDPredefined.TXT_ONLY.getClassID();
public static final ClassID EXCEL_V3_MACRO = new ClassID("{00030002-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
// Excel V5 public static final ClassID EXCEL_V3 = ClassIDPredefined.EXCEL_V3.getClassID();
public static final ClassID EXCEL95 = new ClassID("{00020810-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID EXCEL95_CHART = new ClassID("{00020811-0000-0000-C000-000000000046}"); public static final ClassID EXCEL_V3_CHART = ClassIDPredefined.EXCEL_V3_CHART.getClassID();
// Excel V8 /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID EXCEL97 = new ClassID("{00020820-0000-0000-C000-000000000046}"); public static final ClassID EXCEL_V3_MACRO = ClassIDPredefined.EXCEL_V3_MACRO.getClassID();
public static final ClassID EXCEL97_CHART = new ClassID("{00020821-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
// Excel V11 public static final ClassID EXCEL95 = ClassIDPredefined.EXCEL_V7.getClassID();
public static final ClassID EXCEL2003 = new ClassID("{00020812-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
// Excel V12 public static final ClassID EXCEL95_CHART = ClassIDPredefined.EXCEL_V7_CHART.getClassID();
public static final ClassID EXCEL2007 = new ClassID("{00020830-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID EXCEL2007_MACRO= new ClassID("{00020832-0000-0000-C000-000000000046}"); public static final ClassID EXCEL97 = ClassIDPredefined.EXCEL_V8.getClassID();
public static final ClassID EXCEL2007_XLSB = new ClassID("{00020833-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
// Excel V14 public static final ClassID EXCEL97_CHART = ClassIDPredefined.EXCEL_V8_CHART.getClassID();
public static final ClassID EXCEL2010 = new ClassID("{00024500-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID EXCEL2010_CHART= new ClassID("{00024505-0014-0000-C000-000000000046}"); public static final ClassID EXCEL2003 = ClassIDPredefined.EXCEL_V11.getClassID();
public static final ClassID EXCEL2010_ODS = new ClassID("{EABCECDB-CC1C-4A6F-B4E3-7F888A5ADFC8}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID EXCEL2007 = ClassIDPredefined.EXCEL_V12.getClassID();
public static final ClassID WORD97 = new ClassID("{00020906-0000-0000-C000-000000000046}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID WORD95 = new ClassID("{00020900-0000-0000-C000-000000000046}"); public static final ClassID EXCEL2007_MACRO= ClassIDPredefined.EXCEL_V12_MACRO.getClassID();
public static final ClassID WORD2007 = new ClassID("{F4754C9B-64F5-4B40-8AF4-679732AC0607}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID WORD2007_MACRO = new ClassID("{18A06B6B-2F3F-4E2B-A611-52BE631B2D22}"); public static final ClassID EXCEL2007_XLSB = ClassIDPredefined.EXCEL_V12_XLSB.getClassID();
/** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID POWERPOINT97 = new ClassID("{64818D10-4F9B-11CF-86EA-00AA00B929E8}"); public static final ClassID EXCEL2010 = ClassIDPredefined.EXCEL_V14.getClassID();
public static final ClassID POWERPOINT95 = new ClassID("{EA7BAE70-FB3B-11CD-A903-00AA00510EA3}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID POWERPOINT2007 = new ClassID("{CF4F55F4-8F87-4D47-80BB-5808164BB3F8}"); public static final ClassID EXCEL2010_CHART= ClassIDPredefined.EXCEL_V14_CHART.getClassID();
public static final ClassID POWERPOINT2007_MACRO = new ClassID("{DC020317-E6E2-4A62-B9FA-B3EFE16626F4}"); /** @deprecated use enum {@link ClassIDPredefined} */ @Deprecated
public static final ClassID EXCEL2010_ODS = ClassIDPredefined.EXCEL_V14_ODS.getClassID();
public static final ClassID EQUATION30 = new ClassID("{0002CE02-0000-0000-C000-000000000046}"); /** @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. */ /** The number of bytes occupied by this object in the byte stream. */
public static final int LENGTH = 16; public static final int LENGTH = 16;

View File

@ -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());
}
}

View File

@ -29,6 +29,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import org.apache.poi.hpsf.ClassID; 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.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentInputStream; import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.poifs.filesystem.Entry;
@ -184,7 +185,7 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
@Override @Override
public boolean canExtract(DirectoryNode dn) { public boolean canExtract(DirectoryNode dn) {
ClassID clsId = dn.getStorageClsid(); ClassID clsId = dn.getStorageClsid();
return ClassID.OLE10_PACKAGE.equals(clsId); return ClassIDPredefined.lookup(clsId) == ClassIDPredefined.OLE_V1_PACKAGE;
} }
@Override @Override
@ -271,33 +272,19 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
@Override @Override
public EmbeddedData extract(DirectoryNode dn) throws IOException { public EmbeddedData extract(DirectoryNode dn) throws IOException {
ClassID clsId = dn.getStorageClsid(); ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid());
String contentType, ext; String contentType = null;
if (ClassID.WORD2007.equals(clsId)) { String ext = null;
ext = ".docx";
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; if (clsId != null) {
} else if (ClassID.WORD2007_MACRO.equals(clsId)) { contentType = clsId.getContentType();
ext = ".docm"; ext = clsId.getFileExtension();
contentType = "application/vnd.ms-word.document.macroEnabled.12"; }
} else if (ClassID.EXCEL2007.equals(clsId) || ClassID.EXCEL2003.equals(clsId) || ClassID.EXCEL2010.equals(clsId)) {
ext = ".xlsx"; if (contentType == null || ext == null) {
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";
contentType = "application/zip"; contentType = "application/zip";
ext = ".zip";
} }
DocumentInputStream dis = dn.createDocumentInputStream("package"); DocumentInputStream dis = dn.createDocumentInputStream("package");
@ -315,16 +302,16 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
} }
protected boolean canExtractExcel(DirectoryNode dn) { protected boolean canExtractExcel(DirectoryNode dn) {
ClassID clsId = dn.getStorageClsid(); ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid());
return (ClassID.EXCEL95.equals(clsId) return (ClassIDPredefined.EXCEL_V7 == clsId
|| ClassID.EXCEL97.equals(clsId) || ClassIDPredefined.EXCEL_V8 == clsId
|| dn.hasEntry("Workbook") /*...*/); || dn.hasEntry("Workbook") /*...*/);
} }
protected boolean canExtractWord(DirectoryNode dn) { protected boolean canExtractWord(DirectoryNode dn) {
ClassID clsId = dn.getStorageClsid(); ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid());
return (ClassID.WORD95.equals(clsId) return (ClassIDPredefined.WORD_V7 == clsId
|| ClassID.WORD97.equals(clsId) || ClassIDPredefined.WORD_V8 == clsId
|| dn.hasEntry("WordDocument")); || dn.hasEntry("WordDocument"));
} }

View File

@ -47,7 +47,7 @@ import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException; import org.apache.poi.POIXMLException;
import org.apache.poi.POIXMLProperties; 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.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
@ -2373,7 +2373,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
try (POIFSFileSystem poifs = new POIFSFileSystem()) { try (POIFSFileSystem poifs = new POIFSFileSystem()) {
DirectoryNode root = poifs.getRoot(); DirectoryNode root = poifs.getRoot();
root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray())); 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 // TODO: generate CombObj stream