VCardFormatpublic abstract class VCardFormat extends EndMatcher implements com.sun.kvem.midp.pim.PIMFormatPartial implementation of PIMEncoding for VCard/2.1 and VCard/3.0. |
Constructors Summary |
---|
public VCardFormat()VCard formatting class.
super("VCARD");
|
Methods Summary |
---|
public javax.microedition.pim.PIMItem[] | decode(java.io.InputStream in, java.lang.String encoding, javax.microedition.pim.PIMList list)Constructs one or more PIMItems from serialized data.
LineReader r = new LineReader(in, encoding, this);
ContactImpl contact = decode(r, list);
if (contact == null) {
return null;
} else {
return new ContactImpl[] { contact };
}
| private com.sun.kvem.midp.pim.ContactImpl | decode(com.sun.kvem.midp.pim.LineReader in, javax.microedition.pim.PIMList list)Constructs a single PIMItem from serialized data.
String line = in.readLine();
if (line == null) {
return null;
}
if (!line.toUpperCase().equals("BEGIN:VCARD")) {
throw new UnsupportedPIMFormatException("Not a vCard :'"
+ line + "'");
}
String categoryProperty = getCategoryProperty();
ContactImpl contact = new ContactImpl((AbstractPIMList)list);
while ((line = in.readLine()) != null) {
FormatSupport.DataElement element =
FormatSupport.parseObjectLine(line);
if (element.propertyName.equals("END")) {
return contact;
} else if (element.propertyName.equals("VERSION")) {
if (!element.data.equals(getVersion())) {
throw new UnsupportedPIMFormatException("Version "
+ element.data + " not supported");
}
} else if (element.propertyName.equals(categoryProperty)) {
String[] categories = FormatSupport.split(element.data, ',", 0);
for (int j = 0; j < categories.length; j++) {
try {
contact.addToCategory(categories[j]);
} catch (PIMException e) {
// cannot add to category
}
}
} else {
importData(contact, element.propertyName, element.attributes,
element.data, contact.getPIMListHandle());
}
}
throw new IOException("Unterminated vCard");
| public void | encode(java.io.OutputStream out, java.lang.String encoding, javax.microedition.pim.PIMItem pimItem)Serializes a PIMItem.
Writer w = new OutputStreamWriter(out, encoding);
w.write("BEGIN:VCARD\r\n");
w.write("VERSION:");
w.write(getVersion());
w.write("\r\n");
// write all fields
int[] fields = pimItem.getFields();
FormatSupport.sort(fields);
for (int i = 0; i < fields.length; i++) {
int valueCount = pimItem.countValues(fields[i]);
for (int j = 0; j < valueCount; j++) {
writeValue(w, pimItem, fields[i], j);
}
}
// write categories.
String categories = FormatSupport.join(pimItem.getCategories(), ",");
if (categories.length() > 0) {
w.write(getCategoryProperty());
w.write(":");
w.write(categories);
w.write("\r\n");
}
w.write("END:VCARD\r\n");
w.flush();
| protected abstract java.lang.String | getBinaryEncodingName()Gets the name of the default binary encoding.
This is "BASE64" for vCard 2.1 and "B" for vCard 3.0.
| protected abstract java.lang.String | getCategoryProperty()Gets the vCard property name used to store categories.
| protected abstract java.lang.String | getClassProperty()Gets the vCard property name used to store classes.
| public java.lang.String | getName()Gets the code name of this encoding (e.g. "VCARD/2.1").
return "VCARD/" + getVersion();
| protected abstract java.lang.String | getVersion()Returns the version number of vCard implemented.
| private void | importData(javax.microedition.pim.Contact contact, java.lang.String prefix, java.lang.String[] attributes, java.lang.String data, java.lang.Object listHandle)Parses a single vCard line.
int attr = parseAttributes(attributes);
int field = VCardSupport.getFieldCode(prefix);
if (field == -1 && prefix.equals(getClassProperty())) {
field = Contact.CLASS;
}
if (!PIMHandler.getInstance()
.isSupportedField(listHandle, field)) {
return;
}
switch (field) {
case Contact.FORMATTED_NAME:
case Contact.FORMATTED_ADDR:
case Contact.TEL:
case Contact.EMAIL:
case Contact.TITLE:
case Contact.ORG:
case Contact.NICKNAME:
case Contact.NOTE:
case Contact.UID:
case Contact.URL: {
String sdata = FormatSupport.parseString(attributes, data);
contact.addString(field, attr, sdata);
break;
}
case Contact.NAME:
case Contact.ADDR: {
String[] elements =
FormatSupport.parseStringArray(attributes, data);
int elementCount = PIMHandler.getInstance()
.getStringArraySize(listHandle, field);
if (elements.length != elementCount) {
String[] a = new String[elementCount];
System.arraycopy(elements, 0, a, 0,
Math.min(elements.length, elementCount));
elements = a;
}
contact.addStringArray(field, attr, elements);
break;
}
case Contact.BIRTHDAY:
case Contact.REVISION: {
long date = PIMHandler.getInstance().parseDate(data);
contact.addDate(field, attr, date);
break;
}
case Contact.PHOTO: {
String valueType =
FormatSupport.getAttributeValue(attributes, "VALUE=", null);
if (valueType == null) {
// binary data
byte[] bdata = FormatSupport.parseBinary(attributes, data);
if (bdata.length != 0) {
contact.addBinary(Contact.PHOTO, attr, bdata, 0,
bdata.length);
}
} else if (valueType.equals("URL")) {
String sdata = FormatSupport.parseString(attributes, data);
contact.addString(Contact.PHOTO_URL, attr, sdata);
} else {
// ignore; value type not recognized
}
break;
}
case Contact.PUBLIC_KEY: {
String encoding = FormatSupport.getEncoding(attributes);
if (encoding.equals(FormatSupport.PLAIN_TEXT)) {
String sdata = FormatSupport.parseString(attributes, data);
contact.addString(Contact.PUBLIC_KEY_STRING, attr, sdata);
} else {
byte[] bdata = FormatSupport.parseBinary(attributes, data);
if (bdata.length != 0) {
contact.addBinary(Contact.PUBLIC_KEY, attr,
bdata, 0, bdata.length);
}
}
break;
}
case Contact.CLASS: {
int i = VCardSupport.getClassCode(data);
if (i != -1) {
contact.addInt(Contact.CLASS, Contact.ATTR_NONE, i);
}
break;
}
default:
// System.err.println("No match for field prefix '"
// + prefix + "' (REMOVE THIS MESSAGE)");
}
| public boolean | isTypeSupported(int pimListType)Checks to see if a given PIM list type is supported by this encoding.
return pimListType == PIM.CONTACT_LIST;
| protected abstract int | parseAttributes(java.lang.String[] attributes)Gets the binary value describing all flags in a vCard line.
| protected abstract void | writeAttributes(java.io.Writer w, int attributes)Writes the attributes for a field.
| protected void | writeDate(java.io.Writer w, long date)Writes a vCard date field.
w.write(PIMHandler.getInstance().composeDate(date));
| protected void | writeStringArray(java.io.Writer w, java.lang.String[] data)Writes a vCard field with multiple elements, such as ADR.
for (int i = 0; i < data.length; i++) {
if (data[i] != null) {
w.write(data[i]);
}
if (i != data.length -1) {
w.write(';");
}
}
| protected void | writeValue(java.io.Writer w, javax.microedition.pim.PIMItem item, int field, int index)Writes a single vCard line.
String label = VCardSupport.getFieldLabel(field);
switch (field) {
case Contact.FORMATTED_NAME:
case Contact.FORMATTED_ADDR:
case Contact.PHOTO_URL:
case Contact.TEL:
case Contact.EMAIL:
case Contact.TITLE:
case Contact.ORG:
case Contact.NICKNAME:
case Contact.NOTE:
case Contact.UID:
case Contact.URL:
case Contact.PUBLIC_KEY_STRING: {
String sValue = item.getString(field, index);
if (sValue != null) {
w.write(label);
writeAttributes(w, item.getAttributes(field, index));
w.write(":");
w.write(sValue);
w.write("\r\n");
}
break;
}
case Contact.NAME:
case Contact.ADDR: {
String[] aValue = item.getStringArray(field, index);
if (aValue != null) {
w.write(label);
writeAttributes(w, item.getAttributes(field, index));
w.write(":");
writeStringArray(w, aValue);
w.write("\r\n");
}
break;
}
case Contact.PHOTO:
case Contact.PUBLIC_KEY: {
byte[] bValue = item.getBinary(field, index);
if (bValue != null) {
w.write(label);
w.write(";ENCODING=");
w.write(getBinaryEncodingName());
writeAttributes(w, item.getAttributes(field, index));
w.write(":\r\n ");
w.write(Base64Encoding.toBase64(
bValue,
76 /* line width */,
4 /* indent */));
w.write("\r\n");
}
break;
}
case Contact.BIRTHDAY:
case Contact.REVISION:
w.write(label);
writeAttributes(w, item.getAttributes(field, index));
w.write(":");
writeDate(w, item.getDate(field, index));
w.write("\r\n");
break;
case Contact.CLASS: {
int iValue = item.getInt(field, index);
String sValue = VCardSupport.getClassType(iValue);
if (sValue != null) {
w.write(getClassProperty());
writeAttributes(w, item.getAttributes(field, index));
w.write(":");
w.write(sValue);
w.write("\r\n");
}
break;
}
default:
// field cannot be written. ignore it.
}
|
|