FileDocCategorySizeDatePackage
VCardFormat.javaAPI DocphoneME MR2 API (J2ME)16386Wed May 02 18:00:28 BST 2007com.sun.kvem.midp.pim.formats

VCardFormat

public abstract class VCardFormat extends EndMatcher implements com.sun.kvem.midp.pim.PIMFormat
Partial implementation of PIMEncoding for VCard/2.1 and VCard/3.0.

Fields Summary
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.

param
in Stream containing serialized data
param
encoding Character encoding of the stream
param
list PIMList to which items should be added, or null if the items should not be part of a list
throws
UnsupportedPIMFormatException if the serialized data cannot be interpreted by this encoding.
return
a non-empty array of PIMItems containing the objects described in the serialized data, or null if no items are available
throws
IOException if an error occurs while reading


        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.ContactImpldecode(com.sun.kvem.midp.pim.LineReader in, javax.microedition.pim.PIMList list)
Constructs a single PIMItem from serialized data.

param
in LineReader containing serialized data
param
list PIM list to which the item belongs
throws
UnsupportedPIMFormatException if the serialized data cannot be interpreted by this encoding.
return
an unserialized Contact, or null if no data was available

        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 voidencode(java.io.OutputStream out, java.lang.String encoding, javax.microedition.pim.PIMItem pimItem)
Serializes a PIMItem.

param
out Stream to which serialized data is written
param
encoding Character encoding to use for serialized data
param
pimItem The item to write to the stream
throws
IOException if an error occurs while writing

        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.StringgetBinaryEncodingName()
Gets the name of the default binary encoding. This is "BASE64" for vCard 2.1 and "B" for vCard 3.0.

return
the default binary encoding

protected abstract java.lang.StringgetCategoryProperty()
Gets the vCard property name used to store categories.

return
the vCard category property name

protected abstract java.lang.StringgetClassProperty()
Gets the vCard property name used to store classes.

return
the class property name

public java.lang.StringgetName()
Gets the code name of this encoding (e.g. "VCARD/2.1").

return
the encoding name

        return "VCARD/" + getVersion();
    
protected abstract java.lang.StringgetVersion()
Returns the version number of vCard implemented.

return
the VCard version number

private voidimportData(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.

param
contact element to populate
param
prefix filter for selecting fields
param
attributes fields to be processed
param
data input to be filtered
param
listHandle handle of the list containing the contact being parsed


        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 booleanisTypeSupported(int pimListType)
Checks to see if a given PIM list type is supported by this encoding.

param
pimListType int representing the PIM list type to check
return
true if the type can be read and written by this encoding, false otherwise

        return pimListType == PIM.CONTACT_LIST;
    
protected abstract intparseAttributes(java.lang.String[] attributes)
Gets the binary value describing all flags in a vCard line.

param
attributes fields to be parsed
return
binary coded settings

protected abstract voidwriteAttributes(java.io.Writer w, int attributes)
Writes the attributes for a field.

param
w output stream target
param
attributes data to be written
throws
IOException if an error occurs while writing

protected voidwriteDate(java.io.Writer w, long date)
Writes a vCard date field.

param
w output stream target
param
date data to be written
throws
IOException if an error occurs while writing

        w.write(PIMHandler.getInstance().composeDate(date));
    
protected voidwriteStringArray(java.io.Writer w, java.lang.String[] data)
Writes a vCard field with multiple elements, such as ADR.

param
w output stream target
param
data the strings to write
throws
IOException if an error occurs while writing

        for (int i = 0; i < data.length; i++) {
            if (data[i] != null) {
                w.write(data[i]);
            }
            if (i != data.length -1) {
                w.write(';");
            }
        }
    
protected voidwriteValue(java.io.Writer w, javax.microedition.pim.PIMItem item, int field, int index)
Writes a single vCard line.

param
w output stream target
param
item the data to to written
param
field the attribute to be processed
param
index the offset of the data to be processed
throws
IOException if an error occurs while writing


        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.
        }