Skip to content

Commit

Permalink
ref - Consolidated constants
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 8da8f68 commit db22335
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 71 deletions.
11 changes: 6 additions & 5 deletions VisualCard.Calendar/CalendarTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System;
using Textify.General;
using VisualCard.Calendar.Parsers;
using VisualCard.Parsers;

namespace VisualCard.Calendar
{
Expand Down Expand Up @@ -92,7 +93,7 @@ public static Parts.Calendar[] GetCalendars(StreamReader stream)
continue;
}
else if (!CalendarLine.EqualsNoCase(VCalendarConstants._beginText) &&
!CalendarLine.ToUpper().StartsWith(VCalendarConstants._versionSpecifier) &&
!CalendarLine.ToUpper().StartsWith(VcardConstants._versionSpecifier) &&
!CalendarLine.EqualsNoCase(VCalendarConstants._endText))
append = true;
if (append)
Expand All @@ -111,12 +112,12 @@ public static Parts.Calendar[] GetCalendars(StreamReader stream)

// Now that the beginning of the calendar tag is spotted, parse the version as we need to know how to select the appropriate parser.
// All vCalendars are required to have their own version directly after the BEGIN:VCALENDAR tag
if (CalendarLine.ToUpper().StartsWith(VCalendarConstants._versionSpecifier) &&
!CalendarLine.EqualsNoCase($"{VCalendarConstants._versionSpecifier}:1.0") &&
!CalendarLine.EqualsNoCase($"{VCalendarConstants._versionSpecifier}:2.0") &&
if (CalendarLine.ToUpper().StartsWith(VcardConstants._versionSpecifier) &&
!CalendarLine.EqualsNoCase($"{VcardConstants._versionSpecifier}:1.0") &&
!CalendarLine.EqualsNoCase($"{VcardConstants._versionSpecifier}:2.0") &&
!VersionSpotted)
throw new InvalidDataException($"This has an invalid vCalendar version {CalendarLine}.");
else if (!VersionSpotted && CalendarLine.ToUpper().StartsWith(VCalendarConstants._versionSpecifier))
else if (!VersionSpotted && CalendarLine.ToUpper().StartsWith(VcardConstants._versionSpecifier))
{
VersionSpotted = true;
CalendarVersion = new(CalendarLine.Substring(8));
Expand Down
21 changes: 4 additions & 17 deletions VisualCard.Calendar/Parsers/VCalendarConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using VisualCard.Parsers;

namespace VisualCard.Calendar.Parsers
{
internal static class VCalendarConstants
{
// Mandatory for each vCalendar
internal const string _beginText = _beginSpecifier + ":" + _objectVCalendarSpecifier;
internal const string _endText = _endSpecifier + ":" + _objectVCalendarSpecifier;
internal const string _beginSpecifier = "BEGIN";
internal const string _endSpecifier = "END";
internal const string _versionSpecifier = "VERSION";
internal const string _beginText = VcardConstants._beginSpecifier + ":" + _objectVCalendarSpecifier;
internal const string _endText = VcardConstants._endSpecifier + ":" + _objectVCalendarSpecifier;

// Object specifiers
internal const string _objectVCalendarSpecifier = "VCALENDAR";
Expand All @@ -41,15 +40,7 @@ internal static class VCalendarConstants
internal const string _objectVDaylightSpecifier = "DAYLIGHT";
internal const string _objectVAlarmSpecifier = "VALARM";

// Misc vCalendar constants
internal const string _spaceBreak = " ";
internal const string _tabBreak = "\x0009";

// Available in vCalendar 1.0 and 2.0
internal const char _fieldDelimiter = ';';
internal const char _valueDelimiter = ',';
internal const char _argumentDelimiter = ':';
internal const char _argumentValueDelimiter = '=';
internal const string _productIdSpecifier = "PRODID";
internal const string _uidSpecifier = "UID";
internal const string _dateStartSpecifier = "DTSTART";
Expand Down Expand Up @@ -83,10 +74,6 @@ internal static class VCalendarConstants
internal const string _lastModSpecifier = "LAST-MODIFIED";
internal const string _prioritySpecifier = "PRIORITY";
internal const string _urlSpecifier = "URL";
internal const string _xSpecifier = "X-";
internal const string _typeArgumentSpecifier = "TYPE=";
internal const string _valueArgumentSpecifier = "VALUE=";
internal const string _encodingArgumentSpecifier = "ENCODING=";

// Available in vCalendar 1.0
internal const string _daylightSpecifier = "DAYLIGHT";
Expand Down
34 changes: 17 additions & 17 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public Parts.Calendar Parse()

// First, check to see if we need to construct blocks
string secondLine = i + 1 < CalendarContent.Length ? CalendarContent[i + 1] : "";
bool firstConstructedLine = !_value.StartsWith(VCalendarConstants._spaceBreak) && !_value.StartsWith(VCalendarConstants._tabBreak);
constructing = secondLine.StartsWithAnyOf([VCalendarConstants._spaceBreak, VCalendarConstants._tabBreak]);
bool firstConstructedLine = !_value.StartsWith(VcardConstants._spaceBreak) && !_value.StartsWith(VcardConstants._tabBreak);
constructing = secondLine.StartsWithAnyOf([VcardConstants._spaceBreak, VcardConstants._tabBreak]);
secondLine = secondLine.Length > 1 ? secondLine.Substring(1) : "";
if (constructing)
{
Expand All @@ -104,25 +104,25 @@ public Parts.Calendar Parse()
try
{
// Now, parse a line
if (!_value.Contains(VCalendarConstants._argumentDelimiter))
if (!_value.Contains(VcardConstants._argumentDelimiter))
throw new ArgumentException("The line must contain an argument delimiter.");
string value = _value.Substring(_value.IndexOf(VCalendarConstants._argumentDelimiter) + 1);
string prefixWithArgs = _value.Substring(0, _value.IndexOf(VCalendarConstants._argumentDelimiter));
string value = _value.Substring(_value.IndexOf(VcardConstants._argumentDelimiter) + 1);
string prefixWithArgs = _value.Substring(0, _value.IndexOf(VcardConstants._argumentDelimiter));
string prefix = (prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(0, prefixWithArgs.IndexOf(';')) : prefixWithArgs).ToUpper();
string args = prefixWithArgs.Contains(';') ? prefixWithArgs.Substring(prefix.Length + 1) : "";
string[] splitArgs = args.Split([VCalendarConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
string[] splitValues = value.Split([VCalendarConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
string[] splitArgs = args.Split([VcardConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
string[] splitValues = value.Split([VcardConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
bool isWithType = splitArgs.Length > 0;
List<ArgumentInfo> finalArgs = [];

// Check to see if we have a BEGIN or an END prefix
if (prefix == VCalendarConstants._beginSpecifier)
if (prefix == VcardConstants._beginSpecifier)
{
string finalType = value.ToUpper();
begins.Add((finalType, GetCalendarInheritedInstance(finalType)));
continue;
}
else if (prefix == VCalendarConstants._endSpecifier)
else if (prefix == VcardConstants._endSpecifier)
{
string expectedType = begins[begins.Count - 1].Item1;
if (value == expectedType)
Expand All @@ -142,26 +142,26 @@ public Parts.Calendar Parse()
}

// Get the part type
bool xNonstandard = prefix.StartsWith(VCalendarConstants._xSpecifier);
bool xNonstandard = prefix.StartsWith(VcardConstants._xSpecifier);
bool specifierRequired = CalendarVersion.Major >= 3;
Type calendarType = subPart is not null ? subPart.GetType() : calendar.GetType();
var (type, enumeration, classType, fromString, defaultType, defaultValue, defaultValueType, extraAllowedTypes, allowedValues) = VCalendarParserTools.GetPartType(xNonstandard ? VCalendarConstants._xSpecifier : prefix, VCalendarParserTools.GetObjectTypeFromType(calendarType), CalendarVersion);
var (type, enumeration, classType, fromString, defaultType, defaultValue, defaultValueType, extraAllowedTypes, allowedValues) = VCalendarParserTools.GetPartType(xNonstandard ? VcardConstants._xSpecifier : prefix, VCalendarParserTools.GetObjectTypeFromType(calendarType), CalendarVersion);

// Handle arguments
if (isWithType)
{
// Finalize the arguments
var argsStr = splitArgs.Except(
splitArgs.Where((arg) =>
arg.StartsWith(VCalendarConstants._valueArgumentSpecifier) ||
arg.StartsWith(VCalendarConstants._typeArgumentSpecifier) ||
CalendarVersion.Major == 2 && !arg.Contains(VCalendarConstants._argumentValueDelimiter)
arg.StartsWith(VcardConstants._valueArgumentSpecifier) ||
arg.StartsWith(VcardConstants._typeArgumentSpecifier) ||
CalendarVersion.Major == 2 && !arg.Contains(VcardConstants._argumentValueDelimiter)
)
);
foreach (string arg in argsStr)
{
string keyStr = arg.Substring(0, arg.IndexOf(VCalendarConstants._argumentValueDelimiter));
string valueStr = arg.RemovePrefix($"{keyStr}{VCalendarConstants._argumentValueDelimiter}");
string keyStr = arg.Substring(0, arg.IndexOf(VcardConstants._argumentValueDelimiter));
string valueStr = arg.RemovePrefix($"{keyStr}{VcardConstants._argumentValueDelimiter}");
finalArgs.Add(new(keyStr, valueStr));
}
}
Expand All @@ -176,7 +176,7 @@ public Parts.Calendar Parse()
}

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

// Check for allowed values
Expand Down
5 changes: 3 additions & 2 deletions VisualCard.Calendar/Parsers/VCalendarParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using System.IO;
using VisualCard.Calendar.Parts.Implementations.FreeBusy;
using VisualCard.Parsers.Arguments;
using VisualCard.Parsers;

namespace VisualCard.Calendar.Parsers
{
Expand Down Expand Up @@ -185,7 +186,7 @@ internal static string GetPrefixFromPartsArrayEnum(CalendarPartsArrayEnum partsA
CalendarPartsArrayEnum.Contact => VCalendarConstants._contactSpecifier,

// Extensions are allowed
CalendarPartsArrayEnum.NonstandardNames => VCalendarConstants._xSpecifier,
CalendarPartsArrayEnum.NonstandardNames => VcardConstants._xSpecifier,
_ => ""
};

Expand Down Expand Up @@ -319,7 +320,7 @@ internal static (PartType type, object enumeration, Type? enumType, Func<string,
VCalendarConstants._rNumSpecifier => (PartType.Integers, CalendarIntegersEnum.RecurrTimes, null, null, "", "", "integer", [], []),

// Extensions are allowed
VCalendarConstants._xSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.NonstandardNames, typeof(XNameInfo), XNameInfo.FromStringVcalendarStatic, "", "", "", [], []),
VcardConstants._xSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.NonstandardNames, typeof(XNameInfo), XNameInfo.FromStringVcalendarStatic, "", "", "", [], []),
_ => (PartType.PartsArray, CalendarPartsArrayEnum.IanaNames, typeof(ExtraInfo), ExtraInfo.FromStringVcalendarStatic, "", "", "", [], []),
};
}
Expand Down
10 changes: 5 additions & 5 deletions VisualCard.Calendar/Parts/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ internal string SaveToString(Version version, Dictionary<CalendarPartsArrayEnum,
var cardBuilder = new StringBuilder();

// First, write the header
cardBuilder.AppendLine($"{VCalendarConstants._beginSpecifier}:{objectType}");
cardBuilder.AppendLine($"{VcardConstants._beginSpecifier}:{objectType}");
if (objectType == VCalendarConstants._objectVCalendarSpecifier)
cardBuilder.AppendLine($"{VCalendarConstants._versionSpecifier}:{version}");
cardBuilder.AppendLine($"{VcardConstants._versionSpecifier}:{version}");

// Then, enumerate all the strings
CalendarStringsEnum[] stringEnums = (CalendarStringsEnum[])Enum.GetValues(typeof(CalendarStringsEnum));
Expand All @@ -283,7 +283,7 @@ internal string SaveToString(Version version, Dictionary<CalendarPartsArrayEnum,

// Now, locate the prefix and assemble the line
string prefix = VCalendarParserTools.GetPrefixFromStringsEnum(stringEnum);
cardBuilder.Append($"{prefix}{VCalendarConstants._argumentDelimiter}");
cardBuilder.Append($"{prefix}{VcardConstants._argumentDelimiter}");
cardBuilder.AppendLine($"{VcardCommonTools.MakeStringBlock(stringValue, prefix.Length)}");
}

Expand All @@ -298,7 +298,7 @@ internal string SaveToString(Version version, Dictionary<CalendarPartsArrayEnum,

// Now, locate the prefix and assemble the line
string prefix = VCalendarParserTools.GetPrefixFromIntegersEnum(integerEnum);
cardBuilder.AppendLine($"{prefix}{VCalendarConstants._argumentDelimiter}{integerValue}");
cardBuilder.AppendLine($"{prefix}{VcardConstants._argumentDelimiter}{integerValue}");
}

// Then, enumerate all the arrays
Expand Down Expand Up @@ -365,7 +365,7 @@ internal string SaveToString(Version version, Dictionary<CalendarPartsArrayEnum,
}

// End the card and return it
cardBuilder.AppendLine($"{VCalendarConstants._endSpecifier}:{objectType}");
cardBuilder.AppendLine($"{VcardConstants._endSpecifier}:{objectType}");
return cardBuilder.ToString();
}

Expand Down
21 changes: 11 additions & 10 deletions VisualCard.Calendar/Parts/CalendarBuilderTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Text;
using VisualCard.Calendar.Parsers;
using VisualCard.Calendar.Parts.Implementations;
using VisualCard.Parsers;

namespace VisualCard.Calendar.Parts
{
Expand All @@ -42,40 +43,40 @@ internal static string BuildArguments(BaseCalendarPartInfo partInfo, string defa
(partInfo is XNameInfo xName ? xName.XKeyName :
partInfo is ExtraInfo exName ? exName.KeyName : "") ?? "";
if (noSemicolon)
return extraKeyName + VCalendarConstants._argumentDelimiter.ToString();
return extraKeyName + VcardConstants._argumentDelimiter.ToString();

// Now, initialize the argument builder
StringBuilder argumentsBuilder = new(extraKeyName + VCalendarConstants._fieldDelimiter.ToString());
StringBuilder argumentsBuilder = new(extraKeyName + VcardConstants._fieldDelimiter.ToString());
bool installArguments = valueArguments.Length > 0;
bool installElementTypes = finalElementTypes.Length > 0;
bool installValueType = !string.IsNullOrEmpty(finalValue);

// Install the element types parameter if it exists
if (installElementTypes)
{
argumentsBuilder.Append(VCalendarConstants._typeArgumentSpecifier + string.Join(",", finalElementTypes));
argumentsBuilder.Append(VcardConstants._typeArgumentSpecifier + string.Join(",", finalElementTypes));
noSemicolon = !installArguments && !installValueType;
if (noSemicolon)
{
argumentsBuilder.Append(VCalendarConstants._argumentDelimiter.ToString());
argumentsBuilder.Append(VcardConstants._argumentDelimiter.ToString());
return argumentsBuilder.ToString();
}
else
argumentsBuilder.Append(VCalendarConstants._fieldDelimiter.ToString());
argumentsBuilder.Append(VcardConstants._fieldDelimiter.ToString());
}

// Then, install the value type parameter if it exists
if (installValueType)
{
argumentsBuilder.Append(VCalendarConstants._valueArgumentSpecifier + string.Join(",", finalValue));
argumentsBuilder.Append(VcardConstants._valueArgumentSpecifier + string.Join(",", finalValue));
noSemicolon = !installArguments;
if (noSemicolon)
{
argumentsBuilder.Append(VCalendarConstants._argumentDelimiter.ToString());
argumentsBuilder.Append(VcardConstants._argumentDelimiter.ToString());
return argumentsBuilder.ToString();
}
else
argumentsBuilder.Append(VCalendarConstants._fieldDelimiter.ToString());
argumentsBuilder.Append(VcardConstants._fieldDelimiter.ToString());
}

// Finally, install the remaining arguments if they exist and contain keys and values
Expand All @@ -84,11 +85,11 @@ internal static string BuildArguments(BaseCalendarPartInfo partInfo, string defa
List<string> finalArguments = [];
foreach (var arg in valueArguments)
finalArguments.Add(arg.BuildArguments());
argumentsBuilder.Append(string.Join(VCalendarConstants._fieldDelimiter.ToString(), finalArguments));
argumentsBuilder.Append(string.Join(VcardConstants._fieldDelimiter.ToString(), finalArguments));
}

// We've reached the end.
argumentsBuilder.Append(VCalendarConstants._argumentDelimiter.ToString());
argumentsBuilder.Append(VcardConstants._argumentDelimiter.ToString());
return argumentsBuilder.ToString();
}
}
Expand Down
2 changes: 1 addition & 1 deletion VisualCard.Calendar/Parts/Implementations/AttachInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal override string ToStringVcalendarInternal(Version calendarVersion) =>
internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, ArgumentInfo[] finalArgs, string[] elementTypes, string valueType, Version calendarVersion)
{
// Check to see if the value is prepended by the ENCODING= argument
string attachEncoding = VcardCommonTools.GetValuesString(finalArgs, "b", VCalendarConstants._encodingArgumentSpecifier);
string attachEncoding = VcardCommonTools.GetValuesString(finalArgs, "b", VcardConstants._encodingArgumentSpecifier);
if (!VcardCommonTools.IsEncodingBlob(finalArgs, value))
{
// Since we don't need embedded attachs, we need to check a URL.
Expand Down
Loading

0 comments on commit db22335

Please sign in to comment.