PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/structured-http-headers/shh-user-agent/src/main/java/com/mattunderscore/http/headers/useragent/parser/OSParser.java

https://bitbucket.org/mattunderscorechampion/ws-utils
Java | 128 lines | 89 code | 6 blank | 33 comment | 23 complexity | c774c37895e4ece2eeaa8433ebbfce46 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /* Copyright © 2013 Matthew Champion
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. * Neither the name of mattunderscore.com nor the
  11. names of its contributors may be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
  17. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  23. package com.mattunderscore.http.headers.useragent.parser;
  24. import static com.mattunderscore.http.headers.useragent.parser.ParsingUtils.*;
  25. import com.mattunderscore.http.headers.useragent.details.platform.Linux;
  26. import com.mattunderscore.http.headers.useragent.details.platform.MacOSX;
  27. import com.mattunderscore.http.headers.useragent.details.platform.Machintosh;
  28. import com.mattunderscore.http.headers.useragent.details.platform.OSPlatform;
  29. import com.mattunderscore.http.headers.useragent.details.platform.Windows;
  30. /**
  31. * Parser for operating systems.
  32. * @author Matt Champion
  33. * @since 0.2.7
  34. */
  35. /*package*/ class OSParser implements TokenParser
  36. {
  37. private static final ArchitectureParser archParser = new ArchitectureParser();
  38. /*package*/ OSParser()
  39. {
  40. }
  41. /**
  42. * Parses the next token for a operating system.
  43. * @param state
  44. */
  45. @Override
  46. public String parseToken(ParsingState state)
  47. {
  48. String remainingHeader = state.getRemaining();
  49. if (remainingHeader.startsWith("Machintosh"))
  50. {
  51. state.addDetail(new Machintosh());
  52. remainingHeader = remainingHeader.substring(10);
  53. }
  54. else if (remainingHeader.startsWith("Windows NT"))
  55. {
  56. String version = nextElement(remainingHeader.substring(11));
  57. state.addDetail(new Windows("Windows NT",version));
  58. remainingHeader = remainingHeader.substring(11 + version.length());
  59. }
  60. else if (remainingHeader.startsWith("WinNT"))
  61. {
  62. String version = nextElement(remainingHeader.substring(6));
  63. state.addDetail(new Windows("Windows NT",version));
  64. remainingHeader = remainingHeader.substring(6 + version.length());
  65. }
  66. else if (remainingHeader.startsWith("Win95"))
  67. {
  68. state.addDetail(new OSPlatform("Windows 95"));
  69. remainingHeader = remainingHeader.substring(5);
  70. }
  71. else if (remainingHeader.startsWith("Windows 95"))
  72. {
  73. state.addDetail(new OSPlatform("Windows 95"));
  74. remainingHeader = remainingHeader.substring(10);
  75. }
  76. else if (remainingHeader.startsWith("Win98"))
  77. {
  78. state.addDetail(new OSPlatform("Windows 98"));
  79. remainingHeader = remainingHeader.substring(10);
  80. }
  81. else if (remainingHeader.startsWith("Windows 98; Win 9x 4.90"))
  82. {
  83. state.addDetail(new Windows("Windows 98","","Windows Millennium Edition (Windows Me)"));
  84. remainingHeader = remainingHeader.substring(23);
  85. }
  86. else if (remainingHeader.startsWith("Windows 98"))
  87. {
  88. state.addDetail(new OSPlatform("Windows 98"));
  89. remainingHeader = remainingHeader.substring(10);
  90. }
  91. else if (remainingHeader.startsWith("like Mac OS X"))
  92. {
  93. state.addDetail(new MacOSX("like"));
  94. remainingHeader = remainingHeader.substring(13);
  95. }
  96. else if (remainingHeader.startsWith("Mac OS X"))
  97. {
  98. String version = nextElement(remainingHeader.substring(9));
  99. state.addDetail(new MacOSX(version));
  100. remainingHeader = remainingHeader.substring(9 + version.length());
  101. }
  102. else if (remainingHeader.startsWith("Linux"))
  103. {
  104. remainingHeader = remainingHeader.substring(6);
  105. state.setRemaining(remainingHeader);
  106. String arch = archParser.parseToken(state);
  107. if (arch != null)
  108. {
  109. state.addDetail(new Linux(arch));
  110. }
  111. else
  112. {
  113. state.addDetail(new Linux());
  114. }
  115. remainingHeader = state.getRemaining();
  116. }
  117. state.setRemaining(remainingHeader);
  118. return null;
  119. }
  120. }