Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/IO/Binary/BinarySerializer.cs @ 13398

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

removed warnings from HeuristicLab.Netron (ticket #915)

File size: 3.7 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        Document document = control.Document;
33
34
35        //Warning!: cleaning up, you need to unhook all events since
36        // unserializable classes hooked to events will give problems.       
37        f.Serialize<Document>(fs, document);
38        return true;
39      }
40      catch (Exception exc) {
41        //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);
42        Trace.WriteLine(exc.Message, "BinarySerializer.SaveAs");
43
44        //DumpInfo();
45      }
46      finally
47      {
48        fs.Close();
49      }
50      return false;
51    }
52        /// <summary>
53        /// Opens the binary-saved diagram
54        /// </summary>
55        /// <param name="fileName">Name of the file.</param>
56        /// <param name="control">The control.</param>
57    public static  bool Open (string fileName, DiagramControlBase control)
58    {
59      FileStream fs=null;
60     
61      try
62      {
63        fs= File.OpenRead(fileName);
64      }
65      catch (System.IO.DirectoryNotFoundException exc)
66      {
67        System.Windows.Forms.MessageBox.Show(exc.Message);
68      }
69      catch(System.IO.FileLoadException exc)
70      {       
71        System.Windows.Forms.MessageBox.Show(exc.Message);
72      }
73      catch (System.IO.FileNotFoundException exc)
74      {
75        System.Windows.Forms.MessageBox.Show(exc.Message);
76      }
77      catch(Exception exc)
78      {       
79        throw new Exception("Non-CLS exception caught.", exc);
80      }
81      //donnot open anything if filestream is not there
82      if (fs==null)
83            {
84                return false;
85            }
86
87      try
88      {
89
90                GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>();     
91                //one-line deserialization, no bits 'nd bytes necessary....C# is amazing...
92        Document document =  f.Deserialize<Document>(fs);
93
94                if(document == null)
95                    throw new InconsistencyException("The deserialization return 'null'.");
96                //call the standard method at the control level to attach a new document
97                //In principle you could create a document programmatically and attach it this way as well.
98                control.AttachToDocument(document);
99                return true;
100      }
101      catch(SerializationException exc)     
102      {
103        MessageBox.Show(exc.Message);
104      }
105      catch(System.Reflection.TargetInvocationException exc)
106      {
107        MessageBox.Show(exc.Message, "BinarySerializer.Open");
108      }
109      catch(Exception exc)
110      {
111                MessageBox.Show(exc.Message, "BinarySerializer.Open");
112      }
113      finally
114      {
115                if (fs != null)
116                {
117                    fs.Close();
118                }
119      }
120            return false;
121    }
122
123   
124    #endregion
125  }
126}
Note: See TracBrowser for help on using the repository browser.