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