001    /**
002     * Copyright (C) 2010, Skype Limited
003     *
004     * All intellectual property rights, including but not limited to copyrights,
005     * trademarks and patents, as well as know how and trade secrets contained in,
006     * relating to, or arising from the internet telephony software of
007     * Skype Limited (including its affiliates, "Skype"), including without
008     * limitation this source code, Skype API and related material of such
009     * software proprietary to Skype and/or its licensors ("IP Rights") are and
010     * shall remain the exclusive property of Skype and/or its licensors.
011     * The recipient hereby acknowledges and agrees that any unauthorized use of
012     * the IP Rights is a violation of intellectual property laws.
013     *
014     * Skype reserves all rights and may take legal action against infringers of
015     * IP Rights.
016     *
017     * The recipient agrees not to remove, obscure, make illegible or alter any
018     * notices or indications of the IP Rights and/or Skype's rights and
019     * ownership thereof.
020     */
021    
022    package com.skype.ipc;
023    
024    import java.util.HashMap;
025    
026    import com.skype.util.Log;
027    
028    public abstract class AbstractTaggedInputMessage extends AbstractDecoder
029    {
030        private static final String      TAG = "AbstractTaggedInputMessage";
031        private HashMap<Integer, Object> parms;
032        protected boolean                invalid;
033    
034        // CTOR
035        public AbstractTaggedInputMessage(Transport in)
036        {
037            super(in);
038            invalid = false;
039            parms = new HashMap<Integer, Object>();
040        }
041    
042        // PROTECTED
043        protected Object validateAndGetParm(int tag)
044        {
045            if (invalid) {
046                Log.e(TAG, "validateAndGetParm called for invalid message. Returnvalue will be nonsense.");
047                return null;
048            }
049            Object o = parms.get(tag);
050            if (o == null) {
051                return null;
052            }
053            if (o instanceof ResponseListElement) {
054                Object oelement = ((ResponseListElement) o).GetNext();
055                if (oelement == null) {
056                    Log.e(TAG, "validateAndGetParm(" + tag + ") called for ResponseListElement while hasMore="
057                            + ((ResponseListElement) o).HasMore() + ". Returnvalue will be nonsense.");
058                }
059                return oelement;
060            }
061            return o;
062        }
063    
064        protected void addParm(int key, Object o)
065        {
066            parms.put(key, o);
067        }
068    
069        // PUBLIC
070    
071        // use this to check whether this Response can be used and read, or not
072        public boolean isValid()
073        {
074            return (!invalid);
075        }
076    
077        public String GetAsString(int tag)
078        {
079            Object retval = validateAndGetParm(tag);
080            if (retval == null) {
081                // default value of an omitted String value
082                Log.d(TAG, "Missing string value at tag=" + tag + ". Defaulting to \"\".");
083                return new String("");
084            }
085            return (String) retval;
086        }
087    
088        public byte[] GetAsBinary(int tag)
089        {
090            Object retval = validateAndGetParm(tag);
091            if (retval == null) {
092                // default value of an omitted binary[] value
093                Log.d(TAG, "Missing binary value at tag=" + tag + ". Defaulting to null.");
094            }
095            return (byte[]) retval;
096        }
097    
098        public int GetAsInt(int tag)
099        {
100            Object retval = validateAndGetParm(tag);
101            if (retval == null) {
102                // default value of an omitted integer value
103                Log.d(TAG, "Missing integer value at tag=" + tag + ". Defaulting to 0.");
104                return 0;
105            }
106            return ((Number) retval).intValue();
107        }
108    
109        public boolean GetAsBoolean(int tag)
110        {
111            Object retval = validateAndGetParm(tag);
112            if (retval == null) {
113                // default value of an omitted boolean value
114                Log.d(TAG, "Missing boolean value at tag=" + tag + ". Defaulting to false.");
115                return false;
116            }
117            return (((Number) retval).intValue() != 0);
118        }
119    
120        // returns oid of particular tagged list
121        public int GetOid(int tag)
122        {
123            Object retval = validateAndGetParm(tag);
124            if (retval == null) {
125                // default value of an omitted integer value
126                Log.d(TAG, "Missing oid value at tag=" + tag + ". Defaulting to NULL_VALUE.");
127                return NULL_VALUE;
128            }
129            return ((Number) retval).intValue();
130        }
131    
132        // returns true, if there is more occurrences of the last piece of data
133        // read. I.e. if GetAsInt
134        public boolean HasMore(int tag)
135        {
136            Object o = parms.get(tag);
137            if (o != null) {
138                if (o instanceof ResponseListElement) {
139                    return ((ResponseListElement) o).HasMore();
140                }
141            }
142            return false;
143        }
144    
145    }