You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2016/01/23 18:59:50 UTC

svn commit: r1726426 - in /turbine/core/trunk/src: java/org/apache/turbine/util/BrowserDetector.java test/org/apache/turbine/util/BrowserDetectorTest.java

Author: tv
Date: Sat Jan 23 17:59:50 2016
New Revision: 1726426

URL: http://svn.apache.org/viewvc?rev=1726426&view=rev
Log:
Update BrowserDetector to support contemporary browsers

Modified:
    turbine/core/trunk/src/java/org/apache/turbine/util/BrowserDetector.java
    turbine/core/trunk/src/test/org/apache/turbine/util/BrowserDetectorTest.java

Modified: turbine/core/trunk/src/java/org/apache/turbine/util/BrowserDetector.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/util/BrowserDetector.java?rev=1726426&r1=1726425&r2=1726426&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/util/BrowserDetector.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/util/BrowserDetector.java Sat Jan 23 17:59:50 2016
@@ -1,5 +1,14 @@
 package org.apache.turbine.util;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import net.sf.uadetector.OperatingSystem;
+import net.sf.uadetector.ReadableUserAgent;
+import net.sf.uadetector.UserAgentStringParser;
+import net.sf.uadetector.VersionNumber;
+import net.sf.uadetector.service.UADetectorServiceFactory;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -20,50 +29,28 @@ package org.apache.turbine.util;
  */
 
 /**
- * This class parses the user agent string and sets javasciptOK and
- * cssOK following the rules described below.  If you want to check
- * for specific browsers/versions then use this class to parse the
- * user agent string and use the accessor methods in this class.
- *
- * JavaScriptOK means that the browser understands JavaScript on the
- * same level the Navigator 3 does.  Specifically, it can use named
- * images.  This allows easier rollovers.  If a browser doesn't do
- * this (Nav 2 or MSIE 3), then we just assume it can't do any
- * JavaScript.  Referencing images by load order is too hard to
- * maintain.
- *
- * CSSOK is kind of sketchy in that Nav 4 and MSIE work differently,
- * but they do seem to have most of the functionality.  MSIE 4 for the
- * Mac has buggy CSS support, so we let it do JavaScript, but no CSS.
- *
- * Ported from Leon's PHP code at
- * http://www.working-dogs.com/freetrade by Frank.
+ * This class parses the user agent string and provides getters for
+ * its parts. It uses UADetector (http://uadetector.sourceforge.net/)
  *
  * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
  * @author <a href="mailto:leon@clearink.com">Leon Atkisnon</a>
  * @author <a href="mailto:mospaw@polk-county.com">Chris Mospaw</a>
  * @author <a href="mailto:bgriffin@cddb.com">Benjamin Elijah Griffin</a>
- * @version $Id$
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
  */
 public class BrowserDetector
 {
-    /** Internet Explorer */
-    public static final String MSIE = "MSIE";
-    /** Opera */
-    public static final String OPERA = "Opera";
-    /** Mozilla, Firefox and friends */
-    public static final String MOZILLA = "Mozilla";
-
-    /** Running on Windows */
-    public static final String WINDOWS = "Windows";
-    /** Running on Unix */
-    public static final String UNIX = "Unix";
-    /** Running on Mac OS X */
-    public static final String MACINTOSH = "Macintosh";
-
     /** The user agent string. */
     private String userAgentString = "";
 
+    /** The user agent cache. */
+    private static volatile Map<String, ReadableUserAgent> userAgentCache =
+            new HashMap<String, ReadableUserAgent>();
+
+    /** The user agent parser */
+    private static UserAgentStringParser parser =
+            UADetectorServiceFactory.getCachingAndUpdatingParser();
+
     /** The browser name specified in the user agent string. */
     private String browserName = "";
 
@@ -78,15 +65,6 @@ public class BrowserDetector
      */
     private String browserPlatform = "unknown";
 
-    /** Whether or not javascript works in this browser. */
-    private boolean javascriptOK = false;
-
-    /** Whether or not CSS works in this browser. */
-    private boolean cssOK = false;
-
-    /** Whether or not file upload works in this browser. */
-    private boolean fileUploadOK = false;
-
     /**
      * Constructor used to initialize this class.
      *
@@ -105,38 +83,7 @@ public class BrowserDetector
      */
     public BrowserDetector(RunData data)
     {
-        this.userAgentString = data.getUserAgent();
-        parse();
-    }
-
-    /**
-     * Whether or not CSS works in this browser.
-     *
-     * @return True if CSS works in this browser.
-     */
-    public boolean isCssOK()
-    {
-        return cssOK;
-    }
-
-    /**
-     * Whether or not file upload works in this browser.
-     *
-     * @return True if file upload works in this browser.
-     */
-    public boolean isFileUploadOK()
-    {
-        return fileUploadOK;
-    }
-
-    /**
-     * Whether or not Javascript works in this browser.
-     *
-     * @return True if Javascript works in this browser.
-     */
-    public boolean isJavascriptOK()
-    {
-        return javascriptOK;
+        this(data.getUserAgent());
     }
 
     /**
@@ -180,190 +127,36 @@ public class BrowserDetector
     }
 
     /**
+     * The user agent for this class.
+     *
+     * @return A user agent.
+     */
+    public ReadableUserAgent getUserAgent()
+    {
+        return userAgentCache.get(userAgentString);
+    }
+
+    /**
      * Helper method to initialize this class.
      */
     private void parse()
     {
-        int versionStartIndex = userAgentString.indexOf("/");
-        int versionEndIndex = userAgentString.indexOf(" ");
+        ReadableUserAgent userAgent = userAgentCache.get(userAgentString);
 
-        // Get the browser name and version.
-        browserName = userAgentString.substring(0, versionStartIndex);
-        try
-        {
-            // Not all user agents will have a space in the reported
-            // string.
-            String agentSubstring = null;
-            if (versionEndIndex < 0)
-            {
-                agentSubstring
-                        = userAgentString.substring(versionStartIndex + 1);
-            }
-            else
-            {
-                agentSubstring = userAgentString
-                        .substring(versionStartIndex + 1, versionEndIndex);
-            }
-            browserVersion = toFloat(agentSubstring);
-        }
-        catch (NumberFormatException e)
+        if (userAgent == null)
         {
-            // Just use the default value.
-        }
-
-        // MSIE lies about its name.  Of course...
-        if (userAgentString.indexOf(MSIE) != -1)
-        {
-            // Ex: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
-            versionStartIndex = (userAgentString.indexOf(MSIE)
-                    + MSIE.length() + 1);
-            versionEndIndex = userAgentString.indexOf(";", versionStartIndex);
-
-            browserName = MSIE;
-            try
-            {
-                browserVersion = toFloat(userAgentString
-                        .substring(versionStartIndex, versionEndIndex));
-            }
-            catch (NumberFormatException e)
-            {
-                // Just use the default value.
-            }
-
-            // PHP code
-            // $Browser_Name = "MSIE";
-            // $Browser_Version = strtok("MSIE");
-            // $Browser_Version = strtok(" ");
-            // $Browser_Version = strtok(";");
-        }
-
-        // Opera isn't completely honest, either...
-        // Modificaton by Chris Mospaw <mo...@polk-county.com>
-        if (userAgentString.indexOf(OPERA) != -1)
-        {
-            // Ex: Mozilla/4.0 (Windows NT 4.0;US) Opera 3.61  [en]
-            // Ex: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.02
-            versionStartIndex = (userAgentString.indexOf(OPERA)
-                    + OPERA.length() + 1);
-            versionEndIndex = userAgentString.indexOf(" ", versionStartIndex);
-            if (versionEndIndex == -1)
-            {
-                versionEndIndex = userAgentString.length();
-            }
-
-            browserName = OPERA;
-            try
-            {
-                browserVersion = toFloat(userAgentString
-                        .substring(versionStartIndex, versionEndIndex));
-            }
-            catch (NumberFormatException e)
-            {
-                // Just use the default value.
-            }
-
-            // PHP code
-            // $Browser_Name = "Opera";
-            // $Browser_Version = strtok("Opera");
-            // $Browser_Version = strtok("/");
-            // $Browser_Version = strtok(";");
+            userAgent = parser.parse(userAgentString);
+            userAgentCache.put(userAgentString, userAgent);
         }
 
+        // Get the browser name and version.
+        browserName = userAgent.getName();
+        VersionNumber version = userAgent.getVersionNumber();
+        browserVersion = toFloat(version.toVersionString());
 
         // Try to figure out what platform.
-        if ((userAgentString.indexOf("Windows") != -1)
-                || (userAgentString.indexOf("WinNT") != -1)
-                || (userAgentString.indexOf("Win98") != -1)
-                || (userAgentString.indexOf("Win95") != -1))
-        {
-            browserPlatform = WINDOWS;
-        }
-
-        if (userAgentString.indexOf("Mac") != -1)
-        {
-            browserPlatform = MACINTOSH;
-        }
-
-        if (userAgentString.indexOf("X11") != -1)
-        {
-            browserPlatform = UNIX;
-        }
-
-        if (browserPlatform == WINDOWS)
-        {
-            if (browserName.equals(MOZILLA))
-            {
-                if (browserVersion >= 3.0)
-                {
-                    javascriptOK = true;
-                    fileUploadOK = true;
-                }
-                if (browserVersion >= 4.0)
-                {
-                    cssOK = true;
-                }
-            }
-            else if (browserName == MSIE)
-            {
-                if (browserVersion >= 4.0)
-                {
-                    javascriptOK = true;
-                    fileUploadOK = true;
-                    cssOK = true;
-                }
-            }
-            else if (browserName == OPERA)
-            {
-                if (browserVersion >= 3.0)
-                {
-                    javascriptOK = true;
-                    fileUploadOK = true;
-                    cssOK = true;
-                }
-            }
-        }
-        else if (browserPlatform == MACINTOSH)
-        {
-            if (browserName.equals(MOZILLA))
-            {
-                if (browserVersion >= 3.0)
-                {
-                    javascriptOK = true;
-                    fileUploadOK = true;
-                }
-                if (browserVersion >= 4.0)
-                {
-                    cssOK = true;
-                }
-            }
-            else if (browserName == MSIE)
-            {
-                if (browserVersion >= 4.0)
-                {
-                    javascriptOK = true;
-                    fileUploadOK = true;
-                }
-                if (browserVersion > 4.0)
-                {
-                    cssOK = true;
-                }
-            }
-        }
-        else if (browserPlatform == UNIX)
-        {
-            if (browserName.equals(MOZILLA))
-            {
-                if (browserVersion >= 3.0)
-                {
-                    javascriptOK = true;
-                    fileUploadOK = true;
-                }
-                if (browserVersion >= 4.0)
-                {
-                    cssOK = true;
-                }
-            }
-        }
+        OperatingSystem os = userAgent.getOperatingSystem();
+        browserPlatform = os.getFamilyName();
     }
 
     /**
@@ -374,7 +167,7 @@ public class BrowserDetector
      */
     private static final float toFloat(String s)
     {
-        return Float.valueOf(s).floatValue();
+        return Float.parseFloat(s);
     }
 
 }

Modified: turbine/core/trunk/src/test/org/apache/turbine/util/BrowserDetectorTest.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/test/org/apache/turbine/util/BrowserDetectorTest.java?rev=1726426&r1=1726425&r2=1726426&view=diff
==============================================================================
--- turbine/core/trunk/src/test/org/apache/turbine/util/BrowserDetectorTest.java (original)
+++ turbine/core/trunk/src/test/org/apache/turbine/util/BrowserDetectorTest.java Sat Jan 23 17:59:50 2016
@@ -38,39 +38,38 @@ public class BrowserDetectorTest extends
     {
         String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5";
         BrowserDetector bd = new BrowserDetector(userAgent);
-        assertEquals(BrowserDetector.MOZILLA, bd.getBrowserName());
-        // Should this really be 5?
-        assertEquals(5f, bd.getBrowserVersion(), 0.0f);
-        assertEquals(BrowserDetector.WINDOWS, bd.getBrowserPlatform());
+        assertEquals("Firefox", bd.getBrowserName());
+        assertEquals(1.5f, bd.getBrowserVersion(), 0.0f);
+        assertEquals("Windows", bd.getBrowserPlatform());
     }
 
     @Test public void testOpera()
     {
         String userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.02";
         BrowserDetector bd = new BrowserDetector(userAgent);
-        assertEquals(BrowserDetector.OPERA, bd.getBrowserName());
+        assertEquals("Opera", bd.getBrowserName());
         assertEquals(8.02f, bd.getBrowserVersion(), 0.0f);
-        assertEquals(BrowserDetector.WINDOWS, bd.getBrowserPlatform());
+        assertEquals("Windows", bd.getBrowserPlatform());
 
         userAgent = "Opera/7.51 (Windows NT 5.1; U) [en]";
         bd = new BrowserDetector(userAgent);
-        assertEquals(BrowserDetector.OPERA, bd.getBrowserName());
+        assertEquals("Opera", bd.getBrowserName());
         assertEquals(7.51f, bd.getBrowserVersion(), 0.0f);
-        assertEquals(BrowserDetector.WINDOWS, bd.getBrowserPlatform());
+        assertEquals("Windows", bd.getBrowserPlatform());
     }
 
     @Test public void testIE()
     {
         String userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
         BrowserDetector bd = new BrowserDetector(userAgent);
-        assertEquals(BrowserDetector.MSIE, bd.getBrowserName());
+        assertEquals("IE", bd.getBrowserName());
         assertEquals(6.0f, bd.getBrowserVersion(), 0.0f);
-        assertEquals(BrowserDetector.WINDOWS, bd.getBrowserPlatform());
+        assertEquals("Windows", bd.getBrowserPlatform());
 
         userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
         bd = new BrowserDetector(userAgent);
-        assertEquals(BrowserDetector.MSIE, bd.getBrowserName());
+        assertEquals("IE", bd.getBrowserName());
         assertEquals(6.0f, bd.getBrowserVersion(), 0.0f);
-        assertEquals(BrowserDetector.WINDOWS, bd.getBrowserPlatform());
+        assertEquals("Windows", bd.getBrowserPlatform());
     }
 }