001 package org.apache.myfaces.tobago.context; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import java.io.Serializable; 021 import java.util.ArrayList; 022 import java.util.Collections; 023 import java.util.List; 024 import java.util.Locale; 025 026 public class UserAgent implements Serializable { 027 028 private static final long serialVersionUID = -3138810465122379395L; 029 030 public static final String DEFAULT_NAME = "standard"; 031 032 public static final UserAgent DEFAULT = new UserAgent(null, null); 033 034 035 public static final UserAgent MSIE = new UserAgent("msie", null); 036 037 public static final UserAgent MSIE_5_0 = new UserAgent("msie", "5_0"); 038 039 public static final UserAgent MSIE_5_5 = new UserAgent("msie", "5_5"); 040 041 public static final UserAgent MSIE_6_0 = new UserAgent("msie", "6_0"); 042 043 public static final UserAgent MSIE_5_0_MAC = new UserAgent("msie", "5_0_mac"); 044 045 public static final UserAgent MSIE_6_0_MAC = new UserAgent("msie", "6_0_mac"); 046 047 048 public static final UserAgent OPERA = new UserAgent("opera", null); 049 050 public static final UserAgent OPERA_5_0 = new UserAgent("opera", "5_0"); 051 052 public static final UserAgent OPERA_6_0 = new UserAgent("opera", "6_0"); 053 054 public static final UserAgent OPERA_7_11 = new UserAgent("opera", "7_11"); 055 056 057 public static final UserAgent MOZILLA = new UserAgent("mozilla", null); 058 059 public static final UserAgent MOZILLA_4_7 = new UserAgent("mozilla", "4_7"); 060 061 public static final UserAgent MOZILLA_5_0 = new UserAgent("mozilla", "5_0"); 062 063 public static final UserAgent MOZILLA_5_0_R1_6 = new UserAgent("mozilla", "5_0_r1_6"); 064 065 066 private String name; 067 068 private String version; 069 070 071 private UserAgent(String name, String version) { 072 this.name = name; 073 this.version = version; 074 } 075 076 077 public boolean isMsie() { 078 return MSIE.name.equals(name); 079 } 080 081 public boolean isMozilla() { 082 return MOZILLA.name.equals(name); 083 } 084 085 public List<String> getFallbackList() { 086 return getFallbackList(false); 087 } 088 089 private List<String> getFallbackList(boolean reverseOrder) { 090 List<String> list = new ArrayList<String>(3); 091 if (version != null) { 092 list.add(name + '_' + version); 093 } 094 if (name != null) { 095 list.add(name); 096 } 097 list.add(DEFAULT_NAME); 098 if (reverseOrder) { 099 Collections.reverse(list); 100 } 101 return list; 102 } 103 104 public static UserAgent getInstance(String header) { 105 if (header == null) { 106 return DEFAULT; 107 } 108 109 header = header.toLowerCase(Locale.ENGLISH).replace('/', ' '); 110 if (header.indexOf("opera") > -1) { 111 if (header.indexOf("opera 5.0") > -1) { 112 return OPERA_5_0; 113 } else if (header.indexOf("opera 6.0") > -1) { 114 return OPERA_6_0; 115 } else if (header.indexOf("opera 7.11") > -1) { 116 return OPERA_7_11; 117 } else { 118 return OPERA; 119 } 120 } else if (header.indexOf("msie") > -1) { 121 if (header.indexOf("msie 5.0") > -1) { 122 if (header.indexOf("mac") > -1) { 123 return MSIE_5_0_MAC; 124 } else { 125 return MSIE_5_0; 126 } 127 } else if (header.indexOf("msie 5.5") > -1) { 128 return MSIE_5_5; 129 } else if (header.indexOf("msie 6.0") > -1) { 130 if (header.indexOf("mac") > -1) { 131 return MSIE_6_0_MAC; 132 } else { 133 return MSIE_6_0; 134 } 135 } else { 136 return MSIE; 137 } 138 } else if (header.indexOf("mozilla") > -1) { 139 if (header.indexOf("mozilla 4.7") > -1) { 140 return MOZILLA_4_7; 141 } else if (header.indexOf("mozilla 5.0") > -1) { 142 if (header.indexOf("rv:1.6") > -1) { 143 return MOZILLA_5_0_R1_6; 144 } else { 145 return MOZILLA_5_0; 146 } 147 } else { 148 return MOZILLA; 149 } 150 } 151 152 return DEFAULT; 153 } 154 155 public static UserAgent getInstanceForId(String id) { 156 if (id == null) { 157 return DEFAULT; 158 } 159 160 if (id.indexOf("opera") == 0) { 161 if (id.equals("opera_5_0")) { 162 return OPERA_5_0; 163 } else if (id.equals("opera_6_0")) { 164 return OPERA_6_0; 165 } else if (id.equals("opera_7_11")) { 166 return OPERA_7_11; 167 } else { 168 return OPERA; 169 } 170 } else if (id.indexOf("msie") == 0) { 171 if (id.equals("msie_5_0")) { 172 return MSIE_5_0; 173 } else if (id.equals("msie_5_0_mac")) { 174 return MSIE_5_0_MAC; 175 } else if (id.equals("msie_5_5")) { 176 return MSIE_5_5; 177 } else if (id.equals("msie_6_0")) { 178 return MSIE_6_0; 179 } else if (id.equals("msie_6_0_mac")) { 180 return MSIE_6_0_MAC; 181 } else { 182 return MSIE; 183 } 184 } else if (id.indexOf("mozilla") == 0) { 185 if (id.equals("mozilla_4_7")) { 186 return MOZILLA_4_7; 187 } else if (id.equals("mozilla_5_0")) { 188 return MOZILLA_5_0; 189 } else if (id.equals("mozilla_5_0_r1_6")) { 190 return MOZILLA_5_0_R1_6; 191 } else { 192 return MOZILLA; 193 } 194 } 195 196 return DEFAULT; 197 } 198 199 200 /** 201 * @deprecated don't use toString() functionallity! 202 */ 203 public String toString() { 204 return version != null 205 ? name + '_' + version 206 : name; 207 } 208 } 209