PageRenderTime 242ms CodeModel.GetById 60ms app.highlight 127ms RepoModel.GetById 33ms app.codeStats 0ms

/src/com/google/wireless/gdata/contacts/serializer/xml/XmlContactEntryGDataSerializer.java

https://github.com/Jib-BAOSP/platform_external_gdata
Java | 256 lines | 204 code | 41 blank | 11 comment | 36 complexity | ac5de8e8af7cc1d59be9018619a7f8d4 MD5 | raw file
  1// Copyright 2007 The Android Open Source Project
  2
  3package com.google.wireless.gdata.contacts.serializer.xml;
  4
  5import com.google.wireless.gdata.contacts.data.ContactEntry;
  6import com.google.wireless.gdata.contacts.data.ContactsElement;
  7import com.google.wireless.gdata.contacts.data.EmailAddress;
  8import com.google.wireless.gdata.contacts.data.ImAddress;
  9import com.google.wireless.gdata.contacts.data.Organization;
 10import com.google.wireless.gdata.contacts.data.PhoneNumber;
 11import com.google.wireless.gdata.contacts.data.PostalAddress;
 12import com.google.wireless.gdata.contacts.data.GroupMembershipInfo;
 13import com.google.wireless.gdata.contacts.parser.xml.XmlContactsGDataParser;
 14import com.google.wireless.gdata.data.StringUtils;
 15import com.google.wireless.gdata.data.ExtendedProperty;
 16import com.google.wireless.gdata.parser.ParseException;
 17import com.google.wireless.gdata.parser.xml.XmlGDataParser;
 18import com.google.wireless.gdata.parser.xml.XmlParserFactory;
 19import com.google.wireless.gdata.serializer.xml.XmlEntryGDataSerializer;
 20
 21import org.xmlpull.v1.XmlSerializer;
 22
 23import java.io.IOException;
 24import java.util.Enumeration;
 25import java.util.Hashtable;
 26
 27/**
 28 *  Serializes Google Contact entries into the Atom XML format.
 29 */
 30public class XmlContactEntryGDataSerializer extends XmlEntryGDataSerializer {
 31
 32  public XmlContactEntryGDataSerializer(XmlParserFactory factory, ContactEntry entry) {
 33    super(factory, entry);
 34  }
 35
 36  @Override
 37  protected void declareExtraEntryNamespaces(XmlSerializer serializer) throws IOException {
 38    super.declareExtraEntryNamespaces(serializer);
 39    serializer.setPrefix(XmlContactsGDataParser.NAMESPACE_CONTACTS,
 40        XmlContactsGDataParser.NAMESPACE_CONTACTS_URI);
 41  }
 42
 43  protected ContactEntry getContactEntry() {
 44    return (ContactEntry) getEntry();
 45  }
 46
 47  /* (non-Javadoc)
 48  * @see XmlEntryGDataSerializer#serializeExtraEntryContents
 49  */
 50  protected void serializeExtraEntryContents(XmlSerializer serializer, int format)
 51      throws ParseException, IOException {
 52    ContactEntry entry = getContactEntry();
 53    entry.validate();
 54
 55    serializeLink(serializer, XmlContactsGDataParser.LINK_REL_EDIT_PHOTO,
 56        entry.getLinkEditPhotoHref(), entry.getLinkEditPhotoType());
 57    serializeLink(serializer, XmlContactsGDataParser.LINK_REL_PHOTO,
 58        entry.getLinkPhotoHref(), entry.getLinkPhotoType());
 59
 60    // Serialize the contact specific parts of this entry.  Note that
 61    // gd:ContactSection and gd:geoPt are likely to be deprecated, and
 62    // are not currently serialized.
 63    Enumeration eachEmail = entry.getEmailAddresses().elements();
 64    while (eachEmail.hasMoreElements()) {
 65      serialize(serializer, (EmailAddress) eachEmail.nextElement());
 66    }
 67
 68    Enumeration eachIm = entry.getImAddresses().elements();
 69    while (eachIm.hasMoreElements()) {
 70      serialize(serializer, (ImAddress) eachIm.nextElement());
 71    }
 72
 73    Enumeration eachPhone = entry.getPhoneNumbers().elements();
 74    while (eachPhone.hasMoreElements()) {
 75      serialize(serializer, (PhoneNumber) eachPhone.nextElement());
 76    }
 77
 78    Enumeration eachAddress = entry.getPostalAddresses().elements();
 79    while (eachAddress.hasMoreElements()) {
 80      serialize(serializer, (PostalAddress) eachAddress.nextElement());
 81    }
 82
 83    Enumeration eachOrganization = entry.getOrganizations().elements();
 84    while (eachOrganization.hasMoreElements()) {
 85      serialize(serializer, (Organization) eachOrganization.nextElement());
 86    }
 87
 88    Enumeration eachExtendedProperty = entry.getExtendedProperties().elements();
 89    while (eachExtendedProperty.hasMoreElements()) {
 90      serialize(serializer, (ExtendedProperty) eachExtendedProperty.nextElement());
 91    }
 92
 93    Enumeration eachGroup = entry.getGroups().elements();
 94    while (eachGroup.hasMoreElements()) {
 95      serialize(serializer, (GroupMembershipInfo) eachGroup.nextElement());
 96    }
 97
 98    serializeYomiName(serializer, entry.getYomiName());
 99  }
100
101  private static void serialize(XmlSerializer serializer, EmailAddress email)
102      throws IOException, ParseException {
103    if (StringUtils.isEmptyOrWhitespace(email.getAddress())) return;
104    serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "email");
105    serializeContactsElement(serializer, email, XmlContactsGDataParser.TYPE_TO_REL_EMAIL);
106    serializer.attribute(null /* ns */, "address", email.getAddress());
107    serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "email");
108  }
109
110  private static void serialize(XmlSerializer serializer, ImAddress im)
111      throws IOException, ParseException {
112    if (StringUtils.isEmptyOrWhitespace(im.getAddress())) return;
113
114    serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "im");
115    serializeContactsElement(serializer, im, XmlContactsGDataParser.TYPE_TO_REL_IM);
116    serializer.attribute(null /* ns */, "address", im.getAddress());
117
118    String protocolString;
119    switch (im.getProtocolPredefined()) {
120      case ImAddress.PROTOCOL_NONE:
121        // don't include the attribute if no protocol was specified
122        break;
123
124      case ImAddress.PROTOCOL_CUSTOM:
125        protocolString = im.getProtocolCustom();
126        if (protocolString == null) {
127          throw new IllegalArgumentException(
128              "the protocol is custom, but the custom string is null");
129        }
130        serializer.attribute(null /* ns */, "protocol", protocolString);
131        break;
132
133      default:
134        protocolString = (String)XmlContactsGDataParser.IM_PROTOCOL_TYPE_TO_STRING_MAP.get(
135            new Byte(im.getProtocolPredefined()));
136        serializer.attribute(null /* ns */, "protocol", protocolString);
137        break;
138    }
139
140    serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "im");
141  }
142
143  private static void serialize(XmlSerializer serializer, PhoneNumber phone)
144      throws IOException, ParseException {
145    if (StringUtils.isEmptyOrWhitespace(phone.getPhoneNumber())) return;
146    serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "phoneNumber");
147    serializeContactsElement(serializer, phone, XmlContactsGDataParser.TYPE_TO_REL_PHONE);
148    serializer.text(phone.getPhoneNumber());
149    serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "phoneNumber");
150  }
151
152  private static void serialize(XmlSerializer serializer, Organization organization)
153      throws IOException, ParseException {
154    final String name = organization.getName();
155    final String title = organization.getTitle();
156
157    if (StringUtils.isEmptyOrWhitespace(name) && StringUtils.isEmptyOrWhitespace(title)) return;
158
159    serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "organization");
160    serializeContactsElement(serializer,
161            organization, XmlContactsGDataParser.TYPE_TO_REL_ORGANIZATION);
162    if (!StringUtils.isEmpty(name)) {
163      serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "orgName");
164      serializer.text(name);
165      serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "orgName");
166    }
167
168    if (!StringUtils.isEmpty(title)) {
169      serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "orgTitle");
170      serializer.text(title);
171      serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "orgTitle");
172    }
173    serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "organization");
174  }
175
176  private static void serialize(XmlSerializer serializer, PostalAddress addr)
177      throws IOException, ParseException {
178    if (StringUtils.isEmptyOrWhitespace(addr.getValue())) return;
179    serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "postalAddress");
180    serializeContactsElement(serializer, addr, XmlContactsGDataParser.TYPE_TO_REL_POSTAL);
181    final String addressValue = addr.getValue();
182    if (addressValue != null) serializer.text(addressValue);
183    serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "postalAddress");
184  }
185
186  private static void serializeContactsElement(XmlSerializer serializer, ContactsElement element,
187      Hashtable typeToRelMap) throws IOException, ParseException {
188    final String label = element.getLabel();
189    boolean hasType = element.getType() != ContactsElement.TYPE_NONE;
190
191    if (((label == null) && !hasType) || ((label != null) && hasType)) {
192      throw new ParseException("exactly one of label or rel must be set");
193    }
194
195    if (label != null) {
196      serializer.attribute(null /* ns */, "label", label);
197    }
198    if (hasType) {
199      serializer.attribute(null /* ns */, "rel",
200          (String)typeToRelMap.get(new Byte(element.getType())));
201    }
202    if (element.isPrimary()) {
203      serializer.attribute(null /* ns */, "primary", "true");
204    }
205  }
206
207  private static void serialize(XmlSerializer serializer, GroupMembershipInfo groupMembershipInfo)
208      throws IOException, ParseException {
209    final String group = groupMembershipInfo.getGroup();
210    final boolean isDeleted = groupMembershipInfo.isDeleted();
211
212    if (StringUtils.isEmptyOrWhitespace(group)) {
213      throw new ParseException("the group must not be empty");
214    }
215
216    serializer.startTag(XmlContactsGDataParser.NAMESPACE_CONTACTS_URI, "groupMembershipInfo");
217    serializer.attribute(null /* ns */, "href", group);
218    serializer.attribute(null /* ns */, "deleted", isDeleted ? "true" : "false");
219    serializer.endTag(XmlContactsGDataParser.NAMESPACE_CONTACTS_URI, "groupMembershipInfo");
220  }
221
222  private static void serialize(XmlSerializer serializer, ExtendedProperty extendedProperty)
223      throws IOException, ParseException {
224    final String name = extendedProperty.getName();
225    final String value = extendedProperty.getValue();
226    final String xmlBlob = extendedProperty.getXmlBlob();
227
228    serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "extendedProperty");
229    if (!StringUtils.isEmpty(name)) {
230      serializer.attribute(null /* ns */, "name", name);
231    }
232    if (!StringUtils.isEmpty(value)) {
233      serializer.attribute(null /* ns */, "value", value);
234    }
235    if (!StringUtils.isEmpty(xmlBlob)) {
236      serializeBlob(serializer, xmlBlob);
237    }
238    serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "extendedProperty");
239  }
240
241  private static void serializeBlob(XmlSerializer serializer, String blob)
242      throws IOException, ParseException {
243     serializer.text(blob);
244  }
245
246  private static void serializeYomiName(XmlSerializer serializer,
247      String yomiName)
248      throws IOException {
249    if (StringUtils.isEmpty(yomiName)) {
250      return;
251    }
252    serializer.startTag(XmlContactsGDataParser.NAMESPACE_CONTACTS_URI, "yomiName");
253    serializer.text(yomiName);
254    serializer.endTag(XmlContactsGDataParser.NAMESPACE_CONTACTS_URI, "yomiName");
255  }
256}