FileDocCategorySizeDatePackage
ImpsUtils.javaAPI DocAndroid 1.5 API5701Wed May 06 22:42:46 BST 2009com.android.im.imps

ImpsUtils

public class ImpsUtils extends Object

Fields Summary
private static final HashMap
sClientInfo
private static String
sSessionCookie
private static int
sSessionCookieNumber
Constructors Summary
private ImpsUtils()

    
Methods Summary
public static ImpsErrorInfocheckResultError(Primitive response)
Checks if the response primitive indicates successful.

param
response the response primitive.
returns
null if the status code is 200 or an ImpsErrorInfo instance

        PrimitiveElement result = response.getElement(ImpsTags.Result);
        if (result == null) {
            return null;
        }

        String resultCode = result.getChild(ImpsTags.Code).getContents();
        if (!ImpsConstants.SUCCESS_CODE.equals(resultCode)) {
            PrimitiveElement descElem = result.getChild(ImpsTags.Description);
            String errorDesc = (descElem == null) ? "" : descElem.getContents();
            int statusCode = parseInt(resultCode, ImErrorInfo.ILLEGAL_SERVER_RESPONSE);
            return new ImpsErrorInfo(statusCode, errorDesc, response);
        }
        return null;
    
static synchronized java.lang.StringgenSessionCookie()

        if(sSessionCookie == null) {
            Random random = new Random();
            sSessionCookie = System.currentTimeMillis() + "" + random.nextInt();
        }
        return sSessionCookie + (sSessionCookieNumber++);
    
public static java.util.MapgetClientInfo()

        return Collections.unmodifiableMap(sClientInfo);
    
public static booleanisFalse(java.lang.String value)
Checks if a string is a boolean value of false in IMPS.

param
value the string value.
return
true if it's false in IMPS

        return ImpsConstants.FALSE.equalsIgnoreCase(value);
    
public static booleanisQualifiedPresence(PrimitiveElement elem)
Check whether the presence element has a qualified attribute value. An attribute value is invalid when: 1. An attribute is authorized but not yet updated for the first time 2. The user wants to indicate that the value of the attribute is unknown.

param
elem the presence element
return
true if the value of attribute is valid.

        if (null == elem || null == elem.getChild(ImpsTags.Qualifier)) {
            return false;
        }

        return ImpsUtils.isTrue(elem.getChildContents(ImpsTags.Qualifier));
    
public static booleanisTrue(java.lang.String value)
Checks if a string is a boolean value of true IMPS.

param
value the string value.
return
true if it's true in IMPS.

        // TODO: v1.2 doesn't support ClientContentLimit
        sClientInfo = new HashMap<String, String>();
        sClientInfo.put(ImpsTags.ClientType, ImpsClientCapability.getClientType());
        sClientInfo.put(ImpsTags.ClientProducer, ImpsConstants.CLIENT_PRODUCER);
        sClientInfo.put(ImpsTags.ClientVersion, ImpsConstants.CLIENT_VERSION);
    
        return ImpsConstants.TRUE.equalsIgnoreCase(value);
    
public static intparseInt(java.lang.String s, int defaultValue)

        try {
            return Integer.parseInt(s);
        } catch (NumberFormatException e) {
            // ignore
            return defaultValue;
        }
    
public static longparseLong(java.lang.String s, long defaultValue)

        try {
            return Long.parseLong(s);
        } catch (NumberFormatException e) {
            // ignore
            return defaultValue;
        }
    
public static java.lang.StringtoImpsBool(boolean isTrue)
Return the IMPS String presentation of the boolean value

param
isTrue the boolean value
return
the String presentation

        if (isTrue) {
            return ImpsConstants.TRUE;
        }

        return ImpsConstants.FALSE;
    
public static java.lang.Stringtrim(java.lang.String str)
Returns a copy of the string, with leading and trailing whitespace omitted. Unlike the standard trim which just removes '\u0020'(the space character), it removes all possible leading and trailing whitespace character.

param
str the string.
return
a copy of the string, with leading and trailing whitespace omitted.

        if (null == str || "".equals(str))
            return str;

        int strLen = str.length();
        int start = 0;
        while (start < strLen && Character.isWhitespace(str.charAt(start)))
            start++;
        int end = strLen - 1;
        while (end >= 0 && Character.isWhitespace(str.charAt(end)))
            end--;
        if (end < start)
            return "";
        str = str.substring(start, end + 1);
        return str;