VCardParserpublic 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 void | judgeVersion(java.lang.String vcardStr)If version not given. Search from vcard string of the VERSION property.
Then instance mParser to appropriate parser.
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 boolean | parse(java.lang.String vcardStr, android.syncml.pim.VDataBuilder builder)Parse the given vcard string
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 void | setVersion(java.lang.String version)Set current version
this.mVersion = version;
| private java.lang.String | verifyVCard(java.lang.String vcardStr)To make sure the vcard string has proper wrap character
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();
|
|