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 2.1. See the specification for more detail about the spec itself.
30 * </p>
31 * <p>
32 * The spec is written in 1996, and currently various types of "vCard 2.1" exist.
33 * To handle real the world vCard formats appropriately and effectively, this class does not
34 * obey with strict vCard 2.1.
35 * In stead, not only vCard spec but also real world vCard is considered.
36 * </p>
37 * e.g. A lot of devices and softwares let vCard importer/exporter to use
38 * the PNG format to determine the type of image, while it is not allowed in
39 * the original specification. As of 2010, we can see even the FLV format
40 * (possible in Japanese mobile phones).
41 * </p>
42 */
43public final class VCardParser_V21 extends VCardParser {
44    /**
45     * A unmodifiable Set storing the property names available in the vCard 2.1 specification.
46     */
47    /* package */ static final Set<String> sKnownPropertyNameSet =
48            Collections.unmodifiableSet(new HashSet<String>(
49                    Arrays.asList("BEGIN", "END", "LOGO", "PHOTO", "LABEL", "FN", "TITLE", "SOUND",
50                            "VERSION", "TEL", "EMAIL", "TZ", "GEO", "NOTE", "URL",
51                            "BDAY", "ROLE", "REV", "UID", "KEY", "MAILER")));
52
53    /**
54     * A unmodifiable Set storing the types known in vCard 2.1.
55     */
56    /* package */ static final Set<String> sKnownTypeSet =
57            Collections.unmodifiableSet(new HashSet<String>(
58                    Arrays.asList("DOM", "INTL", "POSTAL", "PARCEL", "HOME", "WORK",
59                            "PREF", "VOICE", "FAX", "MSG", "CELL", "PAGER", "BBS",
60                            "MODEM", "CAR", "ISDN", "VIDEO", "AOL", "APPLELINK",
61                            "ATTMAIL", "CIS", "EWORLD", "INTERNET", "IBMMAIL",
62                            "MCIMAIL", "POWERSHARE", "PRODIGY", "TLX", "X400", "GIF",
63                            "CGM", "WMF", "BMP", "MET", "PMB", "DIB", "PICT", "TIFF",
64                            "PDF", "PS", "JPEG", "QTIME", "MPEG", "MPEG2", "AVI",
65                            "WAVE", "AIFF", "PCM", "X509", "PGP")));
66
67    /**
68     * A unmodifiable Set storing the values for the type "VALUE", available in the vCard 2.1.
69     */
70    /* package */ static final Set<String> sKnownValueSet =
71            Collections.unmodifiableSet(new HashSet<String>(
72                    Arrays.asList("INLINE", "URL", "CONTENT-ID", "CID")));
73
74    /**
75     * <p>
76     * A unmodifiable Set storing the values for the type "ENCODING", available in the vCard 2.1.
77     * </p>
78     * <p>
79     * Though vCard 2.1 specification does not allow "B" encoding, some data may have it.
80     * We allow it for safety.
81     * </p>
82     */
83    /* package */ static final Set<String> sAvailableEncoding =
84        Collections.unmodifiableSet(new HashSet<String>(
85                Arrays.asList(VCardConstants.PARAM_ENCODING_7BIT,
86                        VCardConstants.PARAM_ENCODING_8BIT,
87                        VCardConstants.PARAM_ENCODING_QP,
88                        VCardConstants.PARAM_ENCODING_BASE64,
89                        VCardConstants.PARAM_ENCODING_B)));
90
91    private final VCardParserImpl_V21 mVCardParserImpl;
92
93    public VCardParser_V21() {
94        mVCardParserImpl = new VCardParserImpl_V21();
95    }
96
97    public VCardParser_V21(int vcardType) {
98        mVCardParserImpl = new VCardParserImpl_V21(vcardType);
99    }
100
101    @Override
102    public void addInterpreter(VCardInterpreter interpreter) {
103        mVCardParserImpl.addInterpreter(interpreter);
104    }
105
106    @Override
107    public void parse(InputStream is) throws IOException, VCardException {
108        mVCardParserImpl.parse(is);
109    }
110
111    @Override
112    public void parseOne(InputStream is) throws IOException, VCardException {
113        mVCardParserImpl.parseOne(is);
114    }
115
116    @Override
117    public void cancel() {
118        mVCardParserImpl.cancel();
119    }
120}
121