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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.service;
  5. import java.io.File;
  6. import java.io.Serializable;
  7. import java.util.Collections;
  8. import java.util.Hashtable;
  9. import java.util.Map;
  10. import java.util.StringTokenizer;
  11. import com.vaadin.terminal.Resource;
  12. import com.vaadin.terminal.ThemeResource;
  13. /**
  14. * Utility class that can figure out mime-types and icons related to files.
  15. * <p>
  16. * Note : The icons are associated purely to mime-types, so a file may not have
  17. * a custom icon accessible with this class.
  18. * </p>
  19. *
  20. * @author Vaadin Ltd.
  21. * @since 3.0
  22. */
  23. @SuppressWarnings("serial")
  24. public class FileTypeResolver implements Serializable {
  25. /**
  26. * Default icon given if no icon is specified for a mime-type.
  27. */
  28. static public Resource DEFAULT_ICON = new ThemeResource(
  29. "../runo/icons/16/document.png");
  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. + "audio/ogg ogg,"
  129. + "audio/mp4 m4a,"
  130. + "audio/x-aac aac,"
  131. + "image/bitmap bmp,"
  132. + "image/gif gif,"
  133. + "image/ief ief,"
  134. + "image/jpeg jpeg jpg jpe,"
  135. + "image/pcx pcx,"
  136. + "image/png png,"
  137. + "image/svg+xml svg svgz,"
  138. + "image/tiff tiff tif,"
  139. + "image/vnd.wap.wbmp wbmp,"
  140. + "image/x-cmu-raster ras,"
  141. + "image/x-coreldraw cdr,"
  142. + "image/x-coreldrawpattern pat,"
  143. + "image/x-coreldrawtemplate cdt,"
  144. + "image/x-corelphotopaint cpt,"
  145. + "image/x-jng jng,"
  146. + "image/x-portable-anymap pnm,"
  147. + "image/x-portable-bitmap pbm,"
  148. + "image/x-portable-graymap pgm,"
  149. + "image/x-portable-pixmap ppm,"
  150. + "image/x-rgb rgb,"
  151. + "image/x-xbitmap xbm,"
  152. + "image/x-xpixmap xpm,"
  153. + "image/x-xwindowdump xwd,"
  154. + "text/comma-separated-values csv,"
  155. + "text/css css,"
  156. + "text/html htm html xhtml,"
  157. + "text/mathml mml,"
  158. + "text/plain txt text diff,"
  159. + "text/richtext rtx,"
  160. + "text/tab-separated-values tsv,"
  161. + "text/vnd.wap.wml wml,"
  162. + "text/vnd.wap.wmlscript wmls,"
  163. + "text/xml xml,"
  164. + "text/x-c++hdr h++ hpp hxx hh,"
  165. + "text/x-c++src c++ cpp cxx cc,"
  166. + "text/x-chdr h,"
  167. + "text/x-csh csh,"
  168. + "text/x-csrc c,"
  169. + "text/x-java java,"
  170. + "text/x-moc moc,"
  171. + "text/x-pascal p pas,"
  172. + "text/x-setext etx,"
  173. + "text/x-sh sh,"
  174. + "text/x-tcl tcl tk,"
  175. + "text/x-tex tex ltx sty cls,"
  176. + "text/x-vcalendar vcs,"
  177. + "text/x-vcard vcf,"
  178. + "video/dl dl,"
  179. + "video/fli fli,"
  180. + "video/gl gl,"
  181. + "video/mpeg mpeg mpg mpe,"
  182. + "video/quicktime qt mov,"
  183. + "video/x-mng mng,"
  184. + "video/x-ms-asf asf asx,"
  185. + "video/x-msvideo avi,"
  186. + "video/x-sgi-movie movie,"
  187. + "video/ogg ogv,"
  188. + "video/mp4 mp4,"
  189. + "x-world/x-vrml vrm vrml wrl";
  190. /**
  191. * File extension to MIME type mapping. All extensions are in lower case.
  192. */
  193. static private Hashtable<String, String> extToMIMEMap = new Hashtable<String, String>();
  194. /**
  195. * MIME type to Icon mapping.
  196. */
  197. static private Hashtable<String, Resource> MIMEToIconMap = new Hashtable<String, Resource>();
  198. static {
  199. // Initialize extension to MIME map
  200. final StringTokenizer lines = new StringTokenizer(initialExtToMIMEMap,
  201. ",");
  202. while (lines.hasMoreTokens()) {
  203. final String line = lines.nextToken();
  204. final StringTokenizer exts = new StringTokenizer(line);
  205. final String type = exts.nextToken();
  206. while (exts.hasMoreTokens()) {
  207. final String ext = exts.nextToken();
  208. addExtension(ext, type);
  209. }
  210. }
  211. // Initialize Icons
  212. ThemeResource folder = new ThemeResource("../runo/icons/16/folder.png");
  213. addIcon("inode/drive", folder);
  214. addIcon("inode/directory", folder);
  215. }
  216. /**
  217. * Gets the mime-type of a file. Currently the mime-type is resolved based
  218. * only on the file name extension.
  219. *
  220. * @param fileName
  221. * the name of the file whose mime-type is requested.
  222. * @return mime-type <code>String</code> for the given filename
  223. */
  224. public static String getMIMEType(String fileName) {
  225. // Checks for nulls
  226. if (fileName == null) {
  227. throw new NullPointerException("Filename can not be null");
  228. }
  229. // Calculates the extension of the file
  230. int dotIndex = fileName.indexOf(".");
  231. while (dotIndex >= 0 && fileName.indexOf(".", dotIndex + 1) >= 0) {
  232. dotIndex = fileName.indexOf(".", dotIndex + 1);
  233. }
  234. dotIndex++;
  235. if (fileName.length() > dotIndex) {
  236. String ext = fileName.substring(dotIndex);
  237. // Ignore any query parameters
  238. int queryStringStart = ext.indexOf('?');
  239. if (queryStringStart > 0) {
  240. ext = ext.substring(0, queryStringStart);
  241. }
  242. // Return type from extension map, if found
  243. final String type = extToMIMEMap.get(ext.toLowerCase());
  244. if (type != null) {
  245. return type;
  246. }
  247. }
  248. return DEFAULT_MIME_TYPE;
  249. }
  250. /**
  251. * Gets the descriptive icon representing file, based on the filename. First
  252. * the mime-type for the given filename is resolved, and then the
  253. * corresponding icon is fetched from the internal icon storage. If it is
  254. * not found the default icon is returned.
  255. *
  256. * @param fileName
  257. * the name of the file whose icon is requested.
  258. * @return the icon corresponding to the given file
  259. */
  260. public static Resource getIcon(String fileName) {
  261. return getIconByMimeType(getMIMEType(fileName));
  262. }
  263. private static Resource getIconByMimeType(String mimeType) {
  264. final Resource icon = MIMEToIconMap.get(mimeType);
  265. if (icon != null) {
  266. return icon;
  267. }
  268. // If nothing is known about the file-type, general file
  269. // icon is used
  270. return DEFAULT_ICON;
  271. }
  272. /**
  273. * Gets the descriptive icon representing a file. First the mime-type for
  274. * the given file name is resolved, and then the corresponding icon is
  275. * fetched from the internal icon storage. If it is not found the default
  276. * icon is returned.
  277. *
  278. * @param file
  279. * the file whose icon is requested.
  280. * @return the icon corresponding to the given file
  281. */
  282. public static Resource getIcon(File file) {
  283. return getIconByMimeType(getMIMEType(file));
  284. }
  285. /**
  286. * Gets the mime-type for a file. Currently the returned file type is
  287. * resolved by the filename extension only.
  288. *
  289. * @param file
  290. * the file whose mime-type is requested.
  291. * @return the files mime-type <code>String</code>
  292. */
  293. public static String getMIMEType(File file) {
  294. // Checks for nulls
  295. if (file == null) {
  296. throw new NullPointerException("File can not be null");
  297. }
  298. // Directories
  299. if (file.isDirectory()) {
  300. // Drives
  301. if (file.getParentFile() == null) {
  302. return "inode/drive";
  303. } else {
  304. return "inode/directory";
  305. }
  306. }
  307. // Return type from extension
  308. return getMIMEType(file.getName());
  309. }
  310. /**
  311. * Adds a mime-type mapping for the given filename extension. If the
  312. * extension is already in the internal mapping it is overwritten.
  313. *
  314. * @param extension
  315. * the filename extension to be associated with
  316. * <code>MIMEType</code>.
  317. * @param MIMEType
  318. * the new mime-type for <code>extension</code>.
  319. */
  320. public static void addExtension(String extension, String MIMEType) {
  321. extToMIMEMap.put(extension.toLowerCase(), MIMEType);
  322. }
  323. /**
  324. * Adds a icon for the given mime-type. If the mime-type also has a
  325. * corresponding icon, it is replaced with the new icon.
  326. *
  327. * @param MIMEType
  328. * the mime-type whose icon is to be changed.
  329. * @param icon
  330. * the new icon to be associated with <code>MIMEType</code>.
  331. */
  332. public static void addIcon(String MIMEType, Resource icon) {
  333. MIMEToIconMap.put(MIMEType, icon);
  334. }
  335. /**
  336. * Gets the internal file extension to mime-type mapping.
  337. *
  338. * @return unmodifiable map containing the current file extension to
  339. * mime-type mapping
  340. */
  341. public static Map<String, String> getExtensionToMIMETypeMapping() {
  342. return Collections.unmodifiableMap(extToMIMEMap);
  343. }
  344. /**
  345. * Gets the internal mime-type to icon mapping.
  346. *
  347. * @return unmodifiable map containing the current mime-type to icon mapping
  348. */
  349. public static Map<String, Resource> getMIMETypeToIconMapping() {
  350. return Collections.unmodifiableMap(MIMEToIconMap);
  351. }
  352. }