|
30
|
1 |
using System; |
|
|
2 |
using System.Net; |
|
|
3 |
using System.Windows; |
|
|
4 |
using System.Windows.Controls; |
|
|
5 |
using System.Windows.Documents; |
|
|
6 |
using System.Windows.Ink; |
|
|
7 |
using System.Windows.Input; |
|
|
8 |
using System.Windows.Media; |
|
|
9 |
using System.Windows.Media.Animation; |
|
|
10 |
using System.Windows.Shapes; |
|
|
11 |
using System.Xml.Linq; |
|
|
12 |
using System.IO; |
|
|
13 |
using System.Collections.Generic; |
|
|
14 |
using System.Threading; |
|
|
15 |
namespace Iri.Modernisation.Data.Models |
|
|
16 |
{ |
|
|
17 |
/// <summary> |
|
|
18 |
/// Classe permettant de charger un XML et de convertir son contenu en un type ReturnType désiré |
|
|
19 |
/// </summary> |
|
|
20 |
/// <typeparam name="ReturnType">Type Désiré</typeparam> |
|
|
21 |
public class Loader<ResultType> |
|
|
22 |
{ |
|
34
|
23 |
|
|
|
24 |
public String Path { get; private set; } |
|
30
|
25 |
/// <summary> |
|
|
26 |
/// WebClient qui permet de charger le XML distant |
|
|
27 |
/// </summary> |
|
34
|
28 |
public WebClient xmlClient {get; private set;} |
|
|
29 |
/// <summary> |
|
|
30 |
/// Fonction permettant de transcrire le document XML en Type désiré |
|
|
31 |
/// </summary> |
|
30
|
32 |
private Func<XDocument, ResultType> ProcessFunction { get; set; } |
|
|
33 |
|
|
34
|
34 |
/// <summary> |
|
|
35 |
/// Constructeur |
|
|
36 |
/// </summary> |
|
|
37 |
/// <param name="processFunction">Fonction de transtypage</param> |
|
30
|
38 |
public Loader(Func<XDocument,ResultType> processFunction) |
|
|
39 |
{ |
|
|
40 |
ProcessFunction = processFunction; |
|
34
|
41 |
xmlClient = new WebClient(); |
|
30
|
42 |
} |
|
34
|
43 |
|
|
|
44 |
/// <summary> |
|
|
45 |
/// Téléchargement des données |
|
|
46 |
/// </summary> |
|
|
47 |
/// <param name="path">Chemin du fichier</param> |
|
|
48 |
public void Load(String path) |
|
30
|
49 |
{ |
|
34
|
50 |
Path = path; |
|
30
|
51 |
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted); |
|
|
52 |
xmlClient.DownloadStringAsync(new Uri(path,UriKind.RelativeOrAbsolute)); |
|
|
53 |
|
|
|
54 |
} |
|
|
55 |
|
|
34
|
56 |
/// <summary> |
|
|
57 |
/// Une fois le fichier télécharger, on commence le transtypage |
|
|
58 |
/// </summary> |
|
|
59 |
/// <param name="sender"></param> |
|
|
60 |
/// <param name="e"></param> |
|
30
|
61 |
void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) |
|
|
62 |
{ |
|
34
|
63 |
//On utilise la fonction donnée lors de la construction du Loader pour créer l'objet grâce au fichier XML |
|
30
|
64 |
ResultType result = ProcessFunction.Invoke(XDocument.Parse(e.Result)); |
|
|
65 |
if (LoaderFinished != null) |
|
|
66 |
{ |
|
34
|
67 |
//Une fois le Transtypage Fini, on lance l'évenement indiquant que le chargement est fini et donnant |
|
|
68 |
// en argument l'objet créé. |
|
30
|
69 |
LoaderFinished(this, new LoaderEventArgs<ResultType>(result)); |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
public event EventHandler<LoaderEventArgs<ResultType>> LoaderFinished; |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
} |
|
|
77 |
public class LoaderEventArgs<ResultType> : EventArgs |
|
|
78 |
{ |
|
|
79 |
public ResultType CreatedObject { get; private set; } |
|
|
80 |
public LoaderEventArgs(ResultType createdObject) |
|
|
81 |
{ |
|
|
82 |
CreatedObject = createdObject; |
|
|
83 |
} |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
} |