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