1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text.util; 18 19 import android.annotation.Nullable; 20 21 /** 22 * This class stores an RFC 822-like name, address, and comment, 23 * and provides methods to convert them to quoted strings. 24 */ 25 @android.ravenwood.annotation.RavenwoodKeepWholeClass 26 public class Rfc822Token { 27 @Nullable 28 private String mName, mAddress, mComment; 29 30 /** 31 * Creates a new Rfc822Token with the specified name, address, 32 * and comment. 33 */ Rfc822Token(@ullable String name, @Nullable String address, @Nullable String comment)34 public Rfc822Token(@Nullable String name, @Nullable String address, @Nullable String comment) { 35 mName = name; 36 mAddress = address; 37 mComment = comment; 38 } 39 40 /** 41 * Returns the name part. 42 */ 43 @Nullable getName()44 public String getName() { 45 return mName; 46 } 47 48 /** 49 * Returns the address part. 50 */ 51 @Nullable getAddress()52 public String getAddress() { 53 return mAddress; 54 } 55 56 /** 57 * Returns the comment part. 58 */ 59 @Nullable getComment()60 public String getComment() { 61 return mComment; 62 } 63 64 /** 65 * Changes the name to the specified name. 66 */ setName(@ullable String name)67 public void setName(@Nullable String name) { 68 mName = name; 69 } 70 71 /** 72 * Changes the address to the specified address. 73 */ setAddress(@ullable String address)74 public void setAddress(@Nullable String address) { 75 mAddress = address; 76 } 77 78 /** 79 * Changes the comment to the specified comment. 80 */ setComment(@ullable String comment)81 public void setComment(@Nullable String comment) { 82 mComment = comment; 83 } 84 85 /** 86 * Returns the name (with quoting added if necessary), 87 * the comment (in parentheses), and the address (in angle brackets). 88 * This should be suitable for inclusion in an RFC 822 address list. 89 */ toString()90 public String toString() { 91 StringBuilder sb = new StringBuilder(); 92 93 if (mName != null && mName.length() != 0) { 94 sb.append(quoteNameIfNecessary(mName)); 95 sb.append(' '); 96 } 97 98 if (mComment != null && mComment.length() != 0) { 99 sb.append('('); 100 sb.append(quoteComment(mComment)); 101 sb.append(") "); 102 } 103 104 if (mAddress != null && mAddress.length() != 0) { 105 sb.append('<'); 106 sb.append(mAddress); 107 sb.append('>'); 108 } 109 110 return sb.toString(); 111 } 112 113 /** 114 * Returns the name, conservatively quoting it if there are any 115 * characters that are likely to cause trouble outside of a 116 * quoted string, or returning it literally if it seems safe. 117 */ quoteNameIfNecessary(String name)118 public static String quoteNameIfNecessary(String name) { 119 int len = name.length(); 120 121 for (int i = 0; i < len; i++) { 122 char c = name.charAt(i); 123 124 if (! ((c >= 'A' && c <= 'Z') || 125 (c >= 'a' && c <= 'z') || 126 (c == ' ') || 127 (c >= '0' && c <= '9'))) { 128 return '"' + quoteName(name) + '"'; 129 } 130 } 131 132 return name; 133 } 134 135 /** 136 * Returns the name, with internal backslashes and quotation marks 137 * preceded by backslashes. The outer quote marks themselves are not 138 * added by this method. 139 */ quoteName(String name)140 public static String quoteName(String name) { 141 StringBuilder sb = new StringBuilder(); 142 143 int len = name.length(); 144 for (int i = 0; i < len; i++) { 145 char c = name.charAt(i); 146 147 if (c == '\\' || c == '"') { 148 sb.append('\\'); 149 } 150 151 sb.append(c); 152 } 153 154 return sb.toString(); 155 } 156 157 /** 158 * Returns the comment, with internal backslashes and parentheses 159 * preceded by backslashes. The outer parentheses themselves are 160 * not added by this method. 161 */ quoteComment(String comment)162 public static String quoteComment(String comment) { 163 int len = comment.length(); 164 StringBuilder sb = new StringBuilder(); 165 166 for (int i = 0; i < len; i++) { 167 char c = comment.charAt(i); 168 169 if (c == '(' || c == ')' || c == '\\') { 170 sb.append('\\'); 171 } 172 173 sb.append(c); 174 } 175 176 return sb.toString(); 177 } 178 hashCode()179 public int hashCode() { 180 int result = 17; 181 if (mName != null) result = 31 * result + mName.hashCode(); 182 if (mAddress != null) result = 31 * result + mAddress.hashCode(); 183 if (mComment != null) result = 31 * result + mComment.hashCode(); 184 return result; 185 } 186 stringEquals(String a, String b)187 private static boolean stringEquals(String a, String b) { 188 if (a == null) { 189 return (b == null); 190 } else { 191 return (a.equals(b)); 192 } 193 } 194 equals(@ullable Object o)195 public boolean equals(@Nullable Object o) { 196 if (!(o instanceof Rfc822Token)) { 197 return false; 198 } 199 Rfc822Token other = (Rfc822Token) o; 200 return (stringEquals(mName, other.mName) && 201 stringEquals(mAddress, other.mAddress) && 202 stringEquals(mComment, other.mComment)); 203 } 204 } 205 206