/structured-http-headers/shh-user-agent/src/main/java/com/mattunderscore/http/headers/useragent/parser/OSParser.java
Java | 128 lines | 89 code | 6 blank | 33 comment | 23 complexity | c774c37895e4ece2eeaa8433ebbfce46 MD5 | raw file
Possible License(s): BSD-3-Clause
- /* Copyright © 2013 Matthew Champion
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- * Neither the name of mattunderscore.com nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
- package com.mattunderscore.http.headers.useragent.parser;
- import static com.mattunderscore.http.headers.useragent.parser.ParsingUtils.*;
- import com.mattunderscore.http.headers.useragent.details.platform.Linux;
- import com.mattunderscore.http.headers.useragent.details.platform.MacOSX;
- import com.mattunderscore.http.headers.useragent.details.platform.Machintosh;
- import com.mattunderscore.http.headers.useragent.details.platform.OSPlatform;
- import com.mattunderscore.http.headers.useragent.details.platform.Windows;
- /**
- * Parser for operating systems.
- * @author Matt Champion
- * @since 0.2.7
- */
- /*package*/ class OSParser implements TokenParser
- {
- private static final ArchitectureParser archParser = new ArchitectureParser();
- /*package*/ OSParser()
- {
- }
- /**
- * Parses the next token for a operating system.
- * @param state
- */
- @Override
- public String parseToken(ParsingState state)
- {
- String remainingHeader = state.getRemaining();
- if (remainingHeader.startsWith("Machintosh"))
- {
- state.addDetail(new Machintosh());
- remainingHeader = remainingHeader.substring(10);
- }
- else if (remainingHeader.startsWith("Windows NT"))
- {
- String version = nextElement(remainingHeader.substring(11));
- state.addDetail(new Windows("Windows NT",version));
- remainingHeader = remainingHeader.substring(11 + version.length());
- }
- else if (remainingHeader.startsWith("WinNT"))
- {
- String version = nextElement(remainingHeader.substring(6));
- state.addDetail(new Windows("Windows NT",version));
- remainingHeader = remainingHeader.substring(6 + version.length());
- }
- else if (remainingHeader.startsWith("Win95"))
- {
- state.addDetail(new OSPlatform("Windows 95"));
- remainingHeader = remainingHeader.substring(5);
- }
- else if (remainingHeader.startsWith("Windows 95"))
- {
- state.addDetail(new OSPlatform("Windows 95"));
- remainingHeader = remainingHeader.substring(10);
- }
- else if (remainingHeader.startsWith("Win98"))
- {
- state.addDetail(new OSPlatform("Windows 98"));
- remainingHeader = remainingHeader.substring(10);
- }
- else if (remainingHeader.startsWith("Windows 98; Win 9x 4.90"))
- {
- state.addDetail(new Windows("Windows 98","","Windows Millennium Edition (Windows Me)"));
- remainingHeader = remainingHeader.substring(23);
- }
- else if (remainingHeader.startsWith("Windows 98"))
- {
- state.addDetail(new OSPlatform("Windows 98"));
- remainingHeader = remainingHeader.substring(10);
- }
- else if (remainingHeader.startsWith("like Mac OS X"))
- {
- state.addDetail(new MacOSX("like"));
- remainingHeader = remainingHeader.substring(13);
- }
- else if (remainingHeader.startsWith("Mac OS X"))
- {
- String version = nextElement(remainingHeader.substring(9));
- state.addDetail(new MacOSX(version));
- remainingHeader = remainingHeader.substring(9 + version.length());
- }
- else if (remainingHeader.startsWith("Linux"))
- {
- remainingHeader = remainingHeader.substring(6);
- state.setRemaining(remainingHeader);
- String arch = archParser.parseToken(state);
- if (arch != null)
- {
- state.addDetail(new Linux(arch));
- }
- else
- {
- state.addDetail(new Linux());
- }
- remainingHeader = state.getRemaining();
- }
- state.setRemaining(remainingHeader);
- return null;
- }
- }