Home | History | Annotate | Download | only in adapter
      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.provider.GalResult;
     19 import com.android.exchange.provider.GalResult.GalData;
     20 import com.android.mail.utils.LogUtils;
     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     // TODO: make this controllable through the debug screen.
     30     private static final boolean DEBUG_GAL_SERVICE = false;
     31 
     32     GalResult mGalResult = new GalResult();
     33 
     34     public GalParser(final InputStream in) throws IOException {
     35         super(in);
     36     }
     37 
     38     public GalResult getGalResult() {
     39         return mGalResult;
     40     }
     41 
     42     @Override
     43     public boolean parse() throws IOException {
     44         if (nextTag(START_DOCUMENT) != Tags.SEARCH_SEARCH) {
     45             throw new IOException();
     46         }
     47         while (nextTag(START_DOCUMENT) != END_DOCUMENT) {
     48             if (tag == Tags.SEARCH_RESPONSE) {
     49                 parseResponse(mGalResult);
     50             } else {
     51                 skipTag();
     52             }
     53          }
     54         // TODO: IO Exception if we can't get the result to parse and a false should be returned
     55         // here if we can't parse the input stream in the case where the schema may have changed.
     56         // To implement this, we'll have to dig a bit deeper into the parsing code.
     57         return true;
     58      }
     59 
     60     private void parseProperties(final GalResult galResult) throws IOException {
     61         final GalData galData = new GalData();
     62         while (nextTag(Tags.SEARCH_PROPERTIES) != END) {
     63             switch(tag) {
     64                 // Display name and email address use both legacy and new code for galData
     65                 case Tags.GAL_DISPLAY_NAME:
     66                     String displayName = getValue();
     67                     galData.put(GalData.DISPLAY_NAME, displayName);
     68                     galData.displayName = displayName;
     69                     break;
     70                 case Tags.GAL_EMAIL_ADDRESS:
     71                     String emailAddress = getValue();
     72                     galData.put(GalData.EMAIL_ADDRESS, emailAddress);
     73                     galData.emailAddress = emailAddress;
     74                     break;
     75                 case Tags.GAL_PHONE:
     76                     galData.put(GalData.WORK_PHONE, getValue());
     77                     break;
     78                 case Tags.GAL_OFFICE:
     79                     galData.put(GalData.OFFICE, getValue());
     80                     break;
     81                 case Tags.GAL_TITLE:
     82                     galData.put(GalData.TITLE, getValue());
     83                     break;
     84                 case Tags.GAL_COMPANY:
     85                     galData.put(GalData.COMPANY, getValue());
     86                     break;
     87                 case Tags.GAL_ALIAS:
     88                     galData.put(GalData.ALIAS, getValue());
     89                     break;
     90                 case Tags.GAL_FIRST_NAME:
     91                     galData.put(GalData.FIRST_NAME, getValue());
     92                     break;
     93                 case Tags.GAL_LAST_NAME:
     94                     galData.put(GalData.LAST_NAME, getValue());
     95                     break;
     96                 case Tags.GAL_HOME_PHONE:
     97                     galData.put(GalData.HOME_PHONE, getValue());
     98                     break;
     99                 case Tags.GAL_MOBILE_PHONE:
    100                     galData.put(GalData.MOBILE_PHONE, getValue());
    101                     break;
    102                 default:
    103                     skipTag();
    104             }
    105         }
    106         galResult.addGalData(galData);
    107     }
    108 
    109      private void parseResult(final GalResult galResult) throws IOException {
    110          while (nextTag(Tags.SEARCH_RESULT) != END) {
    111              if (tag == Tags.SEARCH_PROPERTIES) {
    112                  parseProperties(galResult);
    113              } else {
    114                  skipTag();
    115              }
    116          }
    117      }
    118 
    119      private void parseResponse(final GalResult galResult) throws IOException {
    120          while (nextTag(Tags.SEARCH_RESPONSE) != END) {
    121              if (tag == Tags.SEARCH_STORE) {
    122                  parseStore(galResult);
    123              } else {
    124                  skipTag();
    125              }
    126          }
    127      }
    128 
    129      private void parseStore(final GalResult galResult) throws IOException {
    130          while (nextTag(Tags.SEARCH_STORE) != END) {
    131              if (tag == Tags.SEARCH_RESULT) {
    132                  parseResult(galResult);
    133              } else if (tag == Tags.SEARCH_RANGE) {
    134                  // Retrieve value, even if we're not using it for debug logging
    135                  String range = getValue();
    136                  if (DEBUG_GAL_SERVICE) {
    137                      LogUtils.d(LogUtils.TAG, "GAL result range: " + range);
    138                  }
    139              } else if (tag == Tags.SEARCH_TOTAL) {
    140                  galResult.total = getValueInt();
    141              } else {
    142                  skipTag();
    143              }
    144          }
    145      }
    146 }
    147 
    148