001: package org.claros.intouch.webmail.controllers;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.util.Locale;
006: import java.util.Properties;
007:
008: import org.claros.commons.configuration.Paths;
009:
010: public class IconController {
011: private static Properties propMimes = new Properties();
012: private static Properties propExt = new Properties();
013: private static String defaultIcon = "binary.png";
014: private static Locale loc = new Locale("en", "US");
015:
016: static {
017: try {
018: FileInputStream is = new FileInputStream(new File(Paths
019: .getCfgFolder()
020: + "/mime.properties"));
021: propMimes.load(is);
022: is.close();
023: } catch (Exception e) {
024: e.printStackTrace();
025: }
026:
027: try {
028: FileInputStream is = new FileInputStream(new File(Paths
029: .getCfgFolder()
030: + "/extension.properties"));
031: propExt.load(is);
032: is.close();
033: } catch (Exception e) {
034: e.printStackTrace();
035: }
036: }
037:
038: /**
039: *
040: * @param mime
041: * @return
042: */
043: public static String findIconByMime(String mime) {
044: if (mime == null) {
045: return defaultIcon;
046: }
047: mime = mime.toLowerCase(loc).trim();
048: if (mime.indexOf(";") > 0) {
049: mime = mime.substring(0, mime.indexOf(";"));
050: }
051: if (mime.indexOf(" ") > 0) {
052: mime = mime.substring(0, mime.indexOf(" "));
053: }
054:
055: String ico = propMimes.getProperty(mime);
056: if (ico == null) {
057: if (mime.indexOf("/") > 0) {
058: String mimeType = mime.substring(0, mime.indexOf("/"));
059: ico = propMimes.getProperty(mimeType + "/*",
060: defaultIcon);
061: }
062: }
063: return ico;
064: }
065:
066: /**
067: *
068: * @param name
069: * @return
070: */
071: public static String findIconByName(String name) {
072: if (name != null && name.trim().length() > 0) {
073: int pos = name.lastIndexOf(".");
074: if (pos > 0) {
075: String ext = name.substring(pos + 1);
076: if (ext != null && ext.trim().length() > 0) {
077: String mime = propExt
078: .getProperty(ext.toLowerCase());
079: String ico = propMimes.getProperty(mime);
080: if (ico == null) {
081: if (mime.indexOf("/") > 0) {
082: String mimeType = mime.substring(0, mime
083: .indexOf("/"));
084: ico = propMimes.getProperty(
085: mimeType + "/*", defaultIcon);
086: }
087: }
088: return ico;
089: }
090: }
091: }
092: return null;
093: }
094:
095: /**
096: *
097: * @param name
098: * @return
099: */
100: public static String findMimeByName(String name) {
101: if (name != null && name.trim().length() > 0) {
102: int pos = name.lastIndexOf(".");
103: if (pos > 0) {
104: String ext = name.substring(pos + 1);
105: if (ext != null && ext.trim().length() > 0) {
106: String mime = propExt
107: .getProperty(ext.toLowerCase());
108: return mime;
109: }
110: }
111: }
112: return null;
113: }
114:
115: }
|