VCal vc = new VCal();
int i, j, start;
int N, M;
// first we deal with line folding, by replacing all "\r\n " strings
// with nothing
str = str.replaceAll("\r\n ", "");
// it's supposed to be \r\n, but not everyone does that
str = str.replaceAll("\r\n", "\n");
str = str.replaceAll("\r", "\n");
ArrayList<Parameter> params = new ArrayList<Parameter>();
ArrayList<Property> props = vc.properties;
// then we split into lines
String[] lines = str.split("\n");
Begin begin = null;
//System.out.println("lines.length=" + lines);
N = lines.length;
for (j=0; j<N; j++) {
//System.out.println("===[" + lines[j] + "]===");
String line = lines[j];
int len = line.length();
if (len > 0) {
i = 0;
char c;
do {
c = line.charAt(i);
i++;
} while (c != ';" && c != ':");
String n = line.substring(0, i-1);
Property prop = make(n);
props.add(prop);
if (n.equals("BEGIN")) {
Begin b = (Begin)prop;
b.parent = begin;
begin = b;
props = begin.properties;
}
else if (n.equals("END")) {
begin = begin.parent;
if (begin != null) {
props = begin.properties;
} else {
props = vc.properties;
}
}
//System.out.println("name=[" + prop.name + "]");
params.clear();
while (c == ';") {
Parameter param = new Parameter();
start = i;
i++;
// param name
do {
c = line.charAt(i);
i++;
} while (c != '=");
param.name = line.substring(start, i-1);
//System.out.println(" param.name=[" + param.name + "]");
start = i;
if (line.charAt(start) == '"") {
i++;
start++;
do {
c = line.charAt(i);
i++;
} while (c != '"");
param.value = line.substring(start, i-1);
c = line.charAt(i);
i++;
//System.out.println(" param.valueA=[" + param.value
// + "]");
} else {
do {
c = line.charAt(i);
i++;
} while (c != ';" && c != ':");
param.value = line.substring(start, i-1);
//System.out.println(" param.valueB=["
// + param.value + "]");
}
params.add(param);
}
Object[] array = params.toArray();
prop.parameters = new Parameter[array.length];
System.arraycopy(array, 0, prop.parameters, 0, array.length);
if (c != ':") {
throw new RuntimeException("error finding ':' c=" + c);
}
prop.value = line.substring(i);
prop.values = line.split(",");
}
}
N = vc.properties.size();
Calendar calStart = null;
for (i=0; i<N; i++) {
Property prop = vc.properties.get(i);
String n = prop.name;
if (n.equals("DTSTART")) {
try {
calStart = parseDateTime(prop, vc);
vc.dtstart = prop.value;
} catch (DateException de) {
Log.w("CalendarProvider", "Unable to parse DTSTART=" + n, de);
return null;
}
} else if (n.equals("DTEND")) {
// TODO: store the dtend, compute when expanding instances?
// will we ever need to deal with seeing the DTEND before the
// DTSTART?
try {
if (calStart == null) {
vc.duration = "+P0S";
} else {
Calendar calEnd =
parseDateTime(prop, vc);
long durationMillis =
calEnd.getTimeInMillis() -
calStart.getTimeInMillis();
long durationSeconds = (durationMillis / 1000);
vc.duration = "+P" + durationSeconds + "S";
}
} catch (DateException de) {
Log.w("CalendarProvider", "Unable to parse DTEND=" + n, de);
return null;
}
} else if (n.equals("DURATION")) {
vc.duration = prop.value;
} else if (n.equals("RRULE")) {
vc.rrule = prop.value;
}
}
return vc;