You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ju...@apache.org on 2010/04/29 10:54:21 UTC

svn commit: r939235 - /lucene/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java

Author: jukka
Date: Thu Apr 29 08:54:21 2010
New Revision: 939235

URL: http://svn.apache.org/viewvc?rev=939235&view=rev
Log:
TIKA-89: Rename MimeType and MimeTypes

Extending the MediaTypeRegistry class

Modified:
    lucene/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java

Modified: lucene/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java
URL: http://svn.apache.org/viewvc/lucene/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java?rev=939235&r1=939234&r2=939235&view=diff
==============================================================================
--- lucene/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java (original)
+++ lucene/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java Thu Apr 29 08:54:21 2010
@@ -1,4 +1,4 @@
-/**
+/*
  * 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.
@@ -20,23 +20,73 @@ import java.util.HashMap;
 import java.util.Map;
 
 /**
- * Registry of Internet media types.
+ * Registry of known Internet media types.
  */
-public final class MediaTypeRegistry {
+public class MediaTypeRegistry {
+
+    /**
+     * Registry of known media types, including type aliases. All the types
+     * in this map are base types, i.e. they have no parameters. A canonical
+     * media type is handled as an identity mapping, while an alias is stored
+     * as a mapping from the alias to the corresponding canonical type.
+     */
+    private final Map<MediaType, MediaType> registry =
+        new HashMap<MediaType, MediaType>();
 
-    private final Map<MediaType, MediaType> aliases =
+    /**
+     * Known type inheritance relationships. The mapping is from a media type
+     * to the closest supertype. All types in this map are canonical and have
+     * no parameters.
+     */
+    private final Map<MediaType, MediaType> inheritance =
         new HashMap<MediaType, MediaType>();
 
-    public void addAlias(MediaType canonical, MediaType alias) {
-        aliases.put(alias, canonical);
+    public void addType(MediaType type) {
+        if (type == null || type.hasParameters()) {
+            throw new IllegalArgumentException();
+        } else if (registry.containsKey(type)) {
+            throw new IllegalStateException();
+        } else {
+            registry.put(type, type);
+        }
+    }
+
+    public void addAlias(MediaType type, MediaType alias) {
+        if (type == null || alias == null
+                || type.hasParameters() || alias.hasParameters()) {
+            throw new IllegalArgumentException();
+        } else if (!registry.containsKey(type) || registry.containsKey(alias)) {
+            throw new IllegalStateException();
+        } else {
+            registry.put(alias, type);
+        }
     }
 
-    public MediaType unalias(MediaType type) {
-        MediaType canonical = aliases.get(type.getBaseType());
-        if (canonical != null) {
+    public MediaType normalize(MediaType type) {
+        MediaType canonical = registry.get(type.getBaseType());
+        if (canonical == null) {
+            return type;
+        } else if (type.hasParameters()) {
             return new MediaType(canonical, type.getParameters());
         } else {
+            return canonical;
+        }
+    }
+
+    public MediaType getSuperType(MediaType type) {
+        if (type.hasParameters()) {
             return type;
+        } else if (inheritance.containsKey(type)) {
+            return inheritance.get(type);
+        } else if (type.getSubtype().endsWith("+xml")) {
+            return MediaType.APPLICATION_XML;
+        } else if ("text".equals(type.getType())
+                && !MediaType.TEXT_PLAIN.equals(type)) {
+            return MediaType.TEXT_PLAIN;
+        } else if (!MediaType.OCTET_STREAM.equals(type)) {
+            return MediaType.OCTET_STREAM;
+        } else {
+            return null;
         }
     }