1 /* Copyright (C) 2010 The Android Open Source Project. 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.android.exchange.adapter; 17 18 import com.android.exchange.EasSyncService; 19 import com.android.exchange.provider.GalResult; 20 import com.android.exchange.provider.GalResult.GalData; 21 22 import java.io.IOException; 23 import java.io.InputStream; 24 25 /** 26 * Parse the result of a GAL command. 27 */ 28 public class GalParser extends Parser { 29 private EasSyncService mService; 30 GalResult mGalResult = new GalResult(); 31 32 public GalParser(InputStream in, EasSyncService service) throws IOException { 33 super(in); 34 mService = service; 35 } 36 37 public GalResult getGalResult() { 38 return mGalResult; 39 } 40 41 @Override 42 public boolean parse() throws IOException { 43 if (nextTag(START_DOCUMENT) != Tags.SEARCH_SEARCH) { 44 throw new IOException(); 45 } 46 while (nextTag(START_DOCUMENT) != END_DOCUMENT) { 47 if (tag == Tags.SEARCH_RESPONSE) { 48 parseResponse(mGalResult); 49 } else { 50 skipTag(); 51 } 52 } 53 return mGalResult.total > 0; 54 } 55 56 public void parseProperties(GalResult galResult) throws IOException { 57 GalData galData = new GalData(); 58 while (nextTag(Tags.SEARCH_STORE) != END) { 59 switch(tag) { 60 // Display name and email address use both legacy and new code for galData 61 case Tags.GAL_DISPLAY_NAME: 62 String displayName = getValue(); 63 galData.put(GalData.DISPLAY_NAME, displayName); 64 galData.displayName = displayName; 65 break; 66 case Tags.GAL_EMAIL_ADDRESS: 67 String emailAddress = getValue(); 68 galData.put(GalData.EMAIL_ADDRESS, emailAddress); 69 galData.emailAddress = emailAddress; 70 break; 71 case Tags.GAL_PHONE: 72 galData.put(GalData.WORK_PHONE, getValue()); 73 break; 74 case Tags.GAL_OFFICE: 75 galData.put(GalData.OFFICE, getValue()); 76 break; 77 case Tags.GAL_TITLE: 78 galData.put(GalData.TITLE, getValue()); 79 break; 80 case Tags.GAL_COMPANY: 81 galData.put(GalData.COMPANY, getValue()); 82 break; 83 case Tags.GAL_ALIAS: 84 galData.put(GalData.ALIAS, getValue()); 85 break; 86 case Tags.GAL_FIRST_NAME: 87 galData.put(GalData.FIRST_NAME, getValue()); 88 break; 89 case Tags.GAL_LAST_NAME: 90 galData.put(GalData.LAST_NAME, getValue()); 91 break; 92 case Tags.GAL_HOME_PHONE: 93 galData.put(GalData.HOME_PHONE, getValue()); 94 break; 95 case Tags.GAL_MOBILE_PHONE: 96 galData.put(GalData.MOBILE_PHONE, getValue()); 97 break; 98 default: 99 skipTag(); 100 } 101 } 102 galResult.addGalData(galData); 103 } 104 105 public void parseResult(GalResult galResult) throws IOException { 106 while (nextTag(Tags.SEARCH_STORE) != END) { 107 if (tag == Tags.SEARCH_PROPERTIES) { 108 parseProperties(galResult); 109 } else { 110 skipTag(); 111 } 112 } 113 } 114 115 public void parseResponse(GalResult galResult) throws IOException { 116 while (nextTag(Tags.SEARCH_RESPONSE) != END) { 117 if (tag == Tags.SEARCH_STORE) { 118 parseStore(galResult); 119 } else { 120 skipTag(); 121 } 122 } 123 } 124 125 public void parseStore(GalResult galResult) throws IOException { 126 while (nextTag(Tags.SEARCH_STORE) != END) { 127 if (tag == Tags.SEARCH_RESULT) { 128 parseResult(galResult); 129 } else if (tag == Tags.SEARCH_RANGE) { 130 // Retrieve value, even if we're not using it for debug logging 131 String range = getValue(); 132 if (EasSyncService.DEBUG_GAL_SERVICE) { 133 mService.userLog("GAL result range: " + range); 134 } 135 } else if (tag == Tags.SEARCH_TOTAL) { 136 galResult.total = getValueInt(); 137 } else { 138 skipTag(); 139 } 140 } 141 } 142 } 143 144