public RulesetValidator(IValidationRuleset ruleset, string text) : this(ruleset) { iCalendarText = text; try { iCalendarSerializer serializer = new iCalendarSerializer(); // Turn off speed optimization so line/col from // antlr are accurate. serializer.OptimizeForSpeed = false; iCalendar = serializer.Deserialize(new StringReader(text), typeof(iCalendar)) as iCalendar; } catch (antlr.RecognitionException ex) { _RecognitionError = new ValidationErrorWithLookup( "calendarParseError", ValidationErrorType.Error, true, ex.line, ex.column); } }
public IValidationResult[] Validate() { if (Ruleset != null) { // If no iCalendar was provided, let's ensure it can // at least be basically parsed before moving on to // more in-depth validation rules. if (iCalendar == null && !string.IsNullOrEmpty(iCalendarText)) { try { StringReader sr = new StringReader(iCalendarText); // Turn off speed optimization to ensure we get proper // line/column numbers iCalendarSerializer serializer = new iCalendarSerializer(); serializer.OptimizeForSpeed = false; iCalendar calendar = serializer.Deserialize(sr, typeof(iCalendar)) as iCalendar; } catch (antlr.MismatchedTokenException ex) { return new IValidationResult[] { new ValidationResult( null, false, new IValidationError[] { new ValidationErrorWithLookup("calendarParseError", ValidationErrorType.Error, true, ex.line, ex.column) } ) }; } } // We've passed a basic parsing test, let's move // on to the more complex tests! List<IValidationResult> results = new List<IValidationResult>(); foreach (IValidationRule rule in Ruleset.Rules) { IValidator validator = null; Type validatorType = rule.ValidatorType; if (validatorType != null) validator = ValidatorActivator.Create(validatorType, iCalendar, iCalendarText); if (validator == null) { results.Add( new ValidationResult( rule.Name, false, new IValidationError[] { new ValidationError(null, "Validator for rule '" + rule.Name + "' could not be determined!") } ) ); } else { IValidationResult[] currentResults = validator.Validate(); results.AddRange(currentResults); // Determine if there were any fatal errors in the results. // If there are, then we need to abort any further processing! bool isFatal = false; foreach (IValidationResult result in currentResults) { if (result.Errors != null) { foreach (IValidationError err in result.Errors) { if (err.IsFatal) { isFatal = true; break; } } } if (isFatal) break; } if (isFatal) break; } } return results.ToArray(); } else return new IValidationResult[0]; }
public void Bug3512192() { IICalendar calendar = new iCalendar(); calendar.Method = "PUBLISH"; IEvent evt = calendar.Create<Event>(); evt.Summary = "Test Event"; evt.Start = new iCalDateTime(2012, 3, 27, 22, 00, 00); evt.Duration = TimeSpan.FromHours(1); var attendees = new List<IAttendee>(); var attendee = new Attendee("MAILTO:[email protected]") { CommonName = "Test Name", Role = "OPT-PARTICIPANT", Members = new List<string>() { "Other", "Name" } }; attendees.Add(attendee); evt.Attendees = attendees; // Serialize (save) the iCalendar var serializer = new iCalendarSerializer(calendar); var result = serializer.SerializeToString(calendar); var calendars = serializer.Deserialize(new StringReader(result)) as IICalendarCollection; calendar = calendars.First(); evt = calendar.Events.First(); Assert.AreEqual(1, evt.Attendees.Count); Assert.AreEqual(attendee, evt.Attendees[0]); Assert.AreEqual("Test Name", evt.Attendees[0].CommonName); Assert.AreEqual("OPT-PARTICIPANT", evt.Attendees[0].Role); Assert.AreEqual(1, evt.Attendees[0].Members.Count); }
public void Bug3485766() { IICalendar calendar = new iCalendar(); IEvent evt = calendar.Create<Event>(); evt.Start = new iCalDateTime(2012, 5, 23, 8, 0, 0); evt.Duration = TimeSpan.FromMinutes(30); // Ensure the DTStamp is in universal time to begin with Assert.IsTrue(evt.DTStamp.IsUniversalTime); // Convert to local time evt.DTStamp = new iCalDateTime(evt.DTStamp.Local); // Serialize the calendar var serializer = new iCalendarSerializer(); var serialized = serializer.SerializeToString(calendar); IICalendarCollection calendars = serializer.Deserialize(new StringReader(serialized)) as IICalendarCollection; calendar = calendars.First(); evt = calendar.Events[0]; // Ensure the object was serialized as UTC Assert.IsTrue(evt.DTStamp.IsUniversalTime); }