no message

This commit is contained in:
Christian Fiedler
2013-09-23 14:01:54 +02:00
parent 3ce33a803b
commit 824bd7c8b1

View File

@@ -0,0 +1,315 @@
//-----------------------------------------------------------------------------
// <copyright file="ICSManager.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>fiedlchr</author>
// <sience>27.08.2013</sience>
//-----------------------------------------------------------------------------
namespace CampusAppWP8.Utility
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using CampusAppWPortalLib8.Utility;
/// <summary>ICS manager.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
public static class ICSManager
{
/// <summary>Export to ICS byte.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <param name="icalObj">The ICal object.</param>
/// <returns>ICalObject as byte array.</returns>
public static byte[] ExportToICSByte(ICalObject icalObj)
{
List<byte> retValue = new List<byte>();
retValue.AddRange(SubExportByte(icalObj));
return retValue.ToArray();
}
/// <summary>Import from ICS.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
/// <param name="icsData">Information describing the ICS.</param>
/// <returns>ICalObject object.</returns>
public static ICalObject ImportFromICS(string icsData)
{
ICalObject retValue = null;
ICalObject retPointer = null;
string[] elems = Regex.Split(ICSManager.Unfold(icsData), "\r\n");
List<ICSDict.ICSValueDesc> parentList = new List<ICSDict.ICSValueDesc>();
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;
}
/// <summary>Export to ICS.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <param name="icalObj">The ICal object.</param>
/// <returns>ICalObject as string.</returns>
public static string ExportToICS(ICalObject icalObj)
{
string retValue = string.Empty;
retValue += SubExport(icalObj);
return retValue;
}
/// <summary>Sub export.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <param name="icalObj">The ICal object.</param>
/// <returns>ICalObject as string.</returns>
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;
}
/// <summary>Sub export byte.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <param name="icalObj">The ICal object.</param>
/// <returns>ICalObject as byte array</returns>
private static byte[] SubExportByte(ICalObject icalObj)
{
List<byte> retValue = new List<byte>();
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();
}
/// <summary>Unfold a long ICS string.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <param name="icsData">Information describing the ICS.</param>
/// <returns>Unfolded string.</returns>
private static string Unfold(string icsData)
{
return icsData.Replace("\r\n ", string.Empty).Replace("\r\n\x09", string.Empty);
}
/// <summary>Folds a long string.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <param name="data">The data.</param>
/// <returns>Folded ICS string.</returns>
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;
}
/// <summary>Gets sub value.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
/// <param name="tag">The tag.</param>
/// <param name="elemDesc">Information describing the element.</param>
/// <returns>The sub value.</returns>
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;
}
/// <summary>Check if the object has the name of the tag.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <param name="obj">The object.</param>
/// <param name="tag">The tag.</param>
/// <returns>true if it succeeds, false if it fails.</returns>
private static bool Is(ContentLine obj, string tag)
{
return obj.Name.Equals(tag);
}
/// <summary>check if the two strings are equal.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
/// <param name="str1">The first string.</param>
/// <param name="str2">The second string.</param>
/// <returns>true if it succeeds, false if it fails.</returns>
private static bool Is(string str1, string str2)
{
return str1.Equals(str2);
}
/// <summary>Converts an ICS line to a content line.</summary>
/// <remarks>Fiedler, 27.08.2013.</remarks>
/// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception>
/// <param name="icsLine">The ICS line.</param>
/// <returns>ICS line as a ContentLine.</returns>
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>();
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;
}
/// <summary>Content line.</summary>
/// <remarks>Fiedler, 09.09.2013.</remarks>
private struct ContentLine
{
/// <summary>The name.</summary>
public string Name;
/// <summary>List of parameters.</summary>
public List<string> ParamList;
/// <summary>The value string.</summary>
public string ValueString;
}
}
}