Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UserAgentQuerier |
|
| 3.9166666666666665;3.917 |
1 | /* Copyright © 2013 Matthew Champion | |
2 | All rights reserved. | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without | |
5 | modification, are permitted provided that the following conditions are met: | |
6 | * Redistributions of source code must retain the above copyright | |
7 | notice, this list of conditions and the following disclaimer. | |
8 | * Redistributions in binary form must reproduce the above copyright | |
9 | notice, this list of conditions and the following disclaimer in the | |
10 | documentation and/or other materials provided with the distribution. | |
11 | * Neither the name of mattunderscore.com nor the | |
12 | names of its contributors may be used to endorse or promote products | |
13 | derived from this software without specific prior written permission. | |
14 | ||
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
18 | DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY | |
19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | |
25 | ||
26 | package com.mattunderscore.http.headers.useragent; | |
27 | ||
28 | import com.mattunderscore.http.headers.useragent.details.UserAgentDetail; | |
29 | import com.mattunderscore.http.headers.useragent.details.application.Bot; | |
30 | import com.mattunderscore.http.headers.useragent.details.application.Browser; | |
31 | import com.mattunderscore.http.headers.useragent.details.application.Compatible; | |
32 | import com.mattunderscore.http.headers.useragent.details.application.Firefox; | |
33 | import com.mattunderscore.http.headers.useragent.details.platform.Gecko; | |
34 | ||
35 | /** | |
36 | * Utility class to get some information from a user agent details conveniently. | |
37 | * @author Matt Champion | |
38 | * @since 0.2.6 | |
39 | */ | |
40 | public class UserAgentQuerier | |
41 | { | |
42 | private static final String OPERA = "Opera"; | |
43 | private static final String CHROME = "Chrome"; | |
44 | private static final String SAFARI = "Safari"; | |
45 | private static final String INTERNET_EXPLORER = "Internet Explorer"; | |
46 | ||
47 | private final IUserAgent userAgent; | |
48 | ||
49 | public UserAgentQuerier(IUserAgent userAgent) | |
50 | 22 | { |
51 | 22 | this.userAgent = userAgent; |
52 | 22 | } |
53 | ||
54 | public IUserAgent getUserAgent() | |
55 | { | |
56 | 0 | return userAgent; |
57 | } | |
58 | ||
59 | public boolean isBot() | |
60 | { | |
61 | 22 | for (final UserAgentDetail detail : userAgent.details()) |
62 | { | |
63 | 114 | if (detail instanceof Bot) |
64 | { | |
65 | 4 | return true; |
66 | } | |
67 | 110 | } |
68 | 18 | return false; |
69 | } | |
70 | ||
71 | public boolean isBrowser() | |
72 | { | |
73 | 22 | for (final UserAgentDetail detail : userAgent.details()) |
74 | { | |
75 | 78 | if (detail instanceof Browser) |
76 | { | |
77 | 16 | return true; |
78 | } | |
79 | 62 | } |
80 | 6 | return false; |
81 | } | |
82 | ||
83 | public boolean isIE() | |
84 | { | |
85 | 22 | for (final UserAgentDetail detail : userAgent.details()) |
86 | { | |
87 | 78 | if (detail instanceof Browser) |
88 | { | |
89 | 16 | return INTERNET_EXPLORER.equals(detail.name()); |
90 | } | |
91 | 62 | } |
92 | 6 | return false; |
93 | } | |
94 | ||
95 | public boolean isFirefox() | |
96 | { | |
97 | 16 | for (final UserAgentDetail detail : userAgent.details()) |
98 | { | |
99 | 76 | if (detail instanceof Firefox) |
100 | { | |
101 | 2 | return true; |
102 | } | |
103 | 74 | } |
104 | 14 | return false; |
105 | } | |
106 | ||
107 | public boolean isChrome() | |
108 | { | |
109 | 16 | for (final UserAgentDetail detail : userAgent.details()) |
110 | { | |
111 | 72 | if (detail instanceof Browser) |
112 | { | |
113 | 12 | if (!SAFARI.equals(detail.name())) |
114 | { | |
115 | 8 | return CHROME.equals(detail.name()); |
116 | } | |
117 | } | |
118 | 64 | } |
119 | 8 | return false; |
120 | } | |
121 | ||
122 | public boolean isOpera() | |
123 | { | |
124 | 22 | for (final UserAgentDetail detail : userAgent.details()) |
125 | { | |
126 | 78 | if (detail instanceof Browser) |
127 | { | |
128 | 16 | return OPERA.equals(detail.name()); |
129 | } | |
130 | 62 | } |
131 | 6 | return false; |
132 | } | |
133 | ||
134 | public boolean isSafari() | |
135 | { | |
136 | 20 | boolean claimsSafari = false; |
137 | 20 | boolean claimsChrome = false; |
138 | 20 | for (final UserAgentDetail detail : userAgent.details()) |
139 | { | |
140 | 104 | if (detail instanceof Browser) |
141 | { | |
142 | 16 | if (SAFARI.equals(detail.name())) |
143 | { | |
144 | 2 | claimsSafari = true; |
145 | } | |
146 | 14 | else if (CHROME.equals(detail.name())) |
147 | { | |
148 | 0 | claimsChrome = true; |
149 | } | |
150 | } | |
151 | 104 | } |
152 | 20 | return claimsSafari && !claimsChrome; |
153 | } | |
154 | ||
155 | public boolean likeSafari() | |
156 | { | |
157 | 2 | boolean claimsSafari = false; |
158 | 2 | boolean claimsChrome = false; |
159 | 2 | for (final UserAgentDetail detail : userAgent.details()) |
160 | { | |
161 | 18 | if (detail instanceof Browser) |
162 | { | |
163 | 4 | if (SAFARI.equals(detail.name())) |
164 | { | |
165 | 2 | claimsSafari = true; |
166 | } | |
167 | 2 | else if (CHROME.equals(detail.name())) |
168 | { | |
169 | 2 | claimsChrome = true; |
170 | } | |
171 | } | |
172 | 18 | } |
173 | 2 | return claimsSafari && claimsChrome; |
174 | } | |
175 | ||
176 | public boolean likeGecko() | |
177 | { | |
178 | 20 | for (final UserAgentDetail detail : userAgent.details()) |
179 | { | |
180 | 94 | if (detail instanceof Gecko) |
181 | { | |
182 | 10 | return true; |
183 | } | |
184 | 84 | } |
185 | 10 | return false; |
186 | } | |
187 | ||
188 | public boolean isCompatible() | |
189 | { | |
190 | 22 | for (final UserAgentDetail detail : userAgent.details()) |
191 | { | |
192 | 110 | if (detail instanceof Compatible) |
193 | { | |
194 | 4 | return true; |
195 | } | |
196 | 106 | } |
197 | 18 | return false; |
198 | } | |
199 | } |