A routine for parsing the MIME type out of a String.
(String rawdata)
| 78 | * A routine for parsing the MIME type out of a String. |
| 79 | */ |
| 80 | private void parse(String rawdata) throws MimeTypeParseException { |
| 81 | int slashIndex = rawdata.indexOf('/'); |
| 82 | int semIndex = rawdata.indexOf(';'); |
| 83 | if ((slashIndex < 0) && (semIndex < 0)) { |
| 84 | // neither character is present, so treat it |
| 85 | // as an error |
| 86 | throw new MimeTypeParseException("Unable to find a sub type."); |
| 87 | } else if ((slashIndex < 0) && (semIndex >= 0)) { |
| 88 | // we have a ';' (and therefore a parameter list), |
| 89 | // but no '/' indicating a sub type is present |
| 90 | throw new MimeTypeParseException("Unable to find a sub type."); |
| 91 | } else if ((slashIndex >= 0) && (semIndex < 0)) { |
| 92 | // we have a primary and sub type but no parameter list |
| 93 | primaryType = rawdata.substring(0, slashIndex).trim(). |
| 94 | toLowerCase(Locale.ENGLISH); |
| 95 | subType = rawdata.substring(slashIndex + 1).trim(). |
| 96 | toLowerCase(Locale.ENGLISH); |
| 97 | parameters = new MimeTypeParameterList(); |
| 98 | } else if (slashIndex < semIndex) { |
| 99 | // we have all three items in the proper sequence |
| 100 | primaryType = rawdata.substring(0, slashIndex).trim(). |
| 101 | toLowerCase(Locale.ENGLISH); |
| 102 | subType = rawdata.substring(slashIndex + 1, semIndex).trim(). |
| 103 | toLowerCase(Locale.ENGLISH); |
| 104 | parameters = new MimeTypeParameterList(rawdata.substring(semIndex)); |
| 105 | } else { |
| 106 | // we have a ';' lexically before a '/' which means we |
| 107 | // have a primary type and a parameter list but no sub type |
| 108 | throw new MimeTypeParseException("Unable to find a sub type."); |
| 109 | } |
| 110 | |
| 111 | // now validate the primary and sub types |
| 112 | |
| 113 | // check to see if primary is valid |
| 114 | if (!isValidToken(primaryType)) |
| 115 | throw new MimeTypeParseException("Primary type is invalid."); |
| 116 | |
| 117 | // check to see if sub is valid |
| 118 | if (!isValidToken(subType)) |
| 119 | throw new MimeTypeParseException("Sub type is invalid."); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Retrieve the primary type of this object. |
no test coverage detected