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