Coverage Report - com.mattunderscore.http.headers.useragent.parser.GenericPlatformParser
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericPlatformParser
86%
71/82
87%
35/40
12
 
 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  
 import static com.mattunderscore.http.headers.useragent.parser.ParsingUtils.nextElementStart;
 30  
 
 31  
 import com.mattunderscore.http.headers.useragent.details.application.Browser;
 32  
 import com.mattunderscore.http.headers.useragent.details.application.Compatible;
 33  
 import com.mattunderscore.http.headers.useragent.details.application.ContactInformation;
 34  
 import com.mattunderscore.http.headers.useragent.details.application.EncryptionStrength;
 35  
 import com.mattunderscore.http.headers.useragent.details.application.GoogleBot;
 36  
 import com.mattunderscore.http.headers.useragent.details.application.IE;
 37  
 import com.mattunderscore.http.headers.useragent.details.platform.Feature;
 38  
 import com.mattunderscore.http.headers.useragent.details.platform.Trident;
 39  
 import com.mattunderscore.http.headers.useragent.details.platform.WindowSystem;
 40  
 
 41  
 /**
 42  
  * Parser for generic user agent strings
 43  
  * @author Matt Champion
 44  
  * @since 0.2.7
 45  
  */
 46  
 /*package*/ class GenericPlatformParser implements TokensParser
 47  
 {
 48  2
     private static final DesktopBrowserParser desktopBrowserParser = new DesktopBrowserParser();
 49  2
     private static final OSParser osParser = new OSParser();
 50  2
     private static final ArchitectureParser archParser = new ArchitectureParser();
 51  
 
 52  
     /*package*/ GenericPlatformParser()
 53  2
     {
 54  2
     }
 55  
 
 56  
     /**
 57  
      * Parses the remaining tokens for a platform.
 58  
      * @param state
 59  
      */
 60  
     @Override
 61  
     public void parseTokens(ParsingState state)
 62  
     {
 63  42
         String remainingHeader = state.getRemaining();
 64  212
         while (!"".equals(remainingHeader))
 65  
         {
 66  212
             String oldHeader = remainingHeader;
 67  212
             if (remainingHeader.startsWith("X11"))
 68  
             {
 69  18
                 state.addDetail(new WindowSystem("X11"));
 70  18
                 remainingHeader = remainingHeader.substring(3);
 71  
             }
 72  194
             else if (remainingHeader.startsWith("compatible"))
 73  
             {
 74  12
                 state.addDetail(new Compatible());
 75  12
                 remainingHeader = remainingHeader.substring(10);
 76  
             }
 77  182
             else if (remainingHeader.startsWith("MSIE"))
 78  
             {
 79  8
                 String version = nextElement(remainingHeader.substring(5));
 80  8
                 state.addDetail(new IE(version));
 81  8
                 remainingHeader = remainingHeader.substring(5 + version.length());
 82  8
             }
 83  174
             else if (remainingHeader.startsWith("Trident"))
 84  
             {
 85  8
                 String version = nextElement(remainingHeader.substring(8));
 86  8
                 state.addDetail(new Trident(version));
 87  8
                 remainingHeader = remainingHeader.substring(8 + version.length());
 88  8
             }
 89  166
             else if (remainingHeader.startsWith("Win64"))
 90  
             {
 91  2
                 archParser.setArchitecture(state,"x64");
 92  2
                 remainingHeader = remainingHeader.substring(5);
 93  
             }
 94  164
             else if (remainingHeader.startsWith("WOW64"))
 95  
             {
 96  6
                 state.addDetail(new Feature("WOW64"));
 97  6
                 remainingHeader = remainingHeader.substring(5);
 98  
             }
 99  158
             else if (remainingHeader.startsWith("Win32"))
 100  
             {
 101  0
                 archParser.setArchitecture(state,"x86");
 102  0
                 remainingHeader = remainingHeader.substring(5);
 103  
             }
 104  158
             else if (remainingHeader.startsWith("Konqueror"))
 105  
             {
 106  0
                 state.addDetail(new Browser("Konqueror"));
 107  0
                 String version = nextElement(remainingHeader.substring(10));
 108  0
                 remainingHeader = remainingHeader.substring(10 + version.length());
 109  0
             }
 110  158
             else if (remainingHeader.startsWith("U"))
 111  
             {
 112  8
                 state.addDetail(new EncryptionStrength("USA"));
 113  8
                 remainingHeader = remainingHeader.substring(1);
 114  
             }
 115  150
             else if (remainingHeader.equals("I"))
 116  
             {
 117  0
                 state.addDetail(new EncryptionStrength("International"));
 118  0
                 remainingHeader = remainingHeader.substring(1);
 119  
             }
 120  150
             else if (remainingHeader.startsWith("N"))
 121  
             {
 122  0
                 state.addDetail(new EncryptionStrength("None"));
 123  0
                 remainingHeader = remainingHeader.substring(1);
 124  
             }
 125  150
             else if (remainingHeader.startsWith("rv:"))
 126  
             {
 127  8
                 String version = nextElement(remainingHeader.substring(3));
 128  8
                 remainingHeader = remainingHeader.substring(3 + version.length());
 129  8
                 state.addKeyValuePair("rv", version);
 130  8
             }
 131  142
             else if (remainingHeader.startsWith("Googlebot"))
 132  
             {
 133  4
                 String version = nextElement(remainingHeader.substring(10));
 134  4
                 state.addDetail(new GoogleBot("Googlebot",version));
 135  4
                 remainingHeader = remainingHeader.substring(10 + version.length());
 136  4
             }
 137  138
             else if (remainingHeader.startsWith("+"))
 138  
             {
 139  8
                 String info = nextElement(remainingHeader.substring(1));
 140  8
                 state.addDetail(new ContactInformation(info));
 141  8
                 remainingHeader = remainingHeader.substring(1 + info.length());
 142  8
             }
 143  
             else
 144  
             {
 145  130
                 state.setRemaining(remainingHeader);
 146  130
                 archParser.parseToken(state);
 147  130
                 osParser.parseToken(state);
 148  130
                 remainingHeader = state.getRemaining();
 149  
             }
 150  
 
 151  
             // Clean up
 152  212
             if (remainingHeader.startsWith(";"))
 153  
             {
 154  96
                 remainingHeader = remainingHeader.substring(1);
 155  
             }
 156  116
             else if (remainingHeader.startsWith(")"))
 157  
             {
 158  40
                 state.setRemaining(remainingHeader);
 159  40
                 desktopBrowserParser.parseTokens(state);
 160  40
                 return;
 161  
             }
 162  76
             else if (oldHeader.equals(remainingHeader))
 163  
             {
 164  72
                 int nextElement = nextElementStart(remainingHeader);
 165  72
                 if (nextElement == 0)
 166  
                 {
 167  8
                     remainingHeader = remainingHeader.substring(1);
 168  
                 }
 169  64
                 else if (nextElement != -1)
 170  
                 {
 171  62
                     remainingHeader = remainingHeader.substring(nextElement);
 172  
                 }
 173  
                 else
 174  
                 {
 175  2
                     return;
 176  
                 }
 177  
             }
 178  170
             remainingHeader = remainingHeader.trim();
 179  170
         }
 180  0
         return;
 181  
     }
 182  
 }