diff --git a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj index c022ab30..190ec908 100644 --- a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj +++ b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj @@ -171,6 +171,7 @@ True Constants.resx + diff --git a/CampusAppWP8/CampusAppWP8/Utility/ApiEventHandler.cs b/CampusAppWP8/CampusAppWP8/Utility/ApiEventHandler.cs new file mode 100644 index 00000000..f90b2ec4 --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Utility/ApiEventHandler.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CampusAppWP8.Utility +{ + /// + /// This class handle the events of a api + /// + public class ApiEventHandler + { + /// + /// Initializes a new instance of the class. + /// + public ApiEventHandler() + { + } + + #region Delegate&Events + /// + /// Delegate for the ready event + /// + public delegate void ApiReadyHandler(); + + /// + /// The ready event + /// + public event ApiReadyHandler ApiIsReadyEvent; + + #endregion + + #region Method + + /// + /// Method fire a ready event + /// + public void FireApiReadyevent() + { + this.ApiIsReadyEvent(); + } + + #endregion + } +} diff --git a/CampusAppWP8/CampusAppWP8/Utility/XmlApi.cs b/CampusAppWP8/CampusAppWP8/Utility/XmlApi.cs index c0753741..fc700f3c 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/XmlApi.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/XmlApi.cs @@ -1,12 +1,116 @@ -using System; +using CampusAppWP8.Resources; +using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; +using System.Xml.Serialization; namespace CampusAppWP8.Utility { - public class XmlApi + public class XmlApi : RestApi { + /// + /// The model of the feed + /// + private T model; + + protected string validRootName = Constants.XMLRootElementName; + + /// + /// EventHandler of the api + /// + private readonly ApiEventHandler eventHandler; + + public XmlApi(Uri apiBaseAddress): base(apiBaseAddress) + { + this.eventHandler = new ApiEventHandler(); + } + + /// + /// Gets or sets for the model of the feed + /// + public T Model + { + get + { + return this.model; + } + + set + { + if ((value == null && this.model != null) || !value.Equals(this.model)) + { + this.model = value; + } + } + } + + /// + /// Gets for the event-handler of the feed + /// + public ApiEventHandler EventHandler + { + get { return this.eventHandler; } + } + + /// + /// Method will be execute if the download of the feed is completed and create the model + /// + /// Sender of the event + /// Arguments of the event + public void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e) + { + Exception downloadError = e.Error; + if (downloadError != null) + { + return; + } + + string downloadResult = e.Result; + if (downloadResult != null && !downloadResult.Equals(string.Empty)) + { + this.CreateModel(downloadResult); + } + } + + /// + /// Method create the model of the feed + /// + /// content of the feed + private void CreateModel(string xmlString) + { + if (xmlString == null || xmlString == string.Empty) + { + return; + } + + this.Deserialization(xmlString); + this.EventHandler.FireApiReadyevent(); + } + + /// + /// Method implement the deserialization a Xml-feed + /// + /// content of the feed + protected override void Deserialization(string feedString) + { + XmlSerializer serializer = new XmlSerializer(typeof(T)); + XDocument document = XDocument.Parse(feedString); + if (!document.Root.Name.ToString().Equals(validRootName)) + { + XElement content = document.Root; + document = new XDocument(); + document.Add(new XElement(validRootName, content)); + } + + T model = (T)serializer.Deserialize(document.CreateReader()); + if (model != null) + { + this.Model = model; + } + } } } diff --git a/CampusAppWP8/CampusAppWP8/utility/XMLFeed.cs b/CampusAppWP8/CampusAppWP8/utility/XMLFeed.cs index 5b49b20d..6efa3610 100644 --- a/CampusAppWP8/CampusAppWP8/utility/XMLFeed.cs +++ b/CampusAppWP8/CampusAppWP8/utility/XMLFeed.cs @@ -18,6 +18,9 @@ namespace CampusAppWP8.Utility /// Type for model of the feed public abstract class XmlFeed : Feed { + /// + /// Variablo for the name of the roottag + /// protected string validRootName = Constants.XMLRootElementName; #region Constructor