Home | History | Annotate | Download | only in adapter
      1 /* Copyright (C) 2011 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 java.io.IOException;
     19 import java.io.InputStream;
     20 import java.io.OutputStream;
     21 
     22 /**
     23  * Parse the result of an ItemOperations command; we use this to load attachments in EAS 14.0
     24  */
     25 public class ItemOperationsParser extends Parser {
     26     private final AttachmentLoader mAttachmentLoader;
     27     private int mStatusCode = 0;
     28     private final OutputStream mAttachmentOutputStream;
     29     private final int mAttachmentSize;
     30 
     31     public ItemOperationsParser(AttachmentLoader loader, InputStream in, OutputStream out, int size)
     32             throws IOException {
     33         super(in);
     34         mAttachmentLoader = loader;
     35         mAttachmentOutputStream = out;
     36         mAttachmentSize = size;
     37     }
     38 
     39     public int getStatusCode() {
     40         return mStatusCode;
     41     }
     42 
     43     private void parseProperties() throws IOException {
     44         while (nextTag(Tags.ITEMS_PROPERTIES) != END) {
     45             if (tag == Tags.ITEMS_DATA) {
     46                 // Wrap the input stream in our custom base64 input stream
     47                 Base64InputStream bis = new Base64InputStream(getInput());
     48                 // Read the attachment
     49                 mAttachmentLoader.readChunked(bis, mAttachmentOutputStream, mAttachmentSize);
     50             } else {
     51                 skipTag();
     52             }
     53         }
     54     }
     55 
     56     private void parseFetch() throws IOException {
     57         while (nextTag(Tags.ITEMS_FETCH) != END) {
     58             if (tag == Tags.ITEMS_PROPERTIES) {
     59                 parseProperties();
     60             } else {
     61                 skipTag();
     62             }
     63         }
     64     }
     65 
     66     private void parseResponse() throws IOException {
     67         while (nextTag(Tags.ITEMS_RESPONSE) != END) {
     68             if (tag == Tags.ITEMS_FETCH) {
     69                 parseFetch();
     70             } else {
     71                 skipTag();
     72             }
     73         }
     74     }
     75 
     76     @Override
     77     public boolean parse() throws IOException {
     78         boolean res = false;
     79         if (nextTag(START_DOCUMENT) != Tags.ITEMS_ITEMS) {
     80             throw new IOException();
     81         }
     82         while (nextTag(START_DOCUMENT) != END_DOCUMENT) {
     83             if (tag == Tags.ITEMS_STATUS) {
     84                 // Save the status code
     85                 mStatusCode = getValueInt();
     86             } else if (tag == Tags.ITEMS_RESPONSE) {
     87                 parseResponse();
     88             } else {
     89                 skipTag();
     90             }
     91         }
     92         return res;
     93     }
     94 }
     95