1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using Netron.GraphLib.Interfaces;
|
---|
4 | using Netron.GraphLib.IO.Binary;
|
---|
5 | using System.Diagnostics;
|
---|
6 | using System.Runtime.Serialization.Formatters.Binary;
|
---|
7 | namespace Netron.GraphLib.IO.Reporting
|
---|
8 | {
|
---|
9 | /// <summary>
|
---|
10 | /// Summary description for BinaryReporter.
|
---|
11 | /// </summary>
|
---|
12 | public class BinaryReporter : IReporter
|
---|
13 | {
|
---|
14 |
|
---|
15 | /// <summary>
|
---|
16 | /// Occurs when a report is found and added to the collection
|
---|
17 | /// </summary>
|
---|
18 | public event InfoDelegate OnReport;
|
---|
19 |
|
---|
20 | #region Fields
|
---|
21 | private string path;
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | #region Properties
|
---|
25 | /// <summary>
|
---|
26 | /// Constructor
|
---|
27 | /// </summary>
|
---|
28 | /// <param name="path">the path to either a binary saved diagram or a directory, in the latter case the report will contain a
|
---|
29 | /// collection of reports</param>
|
---|
30 | public BinaryReporter(string path)
|
---|
31 | {
|
---|
32 | this.path = path;
|
---|
33 | }
|
---|
34 |
|
---|
35 | #endregion
|
---|
36 |
|
---|
37 | #region Methods
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Returns a BinaryReport or a collection of BinaryReport objects depending on whether the given path is a file or a directory
|
---|
41 | /// </summary>
|
---|
42 | /// <returns></returns>
|
---|
43 | public object Report()
|
---|
44 | {
|
---|
45 | if(Directory.Exists(path)) //directory
|
---|
46 | return GetReports();
|
---|
47 | else
|
---|
48 | return GetReport(path);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Assuming the given path is a directory this method will return a
|
---|
53 | /// collection of BinaryReports of the binary diagram in the directory
|
---|
54 | /// </summary>
|
---|
55 | /// <returns></returns>
|
---|
56 | private BinaryReportCollection GetReports()
|
---|
57 | {
|
---|
58 | string[] files = Directory.GetFiles(path,"*.netron");
|
---|
59 | BinaryReportCollection col = new BinaryReportCollection();
|
---|
60 | BinaryReport report;
|
---|
61 | for(int k=0; k<files.Length; k++)
|
---|
62 | {
|
---|
63 | report = GetReport(files[k]);
|
---|
64 | if(report!=null) col.Add(report);
|
---|
65 | }
|
---|
66 | return col;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /// <summary>
|
---|
70 | /// Returns a BinaryReport of the diagram save in the given path/file
|
---|
71 | /// </summary>
|
---|
72 | /// <param name="filePath"></param>
|
---|
73 | /// <returns></returns>
|
---|
74 | private BinaryReport GetReport(string filePath)
|
---|
75 | {
|
---|
76 | BinaryReport report = new BinaryReport();
|
---|
77 | BinaryCapsule capsule = LoadCapsule(filePath);
|
---|
78 | if(capsule!=null)
|
---|
79 | {
|
---|
80 |
|
---|
81 | report.Thumbnail = capsule.Thumbnail;
|
---|
82 | GraphInformation info = capsule.Abstract.GraphInformation;
|
---|
83 | report.Author = info.Author;
|
---|
84 | report.CreationDate = info.CreationDate;
|
---|
85 | report.Description = info.Description;
|
---|
86 | report.FileSize = new System.IO.FileInfo(filePath).Length;
|
---|
87 | report.Path = filePath;
|
---|
88 | report.Subject = info.Subject;
|
---|
89 | report.Title = info.Title;
|
---|
90 | if(OnReport!=null) OnReport(report,OutputInfoLevels.Info);
|
---|
91 | return report;
|
---|
92 | }
|
---|
93 | else
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private BinaryCapsule LoadCapsule(string fileName)
|
---|
98 | {
|
---|
99 | FileStream fs=null;
|
---|
100 |
|
---|
101 | try
|
---|
102 | {
|
---|
103 | fs= File.OpenRead(fileName);
|
---|
104 | }
|
---|
105 | catch (System.IO.DirectoryNotFoundException exc)
|
---|
106 | {
|
---|
107 | System.Windows.Forms.MessageBox.Show(exc.Message);
|
---|
108 | }
|
---|
109 | catch(System.IO.FileLoadException exc)
|
---|
110 | {
|
---|
111 | System.Windows.Forms.MessageBox.Show(exc.Message);
|
---|
112 | }
|
---|
113 | catch (System.IO.FileNotFoundException exc)
|
---|
114 | {
|
---|
115 | System.Windows.Forms.MessageBox.Show(exc.Message);
|
---|
116 | }
|
---|
117 | catch
|
---|
118 | {
|
---|
119 | Trace.WriteLine("Non-CLS exception caught.","BinarySerializer.SaveAs");
|
---|
120 | }
|
---|
121 | //donnot open anything if filestream is not there
|
---|
122 | if (fs==null) return null;
|
---|
123 | try
|
---|
124 | {
|
---|
125 |
|
---|
126 | BinaryFormatter f = new BinaryFormatter();
|
---|
127 |
|
---|
128 | BinaryCapsule capsule = (BinaryCapsule) f.Deserialize(fs);
|
---|
129 |
|
---|
130 | return capsule;
|
---|
131 | }
|
---|
132 | catch(System.Runtime.Serialization.SerializationException exc)
|
---|
133 | {
|
---|
134 | Trace.WriteLine(exc.Message,"BinaryReporter.LoadCapsule");
|
---|
135 | return null;
|
---|
136 | }
|
---|
137 | catch(Exception exc)
|
---|
138 | {
|
---|
139 | Trace.WriteLine(exc.Message,"BinaryReporter.LoadCapsule");
|
---|
140 | return null;
|
---|
141 | }
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | }
|
---|
148 | }
|
---|