KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > media > Format


1  /*
2   
3  This software is OSI Certified Open Source Software.
4  OSI Certified is a certification mark of the Open Source Initiative.
5  
6  The license (Mozilla version 1.0) can be read at the MMBase site.
7  See http://www.MMBase.org/license
8  */

9
10 package org.mmbase.applications.media;
11
12 import java.io.File JavaDoc;
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 JavaDoc;
19
20
21 // import org.mmbase.util.ConstantsBundle;
22

23 /**
24  * Makes the 'Format' constants available.
25  *
26  * @author Michiel Meeuwissen
27  * @version $Id: Format.java,v 1.19 2006/01/13 12:36:23 johannes Exp $
28  * @since MMBase-1.7
29  */

30 // See http://www.javaworld.com/javaworld/jw-07-1997/jw-07-enumerated.html
31
public final class Format { // final class!!
32
private static Logger log = Logging.getLoggerInstance(Format.class);
33
34     public final static String JavaDoc RESOURCE = "org.mmbase.applications.media.resources.formats";
35     public static final String JavaDoc PUBLIC_ID_MIMEMAPPING_1_0 = "-//MMBase//DTD mimemapping config 1.0//EN";
36     public static final String JavaDoc DTD_MIMEMAPPING_1_0 = "mimemapping_1_0.dtd";
37     
38
39     // in case you want i18ed format strings.
40

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 JavaDoc mimeMappingFile = new File JavaDoc(MMBaseContext.getConfigPath() + File.separator + "media" + File.separator + "mimemapping.xml");
47         readMimeMapping(mimeMappingFile);
48         FileWatcher watcher = new FileWatcher() {
49                 public void onChange(File JavaDoc file) {
50                     readMimeMapping(file);
51                 }
52             };
53         watcher.add(mimeMappingFile);
54         watcher.start();
55      
56     }
57     
58     static void readMimeMapping(File JavaDoc 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 JavaDoc map = (Element JavaDoc)e.next();
68                 String JavaDoc format = reader.getElementAttributeValue(map, "format");
69                 String JavaDoc codec = reader.getElementAttributeValue(map, "codec");
70                 String JavaDoc 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(); // to make possible to get the Format object by int.
81
private int number; // for storage
82
private String JavaDoc id; // for toString(), and as identifier in config file etc.
83
// Also sync with common extension?
84
// perhaps this could as well be used for storage
85

86     
87     private Format(int n, String JavaDoc i) { // private constructor!!
88
number = n; id = i;
89         while (formats.size() <= number) formats.add(UNKNOWN);
90         formats.set(number, this);
91     }
92     
93     // in contradiction to the example of the cited URL I prefer
94
// to state the number explicitely, because those numbers will
95
// appear in the database, so never may change (so don't
96
// determin the number automaticly
97

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"); /* windows media */
111     public static final Format MOV = new Format(13, "mov");
112     public static final Format WMA = new Format(14, "wma"); /* windows media */
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"); /* windows media */
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     /* more windows media types */
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 JavaDoc toString() { return id; }
143     public static Format get(int i) {
144         try {
145             return (Format) formats.get(i);
146         } catch (java.lang.IndexOutOfBoundsException JavaDoc e) {
147             return UNKNOWN;
148         }
149     }
150
151     /**
152      * don't know if this is nice
153      */

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 JavaDoc 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 JavaDoc getGUIIndicator(Locale locale) {
171         try {
172             ResourceBundle m = ResourceBundle.getBundle(RESOURCE, locale);
173             return m.getString("" + number);
174             // return ConstantsBundle.get(RESOURCE, this.getClass(), number, locale);
175
} catch (Exception JavaDoc 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 JavaDoc getMimeType() {
200         return getMimeType(null);
201     }
202
203     public String JavaDoc getMimeType(String JavaDoc codec) {
204         String JavaDoc format = toString();
205         if(format == null || format.equals("unknown")) {
206             format = "*";
207         }
208         if(codec == null || codec.equals("")) {
209             codec = "*";
210         }
211         
212         String JavaDoc mimeType = (String JavaDoc) mimeMapping.get(format + "/" + codec);
213         while (mimeType == null) {
214             if (! codec.equals("*")) {
215                 mimeType = (String JavaDoc) mimeMapping.get(format + "/*");
216                 if (mimeType != null) break;
217             }
218             if (! format.equals("*")) {
219                 mimeType = (String JavaDoc) mimeMapping.get("*/" + codec);
220                 if (mimeType != null) break;
221             }
222             mimeType = (String JavaDoc) 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 JavaDoc o) {
235         if (o instanceof Format) {
236             Format f = (Format) o;
237             return f.number == number;
238         }
239         return false;
240     }
241     
242     
243     /**
244      * @see java.lang.Object#hashCode()
245      */

246     public int hashCode() {
247         return number;
248     }
249 }
250     
251
Popular Tags