parse method

  1. @override
CalendarParsedResult? parse(
  1. Result result
)
override

Attempts to parse the raw Result's contents as a particular type of information (email, URL, etc.) and return a ParsedResult encapsulating the result of parsing.

@param theResult the raw Result to parse @return ParsedResult encapsulating the parsing result

Implementation

@override
CalendarParsedResult? parse(Result result) {
  final rawText = ResultParser.getMassagedText(result);
  final vEventStart = rawText.indexOf('BEGIN:VEVENT');
  if (vEventStart < 0) {
    return null;
  }

  final summary = _matchSingleVCardPrefixedField('SUMMARY', rawText);
  final start = _matchSingleVCardPrefixedField('DTSTART', rawText);
  if (start == null) {
    return null;
  }
  final end = _matchSingleVCardPrefixedField('DTEND', rawText);
  final duration = _matchSingleVCardPrefixedField('DURATION', rawText);
  final location = _matchSingleVCardPrefixedField('LOCATION', rawText);
  final organizer =
      _stripMailto(_matchSingleVCardPrefixedField('ORGANIZER', rawText));

  final attendees = _matchVCardPrefixedField('ATTENDEE', rawText);
  if (attendees != null) {
    for (int i = 0; i < attendees.length; i++) {
      attendees[i] = _stripMailto(attendees[i])!;
    }
  }
  final description = _matchSingleVCardPrefixedField('DESCRIPTION', rawText);

  final geoString = _matchSingleVCardPrefixedField('GEO', rawText);
  double latitude;
  double longitude;
  if (geoString == null) {
    latitude = double.nan;
    longitude = double.nan;
  } else {
    final semicolon = geoString.indexOf(';');
    if (semicolon < 0) {
      return null;
    }
    try {
      latitude = double.parse(geoString.substring(0, semicolon));
      longitude = double.parse(geoString.substring(semicolon + 1));
    } catch (_) {
      // NumberFormatException
      return null;
    }
  }

  try {
    return CalendarParsedResult(
      summary,
      start,
      end,
      duration,
      location,
      organizer,
      attendees,
      description,
      latitude,
      longitude,
    );
  } catch (_) {
    // IllegalArgumentException
    return null;
  }
}