cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From je...@apache.org
Subject cvs commit: cocoon-2.1/src/test/org/apache/cocoon/util/test MIMEUtilsTestCase.java
Date Sun, 01 Jun 2003 09:17:01 GMT
jefft       2003/06/01 02:17:01

  Modified:    src/java/org/apache/cocoon/util MIMEUtils.java
  Added:       src/java/org/apache/cocoon/util mime.types
               src/test/org/apache/cocoon/util/test MIMEUtilsTestCase.java
  Log:
  Read MIME type <--> file extension mappings from a mime.types file, instead of
  having a hardcoded list.
  
  Revision  Changes    Path
  1.4       +75 -72    cocoon-2.1/src/java/org/apache/cocoon/util/MIMEUtils.java
  
  Index: MIMEUtils.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/util/MIMEUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MIMEUtils.java	30 May 2003 15:30:19 -0000	1.3
  +++ MIMEUtils.java	1 Jun 2003 09:17:00 -0000	1.4
  @@ -54,7 +54,14 @@
   import java.io.FileNotFoundException;
   import java.io.IOException;
   import java.io.BufferedInputStream;
  +import java.io.InputStream;
   import java.io.FileInputStream;
  +import java.io.InputStreamReader;
  +import java.io.Reader;
  +import java.io.BufferedReader;
  +import java.util.Map;
  +import java.util.HashMap;
  +import java.util.StringTokenizer;
   
   /**
    * A collection of <code>File</code>, <code>URL</code> and filename
  @@ -62,81 +69,33 @@
    *
    * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
    * @author <a href="mailto:tk-cocoon@datas-world.de">Torsten Knodt</a>
  + * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
    * @version CVS $Id$
    */
   public class MIMEUtils {
   
  -    final private static HashMap extMap = new HashMap() {
  -        {
  -            put(null, "application/octet-stream");
  -            put(".text", "text/plain");
  -            put(".txt", "text/plain");
  -            put(".html", "text/html");
  -            put(".htm", "text/html");
  -            put(".xml", "text/xml");
  -            put(".css", "text/css");
  -            put(".rtf", "text/rtf");
  -            put(".wml", "text/vnd.wap.wml");
  -            put(".jpg", "image/jpeg");
  -            put(".jpeg", "image/jpeg");
  -            put(".jpe", "image/jpeg");
  -            put(".png", "image/png");
  -            put(".gif", "image/gif");
  -            put(".tif", "image/tiff");
  -            put(".tiff", "image/tiff");
  -            put(".svg", "image/svg-xml");
  -            put(".pdf", "application/pdf");
  -            put(".wrl", "model/vrml");
  -            put(".smil", "application/smil");
  -            put(".js", "application/x-javascript");
  -            put(".zip", "application/zip");
  -            put(".mpeg", "video/mpeg");
  -            put(".mpg", "video/mpeg");
  -            put(".mpe", "video/mpeg");
  -            put(".mov", "video/quicktime");
  -            put(".mid", "audio/midi");
  -            put(".mp3", "audio/mpeg");
  -            put(".vcf", "text/x-vcard");
  -            put(".rdf", "text/rdf");
  -        }
  -    };
  +    /** Our properties are now here: */
  +    private static final String PROPS_FILE = "org/apache/cocoon/util/mime.types";
  +
  +    /** Default extensions for MIME types. */
  +    final private static Map extMap = new HashMap();
  +    /** MIME types for extensions. */
  +    final private static Map mimeMap = new HashMap();
   
  -    final private static HashMap mimeMap = new HashMap() {
  -        {
  -            put(null, null);
  -            put("text/plain", ".text");
  -            put("text/html", ".html");
  -            put("text/xml", ".xml");
  -            put("text/css", ".css");
  -            put("text/rtf", ".rtf");
  -            put("application/rtf", ".rtf");
  -            put("text/vnd.wap.wml", ".wml");
  -            put("image/jpeg", ".jpeg");
  -            put("image/jpg", ".jpeg");
  -            put("image/jpe", ".jpeg");
  -            put("image/png", ".png");
  -            put("image/gif", ".gif");
  -            put("image/tiff", ".tiff");
  -            put("image/tif", ".tiff");
  -            put("image/svg-xml", ".svg");
  -            put("image/svg+xml", ".svg");
  -            put("application/pdf", ".pdf");
  -            put("model/vrml", ".wrl");
  -            put("application/smil", ".smil");
  -            put("application/x-javascript", ".js");
  -            put("application/zip", ".zip");
  -            put("video/mpeg", ".mpeg");
  -            put("video/mpeg", ".mpe");
  -            put("video/mpeg", ".mpg");
  -            put("video/quicktime", ".mov");
  -            put("audio/midi", ".mid");
  -            put("audio/mpeg", ".mp3");
  -            put("text/javascript", ".js");
  -            put("text/vbscript", ".vbs");
  -            put("text/x-vcard", ".vcf");
  -            put("text/rdf", ".rdf");
  +    /**
  +     * Load the MIME type mapping
  +     */
  +    static {
  +        try {
  +            final InputStream is = MIMEUtils.class.getClassLoader().getResourceAsStream(PROPS_FILE);
  +            if ( null == is ) {
  +                throw new RuntimeException("Cocoon cannot load MIME type mappings from
" + PROPS_FILE);
  +            }
  +            loadMimeTypes(new InputStreamReader(is), extMap, mimeMap);
  +        } catch (IOException ioe) {
  +            throw new RuntimeException("Cocoon cannot load MIME type mappings from " +
PROPS_FILE);
           }
  -    };
  +    }
   
       /**
        * Return the MIME type for a given file.
  @@ -148,6 +107,7 @@
       public static String getMIMEType(final File file)
         throws FileNotFoundException, IOException {
           BufferedInputStream in = null;
  +        System.out.println("****** "+file);
   
           try {
               in = new BufferedInputStream(new FileInputStream(file));
  @@ -187,7 +147,7 @@
        * @return MIME type.
        */
       public static String getMIMEType(final String ext) {
  -        return (String) extMap.get(ext);
  +        return (String) mimeMap.get(ext);
       }
   
       /**
  @@ -198,6 +158,49 @@
        * @return Filename extension.
        */
       public static String getDefaultExtension(final String type) {
  -        return (String) mimeMap.get(type);
  +        return (String) extMap.get(type);
  +    }
  +
  +
  +    /**
  +     * Parses a <code>mime.types</code> file, and generates mappings between
  +     * MIME types and extensions.
  +     * For example, if a line contains:
  +     * <pre>text/html   html htm</pre>
  +     * Then 'html' will be the default extension for text/html, and both 'html'
  +     * and 'htm' will have MIME type 'text/html'.
  +     * Lines starting with '#' are treated as comments and ignored.  If an
  +     * extension is listed for two MIME types, the first will be chosen.
  +     *
  +     * @param in Reader of bytes from <code>mime.types</code> file content
  +     * @param extMap Empty map of default extensions, keyed by MIME type. Will
  +     * be filled in by this method.
  +     * @param mimeMap Empty map of MIME types, keyed by extension.  Will be
  +     * filled in by this method.
  +     */
  +    public static void loadMimeTypes(Reader in, Map extMap, Map mimeMap) throws IOException
{
  +        BufferedReader br = new BufferedReader(in);
  +        String line;
  +        while ( (line = br.readLine()) != null) {
  +            if (line.startsWith("#")) continue;
  +            if (line.trim().equals("")) continue;
  +            StringTokenizer tok = new StringTokenizer(line, " \t");
  +            String mimeType = tok.nextToken();
  +            if (tok.hasMoreTokens()) {
  +                String defaultExt = tok.nextToken();
  +                if (!extMap.containsKey(mimeType)) {
  +                    extMap.put(mimeType, "."+defaultExt);
  +                }
  +                if (!mimeMap.containsKey("."+defaultExt)) {
  +                    mimeMap.put("."+defaultExt, mimeType);
  +                }
  +                while (tok.hasMoreTokens()) {
  +                    String ext = tok.nextToken();
  +                    if (!mimeMap.containsKey("."+ext)) {
  +                        mimeMap.put("."+ext, mimeType);
  +                    }
  +                }
  +            }
  +        }
       }
   }
  
  
  
  1.1                  cocoon-2.1/src/java/org/apache/cocoon/util/mime.types
  
  Index: mime.types
  ===================================================================
  # MIME type mappings
  text/plain  txt text
  text/html   html htm
  text/xml    xml
  text/css    css
  text/rtf    rtf
  application/rtf rtf
  text/vnd.wap.wml    wml
  image/jpeg  jpeg jpg jpe
  image/png   png
  image/gif   gif
  image/tiff  tiff tif
  image/svg+xml   svg svgz
  image/svg-xml   svg
  application/pdf pdf
  model/vrml  wrl
  application/smil    smil
  text/javascript js
  application/x-javascript    js
  text/vbscript   vbs
  application/zip zip
  video/mpeg  mpeg mpg mpe
  video/quicktime mov
  audio/midi  mid
  audio/mpeg  mp3
  text/x-vcard    vcf
  text/rdf    rdf
  application/octet-stream
  
  
  
  1.1                  cocoon-2.1/src/test/org/apache/cocoon/util/test/MIMEUtilsTestCase.java
  
  Index: MIMEUtilsTestCase.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.util.test;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.io.StringReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  
  import junit.framework.TestCase;
  
  import org.apache.cocoon.util.MIMEUtils;
  
  /**
   * Test Cases for the MIMEUtils class.
   * @see org.apache.cocoon.util.MIMEUtils
   * Specifically, code for testing the parsing of mime.types files.
   *
   * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
   * @version CVS $Id: MIMEUtilsTestCase.java,v 1.1 2003/06/01 09:17:01 jefft Exp $
   */
  public class MIMEUtilsTestCase extends TestCase
  {
  
      final String NL = System.getProperty("line.separator");
      Map mimeMap;
      Map extMap;
      final String M2E = "MIME to extension mappings";
      final String E2M = "Extension to MIME mappings";
  
      public MIMEUtilsTestCase(String name) {
          super(name);
      }
  
      public static void main(String args[]) {
          junit.textui.TestRunner.run(MIMEUtilsTestCase.class);
      }
  
      public void setUp() {
          mimeMap = new HashMap();
          extMap = new HashMap();
      }
  
      public void tearDown() {
          mimeMap = null;
          extMap = null;
      }
  
      public void tstGetMimeType() {
          assertEquals(E2M, "text/plain", MIMEUtils.getMIMEType(".txt"));
          assertEquals(E2M, "image/jpeg", MIMEUtils.getMIMEType(".jpg"));
          assertEquals(E2M, "video/mpeg", MIMEUtils.getMIMEType(".mpg"));
          assertEquals(E2M, null, MIMEUtils.getMIMEType(".undefined"));
          assertEquals(M2E, ".txt", MIMEUtils.getDefaultExtension("text/plain"));
          assertEquals(M2E, ".jpeg", MIMEUtils.getDefaultExtension("image/jpeg"));
          assertEquals(M2E, ".mpeg", MIMEUtils.getDefaultExtension("video/mpeg"));
          assertEquals(M2E, null, MIMEUtils.getDefaultExtension("application/octet-stream"));
  
      }
  
      public void testTypicalUsage() throws Exception {
          String mime_types="# MIME type mappings"+NL+
              "text/plain  txt text "+NL+
              "text/html   html htm"+NL+
              "   "+NL+
              "text/xml    xml"+NL+
              "text/css    css"+NL+
              "text/javascript		js "+NL+
              "application/x-javascript	js";
  
          MIMEUtils.loadMimeTypes(new StringReader(mime_types), extMap, mimeMap);
          assertEquals(".txt", extMap.get("text/plain"));
          assertEquals(".html", extMap.get("text/html"));
          assertEquals(".xml", extMap.get("text/xml"));
          assertEquals(".css", extMap.get("text/css"));
          assertEquals(".js", extMap.get("text/javascript"));
          assertEquals(".js", extMap.get("application/x-javascript"));
  
          assertEquals("text/plain", mimeMap.get(".text"));
          assertEquals("text/plain", mimeMap.get(".txt"));
          assertEquals("text/html", mimeMap.get(".html"));
          assertEquals("text/html", mimeMap.get(".htm"));
          assertEquals("text/xml", mimeMap.get(".xml"));
          assertEquals("text/css", mimeMap.get(".css"));
          assertEquals("text/javascript", mimeMap.get(".js"));
  
          assertEquals(M2E, 6, extMap.size());
          assertEquals(E2M, 7, mimeMap.size());
      }
  
      public void tstEmpty() throws Exception {
          String mime_types="";
          MIMEUtils.loadMimeTypes(new StringReader(mime_types), extMap, mimeMap);
          assertEquals(M2E, 0, extMap.size());
          assertEquals(E2M, 0, mimeMap.size());
      }
  
      public void tstCommentsAndWhitespace() throws Exception {
          String NL = System.getProperty("line.separator");
          String mime_types="## A commented line"+NL+
              "   "+NL+
              "# Another comment";
          MIMEUtils.loadMimeTypes(new StringReader(mime_types), extMap, mimeMap);
          assertEquals(M2E, 0, extMap.size());
          assertEquals(E2M, 0, mimeMap.size());
      }
  
      public void tstMimeTypeWithoutExtension() throws Exception {
          String NL = System.getProperty("line.separator");
          String mime_types=
              "text/plain  txt text"+NL+
              "application/octet-stream"+NL+NL;
          MIMEUtils.loadMimeTypes(new StringReader(mime_types), extMap, mimeMap);
          assertEquals(".txt", extMap.get("text/plain"));
          assertEquals("text/plain", mimeMap.get(".txt"));
          assertEquals("text/plain", mimeMap.get(".text"));
          assertEquals(M2E, 1, extMap.size());
          assertEquals(E2M, 2, mimeMap.size());
      }
  
  }
  
  
  

Mime
View raw message