001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.net;
020:
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: /**
027: * Contains mappings between mimetype and file extension. Each core type,
028: * i.e. JPEG Image which also maps to 'image/jpeg' and 'image/jpg'
029: * and extensions 'JPG' and 'JPEG', also contains a description e.g.
030: * 'Image(JPEG)'.
031: *
032: * @author Matthew Large
033: * @version $Revision: 1.2 $
034: *
035: */
036: public class MimeTypeMapping {
037:
038: /**
039: * <code>Map</code> of extensions to <code>Mapping</code>s.
040: */
041: private static HashMap m_aExtensionMap = new HashMap();
042:
043: /**
044: * <code>Map</code> of mime types to <code>Mapping</code>s.
045: */
046: private static HashMap m_aMimeTypeMap = new HashMap();
047:
048: /**
049: * GIF mapping.
050: */
051: public static Mapping GIF = new Mapping("Image(GIF)");
052:
053: /**
054: * JPEG mapping.
055: */
056: public static Mapping JPEG = new Mapping("Image(JPEG)");
057:
058: /**
059: * TIFF mapping.
060: */
061: public static Mapping TIFF = new Mapping("Image(TIFF)");
062:
063: /**
064: * PNG mapping
065: */
066: public static Mapping PNG = new Mapping("Image(PNG)");
067:
068: /**
069: * SVG mapping
070: */
071: public static Mapping SVG = new Mapping(
072: "Scalable Vector Graphics (SVG)");
073:
074: /**
075: * PDF mapping
076: */
077: public static Mapping PDF = new Mapping("PDF");
078:
079: /**
080: * Plain text mapping
081: */
082: public static Mapping TEXT = new Mapping("Plain Text");
083:
084: /**
085: * XML mapping
086: */
087: public static Mapping XML = new Mapping("XML");
088:
089: /**
090: * XSLT mapping
091: */
092: public static Mapping XSLT = new Mapping("XSLT");
093:
094: /**
095: * HTML mapping
096: */
097: public static Mapping HTML = new Mapping("HTML");
098:
099: /**
100: * Flash mapping
101: */
102: public static Mapping FLASH = new Mapping("Flash Movie");
103:
104: /**
105: * WAV mapping
106: */
107: public static Mapping WAV = new Mapping("Audio(WAV)");
108:
109: /**
110: * Ogg Vorbis mapping
111: */
112: public static Mapping OGG = new Mapping("Audio(Ogg Vorbis)");
113:
114: /**
115: * MP3 mapping
116: */
117: public static Mapping MP3 = new Mapping("Audio(MP3)");
118:
119: /**
120: * AU mapping
121: */
122: public static Mapping AU = new Mapping("Audio(AU)");
123:
124: /**
125: * Real media mapping
126: */
127: public static Mapping REAL_MEDIA = new Mapping("Real Media");
128:
129: /**
130: * Active Streaming Format mapping
131: */
132: public static Mapping ASF = new Mapping("Active Streaming Format");
133:
134: /**
135: * MS Word mapping
136: */
137: public static Mapping WORD = new Mapping("Word Document");
138:
139: /**
140: * Word perfect mapping
141: */
142: public static Mapping WORD_PERFECT = new Mapping(
143: "Word Perfect Document");
144:
145: /**
146: * RTF mapping
147: */
148: public static Mapping RTF = new Mapping("RTF Document");
149:
150: /**
151: * MS Powerpoint mapping
152: */
153: public static Mapping PPT = new Mapping("PowerPoint Presentation");
154:
155: /**
156: * MS Excel mapping
157: */
158: public static Mapping XLS = new Mapping("Excel Spreadsheet");
159:
160: /**
161: * Harmonise email mapping
162: */
163: public static Mapping EMAIL = new Mapping("Email address");
164:
165: /**
166: * Harmonise URL mapping
167: */
168: public static Mapping URL = new Mapping("URL");
169:
170: /**
171: * MPEG mapping
172: */
173: public static Mapping MOVIE_MPEG = new Mapping("Movie(MPEG)");
174:
175: /**
176: * AVI mapping
177: */
178: public static Mapping MOVIE_AVI = new Mapping("Movie(AVI)");
179:
180: /**
181: * MOV mapping
182: */
183: public static Mapping MOVIE_MOV = new Mapping("Movie(MOV)");
184:
185: /**
186: * CSV mapping
187: */
188: public static Mapping CSV = new Mapping(
189: "Comma Separated Values (CSV)");
190:
191: //initialise all mappings
192: static {
193: Mapping mapping = null;
194:
195: GIF.addExtension("gif");
196: GIF.addMimeType("image/gif");
197: GIF.addMimeType("image/x-xbitmap");
198: GIF.addMimeType("image/gi_");
199: MimeTypeMapping.addMapping(GIF);
200:
201: JPEG.addExtension("jpg");
202: JPEG.addExtension("jpeg");
203: JPEG.addMimeType("image/jpeg");
204: JPEG.addMimeType("image/jpg");
205: JPEG.addMimeType("image/jp_");
206: JPEG.addMimeType("application/jpg");
207: JPEG.addMimeType("application/x-jpg");
208: JPEG.addMimeType("image/pjpeg");
209: JPEG.addMimeType("image/pipeg");
210: JPEG.addMimeType("image/vnd.swiftview-jpeg");
211: JPEG.addMimeType("image/x-xbitmap");
212: MimeTypeMapping.addMapping(JPEG);
213:
214: TIFF.addExtension("tif");
215: TIFF.addExtension("tiff");
216: TIFF.addMimeType("image/tif");
217: TIFF.addMimeType("image/x-tif");
218: TIFF.addMimeType("image/tiff");
219: TIFF.addMimeType("image/x-tiff");
220: TIFF.addMimeType("application/tif");
221: TIFF.addMimeType("application/x-tif");
222: TIFF.addMimeType("application/tiff");
223: TIFF.addMimeType("application/x-tiff");
224: MimeTypeMapping.addMapping(TIFF);
225:
226: PNG.addExtension("png");
227: PNG.addMimeType("image/png");
228: PNG.addMimeType("application/png");
229: PNG.addMimeType("application/x-png");
230: MimeTypeMapping.addMapping(PNG);
231:
232: SVG.addExtension("svg");
233: SVG.addMimeType("image/svg+xml");
234: SVG.addMimeType("image/svg");
235: SVG.addMimeType("image/svg-xml");
236: SVG.addMimeType("text/xml-svg");
237: SVG.addMimeType("image/vnd.adobe.svg+xml");
238: SVG.addMimeType("image/svg-xml");
239: MimeTypeMapping.addMapping(SVG);
240:
241: PDF.addExtension("pdf");
242: PDF.addMimeType("application/pdf");
243: PDF.addMimeType("application/x-pdf");
244: PDF.addMimeType("application/acrobat");
245: PDF.addMimeType("applications/vnd.pdf");
246: PDF.addMimeType("text/pdf");
247: PDF.addMimeType("text/x-pdf");
248: MimeTypeMapping.addMapping(PDF);
249:
250: TEXT.addExtension("txt");
251: TEXT.addMimeType("text/plain");
252: MimeTypeMapping.addMapping(TEXT);
253:
254: XML.addExtension("xml");
255: XML.addMimeType("text/xml");
256: XML.addMimeType("application/xml");
257: XML.addMimeType("application/x-xml");
258: MimeTypeMapping.addMapping(XML);
259:
260: XSLT.addExtension("xsl");
261: XSLT.addExtension("xslt");
262: XSLT.addMimeType("text/xsl");
263: MimeTypeMapping.addMapping(XSLT);
264:
265: HTML.addExtension("html");
266: HTML.addExtension("htm");
267: HTML.addMimeType("text/html");
268: MimeTypeMapping.addMapping(HTML);
269:
270: FLASH.addExtension("swf");
271: FLASH.addMimeType("application/x-shockwave-flash");
272: FLASH.addMimeType("application/x-shockwave-flash2-preview");
273: FLASH.addMimeType("application/futuresplash");
274: FLASH.addMimeType("image/vnd.rn-realflash");
275: MimeTypeMapping.addMapping(FLASH);
276:
277: WAV.addExtension("wav");
278: WAV.addMimeType("audio/wav");
279: WAV.addMimeType("audio/x-wav");
280: WAV.addMimeType("audio/wave");
281: WAV.addMimeType("audio/x-pn-wav");
282: MimeTypeMapping.addMapping(WAV);
283:
284: AU.addExtension("au");
285: AU.addMimeType("audio/basic");
286: AU.addMimeType("audio/x-basic");
287: AU.addMimeType("audio/au");
288: AU.addMimeType("audio/x-au");
289: AU.addMimeType("audio/x-pn-au");
290: AU.addMimeType("audio/rmf");
291: AU.addMimeType("audio/x-rmf");
292: AU.addMimeType("audio/x-ulaw");
293: AU.addMimeType("audio/vnd.qcelp");
294: AU.addMimeType("audio/x-gsm");
295: AU.addMimeType("audio/snd");
296: MimeTypeMapping.addMapping(AU);
297:
298: REAL_MEDIA.addExtension("rm");
299: REAL_MEDIA.addMimeType("application/vnd.rn-realmedia");
300: REAL_MEDIA.addMimeType("audio/vnd.rn-realaudio");
301: REAL_MEDIA.addMimeType("audio/x-pn-realaudio");
302: REAL_MEDIA.addMimeType("audio/x-realaudio");
303: REAL_MEDIA.addMimeType("audio/x-pm-realaudio-plugin");
304: MimeTypeMapping.addMapping(REAL_MEDIA);
305:
306: ASF.addExtension("asf");
307: ASF.addMimeType("audio/asf");
308: ASF.addMimeType("application/asx");
309: ASF.addMimeType("video/x-ms-asf-plugin");
310: ASF.addMimeType("application/x-mplayer2");
311: ASF.addMimeType("video/x-ms-asf");
312: ASF.addMimeType("application/vnd.ms-asf");
313: ASF.addMimeType("video/x-ms-asf-plugin");
314: ASF.addMimeType("video/x-ms-wm");
315: ASF.addMimeType("video/x-ms-wmx");
316: MimeTypeMapping.addMapping(ASF);
317:
318: OGG.addExtension("ogg");
319: OGG.addMimeType("audio/x-ogg");
320: OGG.addMimeType("application/x-ogg");
321: MimeTypeMapping.addMapping(OGG);
322:
323: MP3.addExtension("mp3");
324: MP3.addMimeType("audio/mpeg");
325: MP3.addMimeType("audio/x-mpeg");
326: MP3.addMimeType("audio/mp3");
327: MP3.addMimeType("audio/x-mp3");
328: MP3.addMimeType("audio/mpeg3");
329: MP3.addMimeType("audio/x-mpeg3");
330: MP3.addMimeType("audio/mpg");
331: MP3.addMimeType("audio/x-mpg");
332: MP3.addMimeType("audio/x-mpegaudio");
333: MimeTypeMapping.addMapping(MP3);
334:
335: WORD.addExtension("doc");
336: WORD.addMimeType("application/msword");
337: WORD.addMimeType("application/doc");
338: WORD.addMimeType("appl/text");
339: WORD.addMimeType("application/vnd.msword");
340: WORD.addMimeType("application/vnd.ms-word");
341: WORD.addMimeType("application/winword");
342: WORD.addMimeType("application/word");
343: WORD.addMimeType("application/x-msw6");
344: WORD.addMimeType("application/x-msword");
345: WORD.addMimeType("zz-application/zz-winassoc-doc");
346: MimeTypeMapping.addMapping(WORD);
347:
348: WORD_PERFECT.addExtension("wp");
349: WORD_PERFECT.addMimeType("application/wordperfect5.1");
350: MimeTypeMapping.addMapping(WORD_PERFECT);
351:
352: RTF.addExtension("rtf");
353: RTF.addMimeType("application/rtf");
354: RTF.addMimeType("application/x-rtf");
355: RTF.addMimeType("text/rtf");
356: RTF.addMimeType("text/richtext");
357: RTF.addMimeType("application/x-soffice");
358: MimeTypeMapping.addMapping(RTF);
359:
360: PPT.addExtension("ppt");
361: PPT.addMimeType("application/mspowerpoint");
362: PPT.addMimeType("application/ms-powerpoint");
363: PPT.addMimeType("application/mspowerpnt");
364: PPT.addMimeType("application/vnd-mspowerpoint");
365: PPT.addMimeType("application/vnd.ms-powerpoint");
366: PPT.addMimeType("application/powerpoint");
367: PPT.addMimeType("application/x-powerpoint");
368: PPT.addMimeType("application/x-mspowerpoint");
369: MimeTypeMapping.addMapping(PPT);
370:
371: XLS.addExtension("xls");
372: XLS.addMimeType("application/msexcel");
373: XLS.addMimeType("application/x-msexcel");
374: XLS.addMimeType("application/x-ms-excel");
375: XLS.addMimeType("application/vnd.ms-excel");
376: XLS.addMimeType("application/x-excel");
377: XLS.addMimeType("application/x-dos_ms_excel");
378: XLS.addMimeType("application/xls");
379: XLS.addMimeType("application/x-xls");
380: XLS.addMimeType("zz-application/zz-winassoc-xls");
381: MimeTypeMapping.addMapping(XLS);
382:
383: MOVIE_MPEG.addExtension("mpg");
384: MOVIE_MPEG.addExtension("mpeg");
385: MOVIE_MPEG.addMimeType("video/mpeg");
386: MOVIE_MPEG.addMimeType("video/mpg");
387: MOVIE_MPEG.addMimeType("video/x-mpg");
388: MOVIE_MPEG.addMimeType("video/mpeg2");
389: MOVIE_MPEG.addMimeType("video/x-mpeg");
390: MOVIE_MPEG.addMimeType("video/x-mpeg2a");
391: MimeTypeMapping.addMapping(MOVIE_MPEG);
392:
393: MOVIE_AVI.addExtension("avi");
394: MOVIE_AVI.addMimeType("video/avi");
395: MOVIE_AVI.addMimeType("video/msvideo");
396: MOVIE_AVI.addMimeType("video/x-msvideo");
397: MOVIE_AVI.addMimeType("video/xmpg2");
398: MimeTypeMapping.addMapping(MOVIE_AVI);
399:
400: MOVIE_MOV.addExtension("mov");
401: MOVIE_MOV.addMimeType("video/quicktime");
402: MOVIE_MOV.addMimeType("video/x-quicktime");
403: MimeTypeMapping.addMapping(MOVIE_MOV);
404:
405: EMAIL.addExtension("adr");
406: EMAIL.addMimeType("link/mailto");
407:
408: MimeTypeMapping.addMapping(EMAIL);
409:
410: URL.addExtension("url");
411: URL.addMimeType("link/http");
412:
413: MimeTypeMapping.addMapping(URL);
414:
415: CSV.addExtension("csv");
416: CSV.addMimeType("text/csv");
417: MimeTypeMapping.addMapping(CSV);
418: }
419:
420: /**
421: * Inaccessible constructor.
422: */
423: private MimeTypeMapping() {
424: super ();
425: }
426:
427: /**
428: * Adds a Mapping object to the relevant <code>HashMap<code>s.
429: *
430: * @param mapping Mapping object to be added to all the relevant HashMaps.
431: */
432: private static void addMapping(Mapping mapping) {
433: List aExtensions = mapping.getExtensions();
434: Iterator itor = aExtensions.iterator();
435: while (itor.hasNext()) {
436: m_aExtensionMap.put((String) itor.next(), mapping);
437: }
438: List aMimeTypes = mapping.getMimeTypes();
439: itor = aMimeTypes.iterator();
440: while (itor.hasNext()) {
441: m_aMimeTypeMap.put((String) itor.next(), mapping);
442: }
443: }
444:
445: /**
446: * Gets a list of Mime-Types that match the given file extension.
447: *
448: * @param sExtension File extension to be matched.
449: * @return a list of <code>String</code>s
450: */
451: public static List getMimeTypes(String sExtension) {
452: List result = null;
453:
454: Mapping mapping = (Mapping) MimeTypeMapping.m_aExtensionMap
455: .get(sExtension.toLowerCase());
456:
457: if (mapping != null) {
458: result = mapping.getMimeTypes();
459: }
460:
461: return result;
462: }
463:
464: /**
465: * Gets a list of file extensions that match the given Mime-Type.
466: *
467: * @param sMimeType Mime-Type to be matched.
468: * @return a list of <code>String</code>s
469: */
470: public static List getExtensions(String sMimeType) {
471: if (sMimeType != null
472: && m_aMimeTypeMap != null
473: && MimeTypeMapping.m_aMimeTypeMap.get(sMimeType) != null) {
474: return ((Mapping) MimeTypeMapping.m_aMimeTypeMap
475: .get(sMimeType)).getExtensions();
476: } else {
477: return null;
478: }
479: }
480:
481: /**
482: * Returns an extension which is appropriate for the specified mime-type.
483: *
484: * @param mime the mime-type
485: * @return the extension which is appropriate for the mime type given
486: */
487: public static String getExtensionFromMimeType(String mime) {
488: String ext = null;
489:
490: List extList = MimeTypeMapping.getExtensions(mime);
491:
492: if (extList != null && extList.size() > 0) {
493: ext = (String) extList.get(0);
494: }
495:
496: return ext;
497: }
498:
499: /**
500: * Returns a mime type which matches the specified extension.
501: *
502: * @param ext the extension
503: * @return a mime type which matches the specified extension
504: */
505: public static String getMimeTypeFromExtension(String ext) {
506: String type = ext;
507:
508: List mimeList = MimeTypeMapping.getMimeTypes(ext);
509:
510: if (mimeList != null && mimeList.size() > 0) {
511: type = (String) mimeList.get(0);
512: }
513:
514: return type;
515: }
516:
517: /**
518: * Gets a description that matches the given file extension.
519: *
520: * @param sExtension File extension to be matched.
521: * @return a description of the mapping
522: */
523: public static String getDescriptionForExtension(String sExtension) {
524: return ((Mapping) MimeTypeMapping.m_aExtensionMap
525: .get(sExtension)).getDescription();
526: }
527:
528: /**
529: * Gets a description that matches the given Mime-Type.
530: *
531: * @param sMimeType Mime-Type to be matched.
532: * @return a description of the mapping
533: */
534: public static String getDescriptionForMimeType(String sMimeType) {
535: return ((Mapping) MimeTypeMapping.m_aMimeTypeMap.get(sMimeType))
536: .getDescription();
537: }
538:
539: /**
540: * Class to contain all the information about a single mapping. Each instance
541: * holds a description of the mapping, to be used as the human readable version
542: * of the mapping e.g. 'Image(JPEG)'. Each instance also holds lists of the
543: * Mime-Types and file extensions which are relevant to this mapping.
544: *
545: * @author Matthew Large
546: * @version $Revision: 1.2 $
547: *
548: */
549: public static class Mapping {
550: /**
551: * Description of mapping
552: */
553: private String m_sDescription = "";
554: /**
555: * List of <code>String</code> mime types for mapping
556: */
557: private ArrayList m_aMimeTypes = new ArrayList();
558: /**
559: * List of <code>String</code> extensions for mapping
560: */
561: private ArrayList m_aFileExtensions = new ArrayList();
562:
563: /**
564: * Constructs mapping with the given description
565: *
566: * @param sDescription the description for this mapping
567: */
568: private Mapping(String sDescription) {
569: this .m_sDescription = sDescription;
570: }
571:
572: /**
573: * Returns the descirption for this mapping
574: *
575: * @return the description for this mapping
576: */
577: public String getDescription() {
578: return this .m_sDescription;
579: }
580:
581: /**
582: * Adds the given mime type to the the list of mime types
583: * applicable for this mapping
584: *
585: * @param sMimeType the mime type to add to this mapping
586: */
587: public void addMimeType(String sMimeType) {
588: this .m_aMimeTypes.add(sMimeType);
589: }
590:
591: /**
592: * Returns the list of mime types for this mapping
593: *
594: * @return the list of mime types for this mapping
595: */
596: public List getMimeTypes() {
597: return this .m_aMimeTypes;
598: }
599:
600: /**
601: * Returns the default mime type for this mapping
602: *
603: * @return the default mime type for this mapping
604: */
605: public String getMimeType() {
606: return (String) m_aMimeTypes.get(0);
607: }
608:
609: /**
610: * Adds the given extension to the the list of extensions
611: * applicable for this mapping
612: *
613: * @param sExtension
614: */
615: public void addExtension(String sExtension) {
616: this .m_aFileExtensions.add(sExtension);
617: }
618:
619: /**
620: * Returns the list of extensions applicable for this mapping
621: *
622: * @return a list of extensions
623: */
624: public List getExtensions() {
625: return this .m_aFileExtensions;
626: }
627:
628: /**
629: * Returns the default extension for this mapping
630: *
631: * @return the default extension for this mapping
632: */
633: public String getExtension() {
634: return (String) m_aFileExtensions.get(0);
635: }
636: }
637:
638: /**
639: * Runs a test of this class printing contents to the standard output
640: *
641: * @param args
642: */
643: public static void main(String[] args) {
644: System.out.println(MimeTypeMapping.getMimeTypes("GIF"));
645: System.out.println(MimeTypeMapping.getExtensions("image/gif"));
646: System.out.println(MimeTypeMapping
647: .getDescriptionForExtension("gif"));
648: System.out.println(MimeTypeMapping
649: .getDescriptionForMimeType("image/gif"));
650: System.out
651: .println("----------------------------------------------------");
652: Iterator itor = MimeTypeMapping.m_aExtensionMap.values()
653: .iterator();
654: while (itor.hasNext()) {
655: Mapping map = (Mapping) itor.next();
656: System.out.println(map.getDescription() + " exts="
657: + map.getExtensions() + " mimetypes="
658: + map.getMimeTypes());
659: System.out
660: .println("----------------------------------------------------");
661: }
662: System.exit(0);
663: }
664:
665: }
|