VCardParserImpl_V30.java revision 48dd8e86a81d2ab40eb762975c8211c225002bf0
1/* 2 * Copyright (C) 2010 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 */ 16package com.android.vcard; 17 18import android.util.Log; 19 20import com.android.vcard.exception.VCardException; 21 22import java.io.IOException; 23import java.util.Set; 24 25/** 26 * <p> 27 * Basic implementation achieving vCard 3.0 parsing. 28 * </p> 29 * <p> 30 * This class inherits vCard 2.1 implementation since technically they are similar, 31 * while specifically there's logical no relevance between them. 32 * So that developers are not confused with the inheritance, 33 * {@link VCardParser_V30} does not inherit {@link VCardParser_V21}, while 34 * {@link VCardParserImpl_V30} inherits {@link VCardParserImpl_V21}. 35 * </p> 36 * @hide 37 */ 38/* package */ class VCardParserImpl_V30 extends VCardParserImpl_V21 { 39 private static final String LOG_TAG = VCardConstants.LOG_TAG; 40 41 private String mPreviousLine; 42 private boolean mEmittedAgentWarning = false; 43 44 public VCardParserImpl_V30() { 45 super(); 46 } 47 48 public VCardParserImpl_V30(int vcardType) { 49 super(vcardType); 50 } 51 52 @Override 53 protected int getVersion() { 54 return VCardConfig.VERSION_30; 55 } 56 57 @Override 58 protected String getVersionString() { 59 return VCardConstants.VERSION_V30; 60 } 61 62 @Override 63 protected String getLine() throws IOException { 64 if (mPreviousLine != null) { 65 String ret = mPreviousLine; 66 mPreviousLine = null; 67 return ret; 68 } else { 69 return mReader.readLine(); 70 } 71 } 72 73 /** 74 * vCard 3.0 requires that the line with space at the beginning of the line 75 * must be combined with previous line. 76 */ 77 @Override 78 protected String getNonEmptyLine() throws IOException, VCardException { 79 String line; 80 StringBuilder builder = null; 81 while (true) { 82 line = mReader.readLine(); 83 if (line == null) { 84 if (builder != null) { 85 return builder.toString(); 86 } else if (mPreviousLine != null) { 87 String ret = mPreviousLine; 88 mPreviousLine = null; 89 return ret; 90 } 91 throw new VCardException("Reached end of buffer."); 92 } else if (line.length() == 0) { 93 if (builder != null) { 94 return builder.toString(); 95 } else if (mPreviousLine != null) { 96 String ret = mPreviousLine; 97 mPreviousLine = null; 98 return ret; 99 } 100 } else if (line.charAt(0) == ' ' || line.charAt(0) == '\t') { 101 if (builder != null) { 102 // See Section 5.8.1 of RFC 2425 (MIME-DIR document). 103 // Following is the excerpts from it. 104 // 105 // DESCRIPTION:This is a long description that exists on a long line. 106 // 107 // Can be represented as: 108 // 109 // DESCRIPTION:This is a long description 110 // that exists on a long line. 111 // 112 // It could also be represented as: 113 // 114 // DESCRIPTION:This is a long descrip 115 // tion that exists o 116 // n a long line. 117 builder.append(line.substring(1)); 118 } else if (mPreviousLine != null) { 119 builder = new StringBuilder(); 120 builder.append(mPreviousLine); 121 mPreviousLine = null; 122 builder.append(line.substring(1)); 123 } else { 124 throw new VCardException("Space exists at the beginning of the line"); 125 } 126 } else { 127 if (mPreviousLine == null) { 128 mPreviousLine = line; 129 if (builder != null) { 130 return builder.toString(); 131 } 132 } else { 133 String ret = mPreviousLine; 134 mPreviousLine = line; 135 return ret; 136 } 137 } 138 } 139 } 140 141 /* 142 * vcard = [group "."] "BEGIN" ":" "VCARD" 1 * CRLF 143 * 1 * (contentline) 144 * ;A vCard object MUST include the VERSION, FN and N types. 145 * [group "."] "END" ":" "VCARD" 1 * CRLF 146 */ 147 @Override 148 protected boolean readBeginVCard(boolean allowGarbage) throws IOException, VCardException { 149 // TODO: vCard 3.0 supports group. 150 return super.readBeginVCard(allowGarbage); 151 } 152 153 /** 154 * vCard 3.0 allows iana-token as paramType, while vCard 2.1 does not. 155 */ 156 @Override 157 protected void handleParams(PropertyData propertyData, final String params) 158 throws VCardException { 159 try { 160 super.handleParams(propertyData, params); 161 } catch (VCardException e) { 162 // maybe IANA type 163 String[] strArray = params.split("=", 2); 164 if (strArray.length == 2) { 165 handleAnyParam(propertyData, strArray[0], strArray[1]); 166 } else { 167 // Must not come here in the current implementation. 168 throw new VCardException( 169 "Unknown params value: " + params); 170 } 171 } 172 } 173 174 @Override 175 protected void handleAnyParam( 176 PropertyData propertyData, final String paramName, final String paramValue) { 177 splitAndPutParam(propertyData, paramName, paramValue); 178 } 179 180 @Override 181 protected void handleParamWithoutName(PropertyData propertyData, final String paramValue) { 182 handleType(propertyData, paramValue); 183 } 184 185 /* 186 * vCard 3.0 defines 187 * 188 * param = param-name "=" param-value *("," param-value) 189 * param-name = iana-token / x-name 190 * param-value = ptext / quoted-string 191 * quoted-string = DQUOTE QSAFE-CHAR DQUOTE 192 * QSAFE-CHAR = WSP / %x21 / %x23-7E / NON-ASCII 193 * ; Any character except CTLs, DQUOTE 194 * 195 * QSAFE-CHAR must not contain DQUOTE, including escaped one (\"). 196 */ 197 @Override 198 protected void handleType(PropertyData propertyData, final String paramValue) { 199 splitAndPutParam(propertyData, VCardConstants.PARAM_TYPE, paramValue); 200 } 201 202 /** 203 * Splits parameter values into pieces in accordance with vCard 3.0 specification and 204 * puts pieces into mInterpreter. 205 */ 206 /* 207 * param-value = ptext / quoted-string 208 * quoted-string = DQUOTE QSAFE-CHAR DQUOTE 209 * QSAFE-CHAR = WSP / %x21 / %x23-7E / NON-ASCII 210 * ; Any character except CTLs, DQUOTE 211 * 212 * QSAFE-CHAR must not contain DQUOTE, including escaped one (\") 213 */ 214 private void splitAndPutParam( 215 PropertyData propertyData, String paramName, String paramValue) { 216 // "comma,separated:inside.dquote",pref 217 // --> 218 // - comma,separated:inside.dquote 219 // - pref 220 // 221 // Note: Though there's a code, we don't need to take much care of 222 // wrongly-added quotes like the example above, as they induce 223 // parse errors at the top level (when splitting a line into parts). 224 StringBuilder builder = null; // Delay initialization. 225 boolean insideDquote = false; 226 final int length = paramValue.length(); 227 for (int i = 0; i < length; i++) { 228 final char ch = paramValue.charAt(i); 229 if (ch == '"') { 230 if (insideDquote) { 231 // End of Dquote. 232 propertyData.addParam(paramName, builder.toString()); 233 builder = null; 234 insideDquote = false; 235 } else { 236 if (builder != null) { 237 if (builder.length() > 0) { 238 // e.g. 239 // pref"quoted" 240 Log.w(LOG_TAG, "Unexpected Dquote inside property."); 241 } else { 242 // e.g. 243 // pref,"quoted" 244 // "quoted",pref 245 propertyData.addParam(paramName, builder.toString()); 246 } 247 } 248 insideDquote = true; 249 } 250 } else if (ch == ',' && !insideDquote) { 251 if (builder == null) { 252 Log.w(LOG_TAG, "Comma is used before actual string comes. (" + 253 paramValue + ")"); 254 } else { 255 propertyData.addParam(paramName, builder.toString()); 256 builder = null; 257 } 258 } else { 259 // To stop creating empty StringBuffer at the end of parameter, 260 // we delay creating this object until this point. 261 if (builder == null) { 262 builder = new StringBuilder(); 263 } 264 builder.append(ch); 265 } 266 } 267 if (insideDquote) { 268 // e.g. 269 // "non-quote-at-end 270 Log.d(LOG_TAG, "Dangling Dquote."); 271 } 272 if (builder != null) { 273 if (builder.length() == 0) { 274 Log.w(LOG_TAG, "Unintended behavior. We must not see empty StringBuilder " + 275 "at the end of parameter value parsing."); 276 } else { 277 propertyData.addParam(paramName, builder.toString()); 278 } 279 } 280 } 281 282 @Override 283 protected void handleAgent(final String propertyValue) { 284 // The way how vCard 3.0 supports "AGENT" is completely different from vCard 2.1. 285 // 286 // e.g. 287 // AGENT:BEGIN:VCARD\nFN:Joe Friday\nTEL:+1-919-555-7878\n 288 // TITLE:Area Administrator\, Assistant\n EMAIL\;TYPE=INTERN\n 289 // ET:jfriday@host.com\nEND:VCARD\n 290 // 291 // TODO: fix this. 292 // 293 // issue: 294 // vCard 3.0 also allows this as an example. 295 // 296 // AGENT;VALUE=uri: 297 // CID:JQPUBLIC.part3.960129T083020.xyzMail@host3.com 298 // 299 // This is not vCard. Should we support this? 300 // 301 // Just ignore the line for now, since we cannot know how to handle it... 302 if (!mEmittedAgentWarning) { 303 Log.w(LOG_TAG, "AGENT in vCard 3.0 is not supported yet. Ignore it"); 304 mEmittedAgentWarning = true; 305 } 306 } 307 308 /** 309 * vCard 3.0 does not require two CRLF at the last of BASE64 data. 310 * It only requires that data should be MIME-encoded. 311 */ 312 @Override 313 protected String getBase64(final String firstString) 314 throws IOException, VCardException { 315 final StringBuilder builder = new StringBuilder(); 316 builder.append(firstString); 317 318 while (true) { 319 final String line = getLine(); 320 if (line == null) { 321 throw new VCardException("File ended during parsing BASE64 binary"); 322 } 323 if (line.length() == 0) { 324 break; 325 } else if (!line.startsWith(" ") && !line.startsWith("\t")) { 326 mPreviousLine = line; 327 break; 328 } 329 builder.append(line); 330 } 331 332 return builder.toString(); 333 } 334 335 /** 336 * ESCAPED-CHAR = "\\" / "\;" / "\," / "\n" / "\N") 337 * ; \\ encodes \, \n or \N encodes newline 338 * ; \; encodes ;, \, encodes , 339 * 340 * Note: Apple escapes ':' into '\:' while does not escape '\' 341 */ 342 @Override 343 protected String maybeUnescapeText(final String text) { 344 return unescapeText(text); 345 } 346 347 public static String unescapeText(final String text) { 348 StringBuilder builder = new StringBuilder(); 349 final int length = text.length(); 350 for (int i = 0; i < length; i++) { 351 char ch = text.charAt(i); 352 if (ch == '\\' && i < length - 1) { 353 final char next_ch = text.charAt(++i); 354 if (next_ch == 'n' || next_ch == 'N') { 355 builder.append("\n"); 356 } else { 357 builder.append(next_ch); 358 } 359 } else { 360 builder.append(ch); 361 } 362 } 363 return builder.toString(); 364 } 365 366 @Override 367 protected String maybeUnescapeCharacter(final char ch) { 368 return unescapeCharacter(ch); 369 } 370 371 public static String unescapeCharacter(final char ch) { 372 if (ch == 'n' || ch == 'N') { 373 return "\n"; 374 } else { 375 return String.valueOf(ch); 376 } 377 } 378 379 @Override 380 protected Set<String> getKnownPropertyNameSet() { 381 return VCardParser_V30.sKnownPropertyNameSet; 382 } 383} 384