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.EasSyncService;
     19 import com.android.exchange.provider.GalResult;
     20 
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 
     24 /**
     25  * Parse the result of a GAL command.
     26  */
     27 public class GalParser extends Parser {
     28     private EasSyncService mService;
     29     GalResult mGalResult = new GalResult();
     30 
     31     public GalParser(InputStream in, EasSyncService service) throws IOException {
     32         super(in);
     33         mService = service;
     34     }
     35 
     36     public GalResult getGalResult() {
     37         return mGalResult;
     38     }
     39 
     40     @Override
     41     public boolean parse() throws IOException {
     42         if (nextTag(START_DOCUMENT) != Tags.SEARCH_SEARCH) {
     43             throw new IOException();
     44         }
     45         while (nextTag(START_DOCUMENT) != END_DOCUMENT) {
     46             if (tag == Tags.SEARCH_RESPONSE) {
     47                 parseResponse(mGalResult);
     48             } else {
     49                 skipTag();
     50             }
     51          }
     52          return mGalResult.total > 0;
     53      }
     54 
     55      public void parseProperties(GalResult galResult) throws IOException {
     56          String displayName = null;
     57          String email = null;
     58          while (nextTag(Tags.SEARCH_STORE) != END) {
     59              if (tag == Tags.GAL_DISPLAY_NAME) {
     60                  displayName = getValue();
     61              } else if (tag == Tags.GAL_EMAIL_ADDRESS) {
     62                  email = getValue();
     63              } else {
     64                  skipTag();
     65              }
     66          }
     67          if (displayName != null && email != null) {
     68              galResult.addGalData(0, displayName, email);
     69          }
     70      }
     71 
     72      public void parseResult(GalResult galResult) throws IOException {
     73          while (nextTag(Tags.SEARCH_STORE) != END) {
     74              if (tag == Tags.SEARCH_PROPERTIES) {
     75                  parseProperties(galResult);
     76              } else {
     77                  skipTag();
     78              }
     79          }
     80      }
     81 
     82      public void parseResponse(GalResult galResult) throws IOException {
     83          while (nextTag(Tags.SEARCH_RESPONSE) != END) {
     84              if (tag == Tags.SEARCH_STORE) {
     85                  parseStore(galResult);
     86              } else {
     87                  skipTag();
     88              }
     89          }
     90      }
     91 
     92      public void parseStore(GalResult galResult) throws IOException {
     93          while (nextTag(Tags.SEARCH_STORE) != END) {
     94              if (tag == Tags.SEARCH_RESULT) {
     95                  parseResult(galResult);
     96              } else if (tag == Tags.SEARCH_RANGE) {
     97                  // Retrieve value, even if we're not using it for debug logging
     98                  String range = getValue();
     99                  if (EasSyncService.DEBUG_GAL_SERVICE) {
    100                      mService.userLog("GAL result range: " + range);
    101                  }
    102              } else if (tag == Tags.SEARCH_TOTAL) {
    103                  galResult.total = getValueInt();
    104              } else {
    105                  skipTag();
    106              }
    107          }
    108      }
    109 }
    110 
    111