Home | History | Annotate | Download | only in imps
      1 /*
      2  * Copyright (C) 2007 Esmertec AG.
      3  * Copyright (C) 2007 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.im.imps;
     19 
     20 import java.util.Stack;
     21 
     22 import org.xml.sax.Attributes;
     23 import org.xml.sax.helpers.DefaultHandler;
     24 
     25 import com.android.im.imps.Primitive.TransactionMode;
     26 
     27 public final class PrimitiveContentHandler extends DefaultHandler {
     28     private Primitive mPrimitive;
     29     private String mCurTagName;
     30     private boolean mIsTransContent;
     31     private Stack<PrimitiveElement> mContentElementsStack;
     32 
     33     public PrimitiveContentHandler() {
     34         mPrimitive = new Primitive();
     35         mContentElementsStack = new Stack<PrimitiveElement>();
     36     }
     37 
     38     public void reset() {
     39         mPrimitive = new Primitive();
     40         mContentElementsStack.clear();
     41 
     42         mIsTransContent = false;
     43     }
     44 
     45     public Primitive getPrimitive() {
     46         return mPrimitive;
     47     }
     48 
     49     @Override
     50     public void startElement(String uri, String localName, String qName,
     51             Attributes attributes) {
     52         if (mIsTransContent) {
     53             if (mContentElementsStack.empty()) {
     54                 mPrimitive.setContentElement(localName);
     55                 mContentElementsStack.push(mPrimitive.getContentElement());
     56             } else {
     57                 PrimitiveElement parentPrimitive = mContentElementsStack.peek();
     58                 PrimitiveElement childPrimitive = new PrimitiveElement(
     59                         localName);
     60 
     61                 parentPrimitive.addChild(childPrimitive);
     62                 mContentElementsStack.push(childPrimitive);
     63             }
     64         } else {
     65             if (ImpsTags.TransactionContent.equals(localName)) {
     66                 mIsTransContent = true;
     67             }
     68         }
     69 
     70         mCurTagName = localName;
     71     }
     72 
     73     @Override
     74     public void endElement(String uri, String localName, String qName) {
     75         if (ImpsTags.TransactionContent.equals(localName)) {
     76             mIsTransContent = false;
     77         }
     78 
     79         if (mIsTransContent) {
     80             if (!mContentElementsStack.empty()) {
     81                 mContentElementsStack.pop();
     82             }
     83         }
     84         mCurTagName = null;
     85     }
     86 
     87     @Override
     88     public void characters(char[] ch, int start, int length) {
     89         String contentStr = ImpsUtils.trim(new String(ch, start, length));
     90         if (contentStr == null || contentStr.length() == 0) {
     91             return;
     92         }
     93         if (mIsTransContent) {
     94             if (!ImpsTags.TransactionContent.equals(mCurTagName)) {
     95                 PrimitiveElement curPrimitive = mContentElementsStack.peek();
     96 
     97                 curPrimitive.setContents(contentStr);
     98             }
     99         } else {
    100             if (ImpsTags.TransactionID.equals(mCurTagName)) {
    101                 mPrimitive.setTransactionId(contentStr);
    102             } else if (ImpsTags.TransactionMode.equals(mCurTagName)) {
    103                 mPrimitive.setTransactionMode(TransactionMode.valueOf(contentStr));
    104             } else if (ImpsTags.SessionID.equals(mCurTagName)) {
    105                 mPrimitive.setSession(contentStr);
    106             } else if (ImpsTags.Poll.equals(mCurTagName)) {
    107                 mPrimitive.setPoll(contentStr);
    108             } else if (ImpsTags.CIR.equals(mCurTagName)) {
    109                 mPrimitive.setCir(contentStr);
    110             }
    111         }
    112     }
    113 }
    114