Files
win8phoneApp/CampusAppWP8/CampusAppWP8/Utility/ICSObject.cs
Christian Fiedler 4140f2771e dictionary done
2013-08-28 18:05:16 +02:00

250 lines
7.9 KiB
C#

//-----------------------------------------------------------------------------
// <copyright file="ICSObject.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 System.Threading.Tasks;
using System.Windows.Shapes;
using System.Xml.Serialization;
/// <summary>Ics object. </summary>
public class ICSObject
{
private VCalendar data;
private struct VCalendar
{
public string version;
public VEvent vevent;
}
private struct VEvent
{
public DateTime created;
public string uid;
public DateTime lastModified;
public DateTime stamp;
public string summary;
public DateTime start;
public DateTime end;
public string accessClass;
public string location;
public string description;
public string categories;
}
private struct ContentLine
{
public string name;
public List<string> paramList;
public List<string> valueList;
}
/// <summary>Default constructor. </summary>
/// <remarks>Fiedler, 27.08.2013. </remarks>
public ICSObject()
{
}
public ICSObject(string icsData) : this()
{
this.ImportFromICS(icsData);
}
public void ImportFromICS(string icsData)
{
string[] elems = Regex.Split(this.Unfold(icsData), "\r\n");
ICSDict.ICSValueDesc tempRoot = ICSDict.Root;
foreach (string e in elems)
{
ContentLine obj = this.ToContentLine(e);
ICSDict.ICSElemDesc tempElem = null;
if ((tempElem = tempRoot.GetValue(obj.name)) != null)
{
ICSDict.ICSValueDesc va = null;
if (obj.name.Equals(ICSTag.BEGIN))
{
if ((va = tempElem.GetValue(obj.valueList[0])) != null)
{
tempRoot = va;
}
else
{
throw new NotSupportedException("unsupported BEGIN value (" + obj.valueList[0] + ")");
}
}
else if (obj.name.Equals(ICSTag.END))
{
if ((va = tempElem.GetValue(obj.valueList[0])) != null)
{
}
else
{
throw new NotSupportedException("unsupported END value (" + obj.valueList[0] + ")");
}
}
}
else
{
if (obj.name.IndexOf("X-") == 0)
{
// TODO
}
else
{
throw new NotSupportedException("Tag (" + obj.name + ") was not found in (" + tempRoot.Name + ")");
}
}
/*
switch (obj.name)
{
case ICSTag.BEGIN:
break;
case ICSTag.DT_CREATED:
//this.data.vevent.created = this.ParseUTC(parts[1]);
break;
case ICSTag.USER_ID:
//this.data.vevent.uid = parts[1];
break;
case "LAST-MODIFIED":
//this.data.vevent.lastModified = this.ParseUTC(parts[1]);
break;
case "DTSTAMP":
//this.data.vevent.stamp = this.ParseUTC(parts[1]);
break;
case "SUMMARY":
//this.data.vevent.summary = parts[1];
break;
case "DTSTART":
//this.data.vevent.start = this.ParseUTC(parts[1]);
break;
case "DTEND":
//this.data.vevent.end = this.ParseUTC(parts[1]);
break;
case "CLASS":
//this.data.vevent.accessClass = parts[1];
break;
case "LOCATION":
//this.data.vevent.location = parts[1];
break;
case "DESCRIPTION":
//this.data.vevent.description = parts[1];
break;
case "CATEGORIES":
//this.data.vevent.categories = parts[1];
break;
case "END":
break;
case "VERSION":
//this.data.version = parts[1];
break;
case "PRODID":
break;
case "X-WR-CALNAME":
break;
//default:
// throw new NotImplementedException("unknown tag in ics: " + parts[0]);
}
*/
}
}
public string ExportToICS()
{
string retValue = string.Empty;
return retValue;
}
// TOOLS
private DateTime ParseUTCDateTime(string datetime)
{
// see http://tools.ietf.org/html/rfc5545 3.3.5.
datetime = datetime.Insert(4, "-");
datetime = datetime.Insert(7, "-");
datetime = datetime.Insert(13, ":");
datetime = datetime.Insert(16, ":");
return DateTime.Parse(datetime);
}
private DateTime ParseUTCDate(string date)
{
date = date.Insert(4, "-");
date = date.Insert(7, "-");
return DateTime.Parse(date);
}
private string Unfold(string icsData)
{
return icsData.Replace("\r\n ", "").Replace("\r\n\x09", "");
}
/// <summary>Converts an icsLine to a content line.</summary>
/// <remarks>Fiedler, 27.08.2013. </remarks>
/// <exception cref="NotImplementedException">Thrown when the requested operation is unimplemented.</exception>
/// <param name="icsLine">The ics line.</param>
/// <returns>icsLine as a ContentLine. </returns>
private ContentLine ToContentLine(string icsLine)
{
ContentLine retValue;
string[] valueSplit = icsLine.Split(':');
if (valueSplit.Length != 2)
{
throw new NotImplementedException("ics string has no value");
}
// values
retValue.valueList = new List<string>();
retValue.valueList.AddRange(valueSplit[1].Split(','));
// params
int paramNum = StringManager.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;
}
}
}