[Bedework-commit] carddav r247 -
trunk/clients/javascript/bwAddrbookClient/resources
svnadmin at bedework.org
svnadmin at bedework.org
Tue Feb 8 16:42:24 EST 2011
Author: bleibson
Date: 2011-02-08 16:42:21 -0500 (Tue, 08 Feb 2011)
New Revision: 247
Modified:
trunk/clients/javascript/bwAddrbookClient/resources/vcardParser.js
Log:
o A little of everything including better handling of colons, backslashes, line-continuations....
Modified: trunk/clients/javascript/bwAddrbookClient/resources/vcardParser.js
===================================================================
--- trunk/clients/javascript/bwAddrbookClient/resources/vcardParser.js 2011-02-08 21:42:17 UTC (rev 246)
+++ trunk/clients/javascript/bwAddrbookClient/resources/vcardParser.js 2011-02-08 21:42:21 UTC (rev 247)
@@ -41,7 +41,7 @@
// replace one or more backslashes followed by comma with comma
// replace double-quote with backslash double-quote (escape it)
// replace one or more backslashes followed by a semi-colon with a semi-colon
- cleanedString = string.replace(/\\,/g,",").replace(/\"/g,'\\"').replace(/\\;/g,";");
+ cleanedString = string.replace(/\\+,/g,",").replace(/"/g,'\\"').replace(/\\+;/g,";");
return cleanedString;
}
@@ -121,11 +121,29 @@
var lines = blob.split('\n');
var lastAttributeName = "";
for (var i=0;i<lines.length;i++) {
+ var linebuffer = lines[i];
+ var example = lines[i].match(/Michael/);
+ if (example != null) {
+ alert(example[0]);
+ }
//each line is in the form of a key[;param;param]:value. Sometimes the value contains colons, too.
- if (lines[i] != "") {
- var colonSplit = lines[i].split(':');
+ if (linebuffer != "") {
+ while (i + 1 < lines.length) {
+ //append continuation lines (lines that start with a space character)
+ var rawline = lines[i + 1];
+ var leadingSpace = rawline.match(/^ /);
+ if (leadingSpace != null) {
+ //append this line and avoid processing it the next time through the loop
+ linebuffer += jQuery.trim(rawline);
+ i++;
+ } else {
+ //if the next line doesn't begin with space, move on.
+ break;
+ }
+ }
+ var colonSplit = linebuffer.split(/:(.+)/);
- //split out the key and the paramaters
+ //split out the key and the parameters
var semiColonSplit = colonSplit[0].split(';');
var attribute = semiColonSplit[0];
var attributeInfo = new Array();
@@ -152,16 +170,16 @@
//locate any parameters in the key and write out the parameter array
bwJsonObj += '"params": {'
for (var n=1;n<semiColonSplit.length;n++) {
- var equalsSplit = semiColonSplit[n].split('=');
+ var equalsSplit = semiColonSplit[n].split('=');
- // THIS ISN'T COMPLETE -- NEED to split on comma, too.
- bwJsonObj += '"parameter-name": "' + cleanUpString(equalsSplit[0]) + '",'
- bwJsonObj += '"parameter-value": "' + cleanUpString(equalsSplit[1]) + '"'
+ // THIS ISN'T COMPLETE -- NEED to split on comma, too.
+ bwJsonObj += '"parameter-name": "' + cleanUpString(equalsSplit[0]) + '",'
+ bwJsonObj += '"parameter-value": "' + cleanUpString(equalsSplit[1]) + '"'
- //add a comma between parameters (avoid adding at end)
- if (n != semiColonSplit.length - 1) {
- bwJsonObj += ',';
- }
+ //add a comma between parameters (avoid adding at end)
+ if (n != semiColonSplit.length - 1) {
+ bwJsonObj += ',';
+ }
}
bwJsonObj += '},'
@@ -180,48 +198,12 @@
tmpString = '"' + cleanUpString(colonSplit[1]);
bwJsonObj += jQuery.trim(tmpString);
}
-
- //put back colon(s) and write out what's past the first colon
- for (k=2;k<colonSplit.length;k++) {
- tmpString = ':' + cleanUpString(colonSplit[k]);
- bwJsonObj += jQuery.trim(tmpString);
- }
-
- //look ahead and see if there's more in the next line. Continuation lines begin with a space.
- while (i + 1 < lines.length) {
- var rawline = lines[i + 1];
- if (rawline.substring(0,1) == ' ') {
- //append this line and avoid processing it the next time through the loop
- tmpString = ':' + cleanUpString(rawline);
- bwJsonObj += jQuery.trim(tmpString);
- i++;
- } else {
- //if the next line doesn't begin with space, move on.
- break;
- }
- }
bwJsonObj += '"}';
}
if (attributeType == 2) {
-
- //look ahead and see if there's more in the next line. Continuation lines begin with a space.
- while (i + 1 < lines.length) {
- var rawline = lines[i + 1];
- if (rawline.substring(0,1) == ' ') {
- //append this line and avoid processing it the next time through the loop
- tmpString = ':' + cleanUpString(rawline);
- colonSplit[1] = jQuery.trim(colonSplit[1]) + jQuery.trim(tmpString);
- i++;
- } else {
- //if the next line doesn't begin with space, move on.
- break;
- }
- }
//multiple named values
- //Will need to deal with the possibility of colons in the individual values.
-
var attributeFieldValues = colonSplit[1].split(';');
bwJsonObj += '"values": {';
//one array goes from 1 to length-1 and the other from 0 to length-1. Hope it's clear.
More information about the Bedework-commit
mailing list