Skip to content

Commit

Permalink
ref - Moved ProcessStringValue() to common tools
Browse files Browse the repository at this point in the history
---

Type: ref
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 2, 2024
1 parent d07d599 commit 96e1138
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 95 deletions.
2 changes: 1 addition & 1 deletion VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Parts.Calendar Parse()

// Handle the part type, and extract the value
string valueType = VcardCommonTools.GetFirstValue(splitArgs, defaultValueType, VcardConstants._valueArgumentSpecifier);
string finalValue = VcardParserTools.ProcessStringValue(value, valueType, calendarVersion.Major == 1 ? ';' : ',');
string finalValue = VcardCommonTools.ProcessStringValue(value, valueType, calendarVersion.Major == 1 ? ';' : ',');

// Check for allowed values
if (allowedValues.Length != 0)
Expand Down
92 changes: 92 additions & 0 deletions VisualCard/Parsers/VcardCommonTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using VisualCard.Parsers.Arguments;
using VisualCard.Parsers.Recurrence;

namespace VisualCard.Parsers
{
Expand Down Expand Up @@ -576,5 +578,95 @@ internal static DateTimeOffset ParsePosixRepresentation(string posixDateRepresen
return date;
throw new ArgumentException($"Can't parse date {posixDateRepresentation}");
}

internal static string ProcessStringValue(string value, string valueType, char split = ';')
{
// Now, handle each type individually
string finalValue;
finalValue = Regex.Unescape(value);
foreach (string finalValuePart in finalValue.Split(split))
{
switch (valueType.ToUpper())
{
case "URI":
case "URL":
// Check the URI
if (!Uri.TryCreate(finalValue, UriKind.Absolute, out Uri uri))
throw new InvalidDataException($"URL {finalValue} is invalid");
finalValue = uri is not null ? uri.ToString() : finalValue;
break;
case "UTC-OFFSET":
// Check the UTC offset
VcardCommonTools.ParseUtcOffset(finalValue);
break;
case "DATE":
// Check the date
if (!VcardCommonTools.TryParsePosixDate(finalValue, out _))
throw new InvalidDataException($"Date {finalValue} is invalid");
break;
case "TIME":
// Check the time
if (!VcardCommonTools.TryParsePosixTime(finalValue, out _))
throw new InvalidDataException($"Time {finalValue} is invalid");
break;
case "DATE-TIME":
// Check the date and time
if (!VcardCommonTools.TryParsePosixDateTime(finalValue, out _))
throw new InvalidDataException($"Date and time {finalValue} is invalid");
break;
case "DATE-AND-OR-TIME":
// Check the date and/or time
if (!VcardCommonTools.TryParsePosixDateTime(finalValue, out _) &&
!VcardCommonTools.TryParsePosixTime(finalValue, out _))
throw new InvalidDataException($"Date and/or time {finalValue} is invalid");
break;
case "TIMESTAMP":
// Check the timestamp
if (!VcardCommonTools.TryParsePosixTimestamp(finalValue, out _))
throw new InvalidDataException($"Timestamp {finalValue} is invalid");
break;
case "BOOLEAN":
// Check the boolean
if (!finalValue.Equals("true", StringComparison.OrdinalIgnoreCase) &&
!finalValue.Equals("false", StringComparison.OrdinalIgnoreCase))
throw new InvalidDataException($"Boolean {finalValue} is invalid");
break;
case "INTEGER":
// Check the integer
if (!int.TryParse(finalValue, out _))
throw new InvalidDataException($"Integer {finalValue} is invalid");
break;
case "FLOAT":
// Check the float
string[] floatParts = finalValuePart.Split(split == ';' ? ',' : ';');
foreach (var floatPart in floatParts)
if (!double.TryParse(floatPart, out _))
throw new InvalidDataException($"Float {floatPart} in {finalValue} is invalid");
break;
case "DURATION":
// Check the duration
VcardCommonTools.GetDurationSpan(finalValue);
break;
case "PERIOD":
// Check the period
VcardCommonTools.GetTimePeriod(finalValue);
break;
case "RECUR":
// Check the recursion rules
try
{
RecurrenceParser.ParseRuleV1(finalValue);
}
catch
{
RecurrenceParser.ParseRuleV2(finalValue);
}
break;
}
}

// Return the result
return finalValue;
}
}
}
2 changes: 1 addition & 1 deletion VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public Card Parse()

// Handle the part type
string valueType = VcardCommonTools.GetFirstValue(splitArgs, defaultValueType, VcardConstants._valueArgumentSpecifier);
string finalValue = VcardParserTools.ProcessStringValue(value, valueType);
string finalValue = VcardCommonTools.ProcessStringValue(value, valueType);

// Check for allowed values
if (allowedValues.Length != 0)
Expand Down
93 changes: 0 additions & 93 deletions VisualCard/Parsers/VcardParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
//

using System;
using System.IO;
using VisualCard.Parts.Enums;
using VisualCard.Parts.Implementations;
using VisualCard.Parts;
using System.Text.RegularExpressions;
using VisualCard.Parsers.Recurrence;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Parsers
Expand Down Expand Up @@ -286,95 +283,5 @@ internal static (PartType type, object enumeration, Type? enumType, Func<string,
VcardConstants._xSpecifier => (PartType.PartsArray, PartsArrayEnum.NonstandardNames, typeof(XNameInfo), XNameInfo.FromStringVcardStatic, "", "", "", [], []),
_ => (PartType.PartsArray, PartsArrayEnum.IanaNames, typeof(ExtraInfo), ExtraInfo.FromStringVcardStatic, "", "", "", [], []),
};

internal static string ProcessStringValue(string value, string valueType, char split = ';')
{
// Now, handle each type individually
string finalValue;
finalValue = Regex.Unescape(value);
foreach (string finalValuePart in finalValue.Split(split))
{
switch (valueType.ToUpper())
{
case "URI":
case "URL":
// Check the URI
if (!Uri.TryCreate(finalValue, UriKind.Absolute, out Uri uri))
throw new InvalidDataException($"URL {finalValue} is invalid");
finalValue = uri is not null ? uri.ToString() : finalValue;
break;
case "UTC-OFFSET":
// Check the UTC offset
VcardCommonTools.ParseUtcOffset(finalValue);
break;
case "DATE":
// Check the date
if (!VcardCommonTools.TryParsePosixDate(finalValue, out _))
throw new InvalidDataException($"Date {finalValue} is invalid");
break;
case "TIME":
// Check the time
if (!VcardCommonTools.TryParsePosixTime(finalValue, out _))
throw new InvalidDataException($"Time {finalValue} is invalid");
break;
case "DATE-TIME":
// Check the date and time
if (!VcardCommonTools.TryParsePosixDateTime(finalValue, out _))
throw new InvalidDataException($"Date and time {finalValue} is invalid");
break;
case "DATE-AND-OR-TIME":
// Check the date and/or time
if (!VcardCommonTools.TryParsePosixDateTime(finalValue, out _) &&
!VcardCommonTools.TryParsePosixTime(finalValue, out _))
throw new InvalidDataException($"Date and/or time {finalValue} is invalid");
break;
case "TIMESTAMP":
// Check the timestamp
if (!VcardCommonTools.TryParsePosixTimestamp(finalValue, out _))
throw new InvalidDataException($"Timestamp {finalValue} is invalid");
break;
case "BOOLEAN":
// Check the boolean
if (!finalValue.Equals("true", StringComparison.OrdinalIgnoreCase) &&
!finalValue.Equals("false", StringComparison.OrdinalIgnoreCase))
throw new InvalidDataException($"Boolean {finalValue} is invalid");
break;
case "INTEGER":
// Check the integer
if (!int.TryParse(finalValue, out _))
throw new InvalidDataException($"Integer {finalValue} is invalid");
break;
case "FLOAT":
// Check the float
string[] floatParts = finalValuePart.Split(split == ';' ? ',' : ';');
foreach (var floatPart in floatParts)
if (!double.TryParse(floatPart, out _))
throw new InvalidDataException($"Float {floatPart} in {finalValue} is invalid");
break;
case "DURATION":
// Check the duration
VcardCommonTools.GetDurationSpan(finalValue);
break;
case "PERIOD":
// Check the period
VcardCommonTools.GetTimePeriod(finalValue);
break;
case "RECUR":
// Check the recursion rules
try
{
RecurrenceParser.ParseRuleV1(finalValue);
}
catch
{
RecurrenceParser.ParseRuleV2(finalValue);
}
break;
}
}

// Return the result
return finalValue;
}
}
}

0 comments on commit 96e1138

Please sign in to comment.