You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileTypeResolver.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.service;
  5. import java.io.File;
  6. import java.util.Collections;
  7. import java.util.Hashtable;
  8. import java.util.Map;
  9. import java.util.StringTokenizer;
  10. import com.itmill.toolkit.terminal.Resource;
  11. import com.itmill.toolkit.terminal.ThemeResource;
  12. /**
  13. * Utility class that can figure out mime-types and icons related to files.
  14. * <p>
  15. * Note : The icons are associated purely to mime-types, so a file may not have
  16. * a custom icon accessible with this class.
  17. * </p>
  18. *
  19. * @author IT Mill Ltd.
  20. * @version
  21. * @VERSION@
  22. * @since 3.0
  23. */
  24. public class FileTypeResolver {
  25. /**
  26. * Default icon given if no icon is specified for a mime-type.
  27. */
  28. static public Resource DEFAULT_ICON = new ThemeResource(
  29. "icon/files/file.gif");
  30. /**
  31. * Default mime-type.
  32. */
  33. static public String DEFAULT_MIME_TYPE = "application/octet-stream";
  34. /**
  35. * Initial file extension to mime-type mapping.
  36. */
  37. static private String initialExtToMIMEMap = "application/cu-seeme csm cu,"
  38. + "application/dsptype tsp,"
  39. + "application/futuresplash spl,"
  40. + "application/mac-binhex40 hqx,"
  41. + "application/msaccess mdb,"
  42. + "application/msword doc dot,"
  43. + "application/octet-stream bin,"
  44. + "application/oda oda,"
  45. + "application/pdf pdf,"
  46. + "application/pgp-signature pgp,"
  47. + "application/postscript ps ai eps,"
  48. + "application/rtf rtf,"
  49. + "application/vnd.ms-excel xls xlb,"
  50. + "application/vnd.ms-powerpoint ppt pps pot,"
  51. + "application/vnd.wap.wmlc wmlc,"
  52. + "application/vnd.wap.wmlscriptc wmlsc,"
  53. + "application/wordperfect5.1 wp5,"
  54. + "application/zip zip,"
  55. + "application/x-123 wk,"
  56. + "application/x-bcpio bcpio,"
  57. + "application/x-chess-pgn pgn,"
  58. + "application/x-cpio cpio,"
  59. + "application/x-debian-package deb,"
  60. + "application/x-director dcr dir dxr,"
  61. + "application/x-dms dms,"
  62. + "application/x-dvi dvi,"
  63. + "application/x-xfig fig,"
  64. + "application/x-font pfa pfb gsf pcf pcf.Z,"
  65. + "application/x-gnumeric gnumeric,"
  66. + "application/x-gtar gtar tgz taz,"
  67. + "application/x-hdf hdf,"
  68. + "application/x-httpd-php phtml pht php,"
  69. + "application/x-httpd-php3 php3,"
  70. + "application/x-httpd-php3-source phps,"
  71. + "application/x-httpd-php3-preprocessed php3p,"
  72. + "application/x-httpd-php4 php4,"
  73. + "application/x-ica ica,"
  74. + "application/x-java-archive jar,"
  75. + "application/x-java-serialized-object ser,"
  76. + "application/x-java-vm class,"
  77. + "application/x-javascript js,"
  78. + "application/x-kchart chrt,"
  79. + "application/x-killustrator kil,"
  80. + "application/x-kpresenter kpr kpt,"
  81. + "application/x-kspread ksp,"
  82. + "application/x-kword kwd kwt,"
  83. + "application/x-latex latex,"
  84. + "application/x-lha lha,"
  85. + "application/x-lzh lzh,"
  86. + "application/x-lzx lzx,"
  87. + "application/x-maker frm maker frame fm fb book fbdoc,"
  88. + "application/x-mif mif,"
  89. + "application/x-msdos-program com exe bat dll,"
  90. + "application/x-msi msi,"
  91. + "application/x-netcdf nc cdf,"
  92. + "application/x-ns-proxy-autoconfig pac,"
  93. + "application/x-object o,"
  94. + "application/x-ogg ogg,"
  95. + "application/x-oz-application oza,"
  96. + "application/x-perl pl pm,"
  97. + "application/x-pkcs7-crl crl,"
  98. + "application/x-redhat-package-manager rpm,"
  99. + "application/x-shar shar,"
  100. + "application/x-shockwave-flash swf swfl,"
  101. + "application/x-star-office sdd sda,"
  102. + "application/x-stuffit sit,"
  103. + "application/x-sv4cpio sv4cpio,"
  104. + "application/x-sv4crc sv4crc,"
  105. + "application/x-tar tar,"
  106. + "application/x-tex-gf gf,"
  107. + "application/x-tex-pk pk PK,"
  108. + "application/x-texinfo texinfo texi,"
  109. + "application/x-trash ~ % bak old sik,"
  110. + "application/x-troff t tr roff,"
  111. + "application/x-troff-man man,"
  112. + "application/x-troff-me me,"
  113. + "application/x-troff-ms ms,"
  114. + "application/x-ustar ustar,"
  115. + "application/x-wais-source src,"
  116. + "application/x-wingz wz,"
  117. + "application/x-x509-ca-cert crt,"
  118. + "audio/basic au snd,"
  119. + "audio/midi mid midi,"
  120. + "audio/mpeg mpga mpega mp2 mp3,"
  121. + "audio/mpegurl m3u,"
  122. + "audio/prs.sid sid,"
  123. + "audio/x-aiff aif aiff aifc,"
  124. + "audio/x-gsm gsm,"
  125. + "audio/x-pn-realaudio ra rm ram,"
  126. + "audio/x-scpls pls,"
  127. + "audio/x-wav wav,"
  128. + "image/bitmap bmp,"
  129. + "image/gif gif,"
  130. + "image/ief ief,"
  131. + "image/jpeg jpeg jpg jpe,"
  132. + "image/pcx pcx,"
  133. + "image/png png,"
  134. + "image/tiff tiff tif,"
  135. + "image/vnd.wap.wbmp wbmp,"
  136. + "image/x-cmu-raster ras,"
  137. + "image/x-coreldraw cdr,"
  138. + "image/x-coreldrawpattern pat,"
  139. + "image/x-coreldrawtemplate cdt,"
  140. + "image/x-corelphotopaint cpt,"
  141. + "image/x-jng jng,"
  142. + "image/x-portable-anymap pnm,"
  143. + "image/x-portable-bitmap pbm,"
  144. + "image/x-portable-graymap pgm,"
  145. + "image/x-portable-pixmap ppm,"
  146. + "image/x-rgb rgb,"
  147. + "image/x-xbitmap xbm,"
  148. + "image/x-xpixmap xpm,"
  149. + "image/x-xwindowdump xwd,"
  150. + "text/comma-separated-values csv,"
  151. + "text/css css,"
  152. + "text/html htm html xhtml,"
  153. + "text/mathml mml,"
  154. + "text/plain txt text diff,"
  155. + "text/richtext rtx,"
  156. + "text/tab-separated-values tsv,"
  157. + "text/vnd.wap.wml wml,"
  158. + "text/vnd.wap.wmlscript wmls,"
  159. + "text/xml xml,"
  160. + "text/x-c++hdr h++ hpp hxx hh,"
  161. + "text/x-c++src c++ cpp cxx cc,"
  162. + "text/x-chdr h,"
  163. + "text/x-csh csh,"
  164. + "text/x-csrc c,"
  165. + "text/x-java java,"
  166. + "text/x-moc moc,"
  167. + "text/x-pascal p pas,"
  168. + "text/x-setext etx,"
  169. + "text/x-sh sh,"
  170. + "text/x-tcl tcl tk,"
  171. + "text/x-tex tex ltx sty cls,"
  172. + "text/x-vcalendar vcs,"
  173. + "text/x-vcard vcf,"
  174. + "video/dl dl,"
  175. + "video/fli fli,"
  176. + "video/gl gl,"
  177. + "video/mpeg mpeg mpg mpe,"
  178. + "video/quicktime qt mov,"
  179. + "video/x-mng mng,"
  180. + "video/x-ms-asf asf asx,"
  181. + "video/x-msvideo avi,"
  182. + "video/x-sgi-movie movie,"
  183. + "x-world/x-vrml vrm vrml wrl";
  184. /**
  185. * File extension to MIME type mapping.
  186. */
  187. static private Hashtable extToMIMEMap = new Hashtable();
  188. /**
  189. * MIME type to Icon mapping.
  190. */
  191. static private Hashtable MIMEToIconMap = new Hashtable();
  192. static {
  193. // Initialize extension to MIME map
  194. final StringTokenizer lines = new StringTokenizer(initialExtToMIMEMap,
  195. ",");
  196. while (lines.hasMoreTokens()) {
  197. final String line = lines.nextToken();
  198. final StringTokenizer exts = new StringTokenizer(line);
  199. final String type = exts.nextToken();
  200. while (exts.hasMoreTokens()) {
  201. final String ext = exts.nextToken();
  202. addExtension(ext, type);
  203. }
  204. }
  205. // Initialize Icons
  206. addIcon("inode/drive", new ThemeResource("icon/files/drive.gif"));
  207. addIcon("inode/directory", new ThemeResource("icon/files/folder.gif"));
  208. }
  209. /**
  210. * Gets the mime-type of a file. Currently the mime-type is resolved based
  211. * only on the file name extension.
  212. *
  213. * @param fileName
  214. * the name of the file whose mime-type is requested.
  215. * @return mime-type <code>String</code> for the given filename
  216. */
  217. public static String getMIMEType(String fileName) {
  218. // Checks for nulls
  219. if (fileName == null) {
  220. throw new NullPointerException("Filename can not be null");
  221. }
  222. // Calculates the extension of the file
  223. int dotIndex = fileName.indexOf(".");
  224. while (dotIndex >= 0 && fileName.indexOf(".", dotIndex + 1) >= 0) {
  225. dotIndex = fileName.indexOf(".", dotIndex + 1);
  226. }
  227. dotIndex++;
  228. if (fileName.length() > dotIndex) {
  229. final String ext = fileName.substring(dotIndex);
  230. // Return type from extension map, if found
  231. final String type = (String) extToMIMEMap.get(ext);
  232. if (type != null) {
  233. return type;
  234. }
  235. }
  236. return DEFAULT_MIME_TYPE;
  237. }
  238. /**
  239. * Gets the descriptive icon representing file, based on the filename. First
  240. * the mime-type for the given filename is resolved, and then the
  241. * corresponding icon is fetched from the internal icon storage. If it is
  242. * not found the default icon is returned.
  243. *
  244. * @param fileName
  245. * the name of the file whose icon is requested.
  246. * @return the icon corresponding to the given file
  247. */
  248. public static Resource getIcon(String fileName) {
  249. final String mimeType = getMIMEType(fileName);
  250. final Resource icon = (Resource) MIMEToIconMap.get(mimeType);
  251. if (icon != null) {
  252. return icon;
  253. }
  254. // If nothing is known about the file-type, general file
  255. // icon is used
  256. return DEFAULT_ICON;
  257. }
  258. /**
  259. * Gets the descriptive icon representing a file. First the mime-type for
  260. * the given file name is resolved, and then the corresponding icon is
  261. * fetched from the internal icon storage. If it is not found the default
  262. * icon is returned.
  263. *
  264. * @param file
  265. * the file whose icon is requested.
  266. * @return the icon corresponding to the given file
  267. */
  268. public static Resource getIcon(File file) {
  269. final String mimeType = getMIMEType(file);
  270. final Resource icon = (Resource) MIMEToIconMap.get(mimeType);
  271. if (icon != null) {
  272. return icon;
  273. }
  274. // If nothing is known about the file-type, general file
  275. // icon is used
  276. return DEFAULT_ICON;
  277. }
  278. /**
  279. * Gets the mime-type for a file. Currently the returned file type is
  280. * resolved by the filename extension only.
  281. *
  282. * @param file
  283. * the file whose mime-type is requested.
  284. * @return the files mime-type <code>String</code>
  285. */
  286. public static String getMIMEType(File file) {
  287. // Checks for nulls
  288. if (file == null) {
  289. throw new NullPointerException("File can not be null");
  290. }
  291. // Drives
  292. if (file.getParentFile() == null) {
  293. return "inode/drive";
  294. }
  295. // Directories
  296. if (file.isDirectory()) {
  297. return "inode/directory";
  298. }
  299. // Return type from extension
  300. return getMIMEType(file.getName());
  301. }
  302. /**
  303. * Adds a mime-type mapping for the given filename extension. If the
  304. * extension is already in the internal mapping it is overwritten.
  305. *
  306. * @param extension
  307. * the filename extension to be associated with
  308. * <code>MIMEType</code>.
  309. * @param MIMEType
  310. * the new mime-type for <code>extension</code>.
  311. */
  312. public static void addExtension(String extension, String MIMEType) {
  313. extToMIMEMap.put(extension, MIMEType);
  314. }
  315. /**
  316. * Adds a icon for the given mime-type. If the mime-type also has a
  317. * corresponding icon, it is replaced with the new icon.
  318. *
  319. * @param MIMEType
  320. * the mime-type whose icon is to be changed.
  321. * @param icon
  322. * the new icon to be associated with <code>MIMEType</code>.
  323. */
  324. public static void addIcon(String MIMEType, Resource icon) {
  325. MIMEToIconMap.put(MIMEType, icon);
  326. }
  327. /**
  328. * Gets the internal file extension to mime-type mapping.
  329. *
  330. * @return unmodifiable map containing the current file extension to
  331. * mime-type mapping
  332. */
  333. public static Map getExtensionToMIMETypeMapping() {
  334. return Collections.unmodifiableMap(extToMIMEMap);
  335. }
  336. /**
  337. * Gets the internal mime-type to icon mapping.
  338. *
  339. * @return unmodifiable map containing the current mime-type to icon mapping
  340. */
  341. public static Map getMIMETypeToIconMapping() {
  342. return Collections.unmodifiableMap(MIMEToIconMap);
  343. }
  344. }