VNodeBuilder.java revision 1de396f6df89363169d3a2e61a61fa98d12c1ef8
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.tests.testutils;
17
18import com.android.vcard.VCardConfig;
19import com.android.vcard.VCardInterpreter;
20import com.android.vcard.VCardProperty;
21import com.android.vcard.VCardUtils;
22
23import android.content.ContentValues;
24import android.util.Base64;
25import android.util.CharsetUtils;
26import android.util.Log;
27
28import java.io.UnsupportedEncodingException;
29import java.nio.ByteBuffer;
30import java.nio.charset.Charset;
31import java.util.ArrayList;
32import java.util.Collection;
33import java.util.List;
34import java.util.Map;
35
36/**
37 * <p>
38 * The class storing the parse result to custom datastruct:
39 * {@link VNode}, and {@link PropertyNode}.
40 * Maybe several vcard instance, so use vNodeList to store.
41 * </p>
42 * <p>
43 * This is called VNode, not VCardNode, since it was used for expressing vCalendar (iCal).
44 * </p>
45 */
46public class VNodeBuilder implements VCardInterpreter {
47    private static String LOG_TAG = "VNodeBuilder";
48
49    private List<VNode> mVNodeList = new ArrayList<VNode>();
50    private VNode mCurrentVNode;
51
52    /**
53     * The charset using which VParser parses the text.
54     */
55    private String mSourceCharset;
56
57    /**
58     * The charset with which byte array is encoded to String.
59     */
60    private String mTargetCharset;
61
62    private boolean mStrictLineBreakParsing;
63
64    public VNodeBuilder() {
65        this(VCardConfig.DEFAULT_IMPORT_CHARSET, false);
66    }
67
68    public VNodeBuilder(String targetCharset, boolean strictLineBreakParsing) {
69        mSourceCharset = VCardConfig.DEFAULT_INTERMEDIATE_CHARSET;
70        if (targetCharset != null) {
71            mTargetCharset = targetCharset;
72        } else {
73            mTargetCharset = VCardConfig.DEFAULT_IMPORT_CHARSET;
74        }
75        mStrictLineBreakParsing = strictLineBreakParsing;
76    }
77
78    @Override
79    public void onVCardStarted() {
80    }
81
82    @Override
83    public void onVCardEnded() {
84    }
85
86    @Override
87    public void onEntryStarted() {
88        mCurrentVNode = new VNode();
89        mVNodeList.add(mCurrentVNode);
90    }
91
92    @Override
93    public void onEntryEnded() {
94        int lastIndex = mVNodeList.size() - 1;
95        mVNodeList.remove(lastIndex--);
96        mCurrentVNode = lastIndex >= 0 ? mVNodeList.get(lastIndex) : null;
97    }
98
99    @Override
100    public void onPropertyCreated(VCardProperty property) {
101        // TODO: remove PropertyNode.
102        PropertyNode propNode = new PropertyNode();
103        propNode.propName = property.getName();
104        List<String> groupList = property.getGroupList();
105        if (groupList != null) {
106            propNode.propGroupSet.addAll(groupList);
107        }
108        Map<String, Collection<String>> propertyParameterMap = property.getParameterMap();
109        for (String paramType : propertyParameterMap.keySet()) {
110            Collection<String> paramValueList = propertyParameterMap.get(paramType);
111            if (paramType.equalsIgnoreCase("TYPE")) {
112                propNode.paramMap_TYPE.addAll(paramValueList);
113            } else {
114                for (String paramValue : paramValueList) {
115                    propNode.paramMap.put(paramType, paramValue);
116                }
117            }
118        }
119
120        // TODO: just redundant
121
122        if (property.getRawValue() == null) {
123            propNode.propValue_bytes = null;
124            propNode.propValue_vector.clear();
125            propNode.propValue_vector.add("");
126            propNode.propValue = "";
127            return;
128        }
129
130        final List<String> values = property.getValueList();
131        if (values == null || values.size() == 0) {
132            propNode.propValue_vector.clear();
133            propNode.propValue_vector.add("");
134            propNode.propValue = "";
135        } else {
136            propNode.propValue_vector.addAll(values);
137            propNode.propValue = listToString(propNode.propValue_vector);
138        }
139        propNode.propValue_bytes = property.getByteValue();
140
141        mCurrentVNode.propList.add(propNode);
142    }
143
144    private String listToString(List<String> list){
145        int size = list.size();
146        if (size > 1) {
147            StringBuilder typeListB = new StringBuilder();
148            for (String type : list) {
149                typeListB.append(type).append(";");
150            }
151            int len = typeListB.length();
152            if (len > 0 && typeListB.charAt(len - 1) == ';') {
153                return typeListB.substring(0, len - 1);
154            }
155            return typeListB.toString();
156        } else if (size == 1) {
157            return list.get(0);
158        } else {
159            return "";
160        }
161    }
162
163    public String getResult(){
164        throw new RuntimeException("Not supported");
165    }
166
167    public List<VNode> getVNodeList() {
168        return mVNodeList;
169    }
170
171    public VNode getCurrentVNode() {
172        return mCurrentVNode;
173    }
174}
175