001 package net.sourceforge.mp3dings.utils;
002 import java.io.IOException;
003 import java.io.InputStream;
004 import java.util.ArrayList;
005 import java.util.Enumeration;
006 import java.util.List;
007 import java.util.Properties;
008 import java.util.logging.Logger;
009 import net.sourceforge.mp3dings.editors.imageeditor.*;
010
011 /*
012 Copyright (C) 2007 Moritz Ringler
013 This library is free software; you can redistribute it and/or
014 modify it under the terms of the GNU Lesser General Public
015 License as published by the Free Software Foundation; either
016 version 2.1 of the License, or (at your option) any later version.
017 This library is distributed in the hope that it will be useful,
018 but WITHOUT ANY WARRANTY; without even the implied warranty of
019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020 Lesser General Public License for more details.
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024 */
025
026 public class MimeTypeManager {
027 private static final Logger logger = Logger.getLogger(MimeTypeManager.class.getPackage().getName());
028 private final Properties mimeRegistry;
029 private static MimeTypeManager defaultManager;
030
031 public MimeTypeManager(){
032 mimeRegistry = new Properties();
033 }
034
035 private MimeTypeManager(Properties p){
036 mimeRegistry = p;
037 }
038
039 public static synchronized MimeTypeManager getDefault(){
040 if(defaultManager == null){
041 Properties p = new Properties();
042 try{
043 InputStream is = MimeTypeManager.class.getResourceAsStream("mimeType.properties");
044 try{
045 p.load(is);
046 } finally {
047 is.close();
048 }
049 } catch (IOException ex){
050 logger.severe("MimeTypeManager: Error loading default types");
051 logger.severe(ex.toString());
052 }
053 defaultManager = new MimeTypeManager(p);
054 }
055 return defaultManager;
056 }
057
058 public static synchronized void setDefault(MimeTypeManager mtm){
059 defaultManager = mtm;
060 }
061
062 public String[] getExtensions(String mime){
063 List list = new ArrayList();
064 Enumeration e = mimeRegistry.propertyNames();
065 while(e.hasMoreElements()){
066 String extension = (String) e.nextElement();
067 if(mimeRegistry.getProperty(extension).equals(mime)){
068 list.add(extension);
069 }
070 }
071 if(list.isEmpty() && mime.length() > 8 && mime.startsWith("image/x-")){
072 list.add("."+mime.substring(8));
073 }
074 return (String[]) list.toArray(new String[list.size()]);
075 }
076
077 public String getMimeType(String extension){
078 return (extension == null)
079 ? null : mimeRegistry.getProperty(extension);
080 }
081
082 public void registerExtension(String extension, String mimetype){
083 mimeRegistry.setProperty(extension, mimetype);
084 }
085
086 public static String getExtension(String name){
087 int lastDot = name.lastIndexOf('.');
088 return (lastDot == -1 || lastDot == name.length() - 1)
089 ? null
090 : name.substring(lastDot);
091 }
092
093 }