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 android.accounts.Account;
19import android.text.TextUtils;
20import android.util.Base64;
21import android.util.Log;
22
23import java.util.ArrayList;
24import java.util.Collection;
25import java.util.List;
26
27/**
28 * <p>
29 * The {@link VCardInterpreter} implementation which enables {@link VCardEntryHandler} objects
30 * to easily handle each vCard entry.
31 * </p>
32 * <p>
33 * This class understand details inside vCard and translates it to {@link VCardEntry}.
34 * Then the class throw it to {@link VCardEntryHandler} registered via
35 * {@link #addEntryHandler(VCardEntryHandler)}, so that all those registered objects
36 * are able to handle the {@link VCardEntry} object.
37 * </p>
38 * <p>
39 * If you want to know the detail inside vCard, it would be better to implement
40 * {@link VCardInterpreter} directly, instead of relying on this class and
41 * {@link VCardEntry} created by the object.
42 * </p>
43 */
44public class VCardEntryConstructor implements VCardInterpreter {
45    private static String LOG_TAG = VCardConstants.LOG_TAG;
46
47    /**
48     * Represents current stack of VCardEntry. Used to support nested vCard (vCard 2.1).
49     */
50    private final List<VCardEntry> mEntryStack = new ArrayList<VCardEntry>();
51    private VCardEntry mCurrentEntry;
52
53    private final int mVCardType;
54    private final Account mAccount;
55
56    private final List<VCardEntryHandler> mEntryHandlers = new ArrayList<VCardEntryHandler>();
57
58    public VCardEntryConstructor() {
59        this(VCardConfig.VCARD_TYPE_V21_GENERIC, null, null);
60    }
61
62    public VCardEntryConstructor(final int vcardType) {
63        this(vcardType, null, null);
64    }
65
66    public VCardEntryConstructor(final int vcardType, final Account account) {
67        this(vcardType, account, null);
68    }
69
70    /**
71     * @deprecated targetCharset is not used anymore.
72     * Use {@link #VCardEntryConstructor(int, Account)}
73     */
74    @Deprecated
75    public VCardEntryConstructor(final int vcardType, final Account account,
76            String targetCharset) {
77        mVCardType = vcardType;
78        mAccount = account;
79    }
80
81    public void addEntryHandler(VCardEntryHandler entryHandler) {
82        mEntryHandlers.add(entryHandler);
83    }
84
85    @Override
86    public void onVCardStarted() {
87        for (VCardEntryHandler entryHandler : mEntryHandlers) {
88            entryHandler.onStart();
89        }
90    }
91
92    @Override
93    public void onVCardEnded() {
94        for (VCardEntryHandler entryHandler : mEntryHandlers) {
95            entryHandler.onEnd();
96        }
97    }
98
99    public void clear() {
100        mCurrentEntry = null;
101        mEntryStack.clear();
102    }
103
104    @Override
105    public void onEntryStarted() {
106        mCurrentEntry = new VCardEntry(mVCardType, mAccount);
107        mEntryStack.add(mCurrentEntry);
108    }
109
110    @Override
111    public void onEntryEnded() {
112        mCurrentEntry.consolidateFields();
113        for (VCardEntryHandler entryHandler : mEntryHandlers) {
114            entryHandler.onEntryCreated(mCurrentEntry);
115        }
116
117        final int size = mEntryStack.size();
118        if (size > 1) {
119            VCardEntry parent = mEntryStack.get(size - 2);
120            parent.addChild(mCurrentEntry);
121            mCurrentEntry = parent;
122        } else {
123            mCurrentEntry = null;
124        }
125        mEntryStack.remove(size - 1);
126    }
127
128    @Override
129    public void onPropertyCreated(VCardProperty property) {
130        mCurrentEntry.addProperty(property);
131    }
132}
133