From 824bd7c8b1eaa81e1df16322a705b14d0bc93826 Mon Sep 17 00:00:00 2001 From: Christian Fiedler Date: Mon, 23 Sep 2013 14:01:54 +0200 Subject: [PATCH] no message --- .../CampusAppWP8/Utility/ICSManager.cs | 315 ++++++++++++++++++ 1 file changed, 315 insertions(+) create mode 100644 CampusAppWP8/CampusAppWP8/Utility/ICSManager.cs diff --git a/CampusAppWP8/CampusAppWP8/Utility/ICSManager.cs b/CampusAppWP8/CampusAppWP8/Utility/ICSManager.cs new file mode 100644 index 00000000..530c876c --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Utility/ICSManager.cs @@ -0,0 +1,315 @@ +//----------------------------------------------------------------------------- +// +// Company copyright tag. +// +// fiedlchr +// 27.08.2013 +//----------------------------------------------------------------------------- +namespace CampusAppWP8.Utility +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Text.RegularExpressions; + using CampusAppWPortalLib8.Utility; + + /// ICS manager. + /// Fiedler, 09.09.2013. + public static class ICSManager + { + /// Export to ICS byte. + /// Fiedler, 09.09.2013. + /// The ICal object. + /// ICalObject as byte array. + public static byte[] ExportToICSByte(ICalObject icalObj) + { + List retValue = new List(); + + retValue.AddRange(SubExportByte(icalObj)); + + return retValue.ToArray(); + } + + /// Import from ICS. + /// Fiedler, 09.09.2013. + /// Thrown when the requested operation is not supported. + /// Information describing the ICS. + /// ICalObject object. + public static ICalObject ImportFromICS(string icsData) + { + ICalObject retValue = null; + ICalObject retPointer = null; + + string[] elems = Regex.Split(ICSManager.Unfold(icsData), "\r\n"); + + List parentList = new List(); + parentList.Add(ICSDict.Root); + + foreach (string e in elems) + { + ContentLine prop = ICSManager.ToContentLine(e); + ICSDict.ICSElemDesc tempElem = null; + + if ((tempElem = parentList.Last().GetValue(prop.Name)) != null) + { + object propObj = tempElem.CreateObj(prop.ValueString, (prop.ParamList == null) ? null : prop.ParamList.ToArray()); + + if (ICSManager.Is((propObj as ICSClasses.Interface).GetName(), ICSTag.BEGIN)) + { + retPointer = new ICalObject(); + + if (retValue == null) + { + retValue = retPointer; + } + else + { + // FIXME + retValue.PropertieList.Add(retPointer); + } + + retPointer.Header = propObj as ICSProperties.Begin; + + parentList.Add(ICSManager.GetSubValue((propObj as ICSProperties.Begin).Value, tempElem)); + } + else if (ICSManager.Is((propObj as ICSClasses.Interface).GetName(), ICSTag.END)) + { + if (ICSManager.GetSubValue((propObj as ICSProperties.End).Value, tempElem) != null) + { + parentList.RemoveAt(parentList.Count - 1); + } + } + else + { + retPointer.PropertieList.Add(propObj); + } + } + else + { + if (prop.Name.IndexOf("X-") == 0) + { + // TODO + } + else + { + throw new NotSupportedException("Tag (" + prop.Name + ") was not found in (" + parentList.Last().Name + ")"); + } + } + } + + return retValue; + } + + /// Export to ICS. + /// Fiedler, 09.09.2013. + /// The ICal object. + /// ICalObject as string. + public static string ExportToICS(ICalObject icalObj) + { + string retValue = string.Empty; + + retValue += SubExport(icalObj); + + return retValue; + } + + /// Sub export. + /// Fiedler, 09.09.2013. + /// The ICal object. + /// ICalObject as string. + private static string SubExport(ICalObject icalObj) + { + string retValue = string.Empty; + + retValue += icalObj.Header.GetString() + "\r\n"; + + foreach (object obj in icalObj.PropertieList) + { + if (obj.GetType().Equals(typeof(ICalObject))) + { + retValue += SubExport(obj as ICalObject); + } + else + { + retValue += (obj as ICSClasses.Interface).GetString() + "\r\n"; + } + } + + ICSProperties.End tempEnd = new ICSProperties.End(); + tempEnd.Value = icalObj.Header.Value; + retValue += tempEnd.GetString() + "\r\n"; + + return retValue; + } + + /// Sub export byte. + /// Fiedler, 09.09.2013. + /// The ICal object. + /// ICalObject as byte array + private static byte[] SubExportByte(ICalObject icalObj) + { + List retValue = new List(); + + retValue.AddRange(icalObj.Header.GetBytes()); + retValue.AddRange(Encoding.UTF8.GetBytes("\r\n")); + + foreach (object obj in icalObj.PropertieList) + { + if (obj.GetType().Equals(typeof(ICalObject))) + { + retValue.AddRange(SubExportByte(obj as ICalObject)); + } + else + { + retValue.AddRange((obj as ICSClasses.Interface).GetBytes()); + retValue.AddRange(Encoding.UTF8.GetBytes("\r\n")); + } + } + + ICSProperties.End tempEnd = new ICSProperties.End(); + tempEnd.Value = icalObj.Header.Value; + + retValue.AddRange(tempEnd.GetBytes()); + retValue.AddRange(Encoding.UTF8.GetBytes("\r\n")); + + return retValue.ToArray(); + } + + /// Unfold a long ICS string. + /// Fiedler, 09.09.2013. + /// Information describing the ICS. + /// Unfolded string. + private static string Unfold(string icsData) + { + return icsData.Replace("\r\n ", string.Empty).Replace("\r\n\x09", string.Empty); + } + + /// Folds a long string. + /// Fiedler, 09.09.2013. + /// The data. + /// Folded ICS string. + private static string Fold(string data) + { + string retValue = string.Empty; + + string[] lines = Regex.Split(data, "\r\n"); + + for (int i = 0; i < lines.Count(); i++) + { + for (int k = 0; k < lines[i].Length;) + { + if (k == 0) + { + retValue += lines[i].Substring(k, ((lines[i].Length - k - 75) >= 0) ? 75 : (lines[i].Length - k)); + k += 75; + } + else + { + retValue += " " + lines[i].Substring(k, ((lines[i].Length - k - 74) >= 0) ? 74 : (lines[i].Length - k)); + k += 74; + } + + retValue += "\r\n"; + } + } + + return retValue; + } + + /// Gets sub value. + /// Fiedler, 09.09.2013. + /// Thrown when the requested operation is not supported. + /// The tag. + /// Information describing the element. + /// The sub value. + private static ICSDict.ICSValueDesc GetSubValue(string tag, ICSDict.ICSElemDesc elemDesc) + { + ICSDict.ICSValueDesc retValue = null; + + if ((retValue = elemDesc.GetValue(tag)) == null) + { + throw new NotSupportedException("Tag (" + tag + ") was not found in VALUE list of (" + elemDesc.Name + ")"); + } + + return retValue; + } + + /// Check if the object has the name of the tag. + /// Fiedler, 09.09.2013. + /// The object. + /// The tag. + /// true if it succeeds, false if it fails. + private static bool Is(ContentLine obj, string tag) + { + return obj.Name.Equals(tag); + } + + /// check if the two strings are equal. + /// Fiedler, 09.09.2013. + /// The first string. + /// The second string. + /// true if it succeeds, false if it fails. + private static bool Is(string str1, string str2) + { + return str1.Equals(str2); + } + + /// Converts an ICS line to a content line. + /// Fiedler, 27.08.2013. + /// Thrown when the requested operation is not supported. + /// The ICS line. + /// ICS line as a ContentLine. + private static ContentLine ToContentLine(string icsLine) + { + ContentLine retValue; + + string[] valueSplit = icsLine.Split(new char[] { ':' }, 2); + + if (valueSplit.Length != 2) + { + throw new NotSupportedException("ics string has no value"); + } + + retValue.ValueString = valueSplit[1]; + + // params + int paramNum = DefaultStringManager.CountChar(valueSplit[0], ';'); + + if (paramNum > 0) + { + retValue.ParamList = new List(); + + string[] paramSplit = valueSplit[0].Split(';'); + + retValue.Name = paramSplit[0].ToUpper(); + + for (int i = 1; i < paramSplit.Length; i++) + { + retValue.ParamList.Add(paramSplit[i].ToUpper()); + } + } + else + { + retValue.ParamList = null; + retValue.Name = valueSplit[0].ToUpper(); + } + + return retValue; + } + + /// Content line. + /// Fiedler, 09.09.2013. + private struct ContentLine + { + /// The name. + public string Name; + + /// List of parameters. + public List ParamList; + + /// The value string. + public string ValueString; + } + } +}