diff options
author | PJ Fanning <fanningpj@apache.org> | 2022-07-16 10:13:18 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2022-07-16 10:13:18 +0000 |
commit | 4f54f29a2daa62839d22804d22544e4197cfda75 (patch) | |
tree | 367341b6fac45df026ce3673a421a23567fab957 /poi-examples/src | |
parent | feae2c87490392f2e69212949a1e734e930e7e20 (diff) | |
download | poi-4f54f29a2daa62839d22804d22544e4197cfda75.tar.gz poi-4f54f29a2daa62839d22804d22544e4197cfda75.zip |
[github-345] Use switch instead of if. Thanks to XenoAmess. This closes #345
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902772 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-examples/src')
-rw-r--r-- | poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java | 145 | ||||
-rw-r--r-- | poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/EmbeddedObjects.java | 50 |
2 files changed, 110 insertions, 85 deletions
diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java index 68ea9229ef..8eabd2ced9 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java @@ -68,79 +68,96 @@ public final class LoadEmbedded { for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) { //the OLE2 Class Name of the object String oleName = obj.getOLE2ClassName(); - if (oleName.equals("Worksheet")) { - DirectoryNode dn = (DirectoryNode) obj.getDirectory(); - HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, false); - embeddedWorkbook.close(); - } else if (oleName.equals("Document")) { - DirectoryNode dn = (DirectoryNode) obj.getDirectory(); - HWPFDocument embeddedWordDocument = new HWPFDocument(dn); - embeddedWordDocument.close(); - } else if (oleName.equals("Presentation")) { - DirectoryNode dn = (DirectoryNode) obj.getDirectory(); - SlideShow<?,?> embeddedSlieShow = new HSLFSlideShow(dn); - embeddedSlieShow.close(); - } else { - if(obj.hasDirectoryEntry()){ - // The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is + switch (oleName) { + case "Worksheet": { DirectoryNode dn = (DirectoryNode) obj.getDirectory(); - for (Entry entry : dn) { - //System.out.println(oleName + "." + entry.getName()); - } - } else { - // There is no DirectoryEntry - // Recover the object's data from the HSSFObjectData instance. - byte[] objectData = obj.getObjectData(); + HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, false); + embeddedWorkbook.close(); + break; + } + case "Document": { + DirectoryNode dn = (DirectoryNode) obj.getDirectory(); + HWPFDocument embeddedWordDocument = new HWPFDocument(dn); + embeddedWordDocument.close(); + break; + } + case "Presentation": { + DirectoryNode dn = (DirectoryNode) obj.getDirectory(); + SlideShow<?, ?> embeddedSlieShow = new HSLFSlideShow(dn); + embeddedSlieShow.close(); + break; } + default: + if (obj.hasDirectoryEntry()) { + // The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is + DirectoryNode dn = (DirectoryNode) obj.getDirectory(); + for (Entry entry : dn) { + //System.out.println(oleName + "." + entry.getName()); + } + } else { + // There is no DirectoryEntry + // Recover the object's data from the HSSFObjectData instance. + byte[] objectData = obj.getObjectData(); + } + break; } } } - public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, OpenXML4JException, XmlException { + public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, + OpenXML4JException, XmlException { for (PackagePart pPart : workbook.getAllEmbeddedParts()) { String contentType = pPart.getContentType(); - if (contentType.equals("application/vnd.ms-excel")) { - // Excel Workbook - either binary or OpenXML - try (InputStream stream = pPart.getInputStream()) { - HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(stream); - embeddedWorkbook.close(); - } - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { - // Excel Workbook - OpenXML file format - try (InputStream stream = pPart.getInputStream()) { - XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(stream); - embeddedWorkbook.close(); - } - } else if (contentType.equals("application/msword")) { - // Word Document - binary (OLE2CDF) file format - try (InputStream stream = pPart.getInputStream()) { - HWPFDocument document = new HWPFDocument(stream); - document.close(); - } - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) { - // Word Document - OpenXML file format - try (InputStream stream = pPart.getInputStream()) { - XWPFDocument document = new XWPFDocument(stream); - document.close(); - } - } else if (contentType.equals("application/vnd.ms-powerpoint")) { - // PowerPoint Document - binary file format - try (InputStream stream = pPart.getInputStream()) { - HSLFSlideShow slideShow = new HSLFSlideShow(stream); - slideShow.close(); - } - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) { - // PowerPoint Document - OpenXML file format - try (InputStream stream = pPart.getInputStream()) { - XMLSlideShow slideShow = new XMLSlideShow(stream); - slideShow.close(); - } - } else { - // Any other type of embedded object. - System.out.println("Unknown Embedded Document: " + contentType); - try (InputStream inputStream = pPart.getInputStream()) { + switch (contentType) { + case "application/vnd.ms-excel": + // Excel Workbook - either binary or OpenXML + try (InputStream stream = pPart.getInputStream()) { + HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(stream); + embeddedWorkbook.close(); + } + break; + case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": + // Excel Workbook - OpenXML file format + try (InputStream stream = pPart.getInputStream()) { + XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(stream); + embeddedWorkbook.close(); + } + break; + case "application/msword": + // Word Document - binary (OLE2CDF) file format + try (InputStream stream = pPart.getInputStream()) { + HWPFDocument document = new HWPFDocument(stream); + document.close(); + } + break; + case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": + // Word Document - OpenXML file format + try (InputStream stream = pPart.getInputStream()) { + XWPFDocument document = new XWPFDocument(stream); + document.close(); + } + break; + case "application/vnd.ms-powerpoint": + // PowerPoint Document - binary file format + try (InputStream stream = pPart.getInputStream()) { + HSLFSlideShow slideShow = new HSLFSlideShow(stream); + slideShow.close(); + } + break; + case "application/vnd.openxmlformats-officedocument.presentationml.presentation": + // PowerPoint Document - OpenXML file format + try (InputStream stream = pPart.getInputStream()) { + XMLSlideShow slideShow = new XMLSlideShow(stream); + slideShow.close(); + } + break; + default: + // Any other type of embedded object. + System.out.println("Unknown Embedded Document: " + contentType); + try (InputStream inputStream = pPart.getInputStream()) { - } + } + break; } } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/EmbeddedObjects.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/EmbeddedObjects.java index b4af95c5ae..93f12a0d76 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/EmbeddedObjects.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/EmbeddedObjects.java @@ -37,27 +37,35 @@ public class EmbeddedObjects { String contentType = pPart.getContentType(); try (InputStream is = pPart.getInputStream()) { Closeable document; - if (contentType.equals("application/vnd.ms-excel")) { - // Excel Workbook - either binary or OpenXML - document = new HSSFWorkbook(is); - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { - // Excel Workbook - OpenXML file format - document = new XSSFWorkbook(is); - } else if (contentType.equals("application/msword")) { - // Word Document - binary (OLE2CDF) file format - document = new HWPFDocument(is); - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) { - // Word Document - OpenXML file format - document = new XWPFDocument(is); - } else if (contentType.equals("application/vnd.ms-powerpoint")) { - // PowerPoint Document - binary file format - document = new HSLFSlideShow(is); - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) { - // PowerPoint Document - OpenXML file format - document = new XMLSlideShow(is); - } else { - // Any other type of embedded object. - document = is; + switch (contentType) { + case "application/vnd.ms-excel": + // Excel Workbook - either binary or OpenXML + document = new HSSFWorkbook(is); + break; + case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": + // Excel Workbook - OpenXML file format + document = new XSSFWorkbook(is); + break; + case "application/msword": + // Word Document - binary (OLE2CDF) file format + document = new HWPFDocument(is); + break; + case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": + // Word Document - OpenXML file format + document = new XWPFDocument(is); + break; + case "application/vnd.ms-powerpoint": + // PowerPoint Document - binary file format + document = new HSLFSlideShow(is); + break; + case "application/vnd.openxmlformats-officedocument.presentationml.presentation": + // PowerPoint Document - OpenXML file format + document = new XMLSlideShow(is); + break; + default: + // Any other type of embedded object. + document = is; + break; } document.close(); } |