/// <summary> /// <para>Overrides the method, which loads the previously saved state in the ViewState. The DatePattern object is /// loaded here from the ViewState.</para> /// </summary> /// <param name="savedState"> /// any object instance. This argument is not directly used in this method - it is simply sent to the super /// method. Can be any value. /// </param> protected override void LoadViewState(object savedState) { base.LoadViewState(savedState); //Retrieve the DatePattern from Viewstate DatePattern pattern = ViewState["DatePattern_" + ID] as DatePattern; if (pattern != null) { datePattern = pattern; } }
public void TestConvertFrom() { string stringRep = File.ReadAllText("../../test_files/TestStringRep.txt"); object dp = dpc.ConvertFrom(new CustomTypeDescriptorContext(), new CultureInfo("en"), stringRep); Assert.IsTrue(dp is DatePattern, "Wrong ConvertFrom implementation."); DatePattern objDp = dp as DatePattern; //Check the DatePattern formed Assert.AreEqual(objDp.DisplayDateFormat, "MM/dd/yyyy", "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.InputDateFormat, "yyyy-MM-dd", "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.StartDate, "2007-01-01", "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.StopDate, "2008-12-31", "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.Rules.Count, 14, "Wrong ConvertFrom implementation."); //Check one rule also Assert.AreEqual(objDp.Rules[3].DateType, "DayMonth", "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.Rules[3].DateValue, "02-29", "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.Rules[3].Interleave, 0, "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.Rules[3].ReverseOrder, false, "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.Rules[3].TimeValue, "6:00 PM", "Wrong ConvertFrom implementation."); Assert.AreEqual(objDp.Rules[3].YearDivisor, 4, "Wrong ConvertFrom implementation."); }
/// <summary> /// <para>Overrides the method, which converts the given value to the specified type, using the specified context /// and culture information.</para> /// <para>Exceptions Handling: throws ArgumentNullException if any argument (except /// cultureInfo and value) is null. throws ArgumentException if the value contains not valid DatePattern /// instance. </para> /// </summary> /// <param name="context"> /// an instance of the ITypeDescriptorContext that provides a format context. Can be null. /// </param> /// <param name="culture"> /// an instance of CultureInfo to use as the current culture. Can be null - means that the current culture /// should be used. /// </param> /// <param name="value"> /// and instance of the Object to convert. Can be null. If not null, then should contain a valid DatePattern /// instance. /// </param> /// <param name="destinationType"> /// an instance of Type object to convert the "value" argument to. Can not be null. /// </param> /// <returns> /// an Object instance representing the converted value. In will be string instance in most cases. It should not /// be null. /// </returns> /// <exception cref="ArgumentNullException">If destinationType is null</exception> /// <exception cref="ArgumentException">if the value contains not DatePattern instance.</exception> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { //Must check this before checking for the type of 'value' if (value == null && destinationType == typeof(string)) { return(String.Empty); } //Validate if (!(value is DatePattern)) { throw new ArgumentException("value must be of type DatePattern.", "value"); } Helper.ValidateNotNull(destinationType, "destinationType"); if (destinationType == typeof(string)) { //Prepare StringBuilder builder = new StringBuilder(); DatePattern objDatePattern = value as DatePattern; using (StringWriter stringWriter = new StringWriter()) { using (XmlTextWriter writer = new XmlTextWriter(stringWriter)) { //Serialize //Serialize the DatePattern instance. writer.WriteStartElement("DatePattern"); if (objDatePattern.InputDateFormat != null && !objDatePattern.InputDateFormat.Equals(String.Empty)) { writer.WriteAttributeString("InputDateFormat", objDatePattern.InputDateFormat); } if (objDatePattern.DisplayDateFormat != null && !objDatePattern.DisplayDateFormat.Equals(String.Empty)) { writer.WriteAttributeString("DisplayDateFormat", objDatePattern.DisplayDateFormat); } if (objDatePattern.StartDate != null && !objDatePattern.StartDate.Equals(String.Empty)) { writer.WriteAttributeString("StartDate", objDatePattern.StartDate); } if (objDatePattern.StopDate != null && !objDatePattern.StopDate.Equals(String.Empty)) { writer.WriteAttributeString("StopDate", objDatePattern.StopDate); } //Serialize each child rule of the DatePattern instance. foreach (Rule rule in objDatePattern.Rules) { writer.WriteStartElement("Rule"); if (!(rule.DateType.Equals(String.Empty))) { writer.WriteAttributeString("DateType", rule.DateType); } if (!(rule.DateValue.Equals(String.Empty))) { writer.WriteAttributeString("DateValue", rule.DateValue); } if (!(rule.TimeValue.Equals(String.Empty))) { writer.WriteAttributeString("TimeValue", rule.TimeValue); } if (rule.YearDivisor != 1) { writer.WriteAttributeString("YearDivisor", rule.YearDivisor.ToString()); } if (rule.ReverseOrder != false) { writer.WriteAttributeString("ReverseOrder", rule.ReverseOrder.ToString().ToLower()); } if (rule.Interleave != 0) { writer.WriteAttributeString("Interleave", rule.Interleave.ToString()); } writer.WriteEndElement(); } writer.WriteEndElement(); } return(stringWriter.ToString()); } } else { return(base.ConvertTo(context, culture, value, destinationType)); } }
/// <summary> /// <para>Overrides the method, which converts the given object to the type of this converter, /// using the specified context and culture information.</para> /// </summary> /// <param name="context"> /// an instance of the ITypeDescriptorContext that provides a format context. Can be null. /// </param> /// <param name="culture">an instance of CultureInfo to use as the current culture. Cannot be null.</param> /// <param name="value">an instance of the Object to convert. Can be null.</param> /// <returns> /// an Object instance representing the converted value. In will be DatePattern instance in most cases. It /// should not be null. /// </returns> /// <exception cref="ArgumentException">if the value contains /// not valid string representation of the DatePattern.</exception> /// <exception cref="ArgumentNullException">If culture is null.</exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { Helper.ValidateNotNull(culture, "culture"); DatePattern ret = new DatePattern(); if (value == null) { return(ret); } if (value is string) { string stringRep = value as string; //Empty string representation means a new DatePattern if (stringRep.Trim().Equals(String.Empty)) { return(ret); } try { //Using XmlTextReader so that special characters in date format string like '<' can be handled! using (XmlTextReader reader = new XmlTextReader(stringRep, XmlNodeType.Document, null)) { Rule rule = null; while (reader.Read()) { //Only interesed in the Element nodes. if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { //Create the DatePattern instance with its properties case "DatePattern": { ret.InputDateFormat = GetDatePatternPropertyValue( reader.GetAttribute("InputDateFormat"), String.Empty); ret.DisplayDateFormat = GetDatePatternPropertyValue( reader.GetAttribute("DisplayDateFormat"), String.Empty); ret.StartDate = GetDatePatternPropertyValue( reader.GetAttribute("StartDate"), String.Empty); ret.StopDate = GetDatePatternPropertyValue( reader.GetAttribute("StopDate"), String.Empty); break; } //Create the Rule instance with its properties case "Rule": { if (rule != null) { ret.Rules.Add(rule); } rule = new Rule(); rule.DateValue = GetDatePatternPropertyValue( reader.GetAttribute("DateValue"), String.Empty); rule.DateType = GetDatePatternPropertyValue( reader.GetAttribute("DateType"), String.Empty); rule.TimeValue = GetDatePatternPropertyValue( reader.GetAttribute("TimeValue"), String.Empty); rule.ReverseOrder = bool.Parse(GetDatePatternPropertyValue( reader.GetAttribute("ReverseOrder"), "False")); rule.YearDivisor = int.Parse(GetDatePatternPropertyValue( reader.GetAttribute("YearDivisor"), "1")); rule.Interleave = int.Parse(GetDatePatternPropertyValue( reader.GetAttribute("Interleave"), "0")); break; } } } } //The last rule still needs to be added if (rule != null) { ret.Rules.Add(rule); } return(ret); } } catch (Exception e) { throw new ArgumentException("Could not perform deserialization of DatePattern.", e); } } else { return(base.ConvertFrom(context, culture, value)); } }
public void TearDown() { dp = null; }
public void SetUp() { dp = new DatePattern(); }