Skip to content

Commit

Permalink
refactor: minor refactoring of the MIMEType utility enum
Browse files Browse the repository at this point in the history
- 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<MIMEType`, defaulting to `OTHER` if no specific MIME type
  is found
  • Loading branch information
rdeltour committed Nov 17, 2022
1 parent cbc0b2a commit 2e474e3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 44 deletions.
9 changes: 4 additions & 5 deletions src/main/java/org/w3c/epubcheck/constants/MIMEType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.google.common.base.Optional;

//FIXME 2021 Romain - document
public enum MIMEType
{
Expand All @@ -22,7 +20,8 @@ public enum MIMEType
SEARCH_KEY_MAP("application/vnd.epub.search-key-map+xml"),
SMIL("application/smil+xml"),
SVG("image/svg+xml"),
XHTML("application/xhtml+xml");
XHTML("application/xhtml+xml"),
OTHER("");

private static final Map<String, MIMEType> ENUM_MAP;

Expand Down Expand Up @@ -53,8 +52,8 @@ public boolean is(String string)
ENUM_MAP = Collections.unmodifiableMap(map);
}

public static Optional<MIMEType> 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);
}
}
74 changes: 35 additions & 39 deletions src/main/java/org/w3c/epubcheck/core/CheckerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,44 +24,41 @@ public enum CheckerTypes

public static Checker newChecker(ValidationContext context)
{
Optional<MIMEType> 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);
}
Expand Down

0 comments on commit 2e474e3

Please sign in to comment.