001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.kvem.midp.pim.formats;
028:
029: import javax.microedition.pim.Contact;
030:
031: /**
032: * Helper methods for vCard implementations.
033: *
034: */
035: public class VCardSupport {
036: /**
037: * Gets the field label.
038: * @param field identifier for field
039: * @return label of requested field
040: */
041: public static String getFieldLabel(int field) {
042: switch (field) {
043: case Contact.FORMATTED_NAME:
044: return "FN";
045: case Contact.ADDR:
046: return "ADR";
047: case Contact.BIRTHDAY:
048: return "BDAY";
049: case Contact.NAME:
050: return "N";
051: case Contact.PHOTO:
052: return "PHOTO;ENCODING=BASE64";
053: case Contact.PHOTO_URL:
054: return "PHOTO;VALUE=URL";
055: case Contact.TEL:
056: return "TEL";
057: case Contact.TITLE:
058: return "TITLE";
059: case Contact.REVISION:
060: return "REV";
061: case Contact.URL:
062: return "URL";
063: case Contact.UID:
064: return "UID";
065: case Contact.PUBLIC_KEY:
066: return "KEY;ENCODING=BASE64";
067: case Contact.FORMATTED_ADDR:
068: return "LABEL";
069: case Contact.NICKNAME:
070: return "NICKNAME";
071: case Contact.NOTE:
072: return "NOTE";
073: case Contact.PUBLIC_KEY_STRING:
074: return "KEY";
075: case Contact.EMAIL:
076: return "EMAIL";
077: case Contact.ORG:
078: return "ORG";
079: default:
080: return null;
081: }
082: }
083:
084: /**
085: * Lookup the field identifier byte name.
086: * @param fieldName label for field
087: * @return identifier for requested field
088: */
089: public static int getFieldCode(String fieldName) {
090: if (fieldName.equals("FN"))
091: return Contact.FORMATTED_NAME;
092: else if (fieldName.equals("LABEL"))
093: return Contact.FORMATTED_ADDR;
094: else if (fieldName.equals("ADR"))
095: return Contact.ADDR;
096: else if (fieldName.equals("BDAY"))
097: return Contact.BIRTHDAY;
098: else if (fieldName.equals("N"))
099: return Contact.NAME;
100: else if (fieldName.equals("PHOTO"))
101: return Contact.PHOTO;
102: else if (fieldName.equals("TEL"))
103: return Contact.TEL;
104: else if (fieldName.equals("TITLE"))
105: return Contact.TITLE;
106: else if (fieldName.equals("REV"))
107: return Contact.REVISION;
108: else if (fieldName.equals("URL"))
109: return Contact.URL;
110: else if (fieldName.equals("UID"))
111: return Contact.UID;
112: else if (fieldName.equals("KEY"))
113: return Contact.PUBLIC_KEY;
114: else if (fieldName.equals("NICKNAME"))
115: return Contact.NICKNAME;
116: else if (fieldName.equals("NOTE"))
117: return Contact.NOTE;
118: else if (fieldName.equals("EMAIL"))
119: return Contact.EMAIL;
120: else if (fieldName.equals("ORG"))
121: return Contact.ORG;
122: else
123: return -1;
124: }
125:
126: /**
127: * Lookup the attribute name from identifier.
128: * @param attr the field identifier
129: * @return the name of the attribute
130: */
131: public static String getAttributeLabel(int attr) {
132: switch (attr) {
133: case Contact.ATTR_ASST:
134: return "X-J2MEWTK-ASST";
135: case Contact.ATTR_AUTO:
136: return "CAR";
137: case Contact.ATTR_FAX:
138: return "FAX";
139: case Contact.ATTR_HOME:
140: return "HOME";
141: case Contact.ATTR_MOBILE:
142: return "CELL";
143: case Contact.ATTR_OTHER:
144: return "X-J2MEWTK-OTHER";
145: case Contact.ATTR_PAGER:
146: return "PAGER";
147: case Contact.ATTR_PREFERRED:
148: return "PREF";
149: case Contact.ATTR_SMS:
150: return "MSG";
151: case Contact.ATTR_WORK:
152: return "WORK";
153: default:
154: return Extensions.getContactAttributeLabel(attr);
155: }
156: }
157:
158: /**
159: * Lookup the attribute identifier.
160: * @param label the name of the attribute
161: * @param defaultValue default value to return if
162: * the attribute identifier is not found
163: * @return identifier for requested attribute
164: */
165: public static int getAttributeCode(String label, int defaultValue) {
166: if (label.equals("CAR"))
167: return Contact.ATTR_AUTO;
168: else if (label.equals("FAX"))
169: return Contact.ATTR_FAX;
170: else if (label.equals("HOME"))
171: return Contact.ATTR_HOME;
172: else if (label.equals("CELL"))
173: return Contact.ATTR_MOBILE;
174: else if (label.equals("X-J2MEWTK-OTHER"))
175: return Contact.ATTR_OTHER;
176: else if (label.equals("PAGER"))
177: return Contact.ATTR_PAGER;
178: else if (label.equals("PREF"))
179: return Contact.ATTR_PREFERRED;
180: else if (label.equals("MSG"))
181: return Contact.ATTR_SMS;
182: else if (label.equals("WORK"))
183: return Contact.ATTR_WORK;
184: else if (label.equals("X-J2MEWTK-ASST"))
185: return Contact.ATTR_ASST;
186: else
187: return Extensions.getContactAttributeCode(label,
188: defaultValue);
189: }
190:
191: /**
192: * Gets the value of the vCard CLASS field for the given
193: * value of the Contact.CLASS field.
194: * This method encapsulates the following mapping:
195: * Contact.CLASS_PUBLIC -> "PUBLIC"
196: * Contact.CLASS_PRIVATE -> "PRIVATE"
197: * Contact.CLASS_CONFIDENTIAL -> "CONFIDENTIAL"
198: *
199: * @param fieldValue the value of the Contact.CLASS field
200: * @return a string describing the class for the field value, or null if
201: * fieldValue is out of range
202: */
203: public static String getClassType(int fieldValue) {
204: switch (fieldValue) {
205: case Contact.CLASS_CONFIDENTIAL:
206: return "CONFIDENTIAL";
207: case Contact.CLASS_PRIVATE:
208: return "PRIVATE";
209: case Contact.CLASS_PUBLIC:
210: return "PUBLIC";
211: }
212: return null;
213: }
214:
215: /**
216: * Gets the value of the Contact.CLASS field for the given
217: * value of the vCard CLASS property.
218: * This method encapsulates the following mapping:
219: * Contact.CLASS_PUBLIC <- "PUBLIC"
220: * Contact.CLASS_PRIVATE <- "PRIVATE"
221: * Contact.CLASS_CONFIDENTIAL <- "CONFIDENTIAL"
222: *
223: * @param s the value of the CLASS property
224: * @return the corresponding field of Contact, or -1 if s is not recognized
225: */
226: public static int getClassCode(String s) {
227: switch (s.length()) {
228: case 6:
229: if (s.equals("PUBLIC")) {
230: return Contact.CLASS_PUBLIC;
231: }
232: break;
233: case 7:
234: if (s.equals("PRIVATE")) {
235: return Contact.CLASS_PRIVATE;
236: }
237: break;
238: case 12:
239: if (s.equals("CONFIDENTIAL")) {
240: return Contact.CLASS_CONFIDENTIAL;
241: }
242: break;
243: }
244: return -1;
245: }
246:
247: }
|