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 2011/05/18 20:05:50 UTC

svn commit: r1124360 - in /tika/trunk/tika-core/src: main/java/org/apache/tika/mime/MimeType.java main/java/org/apache/tika/mime/Patterns.java test/java/org/apache/tika/mime/PatternsTest.java

Author: jukka
Date: Wed May 18 18:05:50 2011
New Revision: 1124360

URL: http://svn.apache.org/viewvc?rev=1124360&view=rev
Log:
TIKA-661: MimeType class does contain a String with accessor named Extension. This should be a List<String> Extensions due to several reasons.

Changes based on the patch by Henning Gross

Added:
    tika/trunk/tika-core/src/test/java/org/apache/tika/mime/PatternsTest.java
Modified:
    tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeType.java
    tika/trunk/tika-core/src/main/java/org/apache/tika/mime/Patterns.java

Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeType.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeType.java?rev=1124360&r1=1124359&r2=1124360&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeType.java (original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeType.java Wed May 18 18:05:50 2011
@@ -18,6 +18,8 @@ package org.apache.tika.mime;
 
 import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
 /**
  * Internet media type.
@@ -86,8 +88,11 @@ public final class MimeType implements C
     /** The minimum length of data to provides for magic analyzis */
     private int minLength = 0;
 
-    /** Preferred extension with starting dot or empty string */
-    private String extension = "";
+    /**
+     * All known file extensions of this type, in order of preference
+     * (best first).
+     */
+    private final List<String> extensions = new ArrayList<String>();
 
     /**
      * Creates a media type with the give name and containing media type
@@ -317,15 +322,40 @@ public final class MimeType implements C
     }
 
     /**
-     * Get preferred extension
+     * Returns the preferred file extension of this type, or an empty string
+     * if no extensions are known. Use the {@link #getExtensions()} method to
+     * get the full list of known extensions of this type.
      *
-     * @return extension (with starting dot) or empty string
+     * @since Apache Tika 0.9
+     * @return preferred file extension or empty string
      */
     public String getExtension() {
-        return extension;
+        if (extensions.isEmpty()) {
+            return "";
+        } else {
+            return extensions.get(0);
+        }
     }
 
-    void setExtension(String extension) {
-        this.extension = extension;
+    /**
+     * Returns the list of all known file extensions of this media type.
+     *
+     * @since Apache Tika 1.0
+     * @return known extensions in order of preference (best first)
+     */
+    public List<String> getExtensions() {
+        return Collections.unmodifiableList(extensions);
     }
+
+    /**
+     * Adds a known file extension to this type.
+     *
+     * @param extension file extension
+     */
+    void addExtension(String extension) {
+        if (!extensions.contains(extension)) {
+            extensions.add(extension);
+        }
+    }
+
 }

Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/mime/Patterns.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/Patterns.java?rev=1124360&r1=1124359&r2=1124360&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/mime/Patterns.java (original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/mime/Patterns.java Wed May 18 18:05:50 2011
@@ -102,9 +102,7 @@ class Patterns implements Serializable {
                     && pattern.indexOf('?') == -1 && pattern.indexOf('[') == -1) {
                 String extension = pattern.substring(1);
                 addExtension(extension, type);
-                if (type.getExtension().length()==0) {
-                    type.setExtension(extension);
-                }
+                type.addExtension(extension);
             } else {
                 addGlob(compile(pattern), type);
             }

Added: tika/trunk/tika-core/src/test/java/org/apache/tika/mime/PatternsTest.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/mime/PatternsTest.java?rev=1124360&view=auto
==============================================================================
--- tika/trunk/tika-core/src/test/java/org/apache/tika/mime/PatternsTest.java (added)
+++ tika/trunk/tika-core/src/test/java/org/apache/tika/mime/PatternsTest.java Wed May 18 18:05:50 2011
@@ -0,0 +1,38 @@
+/*
+ * 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.tika.mime;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class PatternsTest extends TestCase {
+
+    private MimeTypes types = MimeTypes.getDefaultMimeTypes();
+
+    public void testExtensions() throws Exception{
+        MimeType jpeg = types.forName("image/jpeg");
+
+        assertEquals(".jpg", jpeg.getExtension());
+
+        List<String> extensions = jpeg.getExtensions();
+        assertTrue(extensions.size() > 1);
+        assertTrue(extensions.contains(".jpg"));
+        assertTrue(extensions.contains(".jpeg"));
+    }
+
+}
\ No newline at end of file