1 5 package org.exoplatform.portlets.content; 6 7 import java.util.HashMap ; 8 import java.util.ResourceBundle ; 9 13 public class MimeTypeManager { 14 static private MimeTypeManager singleton_ ; 15 16 private HashMap mimeTypeMapping_ ; 17 private MimeType defaultMimeType_ ; 18 private MimeType directoryType_ ; 19 20 private MimeTypeManager() { 21 mimeTypeMapping_ = new HashMap () ; 22 ResourceBundle res = ResourceBundle.getBundle("locale.portlet.content.MimeType") ; 23 String extensions = res.getString("support.extensions") ; 24 String [] ext = extensions.split(",") ; 25 for(int i =0; i < ext.length; i++) { 26 String extension = ext[i].trim() ; 27 String mimetype = res.getString("mimetype." + extension) ; 28 String icon = res.getString("mimetype." + extension + ".icon") ; 29 MimeType mt = new MimeType(extension, mimetype, icon) ; 30 mimeTypeMapping_.put(extension, mt) ; 31 } 32 defaultMimeType_ = (MimeType)mimeTypeMapping_.get("bin") ; 33 directoryType_ = (MimeType)mimeTypeMapping_.get("directory") ; 34 } 35 36 public MimeType getMimeTypeByOfFile(String fileName) { 37 String ext = getFileExtension(fileName) ; 38 MimeType mimeType = (MimeType) mimeTypeMapping_.get(ext) ; 39 if (mimeType == null) { 40 mimeType = defaultMimeType_ ; 41 } 42 return mimeType ; 43 } 44 45 public MimeType getDirectoryType() { return directoryType_ ; } 46 47 public String getFileExtension(String fileName) { 48 int idx = fileName.lastIndexOf(".") ; 49 String ext = "bin" ; 50 if (idx > 0) { 51 ext = fileName.substring(idx + 1, fileName.length()) ; 52 } 53 ext = ext.toLowerCase() ; 54 return ext ; 55 } 56 57 static public MimeTypeManager getInstance() { 58 if(singleton_ == null) { 59 singleton_ = new MimeTypeManager() ; 60 } 61 return singleton_ ; 62 } 63 64 static public class MimeType { 65 private String fileExtension_ ; 66 private String mimeType_ ; 67 private String icon_ ; 68 69 public MimeType(String ext, String type, String icon) { 70 fileExtension_ = ext ; 71 mimeType_ = type ; 72 icon_ = icon ; 73 } 74 75 public String getExtension() { return fileExtension_ ; } 76 77 public String getMimeType() { return mimeType_ ; } 78 79 public String getIcon() { return icon_ ; } 80 } 81 } 82 | Popular Tags |