From 2e474e37c43b787f01f9be42f2474877ee3b370b Mon Sep 17 00:00:00 2001 From: Romain Deltour Date: Wed, 16 Nov 2022 10:27:16 +0100 Subject: [PATCH] refactor: minor refactoring of the MIMEType utility enum - add an `OTHER` field to represent any MIME type that is not known to the enumeration - the `#get(String)` method now returns a `MIMEType` instead of an `Optional ENUM_MAP; @@ -53,8 +52,8 @@ public boolean is(String string) ENUM_MAP = Collections.unmodifiableMap(map); } - public static Optional get(String name) + public static MIMEType get(String name) { - return Optional.fromNullable(ENUM_MAP.get(name.toLowerCase(Locale.ENGLISH))); + return ENUM_MAP.getOrDefault(name.toLowerCase(Locale.ROOT), OTHER); } } diff --git a/src/main/java/org/w3c/epubcheck/core/CheckerFactory.java b/src/main/java/org/w3c/epubcheck/core/CheckerFactory.java index 206d43582..fcacad980 100644 --- a/src/main/java/org/w3c/epubcheck/core/CheckerFactory.java +++ b/src/main/java/org/w3c/epubcheck/core/CheckerFactory.java @@ -6,14 +6,13 @@ import com.adobe.epubcheck.css.CSSChecker; import com.adobe.epubcheck.dict.SearchKeyMapChecker; import com.adobe.epubcheck.dtbook.DTBookChecker; -import com.adobe.epubcheck.opf.PublicationResourceChecker; import com.adobe.epubcheck.opf.OPFChecker; import com.adobe.epubcheck.opf.OPFChecker30; +import com.adobe.epubcheck.opf.PublicationResourceChecker; import com.adobe.epubcheck.opf.ValidationContext; import com.adobe.epubcheck.ops.OPSChecker; import com.adobe.epubcheck.overlay.OverlayChecker; import com.adobe.epubcheck.util.EPUBVersion; -import com.google.common.base.Optional; // FIXME 2021 Romain - document public final class CheckerFactory @@ -25,44 +24,41 @@ public enum CheckerTypes public static Checker newChecker(ValidationContext context) { - Optional mimeType = MIMEType.get(context.mimeType); - if (mimeType.isPresent()) + switch (MIMEType.get(context.mimeType)) { - switch (mimeType.get()) - { - case CSS: - return new CSSChecker(context); - case DTBOOK: - return new DTBookChecker(context); - case EPUB: - // FIXME 2021 Romain - ValidationContext-based EPUB checker will come later - break; - case HTML: - if (context.version == EPUBVersion.VERSION_2) return new OPSChecker(context); - break; - case IMAGE_GIF: - case IMAGE_JPEG: - case IMAGE_PNG: - return new BitmapChecker(context); - case OEBPS: - if (context.version == EPUBVersion.VERSION_2) return new OPSChecker(context); - break; - case PACKAGE_DOC: - return (context.version == EPUBVersion.VERSION_2) ? new OPFChecker(context) - : new OPFChecker30(context); - case SEARCH_KEY_MAP: - if (context.version == EPUBVersion.VERSION_3) return new SearchKeyMapChecker(context); - break; - case SVG: - return new OPSChecker(context); - case SMIL: - if (context.version == EPUBVersion.VERSION_3) return new OverlayChecker(context); - break; - case XHTML: - return new OPSChecker(context); - default: - break; - } + case CSS: + return new CSSChecker(context); + case DTBOOK: + return new DTBookChecker(context); + case EPUB: + // FIXME 2021 Romain - ValidationContext-based EPUB checker will come + // later + break; + case HTML: + if (context.version == EPUBVersion.VERSION_2) return new OPSChecker(context); + break; + case IMAGE_GIF: + case IMAGE_JPEG: + case IMAGE_PNG: + return new BitmapChecker(context); + case OEBPS: + if (context.version == EPUBVersion.VERSION_2) return new OPSChecker(context); + break; + case PACKAGE_DOC: + return (context.version == EPUBVersion.VERSION_2) ? new OPFChecker(context) + : new OPFChecker30(context); + case SEARCH_KEY_MAP: + if (context.version == EPUBVersion.VERSION_3) return new SearchKeyMapChecker(context); + break; + case SVG: + return new OPSChecker(context); + case SMIL: + if (context.version == EPUBVersion.VERSION_3) return new OverlayChecker(context); + break; + case XHTML: + return new OPSChecker(context); + default: + break; } return new PublicationResourceChecker(context); }