Skip to content

Commit

Permalink
Fix regression bug introduced during a 'minor cleanup' ugh #45
Browse files Browse the repository at this point in the history
  • Loading branch information
rianjs committed Jul 14, 2016
1 parent 5334829 commit 4eb0484
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Ical.Net.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Ical.Net</id>
<version>2.1.4</version>
<version>2.1.5</version>
<title>Ical.Net (formerly dday.ical)</title>
<authors>Rian Stockbower, Douglas Day, M. David Peterson</authors>
<owners>Rian Stockbower</owners>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,46 @@ public class ComponentSerializer : SerializerBase
{
protected virtual IComparer<ICalendarProperty> PropertySorter => new PropertyAlphabetizer();

public ComponentSerializer() {}
public ComponentSerializer() { }

public ComponentSerializer(ISerializationContext ctx) : base(ctx) {}
public ComponentSerializer(ISerializationContext ctx) : base(ctx) { }

public override Type TargetType => typeof (CalendarComponent);
public override Type TargetType => typeof(CalendarComponent);

public override string SerializeToString(object obj)
{
var component = obj as ICalendarComponent;

var sb = new StringBuilder(1024);
sb.Append(TextUtil.WrapLines("BEGIN:" + component.Name.ToUpper()));
var c = obj as ICalendarComponent;
if (c == null)
{
return null;
}

var properties = new List<ICalendarProperty>(component.Properties.Count);
properties.AddRange(component.Properties.OrderBy(c => c.Name));
var sb = new StringBuilder(512);
sb.Append(TextUtil.WrapLines("BEGIN:" + c.Name.ToUpper()));

// Get a serializer factory
var sf = GetService<ISerializerFactory>();
var serializer = sf.Build(properties.First().GetType(), SerializationContext) as IStringSerializer;

// Sort the calendar properties in alphabetical order before serializing them!
var properties = c.Properties.OrderBy(p => p.Name).ToList();

// Serialize properties
foreach (var p in properties)
{
// Get a serializer for each property.
var serializer = sf.Build(p.GetType(), SerializationContext) as IStringSerializer;
sb.Append(serializer.SerializeToString(p));
}

foreach (var child in component.Children)
// Serialize child objects
foreach (var child in c.Children)
{
// Get a serializer for each child object.
var serializer = sf.Build(child.GetType(), SerializationContext) as IStringSerializer;
sb.Append(serializer.SerializeToString(child));
}

sb.Append(TextUtil.WrapLines("END:" + component.Name.ToUpper()));
sb.Append(TextUtil.WrapLines("END:" + c.Name.ToUpper()));
return sb.ToString();
}

Expand Down
10 changes: 5 additions & 5 deletions ical.NET/Utility/TextUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public static string WrapLines(string value)
var i = 0;
if (current.Length > 75)
{
result.AppendLine(current.ToString(0, 75));
result.Append(current.ToString(0, 75) + "\r\n");
for (i = 75; i < current.Length - 74; i += 74)
{
result.AppendLine(current.ToString(i, 74));
result.Append(current.ToString(i, 74) + "\r\n");
}
}
result.Append(current.ToString(i, current.Length - i));
result.AppendLine();
result.Append("\r\n");

return result.ToString();
}
Expand All @@ -44,7 +44,7 @@ public static string RemoveEmptyLines(string s)
var len = -1;
while (len != s.Length)
{
s = s.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
s = s.Replace("\r\n\r\n", "\r\n");
len = s.Length;
}
return s;
Expand All @@ -58,7 +58,7 @@ public static string RemoveEmptyLines(string s)
public static TextReader Normalize(string s, ISerializationContext ctx)
{
// Replace \r and \n with \r\n.
s = NormalizeToCrLf.Replace(s, Environment.NewLine);
s = NormalizeToCrLf.Replace(s, "\r\n");

var settings = ctx.GetService(typeof (ISerializationSettings)) as ISerializationSettings;
if (settings == null || !settings.EnsureAccurateLineNumbers)
Expand Down

0 comments on commit 4eb0484

Please sign in to comment.