PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/DDay.iCal/Serialization/iCalendar/Serializers/PropertySerializer.cs

https://bitbucket.org/mdavid/dday-ical-svn
C# | 149 lines | 94 code | 26 blank | 29 comment | 15 complexity | 252a3cda7f07e798bfd933dcc5f2affd MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Collections;
  6. namespace DDay.iCal.Serialization.iCalendar
  7. {
  8. public class PropertySerializer :
  9. SerializerBase
  10. {
  11. #region Constructors
  12. public PropertySerializer()
  13. {
  14. }
  15. public PropertySerializer(ISerializationContext ctx) : base(ctx)
  16. {
  17. }
  18. #endregion
  19. #region Overrides
  20. public override Type TargetType
  21. {
  22. get { return typeof(CalendarProperty); }
  23. }
  24. public override string SerializeToString(object obj)
  25. {
  26. ICalendarProperty prop = obj as ICalendarProperty;
  27. if (prop != null &&
  28. prop.Value != null)
  29. {
  30. // Don't serialize the property if the value is null
  31. // Push this object on the serialization context.
  32. SerializationContext.Push(prop);
  33. IDataTypeMapper mapper = GetService<IDataTypeMapper>();
  34. Type serializedType = mapper.GetPropertyMapping(prop);
  35. // Build a list of values that are to be serialized.
  36. List<object> objs = new List<object>();
  37. if (!(prop.Value is string) &&
  38. !(typeof(IEnumerable<string>).IsAssignableFrom(serializedType)) &&
  39. prop.Value is IEnumerable)
  40. {
  41. foreach (object v in (IEnumerable)prop.Value)
  42. objs.Add(v);
  43. }
  44. else objs.Add(prop.Value);
  45. // Get a serializer factory that we can use to serialize
  46. // the property and parameter values
  47. ISerializerFactory sf = GetService<ISerializerFactory>();
  48. StringBuilder result = new StringBuilder();
  49. foreach (object v in objs)
  50. {
  51. // Get a serializer to serialize the property's value.
  52. // If we can't serialize the property's value, the next step is worthless anyway.
  53. IStringSerializer valueSerializer = sf.Build(v.GetType(), SerializationContext) as IStringSerializer;
  54. if (valueSerializer != null)
  55. {
  56. // Iterate through each value to be serialized,
  57. // and give it a property (with parameters).
  58. // FIXME: this isn't always the way this is accomplished.
  59. // Multiple values can often be serialized within the
  60. // same property. How should we fix this?
  61. // NOTE:
  62. // We Serialize the property's value first, as during
  63. // serialization it may modify our parameters.
  64. // FIXME: the "parameter modification" operation should
  65. // be separated from serialization. Perhaps something
  66. // like PreSerialize(), etc.
  67. string value = valueSerializer.SerializeToString(v);
  68. // Get the list of parameters we'll be serializing
  69. ICalendarParameterList parameterList = prop.Parameters;
  70. if (v is ICalendarDataType)
  71. parameterList = ((ICalendarDataType)v).Parameters;
  72. StringBuilder sb = new StringBuilder(prop.Name);
  73. if (parameterList.Count > 0)
  74. {
  75. // Get a serializer for parameters
  76. IStringSerializer parameterSerializer = sf.Build(typeof(ICalendarParameter), SerializationContext) as IStringSerializer;
  77. if (parameterSerializer != null)
  78. {
  79. // Serialize each parameter
  80. List<string> parameters = new List<string>();
  81. foreach (ICalendarParameter param in parameterList)
  82. {
  83. parameters.Add(parameterSerializer.SerializeToString(param));
  84. }
  85. // Separate parameters with semicolons
  86. sb.Append(";");
  87. sb.Append(string.Join(";", parameters.ToArray()));
  88. }
  89. }
  90. sb.Append(":");
  91. sb.Append(value);
  92. result.Append(TextUtil.WrapLines(sb.ToString()));
  93. }
  94. }
  95. // Pop the object off the serialization context.
  96. SerializationContext.Pop();
  97. return result.ToString();
  98. }
  99. return null;
  100. }
  101. public override object Deserialize(TextReader tr)
  102. {
  103. if (tr != null)
  104. {
  105. // Normalize the text before parsing it
  106. tr = TextUtil.Normalize(tr, SerializationContext);
  107. // Create a lexer for our text stream
  108. iCalLexer lexer = new iCalLexer(tr);
  109. iCalParser parser = new iCalParser(lexer);
  110. // Get our serialization context
  111. ISerializationContext ctx = SerializationContext;
  112. // Parse the component!
  113. ICalendarProperty p = parser.property(ctx, null);
  114. // Close our text stream
  115. tr.Close();
  116. // Return the parsed property
  117. return p;
  118. }
  119. return null;
  120. }
  121. #endregion
  122. }
  123. }