Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ArchitectureParser |
|
| 4.333333333333333;4.333 |
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.parser; | |
27 | ||
28 | import static com.mattunderscore.http.headers.useragent.parser.ParsingUtils.nextElement; | |
29 | ||
30 | import com.mattunderscore.http.headers.useragent.details.hardware.Architecture; | |
31 | ||
32 | /** | |
33 | * Parser for architecture. | |
34 | * @author Matt Champion | |
35 | * @since 0.2.7 | |
36 | */ | |
37 | /*package*/ class ArchitectureParser implements TokenParser | |
38 | { | |
39 | /*package*/ ArchitectureParser() | |
40 | 6 | { |
41 | 6 | } |
42 | ||
43 | @Override | |
44 | public String parseToken(final ParsingState state) | |
45 | { | |
46 | 152 | if (!state.hasArchitecture()) { |
47 | 98 | String remaining = state.getRemaining(); |
48 | 98 | String arch = nextElement(remaining); |
49 | 98 | if ("x86_64".equals(arch)) |
50 | { | |
51 | 14 | setArchitecture(state,arch); |
52 | 14 | state.addDetail(new Architecture(arch)); |
53 | 14 | remaining = remaining.substring(6); |
54 | 14 | state.setArchitecture(); |
55 | 14 | state.setRemaining(remaining); |
56 | 14 | return arch; |
57 | } | |
58 | 84 | else if ("i686".equals(arch)) |
59 | { | |
60 | 0 | setArchitecture(state,arch); |
61 | 0 | state.addDetail(new Architecture(arch)); |
62 | 0 | remaining = remaining.substring(4); |
63 | 0 | state.setArchitecture(); |
64 | 0 | state.setRemaining(remaining); |
65 | 0 | return arch; |
66 | } | |
67 | 84 | else if ("x86".equals(arch)) |
68 | { | |
69 | 0 | setArchitecture(state,arch); |
70 | 0 | state.addDetail(new Architecture(arch)); |
71 | 0 | remaining = remaining.substring(3); |
72 | 0 | state.setArchitecture(); |
73 | 0 | state.setRemaining(remaining); |
74 | 0 | return arch; |
75 | } | |
76 | 84 | else if ("x64".equals(arch)) |
77 | { | |
78 | 0 | setArchitecture(state,arch); |
79 | 0 | remaining = remaining.substring(3); |
80 | 0 | state.setRemaining(remaining); |
81 | 0 | return arch; |
82 | } | |
83 | } | |
84 | 138 | return null; |
85 | } | |
86 | ||
87 | /** | |
88 | * Set the architecture if has not already been set. | |
89 | * @param state The current state | |
90 | * @param arch The string token indicating the architecture | |
91 | */ | |
92 | public void setArchitecture(final ParsingState state, final String arch) | |
93 | { | |
94 | 16 | if (!state.hasArchitecture()) |
95 | { | |
96 | 16 | state.addDetail(new Architecture(arch)); |
97 | 16 | state.setArchitecture(); |
98 | } | |
99 | 16 | } |
100 | } |