FileDocCategorySizeDatePackage
VCardParser.javaAPI DocAndroid 1.5 API4593Wed May 06 22:41:56 BST 2009android.syncml.pim.vcard

VCardParser

public class VCardParser extends Object

Fields Summary
VCardParser_V21
mParser
public static final String
VERSION_VCARD21
public static final String
VERSION_VCARD30
public static final int
VERSION_VCARD21_INT
public static final int
VERSION_VCARD30_INT
String
mVersion
private static final String
TAG
Constructors Summary
public VCardParser()


      
    
Methods Summary
private voidjudgeVersion(java.lang.String vcardStr)
If version not given. Search from vcard string of the VERSION property. Then instance mParser to appropriate parser.

param
vcardStr the content of vcard data

        if (mVersion == null) {// auto judge
            int verIdx = vcardStr.indexOf("\nVERSION:");
            if (verIdx == -1) // if not have VERSION, v2.1 default
                mVersion = VERSION_VCARD21;
            else {
                String verStr = vcardStr.substring(verIdx, vcardStr.indexOf(
                        "\n", verIdx + 1));
                if (verStr.indexOf("2.1") > 0)
                    mVersion = VERSION_VCARD21;
                else if (verStr.indexOf("3.0") > 0)
                    mVersion = VERSION_VCARD30;
                else
                    mVersion = VERSION_VCARD21;
            }
        }
        if (mVersion.equals(VERSION_VCARD21))
            mParser = new VCardParser_V21();
        if (mVersion.equals(VERSION_VCARD30))
            mParser = new VCardParser_V30();
    
public booleanparse(java.lang.String vcardStr, android.syncml.pim.VDataBuilder builder)
Parse the given vcard string

param
vcardStr to content to be parsed
param
builder the data builder to hold data
return
true if the string is successfully parsed, else return false
throws
VCardException
throws
IOException


        vcardStr = this.verifyVCard(vcardStr);

        boolean isSuccess = mParser.parse(new ByteArrayInputStream(vcardStr
                .getBytes()), "US-ASCII", builder);
        if (!isSuccess) {
            if (mVersion.equals(VERSION_VCARD21)) {
                if (Config.LOGD)
                    Log.d(TAG, "Parse failed for vCard 2.1 parser."
                            + " Try to use 3.0 parser.");

                this.setVersion(VERSION_VCARD30);

                return this.parse(vcardStr, builder);
            }
            throw new VCardException("parse failed.(even use 3.0 parser)");
        }
        return true;
    
private voidsetVersion(java.lang.String version)
Set current version

param
version the new version

        this.mVersion = version;
    
private java.lang.StringverifyVCard(java.lang.String vcardStr)
To make sure the vcard string has proper wrap character

param
vcardStr the string to be checked
return
string after verified

        this.judgeVersion(vcardStr);
        // -- indent line:
        vcardStr = vcardStr.replaceAll("\r\n", "\n");
        String[] strlist = vcardStr.split("\n");
        StringBuilder v21str = new StringBuilder("");
        for (int i = 0; i < strlist.length; i++) {
            if (strlist[i].indexOf(":") < 0) {
                if (strlist[i].length() == 0 && strlist[i + 1].indexOf(":") > 0)
                    v21str.append(strlist[i]).append("\r\n");
                else
                    v21str.append(" ").append(strlist[i]).append("\r\n");
            } else
                v21str.append(strlist[i]).append("\r\n");
        }
        return v21str.toString();