1 9 10 package org.mmbase.applications.media; 11 12 import java.io.File ; 13 import java.util.*; 14 import org.mmbase.util.*; 15 import org.mmbase.util.logging.Logger; 16 import org.mmbase.util.logging.Logging; 17 import org.mmbase.module.core.MMBaseContext; 18 import org.w3c.dom.Element ; 19 20 21 23 30 public final class Format { private static Logger log = Logging.getLoggerInstance(Format.class); 33 34 public final static String RESOURCE = "org.mmbase.applications.media.resources.formats"; 35 public static final String PUBLIC_ID_MIMEMAPPING_1_0 = "-//MMBase//DTD mimemapping config 1.0//EN"; 36 public static final String DTD_MIMEMAPPING_1_0 = "mimemapping_1_0.dtd"; 37 38 39 41 private static Map mimeMapping = null; 42 static { 43 44 XMLEntityResolver.registerPublicID(PUBLIC_ID_MIMEMAPPING_1_0, DTD_MIMEMAPPING_1_0, Format.class); 45 46 File mimeMappingFile = new File (MMBaseContext.getConfigPath() + File.separator + "media" + File.separator + "mimemapping.xml"); 47 readMimeMapping(mimeMappingFile); 48 FileWatcher watcher = new FileWatcher() { 49 public void onChange(File file) { 50 readMimeMapping(file); 51 } 52 }; 53 watcher.add(mimeMappingFile); 54 watcher.start(); 55 56 } 57 58 static void readMimeMapping(File mimeMappingFile) { 59 mimeMapping = new HashMap(); 60 61 62 if (mimeMappingFile.canRead()) { 63 log.service("Reading " + mimeMappingFile); 64 XMLBasicReader reader = new XMLBasicReader(mimeMappingFile.toString(), Format.class); 65 66 for(Iterator e = reader.getChildElements("mimemapping", "map"); e.hasNext();) { 67 Element map = (Element )e.next(); 68 String format = reader.getElementAttributeValue(map, "format"); 69 String codec = reader.getElementAttributeValue(map, "codec"); 70 String mime = reader.getElementValue(map); 71 72 mimeMapping.put(format + "/" + codec,mime); 73 log.debug("Adding mime mapping " + format + "/" + codec + " -> " + mime); 74 } 75 } else { 76 log.service("The file " + mimeMappingFile + " can not be read"); 77 } 78 } 79 80 private static List formats = new ArrayList(); private int number; private String id; 86 87 private Format(int n, String i) { number = n; id = i; 89 while (formats.size() <= number) formats.add(UNKNOWN); 90 formats.set(number, this); 91 } 92 93 98 public static final Format UNKNOWN = new Format(0, "unknown"); 99 public static final Format MP3 = new Format(1, "mp3"); 100 public static final Format RA = new Format(2, "ra"); 101 public static final Format WAV = new Format(3, "wav"); 102 public static final Format PCM = new Format(4, "pcm"); 103 public static final Format MP2 = new Format(5, "mp2"); 104 public static final Format RM = new Format(6, "rm"); 105 public static final Format VOB = new Format(7, "vob"); 106 public static final Format AVI = new Format(8, "avi"); 107 public static final Format MPEG = new Format(9, "mpeg"); 108 public static final Format MP4 = new Format(10, "mp4"); 109 public static final Format MPG = new Format(11, "mpg"); 110 public static final Format ASF = new Format(12, "asf"); 111 public static final Format MOV = new Format(13, "mov"); 112 public static final Format WMA = new Format(14, "wma"); 113 public static final Format OGG = new Format(15, "ocg"); 114 public static final Format OGM = new Format(16, "ogm"); 115 public static final Format RAM = new Format(17, "ram"); 116 public static final Format WMP = new Format(18, "wmp"); 117 public static final Format HTML = new Format(19, "html"); 118 public static final Format SMIL = new Format(20, "smil"); 119 public static final Format QT = new Format(21, "qt"); 120 121 122 public static final Format ASX = new Format(22, "asx"); 123 public static final Format WAX = new Format(23, "wax"); 124 public static final Format WMV = new Format(24, "wmv"); 125 public static final Format WVX = new Format(25, "wvx"); 126 public static final Format WM = new Format(26, "wm"); 127 public static final Format WMX = new Format(27, "wmx"); 128 public static final Format WMZ = new Format(28, "wmz"); 129 public static final Format WMD = new Format(29, "wmd"); 130 131 public static final Format MID = new Format(30, "mid"); 132 133 public static final Format PODCAST = new Format(50, "podcast"); 134 public static final Format VODCAST = new Format(51, "vodcast"); 135 136 public static final Format M4A = new Format(60, "m4a"); 137 public static final Format M4V = new Format(61, "m4v"); 138 139 public static final Format GGP = new Format(70, "3gpp"); 140 141 public int toInt() { return number; } 142 public String toString() { return id; } 143 public static Format get(int i) { 144 try { 145 return (Format) formats.get(i); 146 } catch (java.lang.IndexOutOfBoundsException e) { 147 return UNKNOWN; 148 } 149 } 150 151 154 public static List getMediaFormats() { 155 return Arrays.asList(new Format[] {MP3, RA, RA,WAV, PCM, MP2, RM, VOB, AVI, MPEG, MP4, MPG, ASF, MOV, WMA, OGG, OGM, RAM, WMP, QT, ASX, WAX, WMV, WVX, WM, WMZ, WMD, MID, PODCAST, VODCAST, M4A, M4V, GGP}); 156 } 157 public static Format get(String id) { 158 id = id.toLowerCase(); 159 Iterator i = formats.iterator(); 160 while (i.hasNext()) { 161 Format format = (Format) i.next(); 162 if(format.toString().equals(id)) { 163 return format; 164 } 165 } 166 log.warn("Unknown media format '" + id + "'. Returning " + UNKNOWN); 167 return UNKNOWN; 168 } 169 170 public String getGUIIndicator(Locale locale) { 171 try { 172 ResourceBundle m = ResourceBundle.getBundle(RESOURCE, locale); 173 return m.getString("" + number); 174 } catch (Exception e) { 176 return "UNKNOWN"; 177 } 178 } 179 180 181 protected static final List windowsMedia = Arrays.asList(new Format[] {ASF, WMP, WMA, ASX, WAX, WMV, WVX, WM, WMX, WMZ, WMD}); 182 protected static final List real = Arrays.asList(new Format[] {RA, RM, RAM}); 183 184 public boolean isReal() { 185 return real.contains(this); 186 } 187 public boolean isWindowsMedia() { 188 return windowsMedia.contains(this); 189 } 190 public List getSimilar() { 191 if (isReal()) { 192 return real; 193 } else if (isWindowsMedia()) { 194 return windowsMedia; 195 } 196 return Arrays.asList(new Format[]{this}); 197 } 198 199 public String getMimeType() { 200 return getMimeType(null); 201 } 202 203 public String getMimeType(String codec) { 204 String format = toString(); 205 if(format == null || format.equals("unknown")) { 206 format = "*"; 207 } 208 if(codec == null || codec.equals("")) { 209 codec = "*"; 210 } 211 212 String mimeType = (String ) mimeMapping.get(format + "/" + codec); 213 while (mimeType == null) { 214 if (! codec.equals("*")) { 215 mimeType = (String ) mimeMapping.get(format + "/*"); 216 if (mimeType != null) break; 217 } 218 if (! format.equals("*")) { 219 mimeType = (String ) mimeMapping.get("*/" + codec); 220 if (mimeType != null) break; 221 } 222 mimeType = (String ) mimeMapping.get("*/*"); 223 if (mimeType == null) mimeType = "application/octet-stream"; 224 break; 225 } 226 227 if (log.isDebugEnabled()) { 228 log.info("Finding mimetype for " + this + " -> " + mimeType + " (used " + mimeMapping + ")"); 229 } 230 return mimeType; 231 232 } 233 234 public boolean equals(Object o) { 235 if (o instanceof Format) { 236 Format f = (Format) o; 237 return f.number == number; 238 } 239 return false; 240 } 241 242 243 246 public int hashCode() { 247 return number; 248 } 249 } 250 251 | Popular Tags |