add docs to utilty

This commit is contained in:
stubbfel
2013-06-04 12:46:33 +02:00
parent 1b3ff185a1
commit 5ba33e7261
11 changed files with 451 additions and 197 deletions

View File

@@ -140,13 +140,13 @@
<DesignTime>True</DesignTime>
<DependentUpon>Constants.resx</DependentUpon>
</Compile>
<Compile Include="utility\Feed.cs" />
<Compile Include="utility\FeedEventHandler.cs" />
<Compile Include="utility\FileList.cs" />
<Compile Include="utility\FileManager.cs" />
<Compile Include="utility\Logger.cs" />
<Compile Include="utility\URLList.cs" />
<Compile Include="utility\XMLFeed.cs" />
<Compile Include="Utility\Feed.cs" />
<Compile Include="Utility\FeedEventHandler.cs" />
<Compile Include="Utility\FileList.cs" />
<Compile Include="Utility\FileManager.cs" />
<Compile Include="Utility\Logger.cs" />
<Compile Include="Utility\URLList.cs" />
<Compile Include="Utility\XmlFeed.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">

View File

@@ -7,7 +7,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using CampusAppWP8.Resources;
using CampusAppWP8.utility;
using CampusAppWP8.Utility;
namespace CampusAppWP8.model.mensa
{

View File

@@ -1,5 +1,5 @@
using CampusAppWP8.model.mensa;
using CampusAppWP8.utility;
using CampusAppWP8.Utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -13,7 +13,7 @@ using Windows.Storage.FileProperties;
namespace CampusAppWP8.pages.mensa
{
public class MensaFeed : XMLFeed<MenuWeekModel>
public class MensaFeed : XmlFeed<MenuWeekModel>
{
public MensaFeed()
@@ -22,13 +22,13 @@ namespace CampusAppWP8.pages.mensa
}
protected override bool checkIsModelUpToDate()
protected override bool CheckIsModelUpToDate()
{
DateTime lastModified = Model.CreateTime;
return checkIsUpToDate(lastModified);
}
protected override bool checkIsFileUpToDate()
protected override bool CheckIsFileUpToDate()
{
DateTime lastModified = FileManager.GetFileInfo(FileName).LastWriteTime;
return checkIsUpToDate(lastModified);

View File

@@ -7,7 +7,7 @@ using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using CampusAppWP8.utility;
using CampusAppWP8.Utility;
using System.Xml.Serialization;
using CampusAppWP8.model.mensa;
using System.Xml.Linq;

View File

@@ -1,96 +1,224 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Windows.Storage;
namespace CampusAppWP8.utility
//-----------------------------------------------------------------------
// <copyright file="Feed.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>03.05.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
/// <summary>
/// This a abstract Class for reading, store and deserialization Feeds from the Web.
/// </summary>
/// <typeparam name="T">Type for model of the feed</typeparam>
public abstract class Feed<T>
{
#region Member
private readonly Uri _feedURL;
private readonly FeedEventHandler _eventHandler;
private readonly string _fileName;
private T _model;
/// <summary>
/// URL of the feed
/// </summary>
private readonly Uri feedURL;
/// <summary>
/// EventHandler of the feed
/// </summary>
private readonly FeedEventHandler eventHandler;
/// <summary>
/// Filename for the storage-file of the feed
/// </summary>
private readonly string fileName;
/// <summary>
/// The model of the feed
/// </summary>
private T model;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="Feed{T}" /> class.
/// </summary>
public Feed()
{
_eventHandler = new FeedEventHandler();
this.eventHandler = new FeedEventHandler();
}
public Feed(Uri FeedURL, string fileName)
/// <summary>
/// Initializes a new instance of the <see cref="Feed{T}" /> class.
/// </summary>
/// <param name="feedURL">Url of the Feed</param>
/// <param name="fileName">Name of the file, which the feed will be stored</param>
public Feed(Uri feedURL, string fileName)
{
_feedURL = FeedURL;
_eventHandler = new FeedEventHandler();
_fileName = fileName;
this.feedURL = feedURL;
this.eventHandler = new FeedEventHandler();
this.fileName = fileName;
}
#endregion
#region Proberty
/// <summary>
/// Gets for the url of the feed
/// </summary>
public Uri FeedURL
{
get { return this.feedURL; }
}
/// <summary>
/// Gets for the event-handler of the feed
/// </summary>
public FeedEventHandler EventHandler
{
get { return this.eventHandler; }
}
/// <summary>
/// Gets or sets for the model of the feed
/// </summary>
public T Model
{
get
{
return this.model;
}
set
{
if ((value == null && this.model != null) || !value.Equals(this.model))
{
this.model = value;
}
}
}
/// <summary>
/// Gets for the storage-file of the feed
/// </summary>
public string FileName
{
get { return this.fileName; }
}
#endregion
#region Methods
#region public
/// <summary>
/// Method load the feed with content. At first is try to load from model, then from file and at last from the web.
/// </summary>
public void LoadFeed()
{
if (isModelUpToDate())
if (this.IsModelUpToDate())
{
return;
}
if (isFileUpToDate())
if (this.IsFileUpToDate())
{
loadFile();
this.LoadFile();
}
else
{
downloadFeed();
this.DownloadFeed();
}
}
private void loadFile()
#endregion
#region protected
/// <summary>
/// The abstract method check if the content of the file is up-to-date, its has to implement by subclasses
/// </summary>
/// <returns>true if its up-to-date, otherwise false</returns>
protected abstract bool CheckIsFileUpToDate();
/// <summary>
/// The abstract method check if the content of the model is up-to-date, its has to implement by subclasses
/// </summary>
/// <returns>true if its up-to-date, otherwise false</returns>
protected abstract bool CheckIsModelUpToDate();
/// <summary>
/// The abstract method convert feed to a model, its has to implement by subclasses
/// </summary>
/// <param name="feedString">content of the feed</param>
protected abstract void Deserialization(string feedString);
#endregion
#region private
/// <summary>
/// Method load content from the file and create the model
/// </summary>
private void LoadFile()
{
string feedString = createFeedString();
createModel(feedString);
string feedString = FileManager.ReadFile(this.FileName);
this.CreateModel(feedString);
}
public bool isFileUpToDate()
/// <summary>
/// Method load content from the web
/// </summary>
private void DownloadFeed()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.DownloadCompleted);
client.DownloadStringAsync(this.FeedURL);
}
if (!FileManager.ExistsFile(FileName))
/// <summary>
/// Method check if the content of the file is up-to-date
/// </summary>
/// <returns>true if its up-to-date, otherwise false</returns>
private bool IsFileUpToDate()
{
if (!FileManager.ExistsFile(this.FileName))
{
return false;
}
return checkIsFileUpToDate();
return this.CheckIsFileUpToDate();
}
public bool isModelUpToDate()
/// <summary>
/// Method check if the content of the model is up-to-date
/// </summary>
/// <returns>true if its up-to-date, otherwise false</returns>
private bool IsModelUpToDate()
{
if (Model != null)
if (this.Model != null)
{
return checkIsModelUpToDate();
return this.CheckIsModelUpToDate();
}
return false;
}
protected abstract bool checkIsFileUpToDate();
protected abstract bool checkIsModelUpToDate();
private void downloadFeed()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(downloadCompleted);
client.DownloadStringAsync(FeedURL);
}
private void downloadCompleted(object sender, DownloadStringCompletedEventArgs e)
/// <summary>
/// Method will be execute if the download of the feed is completed and create the model
/// </summary>
/// <param name="sender">Sender of the event</param>
/// <param name="e">Arguments of the event</param>
private void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Exception downloadError = e.Error;
if (downloadError != null)
@@ -99,61 +227,30 @@ namespace CampusAppWP8.utility
}
string downloadResult = e.Result;
if (downloadResult != null && !downloadResult.Equals(String.Empty))
if (downloadResult != null && !downloadResult.Equals(string.Empty))
{
createModel(downloadResult);
FileManager.WriteFile(FileName, downloadResult);
this.CreateModel(downloadResult);
FileManager.WriteFile(this.FileName, downloadResult);
}
}
private void createModel(string feedString)
/// <summary>
/// Method create the model of the feed
/// </summary>
/// <param name="feedString">content of the feed</param>
private void CreateModel(string feedString)
{
if (feedString == null || feedString == String.Empty)
if (feedString == null || feedString == string.Empty)
{
return;
}
deserialization(feedString);
EventHandler.fireFeedReadyevent();
}
private string createFeedString()
{
return FileManager.ReadFile(FileName);
}
protected abstract void deserialization(string feedString);
#endregion
#region Getter&Setter
public Uri FeedURL
{
get { return _feedURL; }
}
public FeedEventHandler EventHandler
{
get { return _eventHandler; }
}
public T Model
{
get { return _model; }
set
{
if ((value == null && _model != null) || !value.Equals(_model))
{
_model = value;
}
}
}
public string FileName
{
get { return _fileName; }
this.Deserialization(feedString);
this.EventHandler.FireFeedReadyevent();
}
#endregion
#endregion
}
}

View File

@@ -1,19 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CampusAppWP8.utility
//-----------------------------------------------------------------------
// <copyright file="FeedEventHandler.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>03.05.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// This class handle the events of a feed <see cref="CampusAppWP8.Utility.Feed"/>
/// </summary>
public class FeedEventHandler
{
public delegate void FeedReadyHandler();
public event FeedReadyHandler FeedIsReadyEvent;
public FeedEventHandler() { }
public void fireFeedReadyevent()
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="FeedEventHandler" /> class.
/// </summary>
public FeedEventHandler()
{
FeedIsReadyEvent();
}
#endregion
#region Delegate&Events
/// <summary>
/// Delegate for the ready event
/// </summary>
public delegate void FeedReadyHandler();
/// <summary>
/// The ready event
/// </summary>
public event FeedReadyHandler FeedIsReadyEvent;
#endregion
#region Method
/// <summary>
/// Method fire a ready event
/// </summary>
public void FireFeedReadyevent()
{
this.FeedIsReadyEvent();
}
#endregion
}
}

View File

@@ -1,13 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CampusAppWP8.utility
//-----------------------------------------------------------------------
// <copyright file="FileList.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>03.05.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// Class contain some default names of files
/// </summary>
/// <remarks>
/// This Class may be become to a resource file
/// </remarks>
public static class FileList
{
public static string MensaXmlFile = "MesaFeed.xml";
/// <summary>
/// Name of the file for the feed of the mensa
/// </summary>
private static readonly string MensaXmlFile = "MesaFeed.xml";
}
}

View File

@@ -1,76 +1,120 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Streams;
namespace CampusAppWP8.utility
//-----------------------------------------------------------------------
// <copyright file="FileManager.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>03.05.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility
{
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Streams;
/// <summary>
/// This a static Class, which provide some method for files
/// </summary>
public static class FileManager
{
#region members
private static IStorageFolder localFolder = ApplicationData.Current.LocalFolder;
/// <summary>
/// Member for the local folder
/// </summary>
private static readonly IStorageFolder LocalFolder = ApplicationData.Current.LocalFolder;
public static bool isStringFileName(string fileName)
{
try
{
new FileInfo(fileName);
}
catch (Exception e)
{
Logger.logException(e);
return false;
}
return true;
}
private static async void WriteFileAsync(string fileName, string content)
{
IStorageFile storageFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream stream = await storageFile.OpenStreamForWriteAsync())
{
byte[] Content = Encoding.UTF8.GetBytes(content);
await stream.WriteAsync(Content, 0, Content.Length);
}
}
#endregion
#region public
/// <summary>
/// Method write a content to an file
/// </summary>
/// <param name="fileName">name of the file</param>
/// <param name="content">content of the file</param>
public static void WriteFile(string fileName, string content)
{
WriteFileAsync(fileName, content);
}
/// <summary>
/// Method read content from a file
/// </summary>
/// <remarks>
/// Method crash sometimes by OpenStreamForReadAsync() or OpenAccessStream()
/// </remarks>
/// <param name="fileName">name of the file</param>
/// <returns>content of the file, null if file doesn't exist</returns>
public static string ReadFile(string fileName)
{
string content = null;
using (Stream fileStream = localFolder.OpenStreamForReadAsync(fileName).Result)
if (ExistsFile(fileName))
{
return null;
}
using (Stream fileStream = LocalFolder.OpenStreamForReadAsync(fileName).Result)
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
content = streamReader.ReadToEnd();
}
}
return content;
}
/// <summary>
/// Method return info of a file
/// </summary>
/// <param name="fileName">name of the file</param>
/// <returns>info of the file</returns>
public static FileInfo GetFileInfo(string fileName)
{
FileInfo info = new FileInfo(localFolder.Path + "\\" + fileName);
FileInfo info = new FileInfo(LocalFolder.Path + "\\" + fileName);
return info;
}
/// <summary>
/// Method check if a file is existing
/// </summary>
/// <param name="fileName">name of the file</param>
/// <returns>true if file exists, otherwise false</returns>
public static bool ExistsFile(string fileName)
{
{
return GetFileInfo(fileName).Exists;
}
#endregion
#region private
/// <summary>
/// Method write a content to a new file. If the file exists, it will be replaced
/// </summary>
/// <remarks>
/// Method crash sometimes by OpenStreamForWriteAsync() or OpenAccessStream()
/// </remarks>
/// <param name="fileName">name of the file</param>
/// <param name="content">content of the file</param>
private static async void WriteFileAsync(string fileName, string content)
{
IStorageFile storageFile = await LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream stream = await storageFile.OpenStreamForWriteAsync())
{
byte[] contentByte = Encoding.UTF8.GetBytes(content);
await stream.WriteAsync(contentByte, 0, contentByte.Length);
}
}
#endregion
}
}
}

View File

@@ -1,14 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CampusAppWP8.utility
//--------------------------------------------------------------------
// <copyright file="Logger.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>03.05.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// This Class creates logs for the app
/// </summary>
public class Logger
{
public static void logException(Exception exception)
/// <summary>
/// Method log a Exception
/// </summary>
/// <param name="exception">exception which has to log</param>
public static void LogException(Exception exception)
{
Console.WriteLine(exception);
}

View File

@@ -1,13 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CampusAppWP8.utility
//-----------------------------------------------------------------------
// <copyright file="URLList.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>03.05.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// Class contain some default url of feeds
/// </summary>
/// <remarks>
/// This Class may be become to a resource file
/// </remarks>
public static class URLList
{
public static Uri MensaFeedURL = new Uri("http://www.studentenwerk-frankfurt.de/2011/ClassPackage/App_IKMZ_BTU/", UriKind.Absolute);
/// <summary>
/// Url for the feed of the mensa
/// </summary>
public static readonly Uri MensaFeedURL = new Uri("http://www.studentenwerk-frankfurt.de/2011/ClassPackage/App_IKMZ_BTU/", UriKind.Absolute);
}
}

View File

@@ -1,26 +1,54 @@
using CampusAppWP8.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
//-----------------------------------------------------------------------
// <copyright file="XmlFeed.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>03.05.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Utility
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
using CampusAppWP8.Resources;
namespace CampusAppWP8.utility
{
public abstract class XMLFeed<T> : Feed<T>
/// <summary>
/// This abstract Class is for Xml-feeds
/// </summary>
/// <typeparam name="T">Type for model of the feed</typeparam>
public abstract class XmlFeed<T> : Feed<T>
{
public XMLFeed()
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="XmlFeed{T}" /> class.
/// </summary>
public XmlFeed()
{
}
public XMLFeed(Uri feedURL,String fileName):base(feedURL,fileName)
/// <summary>
/// Initializes a new instance of the <see cref="XmlFeed{T}" /> class.
/// </summary>
/// <param name="feedURL">Url of the Feed</param>
/// <param name="fileName">Name of the file, which the feed will be stored</param>
public XmlFeed(Uri feedURL, string fileName)
: base(feedURL, fileName)
{
}
#endregion
protected override void deserialization(string feedString)
#region Methods
/// <summary>
/// Method implement the deserialization a Xml-feed
/// </summary>
/// <param name="feedString">content of the feed</param>
protected override void Deserialization(string feedString)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
XDocument document = XDocument.Parse(feedString);
@@ -31,12 +59,13 @@ namespace CampusAppWP8.utility
document = new XDocument();
document.Add(new XElement(validRootName, content));
}
T model= (T)serializer.Deserialize(document.CreateReader());
T model = (T)serializer.Deserialize(document.CreateReader());
if (model != null)
{
this.Model = model;
this.Model = model;
}
}
#endregion
}
}