Coverage Report - com.mattunderscore.http.headers.useragent.parser.IOSPlatformParser
 
Classes in this File Line Coverage Branch Coverage Complexity
IOSPlatformParser
80%
64/80
68%
30/44
9.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  
 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.hardware.IPad;
 33  
 import com.mattunderscore.http.headers.useragent.details.hardware.IPhone;
 34  
 import com.mattunderscore.http.headers.useragent.details.hardware.IPod;
 35  
 import com.mattunderscore.http.headers.useragent.details.platform.AppleMobileFirmware;
 36  
 import com.mattunderscore.http.headers.useragent.details.platform.IOS;
 37  
 import com.mattunderscore.http.headers.useragent.details.platform.IPhoneSimulator;
 38  
 import com.mattunderscore.http.headers.useragent.details.platform.MacOSX;
 39  
 
 40  
 /**
 41  
  * Parser for iOS user agent strings
 42  
  * @author Matt Champion
 43  
  * @since 0.2.7
 44  
  */
 45  
 /*package*/ class IOSPlatformParser implements TokensParser
 46  
 {
 47  2
     private static final IOSBrowserParser iosParser = new IOSBrowserParser();
 48  
 
 49  
     /*package*/ IOSPlatformParser()
 50  2
     {
 51  2
     }
 52  
 
 53  
     /**
 54  
      * Parses the remaining tokens for an iOS platform.
 55  
      * @param state
 56  
      */
 57  
     @Override
 58  
     public void parseTokens(ParsingState state)
 59  
     {
 60  14
         String header = state.getRemaining();
 61  14
         header = header.trim();
 62  134
         while (!"".equals(header))
 63  
         {
 64  120
             String oldHeader = header;
 65  120
             if (header.startsWith("CPU OS"))
 66  
             {
 67  4
                 String xVersion = nextElement(header.substring(7));
 68  4
                 String version = iOSVersion(xVersion);
 69  4
                 header = header.substring(7 + xVersion.length());
 70  4
                 state.addDetail(new IOS(version));
 71  4
             }
 72  116
             else if (header.startsWith("CPU iPhone OS"))
 73  
             {
 74  10
                 String xVersion = nextElement(header.substring(14));
 75  10
                 String version = iOSVersion(xVersion);
 76  10
                 header = header.substring(14 + xVersion.length());
 77  10
                 state.addDetail(new IOS(version));
 78  10
             }
 79  106
             else if (header.startsWith("iPad"))
 80  
             {
 81  6
                 state.setIPad(true);
 82  6
                 state.addDetail(new IPad());
 83  6
                 header = header.substring(4);
 84  
             }
 85  100
             else if (header.startsWith("iPod"))
 86  
             {
 87  2
                 state.setIPod(true);
 88  2
                 state.addDetail(new IPod());
 89  2
                 header = header.substring(4);
 90  
             }
 91  98
             else if (header.startsWith("iPhone Simulator"))
 92  
             {
 93  0
                 state.setIPhoneSimulator(true);
 94  0
                 state.addDetail(new IPhoneSimulator());
 95  0
                 header = header.substring(16);
 96  
             }
 97  98
             else if (header.startsWith("iPhone"))
 98  
             {
 99  6
                 state.setIPhone(true);
 100  6
                 state.addDetail(new IPhone());
 101  6
                 header = header.substring(6);
 102  
             }
 103  92
             else if (header.startsWith("like Mac OS X"))
 104  
             {
 105  14
                 state.addDetail(new MacOSX("like"));
 106  14
                 header = header.substring(13);
 107  
             }
 108  78
             else if (header.startsWith("Mobile"))
 109  
             {
 110  0
                 if (state.isIPad() || state.isIPhone() ||
 111  0
                         state.isIPod() || state.isIPhoneSimulator())
 112  
                 {
 113  0
                     String version = nextElement(header.substring(7));
 114  0
                     state.addDetail(new AppleMobileFirmware(version));
 115  0
                     header = header.substring(7 + version.length());
 116  0
                 }
 117  
                 else
 118  
                 {
 119  0
                     header = header.substring(6);
 120  
                 }
 121  
             }
 122  78
             else if (header.startsWith("U"))
 123  
             {
 124  10
                 state.addDetail(new EncryptionStrength("USA"));
 125  10
                 header = header.substring(1);
 126  
             }
 127  68
             else if (header.equals("I"))
 128  
             {
 129  0
                 state.addDetail(new EncryptionStrength("International"));
 130  0
                 header = header.substring(1);
 131  
             }
 132  68
             else if (header.startsWith("N"))
 133  
             {
 134  0
                 state.addDetail(new EncryptionStrength("None"));
 135  0
                 header = header.substring(1);
 136  
             }
 137  
             
 138  
             // Clean up
 139  120
             if (header.startsWith(")"))
 140  
             {
 141  14
                 state.setRemaining(header.substring(1));
 142  14
                 iosParser.parseTokens(state);
 143  14
                 header = state.getRemaining();
 144  
             }
 145  106
             else if (oldHeader.equals(header))
 146  
             {
 147  58
                 int nextElement = nextElementStart(header);
 148  58
                 if (nextElement == 0)
 149  
                 {
 150  48
                     header = header.substring(1).trim();
 151  
                 }
 152  10
                 else if (nextElement != -1)
 153  
                 {
 154  10
                     header = header.substring(nextElement).trim();
 155  
                 }
 156  
                 else
 157  
                 {
 158  0
                     return;
 159  
                 }
 160  
             }
 161  120
         }
 162  14
         state.setRemaining(header);
 163  14
         return;
 164  
     }
 165  
 
 166  
     private static String iOSVersion(final String version)
 167  
     {
 168  14
         String theVersion = version;
 169  14
         String rVersion = "";
 170  
         while (true)
 171  
         {
 172  32
             if ("".equals(theVersion))
 173  
             {
 174  0
                 return rVersion;
 175  
             }
 176  32
             int next = theVersion.indexOf("_");
 177  32
             if (next != -1)
 178  
             {
 179  18
                 rVersion += theVersion.substring(0,next) + ".";
 180  18
                 theVersion = theVersion.substring(next+1);
 181  
             }
 182  
             else
 183  
             {
 184  14
                 rVersion += theVersion;
 185  14
                 return rVersion;
 186  
             }
 187  18
         }
 188  
     }
 189  
 }