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