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

ImpsAddress

public abstract class ImpsAddress extends com.android.im.engine.Address
The abstract representation of an IMPS address.

Fields Summary
private static final char[]
SPECIALS
protected String
mUser
protected String
mResource
protected String
mDomain
private String
mFullAddress
Constructors Summary
protected ImpsAddress()


      
    
protected ImpsAddress(String user, String resource, String domain)

        // build the full address and unify the fields to lower case since imps
        // address is case insensitive.
        StringBuilder buf = new StringBuilder(ImpsConstants.ADDRESS_PREFIX);
        if(user != null) {
            buf.append(user.toLowerCase());
        }
        if(resource != null) {
            buf.append('/").append(resource.toLowerCase());
        }
        if(domain != null) {
            buf.append('@").append(domain.toLowerCase());
        }
        mFullAddress = buf.toString();
        mUser = user;
        mResource = resource;
        mDomain = domain;
        verifyAddress();
    
protected ImpsAddress(String full)

        this(full, true);
    
protected ImpsAddress(String full, boolean verify)

        if (full == null || full.length() == 0) {
            throw new IllegalArgumentException();
        }

        //TODO: escape reserved characters.
        if(!full.startsWith(ImpsConstants.ADDRESS_PREFIX)) {
            full = ImpsConstants.ADDRESS_PREFIX + full;
        }

        parse(full);
        mFullAddress = full;

        if (verify) {
            verifyAddress();
        }
    
Methods Summary
public booleanequals(java.lang.Object other)

        return other instanceof ImpsAddress
                && mFullAddress.equalsIgnoreCase(((ImpsAddress)other).mFullAddress);
    
public static com.android.im.imps.ImpsAddressfromPrimitiveElement(PrimitiveElement elem)
Constructs an ImpsAddress from the Sender or Recipient element in a primitive.

param
elem the Sender of Recipient element.
return
the ImpsAddress object or null if it's not a valid element.

        String type = elem.getTagName();
        if(ImpsTags.User.equals(type)) {
            return new ImpsUserAddress(elem.getChildContents(ImpsTags.UserID), false);
        } else if(ImpsTags.Group.equals(type)) {
            PrimitiveElement child = elem.getFirstChild();
            if(child == null) {
                throw new IllegalArgumentException();
            }
            if(ImpsTags.GroupID.equals(child.getTagName())){
                return new ImpsGroupAddress(child.getContents());
            } else {
                String screeName = child.getChildContents(ImpsTags.SName);
                String groupId = child.getChildContents(ImpsTags.GroupID);
                return new ImpsGroupAddress(groupId, screeName);
            }
        } else if(ImpsTags.ContactList.equals(type)) {
            return new ImpsContactListAddress(elem.getContents(), false);
        } else {
            throw new IllegalArgumentException();
        }
    
public java.lang.StringgetDomain()
Gets the Domain-part of the address which identifies the IMPS server domain.

return
the Domain-part of the address.

        return mDomain;
    
abstract com.android.im.engine.ImEntitygetEntity(ImpsConnection connection)
Gets the entity this address object refers to.

param
connection
return
the entity this address refers to or null if the entity not found.

public java.lang.StringgetFullName()
Gets the full string representation of the address.

        return mFullAddress;
    
public java.lang.StringgetResource()
Gets the Resource-part of the address which identifies the referred public or private resource.

return
the Resource-part of the address.

        return mResource;
    
public java.lang.StringgetUser()
Gets the User-part of the address which refers to the identification of the IMPS user.

return
the User-part of the address.

        return mUser;
    
public inthashCode()

        return mFullAddress.toLowerCase().hashCode();
    
private booleanisAlphaSequence(java.lang.String str)

        for(int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(ch < 32 || ch > 126 || isSpecial(ch)) {
                return false;
            }
        }
        return true;
    
private booleanisSpecial(char ch)

        for (char element : SPECIALS) {
            if(ch == element) {
                return true;
            }
        }
        return false;
    
private voidparse(java.lang.String full)

        mUser = parseUser(full);
        mResource = parseResource(full);
        mDomain = parseDomain(full);
    
private java.lang.StringparseDomain(java.lang.String full)

        int beginIndex = full.lastIndexOf('@");
        return beginIndex == -1 ? null : full.substring(beginIndex + 1);
    
private java.lang.StringparseResource(java.lang.String full)

        int beginIndex = full.indexOf('/");
        if (beginIndex == -1) {
            return null;
        }
        int endIndex = full.lastIndexOf('@");
        if (endIndex == -1 || endIndex < beginIndex) {
            endIndex = full.length();
        }
        return full.substring(beginIndex + 1, endIndex);
    
private java.lang.StringparseUser(java.lang.String full)

        int index = full.indexOf('/");
        if(index == 3) {
            return null;
        }
        if (index == -1) {
            index = full.lastIndexOf('@");
        }
        if (index == -1) {
            index = full.length();
        }
        return full.substring(3, index);
    
public voidreadFromParcel(android.os.Parcel source)

        mFullAddress = source.readString();
        parse(mFullAddress);
    
public abstract PrimitiveElementtoPrimitiveElement()
Formats the address to a PrimitiveElement which can be used as the content of Sender or Recipient.

return
a PrimitiveElement.

private voidverifyAddress()

        ImpsLog.log("verifyAddress:" + mUser + ", " + mResource + ",  " + mDomain);
        if(mUser == null && mResource == null) {
            throw new IllegalArgumentException();
        }

        if(mUser != null) {
            if(mUser.length() == 0) {
                throw new IllegalArgumentException("Invalid user");
            }
            if(mUser.charAt(0) == '+") {//mobile number
                for(int i = 1; i < mUser.length(); i++) {
                    if(!Character.isDigit(mUser.charAt(i))) {
                        throw new IllegalArgumentException("Invalid user");
                    }
                }
            } else if(!isAlphaSequence(mUser)) {
                throw new IllegalArgumentException("Invalid user");
            }
        }

        if(mResource != null && !isAlphaSequence(mResource)) {
            throw new IllegalArgumentException("Invalid resource");
        }
        if(mDomain != null && !isAlphaSequence(mDomain)) {
            throw new IllegalArgumentException("Invalid domain");
        }
    
public voidwriteToParcel(android.os.Parcel dest)

        dest.writeString(mFullAddress);