Parse and decode a mailto scheme string. This parser implements RFC 6068. The returned object can be queried for the parsed parameters. Note: scheme name matching is case-sensitive, unlike the formal RFC. As a result, you should always ensure that you write your URI string with the scheme us
(@NonNull String uri)
| 103 | * @exception ParseException if the scheme is not a mailto URI |
| 104 | */ |
| 105 | @NonNull |
| 106 | public static MailTo parse(@NonNull String uri) throws ParseException { |
| 107 | Preconditions.checkNotNull(uri); |
| 108 | |
| 109 | if (!isMailTo(uri)) { |
| 110 | throw new ParseException("Not a mailto scheme"); |
| 111 | } |
| 112 | |
| 113 | // Drop fragment if present |
| 114 | int fragmentIndex = uri.indexOf('#'); |
| 115 | if (fragmentIndex != -1) { |
| 116 | uri = uri.substring(0, fragmentIndex); |
| 117 | } |
| 118 | |
| 119 | String address; |
| 120 | String query; |
| 121 | int queryIndex = uri.indexOf('?'); |
| 122 | if (queryIndex == -1) { |
| 123 | address = Uri.decode(uri.substring(MAILTO_SCHEME.length())); |
| 124 | query = null; |
| 125 | } else { |
| 126 | address = Uri.decode(uri.substring(MAILTO_SCHEME.length(), queryIndex)); |
| 127 | query = uri.substring(queryIndex + 1); |
| 128 | } |
| 129 | |
| 130 | MailTo mailTo = new MailTo(); |
| 131 | |
| 132 | // Parse out the query parameters |
| 133 | if (query != null) { |
| 134 | @SuppressWarnings("StringSplitter") |
| 135 | String[] queries = query.split("&"); |
| 136 | for (String queryParameter : queries) { |
| 137 | String[] nameValueArray = queryParameter.split("=", 2); |
| 138 | if (nameValueArray.length == 0) { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | // insert the headers with the name in lowercase so that |
| 143 | // we can easily find common headers |
| 144 | String queryParameterKey = Uri.decode(nameValueArray[0]).toLowerCase(Locale.ROOT); |
| 145 | String queryParameterValue = nameValueArray.length > 1 |
| 146 | ? Uri.decode(nameValueArray[1]) : null; |
| 147 | |
| 148 | mailTo.mHeaders.put(queryParameterKey, queryParameterValue); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Address can be specified in both the headers and just after the |
| 153 | // mailto line. Join the two together. |
| 154 | String toParameter = mailTo.getTo(); |
| 155 | if (toParameter != null) { |
| 156 | address += ", " + toParameter; |
| 157 | } |
| 158 | mailTo.mHeaders.put(TO, address); |
| 159 | |
| 160 | return mailTo; |
| 161 | } |
| 162 |