1/*
2 * Copyright (C) 2009 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 com.android.vcard.exception.VCardException;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.util.Arrays;
23import java.util.Collections;
24import java.util.HashSet;
25import java.util.Set;
26
27/**
28 * <p>
29 * vCard parser for vCard 3.0. See RFC 2426 for more detail.
30 * </p>
31 * <p>
32 * This parser allows vCard format which is not allowed in the RFC, since
33 * we have seen several vCard 3.0 files which don't comply with it.
34 * </p>
35 * <p>
36 * e.g. vCard 3.0 does not allow "CHARSET" attribute, but some actual files
37 * have it and they uses non UTF-8 charsets. UTF-8 is recommended in RFC 2426,
38 * but it is not a must. We silently allow "CHARSET".
39 * </p>
40 */
41public class VCardParser_V30 extends VCardParser {
42    /* package */ static final Set<String> sKnownPropertyNameSet =
43            Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
44                    "BEGIN", "END", "LOGO", "PHOTO", "LABEL", "FN", "TITLE", "SOUND",
45                    "VERSION", "TEL", "EMAIL", "TZ", "GEO", "NOTE", "URL",
46                    "BDAY", "ROLE", "REV", "UID", "KEY", "MAILER", // 2.1
47                    "NAME", "PROFILE", "SOURCE", "NICKNAME", "CLASS",
48                    "SORT-STRING", "CATEGORIES", "PRODID",  // 3.0
49                    "IMPP"))); // RFC 4770
50
51    /**
52     * <p>
53     * A unmodifiable Set storing the values for the type "ENCODING", available in the vCard 3.0.
54     * </p>
55     * <p>
56     * Though vCard 2.1 specification does not allow "7BIT" or "BASE64", we allow them for safety.
57     * </p>
58     * <p>
59     * "QUOTED-PRINTABLE" is not allowed in vCard 3.0 and not in this parser either,
60     * because the encoding ambiguates how the vCard file to be parsed.
61     * </p>
62     */
63    /* package */ static final Set<String> sAcceptableEncoding =
64            Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
65                    VCardConstants.PARAM_ENCODING_7BIT,
66                    VCardConstants.PARAM_ENCODING_8BIT,
67                    VCardConstants.PARAM_ENCODING_BASE64,
68                    VCardConstants.PARAM_ENCODING_B)));
69
70    private final VCardParserImpl_V30 mVCardParserImpl;
71
72    public VCardParser_V30() {
73        mVCardParserImpl = new VCardParserImpl_V30();
74    }
75
76    public VCardParser_V30(int vcardType) {
77        mVCardParserImpl = new VCardParserImpl_V30(vcardType);
78    }
79
80    @Override
81    public void addInterpreter(VCardInterpreter interpreter) {
82        mVCardParserImpl.addInterpreter(interpreter);
83    }
84
85    @Override
86    public void parse(InputStream is) throws IOException, VCardException {
87        mVCardParserImpl.parse(is);
88    }
89
90    @Override
91    public void parseOne(InputStream is) throws IOException, VCardException {
92        mVCardParserImpl.parseOne(is);
93    }
94
95    @Override
96    public void cancel() {
97        mVCardParserImpl.cancel();
98    }
99}
100