Source for org.jfree.repository.DefaultMimeRegistry

   1: /**
   2:  * ===========================================================
   3:  * LibRepository : a free Java content repository access layer
   4:  * ===========================================================
   5:  *
   6:  * Project Info:  http://jfreereport.pentaho.org/librepository/
   7:  *
   8:  * (C) Copyright 2006, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * DefaultMimeRegistry.java
  27:  * ------------
  28:  * (C) Copyright 2006, by Pentaho Corporation.
  29:  */
  30: 
  31: package org.jfree.repository;
  32: 
  33: import org.jfree.util.StringUtils;
  34: 
  35: /**
  36:  * Creation-Date: 13.11.2006, 12:24:19
  37:  *
  38:  * @author Thomas Morgner
  39:  */
  40: public class DefaultMimeRegistry implements MimeRegistry
  41: {
  42:   public DefaultMimeRegistry()
  43:   {
  44:   }
  45: 
  46:   public String getMimeType(final ContentItem item)
  47:   {
  48:     final String name = item.getName();
  49:     if (name == null)
  50:     {
  51:       return "application/octet-stream";
  52:     }
  53:     if (StringUtils.endsWithIgnoreCase(name, ".png"))
  54:     {
  55:       return "image/png";
  56:     }
  57:     if (StringUtils.endsWithIgnoreCase(name, ".jpg"))
  58:     {
  59:       return "image/jpeg";
  60:     }
  61:     if (StringUtils.endsWithIgnoreCase(name, ".jpeg"))
  62:     {
  63:       return "image/jpeg";
  64:     }
  65:     if (StringUtils.endsWithIgnoreCase(name, ".gif"))
  66:     {
  67:       return "image/gif";
  68:     }
  69:     if (StringUtils.endsWithIgnoreCase(name, ".pdf"))
  70:     {
  71:       return "application/pdf";
  72:     }
  73:     if (StringUtils.endsWithIgnoreCase(name, ".txt"))
  74:     {
  75:       return "text/plain";
  76:     }
  77:     if (StringUtils.endsWithIgnoreCase(name, ".html"))
  78:     {
  79:       return "text/html";
  80:     }
  81:     if (StringUtils.endsWithIgnoreCase(name, ".htm"))
  82:     {
  83:       return "text/html";
  84:     }
  85:     if (StringUtils.endsWithIgnoreCase(name, ".css"))
  86:     {
  87:       return "text/css";
  88:     }
  89:     return "application/octet-stream";
  90:   }
  91: 
  92:   public String getSuffix(final String mimeType)
  93:   {
  94:     // needs 'libMagic'
  95:     if ("image/png".equals(mimeType))
  96:     {
  97:       return "png";
  98:     }
  99:     if ("image/jpeg".equals(mimeType))
 100:     {
 101:       return "jpg";
 102:     }
 103:     if ("image/jpg".equals(mimeType))
 104:     {
 105:       return "jpg";
 106:     }
 107:     if ("image/gif".equals(mimeType))
 108:     {
 109:       return "gif";
 110:     }
 111:     if ("text/html".equals(mimeType))
 112:     {
 113:       return "html";
 114:     }
 115:     if ("text/plain".equals(mimeType))
 116:     {
 117:       return "txt";
 118:     }
 119:     if ("text/css".equals(mimeType))
 120:     {
 121:       return "css";
 122:     }
 123:     if ("application/pdf".equals(mimeType))
 124:     {
 125:       return "pdf";
 126:     }
 127:     return "dat";
 128:   }
 129: }