KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > vui > BrowserSupport


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

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 JavaDoc;
12 import java.util.*;
13
14
15 /**
16  * Utility class for handling multiple types of voice browsers
17  *
18  * @author Jeff Haynie (jhaynie@vocalocity.net)
19  * @version $Revision: 1.3 $
20  */

21 public final class BrowserSupport {
22     //~ Static fields/initializers /////////////////////////////////////////////
23

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 JavaDoc 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 JavaDoc ex) {
41             ex.printStackTrace();
42         } finally {
43             if (in != null) {
44                 try {
45                     in.close();
46                 } catch (Exception JavaDoc ignore) {
47                 }
48
49                 in = null;
50             }
51         }
52     }
53
54     //~ Methods ////////////////////////////////////////////////////////////////
55

56     /**
57      * for a given user agent, return a vxml browser template directory
58      */

59     public static String JavaDoc getBrowserTemplateDirectory(String JavaDoc userAgent) {
60         String JavaDoc tempDir = (String JavaDoc) 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     /**
73      * for a given user agent, return a near match if any
74      */

75     public static String JavaDoc getTemplateDirectoryFromMatch(String JavaDoc 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; // default
84

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     /**
97      * add a browser match pattern to use when trying to match a user-agent if the exact user agent string is specified.
98      * The uaString can contain a wildcard, such as <tt>VocalOS/*</tt> which will match up to <tt>VocalOS/</tt>.
99      */

100     public static void addBrowserMatch(String JavaDoc uaString, String JavaDoc 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; // * - all/default
109
}
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     /**
121      * load browser settings from properties file
122      */

123     public static void loadBrowserInfo(Properties p) {
124         properties = p;
125
126         String JavaDoc browsers = p.getProperty("browsers");
127
128         if (browsers != null) {
129             StringTokenizer tok = new StringTokenizer(browsers, ",");
130
131             while (tok.hasMoreTokens()) {
132                 String JavaDoc token = tok.nextToken();
133                 String JavaDoc ua = p.getProperty(token + ".useragent");
134
135                 if (ua != null) {
136                     String JavaDoc 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     //~ Inner Classes //////////////////////////////////////////////////////////
155

156     static final class Match {
157         String JavaDoc match;
158         String JavaDoc templateDirectory;
159         String JavaDoc userAgent;
160     }
161 }
162
Popular Tags