XmlContactsGDataParserpublic class XmlContactsGDataParser extends com.google.wireless.gdata.parser.xml.XmlGDataParser GDataParser for a contacts feed. |
Fields Summary |
---|
public static final String | NAMESPACE_CONTACTSNamespace prefix for Contacts | public static final String | NAMESPACE_CONTACTS_URINamespace URI for Contacts | public static final String | LINK_REL_PHOTOThe photo link rels | public static final String | LINK_REL_EDIT_PHOTO | private static final String | GD_NAMESPACEThe phone number type gdata string. | public static final String | TYPESTRING_MOBILE | public static final String | TYPESTRING_HOME | public static final String | TYPESTRING_WORK | public static final String | TYPESTRING_HOME_FAX | public static final String | TYPESTRING_WORK_FAX | public static final String | TYPESTRING_PAGER | public static final String | TYPESTRING_OTHER | public static final String | IM_PROTOCOL_AIM | public static final String | IM_PROTOCOL_MSN | public static final String | IM_PROTOCOL_YAHOO | public static final String | IM_PROTOCOL_SKYPE | public static final String | IM_PROTOCOL_QQ | public static final String | IM_PROTOCOL_GOOGLE_TALK | public static final String | IM_PROTOCOL_ICQ | public static final String | IM_PROTOCOL_JABBER | private static final Hashtable | REL_TO_TYPE_EMAIL | private static final Hashtable | REL_TO_TYPE_PHONE | private static final Hashtable | REL_TO_TYPE_POSTAL | private static final Hashtable | REL_TO_TYPE_IM | private static final Hashtable | REL_TO_TYPE_ORGANIZATION | private static final Hashtable | IM_PROTOCOL_STRING_TO_TYPE_MAP | public static final Hashtable | TYPE_TO_REL_EMAIL | public static final Hashtable | TYPE_TO_REL_PHONE | public static final Hashtable | TYPE_TO_REL_POSTAL | public static final Hashtable | TYPE_TO_REL_IM | public static final Hashtable | TYPE_TO_REL_ORGANIZATION | public static final Hashtable | IM_PROTOCOL_TYPE_TO_STRING_MAP |
Constructors Summary |
---|
public XmlContactsGDataParser(InputStream is, XmlPullParser parser)Creates a new XmlEventsGDataParser.
super(is, parser);
|
Methods Summary |
---|
protected com.google.wireless.gdata.data.Entry | createEntry()
return new ContactEntry();
| protected com.google.wireless.gdata.data.Feed | createFeed()
return new ContactsFeed();
| protected void | handleExtraElementInEntry(com.google.wireless.gdata.data.Entry entry)
XmlPullParser parser = getParser();
if (!(entry instanceof ContactEntry)) {
throw new IllegalArgumentException("Expected ContactEntry!");
}
ContactEntry contactEntry = (ContactEntry) entry;
String name = parser.getName();
if ("email".equals(name)) {
EmailAddress emailAddress = new EmailAddress();
parseContactsElement(emailAddress, parser, REL_TO_TYPE_EMAIL);
// TODO: remove this when the feed is upgraded
if (emailAddress.getType() == 4) {
emailAddress.setType(EmailAddress.TYPE_OTHER);
emailAddress.setIsPrimary(true);
emailAddress.setLabel(null);
}
emailAddress.setAddress(parser.getAttributeValue(null /* ns */, "address"));
contactEntry.addEmailAddress(emailAddress);
} else if ("deleted".equals(name)) {
contactEntry.setDeleted(true);
} else if ("im".equals(name)) {
ImAddress imAddress = new ImAddress();
parseContactsElement(imAddress, parser, REL_TO_TYPE_IM);
imAddress.setAddress(parser.getAttributeValue(null /* ns */, "address"));
imAddress.setLabel(parser.getAttributeValue(null /* ns */, "label"));
String protocolString = parser.getAttributeValue(null /* ns */, "protocol");
if (protocolString == null) {
imAddress.setProtocolPredefined(ImAddress.PROTOCOL_NONE);
imAddress.setProtocolCustom(null);
} else {
Byte predefinedProtocol = (Byte) IM_PROTOCOL_STRING_TO_TYPE_MAP.get(protocolString);
if (predefinedProtocol == null) {
imAddress.setProtocolPredefined(ImAddress.PROTOCOL_CUSTOM);
imAddress.setProtocolCustom(protocolString);
} else {
imAddress.setProtocolPredefined(predefinedProtocol.byteValue());
imAddress.setProtocolCustom(null);
}
}
contactEntry.addImAddress(imAddress);
} else if ("postalAddress".equals(name)) {
PostalAddress postalAddress = new PostalAddress();
parseContactsElement(postalAddress, parser, REL_TO_TYPE_POSTAL);
postalAddress.setValue(XmlUtils.extractChildText(parser));
contactEntry.addPostalAddress(postalAddress);
} else if ("phoneNumber".equals(name)) {
PhoneNumber phoneNumber = new PhoneNumber();
parseContactsElement(phoneNumber, parser, REL_TO_TYPE_PHONE);
phoneNumber.setPhoneNumber(XmlUtils.extractChildText(parser));
contactEntry.addPhoneNumber(phoneNumber);
} else if ("organization".equals(name)) {
Organization organization = new Organization();
parseContactsElement(organization, parser, REL_TO_TYPE_ORGANIZATION);
handleOrganizationSubElement(organization, parser);
contactEntry.addOrganization(organization);
} else if ("extendedProperty".equals(name)) {
ExtendedProperty extendedProperty = new ExtendedProperty();
parseExtendedProperty(extendedProperty);
contactEntry.addExtendedProperty(extendedProperty);
} else if ("groupMembershipInfo".equals(name)) {
GroupMembershipInfo group = new GroupMembershipInfo();
group.setGroup(parser.getAttributeValue(null /* ns */, "href"));
group.setDeleted("true".equals(parser.getAttributeValue(null /* ns */, "deleted")));
contactEntry.addGroup(group);
} else if ("yomiName".equals(name)) {
String yomiName = XmlUtils.extractChildText(parser);
contactEntry.setYomiName(yomiName);
}
| protected void | handleExtraLinkInEntry(java.lang.String rel, java.lang.String type, java.lang.String href, com.google.wireless.gdata.data.Entry entry)
if (LINK_REL_PHOTO.equals(rel)) {
ContactEntry contactEntry = (ContactEntry) entry;
contactEntry.setLinkPhoto(href, type);
} else if (LINK_REL_EDIT_PHOTO.equals(rel)) {
ContactEntry contactEntry = (ContactEntry) entry;
contactEntry.setLinkEditPhoto(href, type);
}
| private static void | handleOrganizationSubElement(com.google.wireless.gdata.contacts.data.Organization element, org.xmlpull.v1.XmlPullParser parser)
int depth = parser.getDepth();
while (true) {
String tag = XmlUtils.nextDirectChildTag(parser, depth);
if (tag == null) break;
if ("orgName".equals(tag)) {
element.setName(XmlUtils.extractChildText(parser));
} else if ("orgTitle".equals(tag)) {
element.setTitle(XmlUtils.extractChildText(parser));
}
}
| private static void | parseContactsElement(com.google.wireless.gdata.contacts.data.ContactsElement element, org.xmlpull.v1.XmlPullParser parser, java.util.Hashtable relToTypeMap)
String rel = parser.getAttributeValue(null /* ns */, "rel");
String label = parser.getAttributeValue(null /* ns */, "label");
if ((label == null && rel == null) || (label != null && rel != null)) {
// TODO: remove this once the focus feed is fixed to not send this case
rel = TYPESTRING_OTHER;
}
if (rel != null) {
final Object type = relToTypeMap.get(rel.toLowerCase());
if (type == null) {
throw new XmlPullParserException("unknown rel, " + rel);
}
element.setType(((Byte) type).byteValue());
}
element.setLabel(label);
element.setIsPrimary("true".equals(parser.getAttributeValue(null /* ns */, "primary")));
| private void | parseExtendedProperty(com.google.wireless.gdata.data.ExtendedProperty extendedProperty)Parse the ExtendedProperty. The parser is assumed to be at the beginning of the tag
for the ExtendedProperty.
XmlPullParser parser = getParser();
extendedProperty.setName(parser.getAttributeValue(null /* ns */, "name"));
extendedProperty.setValue(parser.getAttributeValue(null /* ns */, "value"));
extendedProperty.setXmlBlob(XmlUtils.extractFirstChildTextIgnoreRest(parser));
| private static java.util.Hashtable | swapMap(java.util.Hashtable originalMap)
Hashtable map;
map = new Hashtable();
map.put(TYPESTRING_HOME, new Byte(EmailAddress.TYPE_HOME));
map.put(TYPESTRING_WORK, new Byte(EmailAddress.TYPE_WORK));
map.put(TYPESTRING_OTHER, new Byte(EmailAddress.TYPE_OTHER));
// TODO: this is a hack to support the old feed
map.put(GD_NAMESPACE + "primary", (byte)4);
REL_TO_TYPE_EMAIL = map;
TYPE_TO_REL_EMAIL = swapMap(map);
map = new Hashtable();
map.put(TYPESTRING_HOME, new Byte(PhoneNumber.TYPE_HOME));
map.put(TYPESTRING_MOBILE, new Byte(PhoneNumber.TYPE_MOBILE));
map.put(TYPESTRING_PAGER, new Byte(PhoneNumber.TYPE_PAGER));
map.put(TYPESTRING_WORK, new Byte(PhoneNumber.TYPE_WORK));
map.put(TYPESTRING_HOME_FAX, new Byte(PhoneNumber.TYPE_HOME_FAX));
map.put(TYPESTRING_WORK_FAX, new Byte(PhoneNumber.TYPE_WORK_FAX));
map.put(TYPESTRING_OTHER, new Byte(PhoneNumber.TYPE_OTHER));
REL_TO_TYPE_PHONE = map;
TYPE_TO_REL_PHONE = swapMap(map);
map = new Hashtable();
map.put(TYPESTRING_HOME, new Byte(PostalAddress.TYPE_HOME));
map.put(TYPESTRING_WORK, new Byte(PostalAddress.TYPE_WORK));
map.put(TYPESTRING_OTHER, new Byte(PostalAddress.TYPE_OTHER));
REL_TO_TYPE_POSTAL = map;
TYPE_TO_REL_POSTAL = swapMap(map);
map = new Hashtable();
map.put(TYPESTRING_HOME, new Byte(ImAddress.TYPE_HOME));
map.put(TYPESTRING_WORK, new Byte(ImAddress.TYPE_WORK));
map.put(TYPESTRING_OTHER, new Byte(ImAddress.TYPE_OTHER));
REL_TO_TYPE_IM = map;
TYPE_TO_REL_IM = swapMap(map);
map = new Hashtable();
map.put(TYPESTRING_WORK, new Byte(Organization.TYPE_WORK));
map.put(TYPESTRING_OTHER, new Byte(Organization.TYPE_OTHER));
REL_TO_TYPE_ORGANIZATION = map;
TYPE_TO_REL_ORGANIZATION = swapMap(map);
map = new Hashtable();
map.put(IM_PROTOCOL_AIM, new Byte(ImAddress.PROTOCOL_AIM));
map.put(IM_PROTOCOL_MSN, new Byte(ImAddress.PROTOCOL_MSN));
map.put(IM_PROTOCOL_YAHOO, new Byte(ImAddress.PROTOCOL_YAHOO));
map.put(IM_PROTOCOL_SKYPE, new Byte(ImAddress.PROTOCOL_SKYPE));
map.put(IM_PROTOCOL_QQ, new Byte(ImAddress.PROTOCOL_QQ));
map.put(IM_PROTOCOL_GOOGLE_TALK, new Byte(ImAddress.PROTOCOL_GOOGLE_TALK));
map.put(IM_PROTOCOL_ICQ, new Byte(ImAddress.PROTOCOL_ICQ));
map.put(IM_PROTOCOL_JABBER, new Byte(ImAddress.PROTOCOL_JABBER));
IM_PROTOCOL_STRING_TO_TYPE_MAP = map;
IM_PROTOCOL_TYPE_TO_STRING_MAP = swapMap(map);
Hashtable newMap = new Hashtable();
Enumeration enumeration = originalMap.keys();
while (enumeration.hasMoreElements()) {
Object key = enumeration.nextElement();
Object value = originalMap.get(key);
if (newMap.containsKey(value)) {
throw new IllegalArgumentException("value " + value
+ " was already encountered");
}
newMap.put(value, key);
}
return newMap;
|
|