Coverage Report - com.mattunderscore.http.headers.useragent.parser.AndroidPlatformParser
 
Classes in this File Line Coverage Branch Coverage Complexity
AndroidPlatformParser
82%
38/46
75%
18/24
8
 
 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.EncryptionStrength;
 32  
 import com.mattunderscore.http.headers.useragent.details.platform.Linux;
 33  
 import com.mattunderscore.http.headers.useragent.details.platform.OSPlatform;
 34  
 
 35  
 /**
 36  
  * Parser for Android user agent strings
 37  
  * @author Matt Champion
 38  
  * @since 0.2.7
 39  
  */
 40  
 /*package*/ class AndroidPlatformParser implements TokensParser
 41  
 {
 42  2
     private static final DesktopBrowserParser desktopBrowserParser = new DesktopBrowserParser();
 43  2
     private static final ArchitectureParser archParser = new ArchitectureParser();
 44  
 
 45  
     /*package*/ AndroidPlatformParser()
 46  2
     {
 47  2
     }
 48  
 
 49  
     /**
 50  
      * Parses the remaining tokens for an Android.
 51  
      * @param state
 52  
      */
 53  
     @Override
 54  
     public void parseTokens(ParsingState state)
 55  
     {
 56  4
         String remainingHeader = state.getRemaining();
 57  32
         while (!"".equals(remainingHeader))
 58  
         {
 59  32
             String oldHeader = remainingHeader;
 60  32
             if (remainingHeader.startsWith("Android"))
 61  
             {
 62  4
                 String version = nextElement(remainingHeader.substring(8));
 63  4
                 state.addDetail(new OSPlatform("Android",version));
 64  4
                 remainingHeader = remainingHeader.substring(8 + version.length());
 65  4
                 state.setRemaining(remainingHeader);
 66  4
             }
 67  28
             else if (remainingHeader.startsWith("U"))
 68  
             {
 69  4
                 state.addDetail(new EncryptionStrength("USA"));
 70  4
                 remainingHeader = remainingHeader.substring(1);
 71  
             }
 72  24
             else if (remainingHeader.equals("I"))
 73  
             {
 74  0
                 state.addDetail(new EncryptionStrength("International"));
 75  0
                 remainingHeader = remainingHeader.substring(1);
 76  
             }
 77  24
             else if (remainingHeader.startsWith("N"))
 78  
             {
 79  0
                 state.addDetail(new EncryptionStrength("None"));
 80  0
                 remainingHeader = remainingHeader.substring(1);
 81  
             }
 82  24
             else if (remainingHeader.startsWith("Linux"))
 83  
             {
 84  4
                 remainingHeader = remainingHeader.substring(6);
 85  4
                 state.setRemaining(remainingHeader);
 86  4
                 String arch = archParser.parseToken(state);
 87  4
                 if (arch != null)
 88  
                 {
 89  0
                     state.addDetail(new Linux(arch));
 90  
                 }
 91  
                 else
 92  
                 {
 93  4
                     state.addDetail(new Linux());
 94  
                 }
 95  4
                 remainingHeader = state.getRemaining();
 96  
             }
 97  
 
 98  
             // Clean up
 99  32
             if (remainingHeader.startsWith(";"))
 100  
             {
 101  12
                 remainingHeader = remainingHeader.substring(1);
 102  
             }
 103  20
             else if (remainingHeader.startsWith(")"))
 104  
             {
 105  4
                 state.setRemaining(remainingHeader);
 106  4
                 desktopBrowserParser.parseTokens(state);
 107  4
                 return;
 108  
             }
 109  16
             else if (oldHeader.equals(remainingHeader))
 110  
             {
 111  12
                 int nextElement = nextElementStart(remainingHeader);
 112  12
                 if (nextElement == 0)
 113  
                 {
 114  0
                     remainingHeader = remainingHeader.substring(1);
 115  
                 }
 116  12
                 else if (nextElement != -1)
 117  
                 {
 118  12
                     remainingHeader = remainingHeader.substring(nextElement);
 119  
                 }
 120  
                 else
 121  
                 {
 122  0
                     return;
 123  
                 }
 124  
             }
 125  28
             remainingHeader = remainingHeader.trim();
 126  28
         }
 127  0
         return;
 128  
     }
 129  
 }