parse static method

ICalendar parse(
  1. String icsString
)

Implementation

static ICalendar parse(String icsString) {
  final lines = icsString.split('\n');
  CalHeaders? headers;
  List<ICalendarComponent> components = [];
  String? currentComponent;
  StringBuffer componentBuffer = StringBuffer();

  for (var line in lines) {
    line = line.trim();
    if (line.startsWith('BEGIN:')) {
      currentComponent = line.substring(6);
      componentBuffer = StringBuffer();
      componentBuffer.write('$line\n');
    } else if (line.startsWith('END:')) {
      final endComponent = line.substring(4);
      componentBuffer.write('$line\n');
      if (currentComponent == endComponent) {
        // Complete component
        if (currentComponent?.toUpperCase() == 'VEVENT') {
          components.add(VEvent.parse(componentBuffer.toString()));
        } else if (currentComponent?.toUpperCase() == 'VTODO') {
          components.add(VTodo.parse(componentBuffer.toString()));
        } else if (currentComponent?.toUpperCase() == 'EXDATE') {
          components.add(ExDate.parse(componentBuffer.toString()));
        } else if (currentComponent?.toUpperCase() == 'VALARM') {
          components.add(VAlarm.parse(componentBuffer.toString()));
        } else if (currentComponent?.toUpperCase() == 'VATTACHMENT') {
          components.add(VAttachment.parse(componentBuffer.toString()));
        }else if (currentComponent?.toUpperCase() == 'VFREEBUSY') {
          components.add(VFreeBusy.parse(componentBuffer.toString()));
        } else if (currentComponent?.toUpperCase() == 'VJOURNAL') {
          components.add(VJournal.parse(componentBuffer.toString()));
        } else if (currentComponent?.toUpperCase() == 'VPARTICIPANT') {
          components.add(VParticipant.parse(componentBuffer.toString()));
        } else if (currentComponent?.toUpperCase() == 'VTIMEZONE') {
          components.add(VTimezone.parse(componentBuffer.toString()));
        }
        currentComponent = null;
      }
    } else if (currentComponent != null) {
      componentBuffer.write('$line\n');
    } else {
      // Assume header lines
      if (line.startsWith('PRODID') || line.startsWith('VERSION') || line.startsWith('CALSCALE')) {
        headers ??= CalHeaders();
        final parts = line.split(':');
        if (parts.length == 2) {
          final key = parts[0];
          final value = parts.getRange(1, parts.length).join(':');
          switch (key) {
            case 'PRODID':
              headers.prodId = value;
              break;
            case 'VERSION':
              headers.version = value;
              break;
            case 'CALSCALE':
              headers.calScale = value;
              break;
          }
        }
      }
    }
  }

  final calendar = ICalendar._internal(headers ?? CalHeaders());
  calendar.components = components;
  return calendar;
}