001: // Utils.java
002: // $Id: Utils.java,v 1.14 2003/08/06 14:54:06 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.mime;
007:
008: import java.util.Hashtable;
009: import java.util.Locale;
010: import java.util.Vector;
011:
012: import org.w3c.tools.sorter.Sorter;
013:
014: /**
015: * @version $Revision: 1.14 $
016: * @author Benoît Mahé (bmahe@w3.org)
017: */
018: public class Utils {
019:
020: private static Hashtable extension_map = new Hashtable();
021:
022: private static void setSuffix(String ext, String ct) {
023: extension_map.put(ext, ct);
024: }
025:
026: private static Hashtable charset_map = new Hashtable();
027:
028: private static void setCharset(String lang, String charset) {
029: charset_map.put(lang, charset);
030: }
031:
032: static {
033: setSuffix("", "content/unknown");
034: setSuffix("uu", "application/octet-stream");
035: setSuffix("saveme", "application/octet-stream");
036: setSuffix("dump", "application/octet-stream");
037: setSuffix("hqx", "application/octet-stream");
038: setSuffix("arc", "application/octet-stream");
039: setSuffix("o", "application/octet-stream");
040: setSuffix("a", "application/octet-stream");
041: setSuffix("bin", "application/octet-stream");
042: setSuffix("exe", "application/octet-stream");
043: setSuffix("z", "application/octet-stream");
044: setSuffix("gz", "application/octet-stream");
045: setSuffix("jar", "application/octet-stream");
046: setSuffix("oda", "application/oda");
047: setSuffix("pdf", "application/pdf");
048: setSuffix("eps", "application/postscript");
049: setSuffix("ai", "application/postscript");
050: setSuffix("ps", "application/postscript");
051: setSuffix("rtf", "application/rtf");
052: setSuffix("dvi", "application/x-dvi");
053: setSuffix("hdf", "application/x-hdf");
054: setSuffix("latex", "application/x-latex");
055: setSuffix("cdf", "application/x-netcdf");
056: setSuffix("nc", "application/x-netcdf");
057: setSuffix("tex", "application/x-tex");
058: setSuffix("texinfo", "application/x-texinfo");
059: setSuffix("texi", "application/x-texinfo");
060: setSuffix("t", "application/x-troff");
061: setSuffix("tr", "application/x-troff");
062: setSuffix("roff", "application/x-troff");
063: setSuffix("man", "application/x-troff-man");
064: setSuffix("me", "application/x-troff-me");
065: setSuffix("ms", "application/x-troff-ms");
066: setSuffix("src", "application/x-wais-source");
067: setSuffix("wsrc", "application/x-wais-source");
068: setSuffix("zip", "application/zip");
069: setSuffix("bcpio", "application/x-bcpio");
070: setSuffix("cpio", "application/x-cpio");
071: setSuffix("gtar", "application/x-gtar");
072: setSuffix("shar", "application/x-shar");
073: setSuffix("sh", "application/x-shar");
074: setSuffix("sv4cpio", "application/x-sv4cpio");
075: setSuffix("sv4crc", "application/x-sv4crc");
076: setSuffix("tar", "application/x-tar");
077: setSuffix("ustar", "application/x-ustar");
078: setSuffix("snd", "audio/basic");
079: setSuffix("au", "audio/basic");
080: setSuffix("aifc", "audio/x-aiff");
081: setSuffix("aif", "audio/x-aiff");
082: setSuffix("aiff", "audio/x-aiff");
083: setSuffix("wav", "audio/x-wav");
084: setSuffix("gif", "image/gif");
085: setSuffix("ief", "image/ief");
086: setSuffix("jfif", "image/jpeg");
087: setSuffix("jfif-tbnl", "image/jpeg");
088: setSuffix("jpe", "image/jpeg");
089: setSuffix("jpg", "image/jpeg");
090: setSuffix("jpeg", "image/jpeg");
091: setSuffix("tif", "image/tiff");
092: setSuffix("tiff", "image/tiff");
093: setSuffix("ras", "image/x-cmu-rast");
094: setSuffix("pnm", "image/x-portable-anymap");
095: setSuffix("pbm", "image/x-portable-bitmap");
096: setSuffix("pgm", "image/x-portable-graymap");
097: setSuffix("ppm", "image/x-portable-pixmap");
098: setSuffix("rgb", "image/x-rgb");
099: setSuffix("png", "image/png");
100: setSuffix("xbm", "image/x-xbitmap");
101: setSuffix("xml", "text/xml");
102: setSuffix("xsl", "text/xml");
103: setSuffix("xsd", "text/xml");
104: setSuffix("xpm", "image/x-xpixmap");
105: setSuffix("xwd", "image/x-xwindowdump");
106: setSuffix("htm", "text/html");
107: setSuffix("html", "text/html");
108: setSuffix("xhtm", "application/xhtml+xml");
109: setSuffix("xhtml", "application/xhtml+xml");
110: setSuffix("css", "text/css");
111: setSuffix("text", "text/plain");
112: setSuffix("c", "text/plain");
113: setSuffix("cc", "text/plain");
114: setSuffix("c++", "text/plain");
115: setSuffix("h", "text/plain");
116: setSuffix("pl", "text/plain");
117: setSuffix("txt", "text/plain");
118: setSuffix("java", "text/plain");
119: setSuffix("rtx", "application/rtf");
120: setSuffix("tsv", "texyt/tab-separated-values");
121: setSuffix("etx", "text/x-setext");
122: setSuffix("mpg", "video/mpeg");
123: setSuffix("mpe", "video/mpeg");
124: setSuffix("mpeg", "video/mpeg");
125: setSuffix("mov", "video/quicktime");
126: setSuffix("qt", "video/quicktime");
127: setSuffix("avi", "application/x-troff-msvideo");
128: setSuffix("movie", "video/x-sgi-movie");
129: setSuffix("mv", "video/x-sgi-movie");
130: setSuffix("mime", "message/rfc822");
131: setSuffix("smi", "application/smil");
132: setSuffix("smil", "application/smil");
133: setSuffix("sgml", "text/sgml");
134: setSuffix("sgm", "text/sgml");
135: setSuffix("svg", "image/svg+xml");
136:
137: setCharset("ar", "ISO-8859-6");
138: setCharset("be", "ISO-8859-5");
139: setCharset("bg", "ISO-8859-5");
140: setCharset("ca", "ISO-8859-1");
141: setCharset("cs", "ISO-8859-2");
142: setCharset("da", "ISO-8859-1");
143: setCharset("de", "ISO-8859-1");
144: setCharset("el", "ISO-8859-7");
145: setCharset("en", "ISO-8859-1");
146: setCharset("es", "ISO-8859-1");
147: setCharset("et", "ISO-8859-1");
148: setCharset("fi", "ISO-8859-1");
149: setCharset("fr", "ISO-8859-1");
150: setCharset("hr", "ISO-8859-2");
151: setCharset("hu", "ISO-8859-2");
152: setCharset("is", "ISO-8859-1");
153: setCharset("it", "ISO-8859-1");
154: setCharset("iw", "ISO-8859-8");
155: setCharset("ja", "Shift_JIS");
156: setCharset("ko", "EUC-KR");
157: setCharset("lt", "ISO-8859-2");
158: setCharset("lv", "ISO-8859-2");
159: setCharset("mk", "ISO-8859-5");
160: setCharset("nl", "ISO-8859-1");
161: setCharset("no", "ISO-8859-1");
162: setCharset("pl", "ISO-8859-2");
163: setCharset("pt", "ISO-8859-1");
164: setCharset("ro", "ISO-8859-2");
165: setCharset("ru", "ISO-8859-5");
166: setCharset("sh", "ISO-8859-5");
167: setCharset("sk", "ISO-8859-2");
168: setCharset("sl", "ISO-8859-2");
169: setCharset("sq", "ISO-8859-2");
170: setCharset("sr", "ISO-8859-5");
171: setCharset("sv", "ISO-8859-1");
172: setCharset("tr", "ISO-8859-9");
173: setCharset("uk", "ISO-8859-5");
174: setCharset("zh", "GB2312");
175: setCharset("zh_TW", "Big5");
176:
177: }
178:
179: /**
180: * A useful utility routine that tries to guess the content-type
181: * of an object based upon its extension.
182: */
183: public static String guessContentTypeFromName(String fname) {
184: String ext = "";
185: int i = fname.lastIndexOf('#');
186:
187: if (i != -1)
188: fname = fname.substring(0, i - 1);
189: i = fname.lastIndexOf('.');
190: i = Math.max(i, fname.lastIndexOf('/'));
191: i = Math.max(i, fname.lastIndexOf('?'));
192:
193: if (i != -1 && fname.charAt(i) == '.') {
194: if (++i < fname.length()) {
195: ext = fname.substring(i).toLowerCase();
196: }
197: }
198: return (String) extension_map.get(ext);
199: }
200:
201: public static MimeType getMimeType(String filename) {
202: try {
203: return new MimeType(guessContentTypeFromName(filename));
204: } catch (MimeTypeFormatException ex) {
205: return null;
206: }
207: }
208:
209: public static Vector getKnownMimeTypesinVector() {
210: return Sorter.sortStringEnumeration(extension_map.elements());
211: }
212:
213: public static String[] getKnownMimeTypes() {
214: Vector sorted = Sorter.sortStringEnumeration(extension_map
215: .elements());
216: String array[] = new String[sorted.size()];
217: sorted.copyInto(array);
218: return array;
219: }
220:
221: public static Vector getKnownExtensionsinVector() {
222: return Sorter.sortStringEnumeration(extension_map.keys());
223: }
224:
225: public static String[] getKnownExtensions() {
226: Vector sorted = Sorter.sortStringEnumeration(extension_map
227: .keys());
228: String array[] = new String[sorted.size()];
229: sorted.copyInto(array);
230: return array;
231: }
232:
233: public static Vector getKnownLanguagesinVector() {
234: return Sorter.sortStringEnumeration(charset_map.keys());
235: }
236:
237: public static String[] getKnownLanguages() {
238: Vector sorted = Sorter
239: .sortStringEnumeration(charset_map.keys());
240: String array[] = new String[sorted.size()];
241: sorted.copyInto(array);
242: return array;
243: }
244:
245: public static Vector getKnownCharsetsinVector() {
246: return Sorter.sortStringEnumeration(charset_map.elements());
247: }
248:
249: public static String[] getKnownCharsets() {
250: Vector sorted = Sorter.sortStringEnumeration(charset_map
251: .elements());
252: String array[] = new String[sorted.size()];
253: sorted.copyInto(array);
254: return array;
255: }
256:
257: public static String getCharset(Locale locale) {
258: return getCharset(locale.getLanguage());
259: }
260:
261: public static String getCharset(String lang) {
262: return (String) charset_map.get(lang);
263: }
264:
265: }
|