Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/IO/Binary/BinarySerializer.cs @ 2768

Last change on this file since 2768 was 2768, checked in by mkommend, 14 years ago

added solution folders and sources for the netron library (ticket #867)

File size: 3.9 KB
Line 
1using System;
2using System.Diagnostics;
3using System.IO;
4using System.Runtime.Serialization;
5using System.Runtime.Serialization.Formatters.Binary;
6using System.Xml.Serialization;
7using System.Xml;
8using System.Windows.Forms;
9namespace Netron.Diagramming.Core
10{
11  /// <summary>
12  /// Utility class to binary (de)serialize a diagram (from) to file
13  /// </summary>
14  public static class BinarySerializer
15  {
16    #region Methods
17
18        /// <summary>
19        /// Binary saves the diagram
20        /// </summary>
21        /// <param name="fileName">the file-path</param>
22        /// <param name="control">The control.</param>
23        /// <returns></returns>
24    public static  bool SaveAs(string fileName, DiagramControlBase control)
25    {
26
27      FileStream fs = new FileStream(fileName, FileMode.Create);
28
29      GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>();     
30     
31      try
32      {
33        Document document = control.Document;
34       
35
36        //Warning!: cleaning up, you need to unhook all events since
37                // unserializable classes hooked to events will give problems.       
38        f.Serialize<Document>(fs, document );
39        return true;
40      }     
41      catch(Exception exc)     
42      {
43        //site.OutputInfo("The graph was not saved, because some graph events were attached to non-serializable classes.\r\n This is a known issue and will be resolved in a later stadium.",OutputInfoLevels.Exception);
44        Trace.WriteLine(exc.Message, "BinarySerializer.SaveAs");
45       
46        //DumpInfo();
47      }
48      catch
49      {
50        Trace.WriteLine("Non-CLS exception caught.","BinarySerializer.SaveAs");
51      }
52      finally
53      {
54        fs.Close();
55      }
56      return false;
57    }
58        /// <summary>
59        /// Opens the binary-saved diagram
60        /// </summary>
61        /// <param name="fileName">Name of the file.</param>
62        /// <param name="control">The control.</param>
63    public static  bool Open (string fileName, DiagramControlBase control)
64    {
65      FileStream fs=null;
66     
67      try
68      {
69        fs= File.OpenRead(fileName);
70      }
71      catch (System.IO.DirectoryNotFoundException exc)
72      {
73        System.Windows.Forms.MessageBox.Show(exc.Message);
74      }
75      catch(System.IO.FileLoadException exc)
76      {       
77        System.Windows.Forms.MessageBox.Show(exc.Message);
78      }
79      catch (System.IO.FileNotFoundException exc)
80      {
81        System.Windows.Forms.MessageBox.Show(exc.Message);
82      }
83      catch(Exception exc)
84      {       
85        throw new Exception("Non-CLS exception caught.", exc);
86      }
87      //donnot open anything if filestream is not there
88      if (fs==null)
89            {
90                return false;
91            }
92
93      try
94      {
95
96                GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>();     
97                //one-line deserialization, no bits 'nd bytes necessary....C# is amazing...
98        Document document =  f.Deserialize<Document>(fs);
99
100                if(document == null)
101                    throw new InconsistencyException("The deserialization return 'null'.");
102                //call the standard method at the control level to attach a new document
103                //In principle you could create a document programmatically and attach it this way as well.
104                control.AttachToDocument(document);
105                return true;
106      }
107      catch(SerializationException exc)     
108      {
109        MessageBox.Show(exc.Message);
110      }
111      catch(System.Reflection.TargetInvocationException exc)
112      {
113        MessageBox.Show(exc.Message, "BinarySerializer.Open");
114      }
115      catch(Exception exc)
116      {
117                MessageBox.Show(exc.Message, "BinarySerializer.Open");
118      }
119      catch
120      {
121                MessageBox.Show("Non-CLS exception caught.", "BinarySerializer.Open");
122       
123      }
124      finally
125      {
126                if (fs != null)
127                {
128                    fs.Close();
129                }
130      }
131            return false;
132    }
133
134   
135    #endregion
136  }
137}
Note: See TracBrowser for help on using the repository browser.