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 java.io.IOException;
030: import java.io.Writer;
031:
032: /**
033: * Implementation of PIMEncoding for VCard/3.0.
034: *
035: */
036: public class VCard30Format extends VCardFormat {
037:
038: /**
039: * Writes the attributes for a field.
040: * @param w output stream target
041: * @param attributes fields to be written
042: * @throws IOException if an error occurs while writing
043: */
044: protected void writeAttributes(Writer w, int attributes)
045: throws IOException {
046: boolean writtenData = false;
047: for (int i = 0; i < 32; i++) {
048: long mask = 1l << i;
049: if ((attributes & mask) != 0) {
050: String attributeLabel = VCardSupport
051: .getAttributeLabel((int) mask);
052: if (attributeLabel != null) {
053: if (writtenData) {
054: w.write(",");
055: } else {
056: w.write(";TYPE=");
057: writtenData = true;
058: }
059: w.write(attributeLabel);
060: }
061: }
062: }
063: }
064:
065: /**
066: * Get the binary value describing all flags in a vCard line.
067: * @param attributes fields to parse
068: * @return binary coded flags
069: */
070: protected int parseAttributes(String[] attributes) {
071: int code = 0;
072: for (int i = 0; i < attributes.length; i++) {
073: if (attributes[i].startsWith("TYPE=")) {
074: String[] s = FormatSupport.split(attributes[i], ',', 5);
075: for (int j = 0; j < s.length; j++) {
076: code |= VCardSupport.getAttributeCode(s[j], 0);
077: }
078: } else {
079: code |= VCardSupport.getAttributeCode(attributes[i], 0);
080: }
081: }
082: return code;
083: }
084:
085: /**
086: * Gets the binary encoding name.
087: * @return the binary encoding name "B"
088: */
089: protected String getBinaryEncodingName() {
090: return "B";
091: }
092:
093: /**
094: * Gets the category property name.
095: * @return the category property name "CATEGORY"
096: */
097: protected String getCategoryProperty() {
098: return "CATEGORY";
099: }
100:
101: /**
102: * Gets the class property name.
103: * @return the class property name "CLASS"
104: */
105: protected String getClassProperty() {
106: return "CLASS";
107: }
108:
109: /**
110: * Gets the VCard version number.
111: * @return the VCard version number "3.0"
112: */
113: protected String getVersion() {
114: return "3.0";
115: }
116:
117: }
|