Coverage Report - com.mattunderscore.http.headers.useragent.parser.ParsingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ParsingUtils
75%
43/57
66%
24/36
5.4
 
 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 com.mattunderscore.http.headers.useragent.details.application.ContactInformation;
 29  
 
 30  
 /**
 31  
  * Utilities for parsing the user agent string.
 32  
  * @author Matt Champion
 33  
  */
 34  
 /*package*/ final class ParsingUtils
 35  
 {
 36  
     private ParsingUtils()
 37  0
     {
 38  0
     }
 39  
 
 40  
     /**
 41  
      * Get the next token of the user agent string.
 42  
      * @param version
 43  
      * @return
 44  
      */
 45  
     public static String nextElement(final String version)
 46  
     {
 47  366
         String theVersion = version.trim();
 48  366
         if (theVersion.length() > 0)
 49  
         {
 50  366
             int next = nextElementStart(theVersion);
 51  366
             if (next != -1)
 52  
             {
 53  322
                 return theVersion.substring(0,next);
 54  
             }
 55  
             else
 56  
             {
 57  44
                 return theVersion;
 58  
             }
 59  
         }
 60  
         else
 61  
         {
 62  0
             return "";
 63  
         }
 64  
     }
 65  
 
 66  
     /**
 67  
      * Gets the index of the start of the next token.
 68  
      * @param header
 69  
      * @return
 70  
      */
 71  
     public static int nextElementStart(final String header)
 72  
     {
 73  630
         int space = header.indexOf(" ");
 74  630
         int semi = header.indexOf(";");
 75  630
         int closeBracket = header.indexOf(")");
 76  630
         int openBracket = header.indexOf("(");
 77  630
         int first = Integer.MAX_VALUE;
 78  630
         boolean set = false;
 79  630
         if (space != -1)
 80  
         {
 81  530
             first = space;
 82  530
             set = true;
 83  
         }
 84  630
         if (semi != -1 && semi < first)
 85  
         {
 86  106
             first = semi;
 87  106
             set = true;
 88  
         }
 89  630
         if (closeBracket != -1 && closeBracket < first)
 90  
         {
 91  156
             first = closeBracket;
 92  156
             set = true;
 93  
         }
 94  630
         if (openBracket != -1 && openBracket < first)
 95  
         {
 96  48
             first = openBracket;
 97  48
             set = true;
 98  
         }
 99  630
         if (set)
 100  
         {
 101  578
             return first;
 102  
         }
 103  
         else
 104  
         {
 105  52
             return -1;
 106  
         }
 107  
     }
 108  
 
 109  
     /**
 110  
      * Add contact information if it is the next token.
 111  
      * @param state
 112  
      */
 113  
     public static void contactInformation(final ParsingState state)
 114  
     {
 115  4
         String remainingHeader = state.getRemaining();
 116  4
         if (remainingHeader.startsWith("(+"))
 117  
         {
 118  4
             String info = shortToken(remainingHeader.substring(2));
 119  4
             state.addDetail(new ContactInformation(info));
 120  4
         }
 121  0
         else if (remainingHeader.startsWith("("))
 122  
         {
 123  0
             String info = shortToken(remainingHeader.substring(1));
 124  0
             state.addDetail(new ContactInformation(info));
 125  0
         }
 126  0
         else if (remainingHeader.startsWith("http://"))
 127  
         {
 128  0
             String info = shortToken(remainingHeader);
 129  0
             state.addDetail(new ContactInformation(info));
 130  
         }
 131  4
     }
 132  
 
 133  
     private static String shortToken(final String header)
 134  
     {
 135  4
         String theHeader = header.trim();
 136  4
         int space = theHeader.indexOf(" ");
 137  4
         int bracket = theHeader.indexOf(")");
 138  4
         int first = -1;
 139  4
         if (space != -1)
 140  
         {
 141  0
             first = space;
 142  
         }
 143  4
         if (bracket != -1)
 144  
         {
 145  4
             if (space == -1)
 146  
             {
 147  4
                 first = bracket;
 148  
             }
 149  0
             else if (bracket < space)
 150  
             {
 151  0
                 first = bracket;
 152  
             }
 153  
         }
 154  4
         if (first != -1)
 155  
         {
 156  4
             return theHeader.substring(0,first);
 157  
         }
 158  
         else
 159  
         {
 160  0
             return theHeader;
 161  
         }
 162  
     }
 163  
 }