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

FormatSupport

public class FormatSupport extends Object
Supporting methods for interpreting vCard and vCalendar encodings.

Fields Summary
public static final String
QUOTED_PRINTABLE
Code name of the Quoted-Printable binary encoding.
public static final String
BASE64
Code name of the Base64 binary encoding.
public static final String
PLAIN_TEXT
Code name of plain text binary encoding.
public static final String
UTF8
Name of default character encoding.
public static final char
DAILY
Repeat rule daily frequency char representation.
public static final char
WEEKLY
Repeat rule weekly frequency char representation.
public static final char
MONTHLY
Repeat rule monthly frequency char representation.
public static final char
YEARLY
Repeat rule yearly frequency char representation.
public static final char
DAY_IN_MONTH
Repeat rule day-in-month char representation.
public static final char
WEEK_IN_MONTH
Repeat rule week-in-month char representation.
public static final char
DAY_IN_YEAR
Repeat rule day-in-year char representation.
public static final char
MONTH_IN_YEAR
Repeat rule month-in-year char representation.
Constructors Summary
Methods Summary
public static booleancontains(int[] a, int value)
Checks to see if a sorted array of integers contains a given integer.

param
a input array to be checked
param
value to be checked int the array
return
true if the value is found int the array

        // binary chop search
        int lowerBound = 0;
        int upperBound = a.length - 1;
        while (upperBound - lowerBound >= 0) {
            int i = lowerBound + (upperBound - lowerBound) / 2;
            int v = a[i];
            if (v > value) {
                // look between lowerBound and i
                upperBound = i - 1;
            } else if (v < value) {
                // look between i and upperBound
                lowerBound = i + 1;
            } else {
                return true;
            }
        }
        return false;
    
public static java.lang.StringconvertString(java.lang.String data, java.lang.String encoding, java.lang.String charset)
Converts a string from the given UTF-8 plain text encoding to the specified encoding.

param
data input data to be converted
param
encoding input data encoding
param
charset output encoding
return
encoded string

        if (encoding.equals(QUOTED_PRINTABLE)) {
            byte[] b = QuotedPrintableEncoding.fromQuotedPrintable(data);
            try {
                return new String(b, charset);
            } catch (UnsupportedEncodingException e) {
                // should not happen if charset was returned from getCharSet()
                return new String(b);
            }
        } else if (encoding.equals(BASE64)) {
            byte[] b = Base64Encoding.fromBase64(data);
            try {
                return new String(b, charset);
            } catch (UnsupportedEncodingException e) {
                // should not happen if charset was returned from getCharSet()
                return new String(b);
            }
        } else if (charset.equals(UTF8)) {
            return data;
        } else {
            try {
                return new String(data.getBytes(UTF8), charset);
            } catch (UnsupportedEncodingException e) {
                throw new Error(UTF8 + " encoding not available");
            }
        }
    
public static java.lang.StringgetAttributeValue(java.lang.String[] attributes, java.lang.String key, java.lang.String defaultValue)
Gets an attribute of the form (key)(value), if one exists in the supplied attributes list.

param
attributes an array of attributes
param
key the attribute key (e.g. "CHARSET=")
param
defaultValue a default value to be returned if no matching attribute is found.
return
the value of the requested attribute, or defaultValue if the attribute is not present.


        for (int i = 0; i < attributes.length; i++) {
            if (attributes[i].startsWith(key)) {
                return attributes[i].substring(key.length());
            }
        }
        return defaultValue;
    
public static java.lang.StringgetCharSet(java.lang.String[] attributes)
Gets the character set specified by the given property attributes. The default is UTF-8, unless the attributes contain a CHARSET= entry.

param
attributes an array of vCard or vCalendar property attributes
return
the encoding specified by the attributes


                                               
         
        String charset = getAttributeValue(attributes, "CHARSET=", UTF8);
        try {
            "".getBytes(charset);
            return charset;
        } catch (UnsupportedEncodingException e) {
            // cannot use this encoding.
            return UTF8;
        }
    
public static java.lang.StringgetEncoding(java.lang.String[] attributes)
Gets the encoding used for a value with the given attributes.

param
attributes an array of attributes
return
either VCardSupport.QUOTED_PRINTABLE, VCardSupport.BASE64 or VCardSupport.PLAIN_TEXT

        for (int i = 0; i < attributes.length; i++) {
            String s = attributes[i].toUpperCase();
            if (s.equals("ENCODING=QUOTED-PRINTABLE")
            || s.equals("QUOTED-PRINTABLE")) {
                return QUOTED_PRINTABLE;
            }
            if (s.equals("ENCODING=BASE64")
            || s.equals("BASE64")
            || s.equals("ENCODING=B")) {
                return BASE64;
            }
        }
        return PLAIN_TEXT;
    
public static java.lang.Stringjoin(java.lang.String[] elements, java.lang.String separator)
Joins the elements of a string array together into a single string.

param
elements the string array
param
separator the string to be included between each pair of successive elements
return
a string containing, alternately, elements of the string array and the separator string

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < elements.length; i++) {
            if (i > 0) {
                sb.append(separator);
            }
            sb.append(elements[i]);
        }
        return sb.toString();
    
public static byte[]parseBinary(java.lang.String[] attributes, java.lang.String data)
Interpret a vCard or vCalendar data element as a byte array, taking into account any encoding parameters specified in the attribute array.

param
attributes An array of attributes obtained from a class to parseObjectLine.
param
data The string data of a vCard or vCalendar object line, obtained from a call to parseObjectLine.
return
the decoded binary data

        String encoding = getEncoding(attributes);
        if (encoding.equals(QUOTED_PRINTABLE)) {
            return QuotedPrintableEncoding.fromQuotedPrintable(data);
        } else if (encoding.equals(BASE64)) {
            return Base64Encoding.fromBase64(data);
        } else {
            return data.getBytes();
        }
    
public static com.sun.kvem.midp.pim.formats.FormatSupport$DataElementparseObjectLine(java.lang.String line)
Extracts data from a vCard or vCalendar line.

param
line the input line, in the form (propertyname)[;(attributes)]:(data)
return
the property data
throws
UnsupportedPIMFormatException if the line is not in the expected format


        // break the line into property name, attributes and data
        int i = line.indexOf(':");
        if (i == -1 || i == 0) {
            // every line in a vCalendar object must have a colon delimiter
            throw new UnsupportedPIMFormatException(
            "Invalid line: '" + line + "'");
        }
        DataElement element = new DataElement();
        element.data = line.substring(i + 1).trim();
        String prefix = line.substring(0, i).trim();
        i = prefix.indexOf(';");
        if (i == -1) {
            element.propertyName = prefix.toUpperCase();
            element.attributes = new String[0];
        } else {
            element.propertyName = prefix.substring(0, i).toUpperCase();
            element.attributes = FormatSupport.split(prefix, ';", i + 1);
            for (int j = 0; j < element.attributes.length; j++) {
                element.attributes[j] = element.attributes[j].toUpperCase();
            }
        }
        // propertyName could contain a group name. (e.g. HOME.FN:)
        // we don't have to do anything with the group name - there is
        // really nothing to do with it - but we do have to process it.
        // remove a group name, if one exists:
        i = element.propertyName.lastIndexOf('.");
        if (i != -1) {
            element.propertyName = element.propertyName.substring(i + 1);
        }
        return element;
    
public static java.lang.StringparseString(java.lang.String[] attributes, java.lang.String data)
Interpret a vCard or vCalendar data element as a string, taking into account any encoding parameters specified in the attribute array.

param
attributes An array of attributes obtained from a class to parseObjectLine.
param
data The string data of a vCard or vCalendar object line, obtained from a call to parseObjectLine.
return
the decoded string data

        String charset = getCharSet(attributes);
        String encoding = getEncoding(attributes);
        return convertString(data, encoding, charset);
    
public static java.lang.String[]parseStringArray(java.lang.String[] attributes, java.lang.String data)
Interpret a vCard or vCalendar data element as a string array, taking into account any encoding parameters specified in the attribute array.

param
attributes An array of attributes obtained from a call to parseObjectLine.
param
data The string data of a vCard or vCalendar object line, obtained from a call to parseObjectLine.
return
the decoded string array data

        String charset = getCharSet(attributes);
        String encoding = getEncoding(attributes);
        String[] elements = split(data, ';", 0, false);
        for (int i = 0; i < elements.length; i++) {
            elements[i] = convertString(elements[i], encoding, charset);
            // treat empty elements as null
            if ("".equals(elements[i])) {
                elements[i] = null;
            }
        }
        return elements;
    
public static voidsort(int[] a)
Sorts an array of integers.

param
a the list of integers

        // insertion sort
        for (int j = 1; j < a.length; j++) {
            int v = a[j];
            int i = j - 1;
            while (i >= 0 && a[i] > v) {
                a[i + 1] = a[i];
                i--;
            }
            a[i + 1] = v;
        }
    
public static java.lang.String[]split(java.lang.String data, char separatorChar, int startingPoint, boolean skipFirstIfEmpty)
Parses a separated list of strings into a string array. An escaped separator (backslash followed by separatorChar) is not treated as a separator.

param
data input list to be parsed
param
separatorChar the character used to separate items
param
startingPoint Only use the part of the string that follows this index
param
skipFirstIfEmpty whether the first element should be skiped if it's empty (data starts with the separator). This flag is used to support empty category name
return
a non-null string array containing string elements

        if (startingPoint == data.length()) {
            return new String[0];
        }

        // support for empty category name:
        // if data starts with separator, just skip it
        if (skipFirstIfEmpty && data.startsWith("" + separatorChar)) {
            startingPoint++;
        }

        // tokenize elements
        Vector elementList = new Vector();
        int startSearchAt = startingPoint;
        int startOfElement = startingPoint;
        for (int i; (i = data.indexOf(separatorChar, startSearchAt)) != -1; ) {
            if (i != 0 && data.charAt(i - 1) == '\\") {
                // escaped separator. don't treat it as a real separator
                startSearchAt = i + 1;
            } else {
                String element = data.substring(startOfElement, i);
                elementList.addElement(element);
                startSearchAt = startOfElement = i + 1;
            }
        }

        // there is no separator found
        if (elementList.size() == 0) {
            return new String[] { data.substring(startOfElement) };
        }

        // add the last element
        elementList.addElement(data.substring(startOfElement));

        // convert Vector to array
        int size = elementList.size();
        String[] elements = new String[size];
        for (int i = 0; i < size; i++) {
            elements[i] = (String) elementList.elementAt(i);
        }

        return elements;
    
public static java.lang.String[]split(java.lang.String data, char separatorChar, int startingPoint)
Parses a separated list of strings into a string array. An escaped separator (backslash followed by separatorChar) is not treated as a separator.

param
data input list to be parsed
param
separatorChar the character used to separate items
param
startingPoint Only use the part of the string that follows this index
return
a non-null string array containing string elements

        return split(data, separatorChar, startingPoint, true);