//# 0 errors, 28 messages
//#
package net.sourceforge.pebble.logging;
    //#useragentconsolidator.java:1:1: class: net.sourceforge.pebble.logging.UserAgentConsolidator


/**
 * Consolidate UserAgent String to a short name. This explicitly checks for a
 * few known snippets from the UserAgent that a browser reports. There's a
 * number of them missing - these are some of the most common as well as some
 * ancient ones.
 * 
 * Extracted from {@link net.sourceforge.pebble.web.action.ViewUserAgentsAction}
 */
public class UserAgentConsolidator {
    //#useragentconsolidator.java:12: method: void net.sourceforge.pebble.logging.UserAgentConsolidator.net.sourceforge.pebble.logging.UserAgentConsolidator()
    //#useragentconsolidator.java:12: end of method: void net.sourceforge.pebble.logging.UserAgentConsolidator.net.sourceforge.pebble.logging.UserAgentConsolidator()

  /**
   * UserAgent identifications that are known. Be careful when you add more: they are 
   * supposed to be checked for matches in the order they appear here. So place more 
   * specific names (e.g. containing a version number) at the beginning, more generic 
   * ones at the end. 
   */
  private static final String[] KNOWN_AGENTS = new String[] { "MSIE 5.0",
    //#useragentconsolidator.java:20: method: net.sourceforge.pebble.logging.UserAgentConsolidator.net.sourceforge.pebble.logging.UserAgentConsolidator__static_init
    //#output(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): KNOWN_AGENTS
    //#output(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): __Descendant_Table[net/sourceforge/pebble/logging/UserAgentConsolidator]
    //#output(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): new String[](UserAgentConsolidator__static_init#1) num objects
    //#output(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): KNOWN_AGENTS.length
    //#output(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): KNOWN_AGENTS[0..17]
    //#new obj(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): new String[](UserAgentConsolidator__static_init#1)
    //#post(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): KNOWN_AGENTS == &new String[](UserAgentConsolidator__static_init#1)
    //#post(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): __Descendant_Table[net/sourceforge/pebble/logging/UserAgentConsolidator] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): new String[](UserAgentConsolidator__static_init#1) num objects == 1
    //#post(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): KNOWN_AGENTS.length == 18
    //#post(net.sourceforge.pebble.logging.UserAgentConsolidator__static_init): KNOWN_AGENTS[0..17] 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"}
      "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"};  // Are all of these real? Should we remove old ones?
    //#useragentconsolidator.java:24: end of method: net.sourceforge.pebble.logging.UserAgentConsolidator.net.sourceforge.pebble.logging.UserAgentConsolidator__static_init

  /**
   * Consolidate given user agent to a short name if the agent is recognized,
   * UserAgentConsolidator.OTHER otherwise.
   * 
   * @param userAgent
   *          Name that the useragent identifies as
   * @return short name or UserAgentConsolidator.OTHER
   */
  public static String consolidate(String userAgent) {
    String consolidatedUserAgent = "Other"; // not localized...
    //#useragentconsolidator.java:35: method: String net.sourceforge.pebble.logging.UserAgentConsolidator.consolidate(String)
    //#input(String consolidate(String)): KNOWN_AGENTS
    //#input(String consolidate(String)): KNOWN_AGENTS.length
    //#input(String consolidate(String)): KNOWN_AGENTS[0..17]
    //#input(String consolidate(String)): userAgent
    //#output(String consolidate(String)): return_value
    //#pre[1] (String consolidate(String)): (soft) init'ed(KNOWN_AGENTS[0..17])
    //#pre[2] (String consolidate(String)): (soft) userAgent != null
    //#post(String consolidate(String)): init'ed(return_value)
    //#test_vector(String consolidate(String)): java.lang.String:contains(...)@37: {0}, {1}
    for(String knownAgent : KNOWN_AGENTS) {
      if (userAgent.contains(knownAgent)) {
        consolidatedUserAgent = knownAgent;
        break;
      } 
    }
    return consolidatedUserAgent;
    //#useragentconsolidator.java:42: end of method: String net.sourceforge.pebble.logging.UserAgentConsolidator.consolidate(String)
  }

}
    //#useragentconsolidator.java:: end of class: net.sourceforge.pebble.logging.UserAgentConsolidator
