+ return lines;
+ }
+
+ /**
+ * Return a {@link String} representation of the given {@link Contact}, line
+ * by line.
+ *
+ * @param card
+ * the contact to convert
+ *
+ * @param startingBKey
+ * the starting BKey number (all the other will follow) or -1 for
+ * no BKey
+ *
+ * @return the {@link String} representation
+ */
+ public static List<String> toStrings(Contact contact, int startingBKey) {
+ List<String> lines = new LinkedList<String>();
+
+ lines.add("BEGIN:VCARD");
+ lines.add("VERSION:2.1");
+ for (Data data : contact) {
+ lines.addAll(toStrings(data));
+ }
+ lines.add("END:VCARD");
+
+ return lines;
+ }
+
+ /**
+ * Return a {@link String} representation of the given {@link Data}, line by
+ * line.
+ *
+ * @param data
+ * the data to convert
+ *
+ * @return the {@link String} representation
+ */
+ public static List<String> toStrings(Data data) {
+ List<String> lines = new LinkedList<String>();
+
+ StringBuilder dataBuilder = new StringBuilder();
+ if (data.getGroup() != null && !data.getGroup().trim().equals("")) {
+ dataBuilder.append(data.getGroup().trim());
+ dataBuilder.append('.');
+ }
+ dataBuilder.append(data.getName());
+ for (TypeInfo type : data) {
+ dataBuilder.append(';');
+ dataBuilder.append(type.getName());
+ if (type.getValue() != null && !type.getValue().trim().equals("")) {
+ dataBuilder.append('=');
+ dataBuilder.append(type.getValue());
+ }
+ }
+ dataBuilder.append(':');
+
+ // TODO: bkey!
+ dataBuilder.append(data.getValue());
+
+ // RFC says: Content lines SHOULD be folded to a maximum width of 75
+ // octets -> since it is SHOULD, we will just cut it as 74/75 chars
+ // depending if the last one fits in one char (note: chars != octet)
+ boolean continuation = false;
+ while (dataBuilder.length() > 0) {
+ int stop = 74;
+ if (continuation)
+ stop--; // the space takes 1
+ if (dataBuilder.length() > stop) {
+ char car = dataBuilder.charAt(stop - 1);
+ // RFC forbids cutting a character in 2
+ if (Character.isHighSurrogate(car)) {
+ stop++;
+ }
+ }
+
+ stop = Math.min(stop, dataBuilder.length());
+ if (continuation) {
+ lines.add(' ' + dataBuilder.substring(0, stop));
+ } else {
+ lines.add(dataBuilder.substring(0, stop));
+ }
+ dataBuilder.delete(0, stop);
+
+ continuation = true;
+ }