1 5 package com.opensymphony.webwork.views.jsp.vui; 6 7 import com.opensymphony.webwork.util.ClassLoaderUtils; 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.io.InputStream ; 12 import java.util.*; 13 14 15 21 public final class BrowserSupport { 22 24 static Log log = LogFactory.getLog(BrowserSupport.class); 25 static Properties properties; 26 static Map browserMap = Collections.synchronizedMap(new HashMap(2)); 27 static List browserMatch = Collections.synchronizedList(new ArrayList(2)); 28 29 static { 30 InputStream in = null; 31 Properties p = new Properties(); 32 33 try { 34 in = ClassLoaderUtils.getResourceAsStream("/vxml.properties", BrowserSupport.class); 35 36 if (in != null) { 37 p.load(in); 38 loadBrowserInfo(p); 39 } 40 } catch (Exception ex) { 41 ex.printStackTrace(); 42 } finally { 43 if (in != null) { 44 try { 45 in.close(); 46 } catch (Exception ignore) { 47 } 48 49 in = null; 50 } 51 } 52 } 53 54 56 59 public static String getBrowserTemplateDirectory(String userAgent) { 60 String tempDir = (String ) browserMap.get(userAgent); 61 62 if (tempDir == null) { 63 tempDir = getTemplateDirectoryFromMatch(userAgent); 64 65 if (tempDir == null) { 66 } 67 } 68 69 return ((tempDir == null) ? "/template/vxml/" : tempDir); 70 } 71 72 75 public static String getTemplateDirectoryFromMatch(String userAgent) { 76 Iterator i = browserMatch.iterator(); 77 Match dm = null; 78 79 while (i.hasNext()) { 80 Match m = (Match) i.next(); 81 82 if (m.match == null) { 83 dm = m; 85 continue; 86 } 87 88 if (userAgent.startsWith(m.match)) { 89 return m.templateDirectory; 90 } 91 } 92 93 return ((dm == null) ? null : dm.templateDirectory); 94 } 95 96 100 public static void addBrowserMatch(String uaString, String templateDir) { 101 int w = uaString.indexOf("*"); 102 Match m = new Match(); 103 m.userAgent = uaString; 104 105 if (w > 0) { 106 m.match = uaString.substring(0, w); 107 } else { 108 m.match = null; } 110 111 m.templateDirectory = templateDir; 112 113 if (log.isDebugEnabled()) { 114 log.debug("Loading useragent match: " + m.match + " [" + uaString + "] directory: " + templateDir); 115 } 116 117 browserMatch.add(m); 118 } 119 120 123 public static void loadBrowserInfo(Properties p) { 124 properties = p; 125 126 String browsers = p.getProperty("browsers"); 127 128 if (browsers != null) { 129 StringTokenizer tok = new StringTokenizer(browsers, ","); 130 131 while (tok.hasMoreTokens()) { 132 String token = tok.nextToken(); 133 String ua = p.getProperty(token + ".useragent"); 134 135 if (ua != null) { 136 String template = p.getProperty(token + ".templatedirectory"); 137 138 if (template != null) { 139 if (log.isDebugEnabled()) { 140 log.debug("Loading useragent: " + token + " [" + ua + "] directory: " + template); 141 } 142 143 browserMap.put(ua, template); 144 145 if (ua.indexOf("*") > -1) { 146 addBrowserMatch(ua, template); 147 } 148 } 149 } 150 } 151 } 152 } 153 154 156 static final class Match { 157 String match; 158 String templateDirectory; 159 String userAgent; 160 } 161 } 162 | Popular Tags |