/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.contacts;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.contacts.BillingInformation;
import com.google.gdata.data.contacts.Birthday;
import com.google.gdata.data.contacts.CalendarLink;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactGroupEntry;
import com.google.gdata.data.contacts.DirectoryServer;
import com.google.gdata.data.contacts.Event;
import com.google.gdata.data.contacts.ExternalId;
import com.google.gdata.data.contacts.Gender;
import com.google.gdata.data.contacts.GroupMembershipInfo;
import com.google.gdata.data.contacts.Hobby;
import com.google.gdata.data.contacts.Initials;
import com.google.gdata.data.contacts.Jot;
import com.google.gdata.data.contacts.Language;
import com.google.gdata.data.contacts.MaidenName;
import com.google.gdata.data.contacts.Mileage;
import com.google.gdata.data.contacts.Nickname;
import com.google.gdata.data.contacts.Occupation;
import com.google.gdata.data.contacts.Priority;
import com.google.gdata.data.contacts.Relation;
import com.google.gdata.data.contacts.Sensitivity;
import com.google.gdata.data.contacts.ShortName;
import com.google.gdata.data.contacts.Subject;
import com.google.gdata.data.contacts.UserDefinedField;
import com.google.gdata.data.contacts.Website;
import com.google.gdata.data.extensions.AdditionalName;
import com.google.gdata.data.extensions.City;
import com.google.gdata.data.extensions.Country;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.ExtendedProperty;
import com.google.gdata.data.extensions.FamilyName;
import com.google.gdata.data.extensions.FormattedAddress;
import com.google.gdata.data.extensions.FullName;
import com.google.gdata.data.extensions.GivenName;
import com.google.gdata.data.extensions.Im;
import com.google.gdata.data.extensions.Name;
import com.google.gdata.data.extensions.NamePrefix;
import com.google.gdata.data.extensions.NameSuffix;
import com.google.gdata.data.extensions.Neighborhood;
import com.google.gdata.data.extensions.OrgDepartment;
import com.google.gdata.data.extensions.OrgJobDescription;
import com.google.gdata.data.extensions.OrgName;
import com.google.gdata.data.extensions.OrgSymbol;
import com.google.gdata.data.extensions.OrgTitle;
import com.google.gdata.data.extensions.Organization;
import com.google.gdata.data.extensions.PhoneNumber;
import com.google.gdata.data.extensions.PoBox;
import com.google.gdata.data.extensions.PostCode;
import com.google.gdata.data.extensions.Region;
import com.google.gdata.data.extensions.Street;
import com.google.gdata.data.extensions.StructuredPostalAddress;
import com.google.gdata.data.extensions.When;
import com.google.gdata.data.extensions.Where;
import com.google.gdata.util.XmlBlob;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Helper class to parse, update and display a contact.
* It uses a bunch of anonymous inner classes for every kind of element to
* perform the actual job. It is defined as an enum to take advantage of the
* automatic instantiation and constant-specific methods.
*
*
*/
public enum ElementHelper implements ElementHelperInterface {
BILLING_INFORMATION {
public void parse(ContactEntry contact, ElementParser parser) {
BillingInformation billingInformation = new BillingInformation();
billingInformation.setValue(parser.get(PropertyName.VALUE));
contact.setBillingInformation(billingInformation);
}
public void print(PrintStream out, ContactEntry contact) {
if (contact.hasBillingInformation()) {
out.println("billing information: "
+ contact.getBillingInformation().getValue());
}
}
public void update(ContactEntry dest, ContactEntry src) {
if (src.hasBillingInformation()) {
dest.setBillingInformation(src.getBillingInformation());
}
}
public String getUsage() {
return "<billing_information>";
}
},
BIRTHDAY {
public void parse(ContactEntry contact, ElementParser parser) {
Birthday birthday = new Birthday();
birthday.setWhen(parser.get(PropertyName.VALUE));
contact.setBirthday(birthday);
}
public void print(PrintStream out, ContactEntry contact) {
if (contact.hasBirthday()) {
out.println("birthday: " + contact.getBirthday().getWhen());
}
}
public void update(ContactEntry dest, ContactEntry src) {
if (src.hasBirthday()) {
dest.setBirthday(src.getBirthday());
}
}
public String getUsage() {
return "YYYY-MM-DD|--MM-DD";
}
},
CALENDAR_LINK(true) {
public void parse(ContactEntry contact, ElementParser parser) {
CalendarLink calendarLink = new CalendarLink();
calendarLink.setHref(parser.get(PropertyName.VALUE));
if (parser.has(PropertyName.REL)) {
calendarLink.setRel(
CalendarLink.Rel.valueOf(
parser.get(PropertyName.REL).toUpperCase()));
}
if (parser.has(PropertyName.LABEL)) {
calendarLink.setLabel(parser.get(PropertyName.LABEL));
}
if (parser.has(PropertyName.PRIMARY)) {
calendarLink.setPrimary(parser.is(PropertyName.PRIMARY));
}
contact.addCalendarLink(calendarLink);
}
public void print(PrintStream out, ContactEntry contact) {
if (contact.hasCalendarLinks()) {
out.println("calendar links:");
for (CalendarLink calendarLink : contact.getCalendarLinks()) {
out.print(" " + calendarLink.getHref());
if (calendarLink.hasRel()) {
out.print(" rel:" + calendarLink.getRel());
} else if (calendarLink.hasLabel()) {
out.print(" label:" + calendarLink.getLabel());
}
if (calendarLink.hasPrimary() && calendarLink.getPrimary()) {
out.print(" (primary)");
}
out.println();
}
}
}
public void update(ContactEntry dest, ContactEntry src) {
if (src.hasCalendarLinks()) {
List<CalendarLink> calendarLinks = dest.getCalendarLinks();
calendarLinks.clear();
calendarLinks.addAll(src.getCalendarLinks());
}
}
public String getUsage() {
return "<href>"
+ "[,rel:<rel>]"
+ "[,label:<label>]"
+ "[,primary:true|false]";
}
},
DIRECTORY_SERVER {
public void parse(ContactEntry contact, ElementParser parser) {
DirectoryServer directoryServer = new DirectoryServer();
directoryServer.setValue(parser.get(PropertyName.VALUE));
contact.setDirectoryServer(directoryServer);
}
public void print(PrintStream out, ContactEntry contact) {
if (contact.hasDirectoryServer()) {
out.println("directory server: "
+ contact.getDirectoryServer().getValue());
}
}
public void update(ContactEntry dest, ContactEntry src) {
if (src.hasDirectoryServer()) {
dest.setDirectoryServer(src.getDirectoryServer());
}
}
public String getUsage() {
return "<directory_server>";
}
},
EMAIL(true) {
public void parse(Cont
- 1
- 2
前往页