useragentconsolidator.java

### File Source: useragentconsolidator.java

         1  package net.sourceforge.pebble.logging;
         2
         3
         4  /**
         5   * Consolidate UserAgent String to a short name. This explicitly checks for a
         6   * few known snippets from the UserAgent that a browser reports. There's a
         7   * number of them missing - these are some of the most common as well as some
         8   * ancient ones.
         9   *
        10   * Extracted from {@link net.sourceforge.pebble.web.action.ViewUserAgentsAction}
        11   */
             /*
        P/P   *  Method: void net.sourceforge.pebble.logging.UserAgentConsolidator()
              */
        12  public class UserAgentConsolidator {
        13
        14    /**
        15     * UserAgent identifications that are known. Be careful when you add more: they are
        16     * supposed to be checked for matches in the order they appear here. So place more
        17     * specific names (e.g. containing a version number) at the beginning, more generic
        18     * ones at the end.
        19     */
               /*
        P/P     *  Method: net.sourceforge.pebble.logging.UserAgentConsolidator__static_init
                *
                *  Postconditions:
                *    KNOWN_AGENTS == &new String[](UserAgentConsolidator__static_init#1)
                *    new String[](UserAgentConsolidator__static_init#1) num objects == 1
                *    KNOWN_AGENTS.length == 18
                *    KNOWN_AGENTS[...] in Addr_Set{&"MSIE 5.0",&"MSIE 6.0",&"MSIE 7.0",&"MSIE 8.0",&"MSIE 9.0",&"Firefox.1.",&"Firefox.2.",&"Firefox.3.0",&"Firefox.3.5",&"Firefox.3.6",&"Safari",&"Opera",&"Chrome",&"Bloglines",&"Googlebot",&"Feedfetcher-Google",&"Yahoo! Slurp",&"Bing"}
                */
        20    private static final String[] KNOWN_AGENTS = new String[] { "MSIE 5.0",
        21        "MSIE 6.0", "MSIE 7.0", "MSIE 8.0", "MSIE 9.0", "Firefox/1.",
        22        "Firefox/2.", "Firefox/3.0", "Firefox/3.5", "Firefox/3.6", "Safari",
        23        "Opera", "Chrome", "Bloglines", "Googlebot", "Feedfetcher-Google",
        24        "Yahoo! Slurp", "Bing"};  // Are all of these real? Should we remove old ones?
        25
        26    /**
        27     * Consolidate given user agent to a short name if the agent is recognized,
        28     * UserAgentConsolidator.OTHER otherwise.
        29     *
        30     * @param userAgent
        31     *          Name that the useragent identifies as
        32     * @return short name or UserAgentConsolidator.OTHER
        33     */
        34    public static String consolidate(String userAgent) {
                 /*
        P/P       *  Method: String consolidate(String)
                  *
                  *  Preconditions:
                  *    (soft) init'ed(KNOWN_AGENTS[...])
                  *    (soft) userAgent != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    java.lang.String:contains(...)@37: {0}, {1}
                  */
        35      String consolidatedUserAgent = "Other"; // not localized...
        36      for(String knownAgent : KNOWN_AGENTS) {
        37        if (userAgent.contains(knownAgent)) {
        38          consolidatedUserAgent = knownAgent;
        39          break;
        40        }
        41      }
        42      return consolidatedUserAgent;
        43    }
        44
        45  }

SofCheck Inspector Build Version : 2.22510

useragentconsolidator.java | 2010-Jun-25 19:40:32
useragentconsolidator.class | 2010-Jul-19 20:23:38