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_7_O = new UserAgent("msie", "7_0"); 044 045 public static final UserAgent MSIE_5_0_MAC = new UserAgent("msie", "5_0_mac"); 046 047 public static final UserAgent MSIE_6_0_MAC = new UserAgent("msie", "6_0_mac"); 048 049 050 public static final UserAgent OPERA = new UserAgent("opera", null); 051 052 public static final UserAgent OPERA_5_0 = new UserAgent("opera", "5_0"); 053 054 public static final UserAgent OPERA_6_0 = new UserAgent("opera", "6_0"); 055 056 public static final UserAgent OPERA_7_11 = new UserAgent("opera", "7_11"); 057 058 059 public static final UserAgent MOZILLA = new UserAgent("mozilla", null); 060 061 public static final UserAgent MOZILLA_4_7 = new UserAgent("mozilla", "4_7"); 062 063 public static final UserAgent MOZILLA_5_0 = new UserAgent("mozilla", "5_0"); 064 065 public static final UserAgent MOZILLA_5_0_R1_6 = new UserAgent("mozilla", "5_0_r1_6"); 066 067 068 private String name; 069 070 private String version; 071 072 073 private UserAgent(String name, String version) { 074 this.name = name; 075 this.version = version; 076 } 077 078 079 public boolean isMsie() { 080 return MSIE.name.equals(name); 081 } 082 083 public boolean isMozilla() { 084 return MOZILLA.name.equals(name); 085 } 086 087 public List<String> getFallbackList() { 088 return getFallbackList(false); 089 } 090 091 private List<String> getFallbackList(boolean reverseOrder) { 092 List<String> list = new ArrayList<String>(3); 093 if (version != null) { 094 list.add(name + '_' + version); 095 } 096 if (name != null) { 097 list.add(name); 098 } 099 list.add(DEFAULT_NAME); 100 if (reverseOrder) { 101 Collections.reverse(list); 102 } 103 return list; 104 } 105 106 public static UserAgent getInstance(String header) { 107 if (header == null) { 108 return DEFAULT; 109 } 110 111 header = header.toLowerCase(Locale.ENGLISH).replace('/', ' '); 112 if (header.indexOf("opera") > -1) { 113 if (header.indexOf("opera 5.0") > -1) { 114 return OPERA_5_0; 115 } else if (header.indexOf("opera 6.0") > -1) { 116 return OPERA_6_0; 117 } else if (header.indexOf("opera 7.11") > -1) { 118 return OPERA_7_11; 119 } else { 120 return OPERA; 121 } 122 } else if (header.indexOf("msie") > -1) { 123 if (header.indexOf("msie 5.0") > -1) { 124 if (header.indexOf("mac") > -1) { 125 return MSIE_5_0_MAC; 126 } else { 127 return MSIE_5_0; 128 } 129 } else if (header.indexOf("msie 5.5") > -1) { 130 return MSIE_5_5; 131 } else if (header.indexOf("msie 6.0") > -1) { 132 if (header.indexOf("mac") > -1) { 133 return MSIE_6_0_MAC; 134 } else { 135 return MSIE_6_0; 136 } 137 } else if (header.indexOf("msie 7.0") > -1) { 138 return MSIE_7_O; 139 } else { 140 return MSIE; 141 } 142 } else if (header.indexOf("mozilla") > -1) { 143 if (header.indexOf("mozilla 4.7") > -1) { 144 return MOZILLA_4_7; 145 } else if (header.indexOf("mozilla 5.0") > -1) { 146 if (header.indexOf("rv:1.6") > -1) { 147 return MOZILLA_5_0_R1_6; 148 } else { 149 return MOZILLA_5_0; 150 } 151 } else { 152 return MOZILLA; 153 } 154 } 155 156 return DEFAULT; 157 } 158 159 public static UserAgent getInstanceForId(String id) { 160 if (id == null) { 161 return DEFAULT; 162 } 163 164 if (id.indexOf("opera") == 0) { 165 if (id.equals("opera_5_0")) { 166 return OPERA_5_0; 167 } else if (id.equals("opera_6_0")) { 168 return OPERA_6_0; 169 } else if (id.equals("opera_7_11")) { 170 return OPERA_7_11; 171 } else { 172 return OPERA; 173 } 174 } else if (id.indexOf("msie") == 0) { 175 if (id.equals("msie_5_0")) { 176 return MSIE_5_0; 177 } else if (id.equals("msie_5_0_mac")) { 178 return MSIE_5_0_MAC; 179 } else if (id.equals("msie_5_5")) { 180 return MSIE_5_5; 181 } else if (id.equals("msie_6_0")) { 182 return MSIE_6_0; 183 } else if (id.equals("msie_6_0_mac")) { 184 return MSIE_6_0_MAC; 185 } else if (id.equals("msie_7_0")) { 186 return MSIE_7_O; 187 } else { 188 return MSIE; 189 } 190 } else if (id.indexOf("mozilla") == 0) { 191 if (id.equals("mozilla_4_7")) { 192 return MOZILLA_4_7; 193 } else if (id.equals("mozilla_5_0")) { 194 return MOZILLA_5_0; 195 } else if (id.equals("mozilla_5_0_r1_6")) { 196 return MOZILLA_5_0_R1_6; 197 } else { 198 return MOZILLA; 199 } 200 } 201 202 return DEFAULT; 203 } 204 205 206 /** 207 * @deprecated don't use toString() functionallity! 208 */ 209 public String toString() { 210 return version != null 211 ? name + '_' + version 212 : name; 213 } 214 } 215