VCardParserImpl_V30.java revision be378d5b188f51cf717e5309e3c39180e85833a8
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 = "VCardParserImpl_V30"; 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 @Override 154 protected void readEndVCard(boolean useCache, boolean allowGarbage) 155 throws IOException, VCardException { 156 // TODO: vCard 3.0 supports group. 157 super.readEndVCard(useCache, allowGarbage); 158 } 159 160 /** 161 * vCard 3.0 allows iana-token as paramType, while vCard 2.1 does not. 162 */ 163 @Override 164 protected void handleParams(final String params) throws VCardException { 165 try { 166 super.handleParams(params); 167 } catch (VCardException e) { 168 // maybe IANA type 169 String[] strArray = params.split("=", 2); 170 if (strArray.length == 2) { 171 handleAnyParam(strArray[0], strArray[1]); 172 } else { 173 // Must not come here in the current implementation. 174 throw new VCardException( 175 "Unknown params value: " + params); 176 } 177 } 178 } 179 180 @Override 181 protected void handleAnyParam(final String paramName, final String paramValue) { 182 super.handleAnyParam(paramName, paramValue); 183 } 184 185 @Override 186 protected void handleParamWithoutName(final String paramValue) throws VCardException { 187 super.handleParamWithoutName(paramValue); 188 } 189 190 /* 191 * vCard 3.0 defines 192 * 193 * param = param-name "=" param-value *("," param-value) 194 * param-name = iana-token / x-name 195 * param-value = ptext / quoted-string 196 * quoted-string = DQUOTE QSAFE-CHAR DQUOTE 197 */ 198 @Override 199 protected void handleType(final String ptypevalues) { 200 String[] ptypeArray = ptypevalues.split(","); 201 mInterpreter.propertyParamType("TYPE"); 202 for (String value : ptypeArray) { 203 int length = value.length(); 204 if (length >= 2 && value.startsWith("\"") && value.endsWith("\"")) { 205 mInterpreter.propertyParamValue(value.substring(1, value.length() - 1)); 206 } else { 207 mInterpreter.propertyParamValue(value); 208 } 209 } 210 } 211 212 @Override 213 protected void handleAgent(final String propertyValue) { 214 // The way how vCard 3.0 supports "AGENT" is completely different from vCard 2.1. 215 // 216 // e.g. 217 // AGENT:BEGIN:VCARD\nFN:Joe Friday\nTEL:+1-919-555-7878\n 218 // TITLE:Area Administrator\, Assistant\n EMAIL\;TYPE=INTERN\n 219 // ET:jfriday@host.com\nEND:VCARD\n 220 // 221 // TODO: fix this. 222 // 223 // issue: 224 // vCard 3.0 also allows this as an example. 225 // 226 // AGENT;VALUE=uri: 227 // CID:JQPUBLIC.part3.960129T083020.xyzMail@host3.com 228 // 229 // This is not vCard. Should we support this? 230 // 231 // Just ignore the line for now, since we cannot know how to handle it... 232 if (!mEmittedAgentWarning) { 233 Log.w(LOG_TAG, "AGENT in vCard 3.0 is not supported yet. Ignore it"); 234 mEmittedAgentWarning = true; 235 } 236 } 237 238 /** 239 * vCard 3.0 does not require two CRLF at the last of BASE64 data. 240 * It only requires that data should be MIME-encoded. 241 */ 242 @Override 243 protected String getBase64(final String firstString) 244 throws IOException, VCardException { 245 final StringBuilder builder = new StringBuilder(); 246 builder.append(firstString); 247 248 while (true) { 249 final String line = getLine(); 250 if (line == null) { 251 throw new VCardException("File ended during parsing BASE64 binary"); 252 } 253 if (line.length() == 0) { 254 break; 255 } else if (!line.startsWith(" ") && !line.startsWith("\t")) { 256 mPreviousLine = line; 257 break; 258 } 259 builder.append(line); 260 } 261 262 return builder.toString(); 263 } 264 265 /** 266 * ESCAPED-CHAR = "\\" / "\;" / "\," / "\n" / "\N") 267 * ; \\ encodes \, \n or \N encodes newline 268 * ; \; encodes ;, \, encodes , 269 * 270 * Note: Apple escapes ':' into '\:' while does not escape '\' 271 */ 272 @Override 273 protected String maybeUnescapeText(final String text) { 274 return unescapeText(text); 275 } 276 277 public static String unescapeText(final String text) { 278 StringBuilder builder = new StringBuilder(); 279 final int length = text.length(); 280 for (int i = 0; i < length; i++) { 281 char ch = text.charAt(i); 282 if (ch == '\\' && i < length - 1) { 283 final char next_ch = text.charAt(++i); 284 if (next_ch == 'n' || next_ch == 'N') { 285 builder.append("\n"); 286 } else { 287 builder.append(next_ch); 288 } 289 } else { 290 builder.append(ch); 291 } 292 } 293 return builder.toString(); 294 } 295 296 @Override 297 protected String maybeEscapeCharacter(final char ch) { 298 return escapeCharacter(ch); 299 } 300 301 public static String escapeCharacter(final char ch) { 302 if (ch == 'n' || ch == 'N') { 303 return "\n"; 304 } else { 305 return String.valueOf(ch); 306 } 307 } 308 309 @Override 310 protected Set<String> getKnownPropertyNameSet() { 311 return VCardParser_V30.sKnownPropertyNameSet; 312 } 313} 314