1 | //------------------------------------------------------------------------------
|
---|
2 | // <auto-generated>
|
---|
3 | // This code was generated by a tool.
|
---|
4 | //
|
---|
5 | // Changes to this file may cause incorrect behavior and will be lost if
|
---|
6 | // the code is regenerated.
|
---|
7 | // </auto-generated>
|
---|
8 | //------------------------------------------------------------------------------
|
---|
9 |
|
---|
10 | using System.Linq;
|
---|
11 | using DslModeling = global::Microsoft.VisualStudio.Modeling;
|
---|
12 | using DslValidation = global::Microsoft.VisualStudio.Modeling.Validation;
|
---|
13 | using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.SimulationCore
|
---|
16 | {
|
---|
17 |
|
---|
18 | partial class HLSimDomainModel
|
---|
19 | {
|
---|
20 | ///<Summary>
|
---|
21 | /// Provide an implementation of the partial method to set up the serialization behavior for this model.
|
---|
22 | ///</Summary>
|
---|
23 | ///<remarks>
|
---|
24 | /// This partial method is called from the constructor of the domain class.
|
---|
25 | ///</remarks>
|
---|
26 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance","CA1822:MarkMembersAsStatic", Justification="Alternative implementations might need to reference instance variables, so not marked as static.")]
|
---|
27 | partial void InitializeSerialization(DslModeling::Store store)
|
---|
28 | {
|
---|
29 | // Register the serializers and moniker resolver for this model
|
---|
30 | HLSimSerializationHelper.Instance.InitializeSerialization(store);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | /// <summary>
|
---|
36 | /// Helper class for serializing and deserializing HLSim models.
|
---|
37 | /// </summary>
|
---|
38 | public abstract partial class HLSimSerializationHelperBase
|
---|
39 | {
|
---|
40 | #region Constructor
|
---|
41 | /// <summary>
|
---|
42 | /// Constructor
|
---|
43 | /// </summary>
|
---|
44 | protected HLSimSerializationHelperBase() { }
|
---|
45 | #endregion
|
---|
46 |
|
---|
47 | #region Methods
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Ensure that moniker resolvers and domain element serializers are installed properly on the given store,
|
---|
51 | /// so that deserialization can be carried out correctly.
|
---|
52 | /// </summary>
|
---|
53 | /// <param name="store">Store on which moniker resolvers will be set up.</param>
|
---|
54 | internal protected virtual void InitializeSerialization(DslModeling::Store store)
|
---|
55 | {
|
---|
56 | #region Check Parameters
|
---|
57 | global::System.Diagnostics.Debug.Assert(store != null);
|
---|
58 | if (store == null)
|
---|
59 | throw new global::System.ArgumentNullException("store");
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 | DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(store);
|
---|
63 |
|
---|
64 | // Register the moniker resolver for this model, unless one is already registered
|
---|
65 | DslModeling::IMonikerResolver monikerResolver = store.FindMonikerResolver(HLSimDomainModel.DomainModelId);
|
---|
66 | if (monikerResolver == null)
|
---|
67 | {
|
---|
68 | monikerResolver = new HLSimSerializationBehaviorMonikerResolver(store, directory);
|
---|
69 | store.AddMonikerResolver(HLSimDomainModel.DomainModelId, monikerResolver);
|
---|
70 | }
|
---|
71 |
|
---|
72 | // Add serialization behaviors
|
---|
73 | directory.AddBehavior(HLSimSerializationBehavior.Instance);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /// <Summary>
|
---|
77 | /// Called by the serialization helper to allow any necessary setup to be done on each load / save.
|
---|
78 | /// </Summary>
|
---|
79 | /// <param name="partition">The partition being serialized.</param>
|
---|
80 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
81 | /// <param name="isLoading">Flag to indicate whether the file is being loaded or saved.</param>
|
---|
82 | /// <Remarks>The base implementation does nothing</Remarks>
|
---|
83 | protected virtual void InitializeSerializationContext(DslModeling::Partition partition, DslModeling::SerializationContext serializationContext, bool isLoading)
|
---|
84 | {
|
---|
85 | }
|
---|
86 |
|
---|
87 | /// <summary>
|
---|
88 | /// Return the directory of serializers to use
|
---|
89 | /// </summary>
|
---|
90 | protected virtual DslModeling::DomainXmlSerializerDirectory GetDirectory(DslModeling::Store store)
|
---|
91 | {
|
---|
92 | // Just return the default serialization directory from the store
|
---|
93 | return store.SerializerDirectory;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /// <summary>
|
---|
97 | /// This method returns the moniker resolvers for each of the domain models in the store
|
---|
98 | /// </summary>
|
---|
99 | /// <param name="store">Store on which the moniker resolvers are set up.</param>
|
---|
100 | internal protected virtual global::System.Collections.Generic.IDictionary<global::System.Guid, DslModeling::IMonikerResolver> GetMonikerResolvers(DslModeling::Store store)
|
---|
101 | {
|
---|
102 | #region Check Parameters
|
---|
103 | global::System.Diagnostics.Debug.Assert(store != null);
|
---|
104 | if (store == null)
|
---|
105 | throw new global::System.ArgumentNullException("store");
|
---|
106 | #endregion
|
---|
107 |
|
---|
108 | global::System.Collections.Generic.Dictionary<global::System.Guid, DslModeling::IMonikerResolver> result = new global::System.Collections.Generic.Dictionary<global::System.Guid, DslModeling::IMonikerResolver>();
|
---|
109 | foreach (DslModeling::DomainModelInfo modelInfo in store.DomainDataDirectory.DomainModels)
|
---|
110 | {
|
---|
111 | if (modelInfo.MonikerResolver != null)
|
---|
112 | {
|
---|
113 | result.Add(modelInfo.Id, modelInfo.MonikerResolver);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | return result;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /// <summary>
|
---|
121 | /// Write extension element data inside the current XML element
|
---|
122 | /// </summary>
|
---|
123 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
124 | /// <param name="element">The element whose attributes have just been written.</param>
|
---|
125 | /// <param name="writer">XmlWriter to write serialized data to.</param>
|
---|
126 | /// <remarks>The default implemenation is to write out all non-embedded extension elements,
|
---|
127 | /// regardless of whether they relate to the current element or not.
|
---|
128 | /// The additional data should be written as a series of one or more
|
---|
129 | /// XML elements.</remarks>
|
---|
130 | internal protected virtual void WriteExtensions(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlWriter writer)
|
---|
131 | {
|
---|
132 | if (serializationContext == null)
|
---|
133 | {
|
---|
134 | throw new global::System.ArgumentNullException("serializationContext");
|
---|
135 | }
|
---|
136 | if (element == null)
|
---|
137 | {
|
---|
138 | throw new global::System.ArgumentNullException("element");
|
---|
139 | }
|
---|
140 | if (writer == null)
|
---|
141 | {
|
---|
142 | throw new global::System.ArgumentNullException("writer");
|
---|
143 | }
|
---|
144 |
|
---|
145 | // Build a list of extension elements to serialize
|
---|
146 | global::System.Collections.ObjectModel.ReadOnlyCollection<DslModeling::ModelElement> allExtensionElements = element.Partition.ElementDirectory.FindElements(DslModeling::ExtensionElement.DomainClassId, true);
|
---|
147 | global::System.Collections.Generic.IEnumerable<DslModeling::ExtensionElement> nonEmbeddedExtensionsElements = allExtensionElements.Where(e => DslModeling::DomainClassInfo.FindEmbeddingElementLink(e) == null).OfType<DslModeling::ExtensionElement>();
|
---|
148 |
|
---|
149 | DslModeling::SerializationUtilities.WriteExtensions(serializationContext, writer, nonEmbeddedExtensionsElements);
|
---|
150 | }
|
---|
151 |
|
---|
152 | /// <summary>
|
---|
153 | /// Read any extension data written inside this XML element
|
---|
154 | /// </summary>
|
---|
155 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
156 | /// <param name="element">In-memory ModelElement instance that is currently being read.</param>
|
---|
157 | /// <param name="reader">Reader for the file being read. The reader is positioned after the attributes of the specified element.</param>
|
---|
158 | /// <remarks>The method reads any extension element data, regardless of whether it relates the current
|
---|
159 | /// element or not. There may be no additional data for the specified element.</remarks>
|
---|
160 | internal protected virtual void ReadExtensions(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlReader reader)
|
---|
161 | {
|
---|
162 | if (serializationContext == null)
|
---|
163 | {
|
---|
164 | throw new global::System.ArgumentNullException("serializationContext");
|
---|
165 | }
|
---|
166 | if (element == null)
|
---|
167 | {
|
---|
168 | throw new global::System.ArgumentNullException("element");
|
---|
169 | }
|
---|
170 | if (reader == null)
|
---|
171 | {
|
---|
172 | throw new global::System.ArgumentNullException("reader");
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (string.CompareOrdinal(reader.LocalName, DslModeling::SerializationUtilities.ExtensionsXmlElementName) == 0)
|
---|
176 | {
|
---|
177 | DslModeling::SerializationUtilities.ReadExtensions(serializationContext, reader, element.Partition);
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | /// <summary>
|
---|
182 | /// Writes the specified attribute to the file.
|
---|
183 | /// </summary>
|
---|
184 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
185 | /// <param name="element">The element whose attributes have just been written.</param>
|
---|
186 | /// <param name="writer">XmlWriter to write serialized data to.</param>
|
---|
187 | /// <param name="attributeName">Name of the attribute to be written</param>
|
---|
188 | /// <param name="attributeValue">Value of the attribute to be written</param>
|
---|
189 | /// <remarks>This is an extension point to allow customisation e.g. to encode the data
|
---|
190 | /// being written to the file.</remarks>
|
---|
191 | internal virtual void WriteAttributeString(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlWriter writer, string attributeName, string attributeValue)
|
---|
192 | {
|
---|
193 | writer.WriteAttributeString(attributeName, attributeValue);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /// <summary>
|
---|
197 | /// Writes the specified element to the file.
|
---|
198 | /// </summary>
|
---|
199 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
200 | /// <param name="element">The element whose attributes have just been written.</param>
|
---|
201 | /// <param name="writer">XmlWriter to write serialized data to.</param>
|
---|
202 | /// <param name="elementName">Name of the element to be written.</param>
|
---|
203 | /// <param name="elementValue">Value of the element to be written.</param>
|
---|
204 | /// <remarks>This is an extension point to allow customisation e.g. to encode the data
|
---|
205 | /// being written to the file.</remarks>
|
---|
206 | internal virtual void WriteElementString(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlWriter writer, string elementName, string elementValue)
|
---|
207 | {
|
---|
208 | writer.WriteElementString(elementName, elementValue);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /// <summary>
|
---|
212 | /// Reads and returns the value of an attribute.
|
---|
213 | /// </summary>
|
---|
214 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
215 | /// <param name="element">The element whose attributes have just been written.</param>
|
---|
216 | /// <param name="reader">XmlReader to read the serialized data from.</param>
|
---|
217 | /// <param name="attributeName">The name of the attribute to be read.</param>
|
---|
218 | /// <returns>The value of the attribute.</returns>
|
---|
219 | /// <remarks>This is an extension point to allow customisation e.g. to decode the data
|
---|
220 | /// being written to the file.</remarks>
|
---|
221 | internal virtual string ReadAttribute(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlReader reader, string attributeName)
|
---|
222 | {
|
---|
223 | return reader.GetAttribute(attributeName);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /// <summary>
|
---|
227 | /// Reads and returns the value of an element.
|
---|
228 | /// </summary>
|
---|
229 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
230 | /// <param name="element">The element whose attributes have just been written.</param>
|
---|
231 | /// <param name="reader">XmlReader to read the serialized data from.</param>
|
---|
232 | /// <returns>The value of the element.</returns>
|
---|
233 | /// <remarks>This is an extension point to allow customisation e.g. to decode the data
|
---|
234 | /// being written to the file.</remarks>
|
---|
235 | internal virtual string ReadElementContentAsString(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlReader reader)
|
---|
236 | {
|
---|
237 | return reader.ReadElementContentAsString();
|
---|
238 | }
|
---|
239 |
|
---|
240 | /// <summary>
|
---|
241 | /// Creates and returns the settings used when reading a file.
|
---|
242 | /// </summary>
|
---|
243 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
244 | /// <param name="isDiagram">Indicates whether a diagram or model file is currently being serialized.</param>
|
---|
245 | internal virtual global::System.Xml.XmlReaderSettings CreateXmlReaderSettings(DslModeling::SerializationContext serializationContext, bool isDiagram)
|
---|
246 | {
|
---|
247 | return new global::System.Xml.XmlReaderSettings();
|
---|
248 | }
|
---|
249 |
|
---|
250 | /// <summary>
|
---|
251 | /// Creates and returns the settings used when writing a file.
|
---|
252 | /// </summary>
|
---|
253 | /// <param name="serializationContext">The current serialization context instance.</param>
|
---|
254 | /// <param name="isDiagram">Indicates whether a diagram or model file is currently being serialized.</param>
|
---|
255 | /// <param name="encoding">The encoding to use when writing the file.</param>
|
---|
256 | internal virtual global::System.Xml.XmlWriterSettings CreateXmlWriterSettings(DslModeling::SerializationContext serializationContext, bool isDiagram, global::System.Text.Encoding encoding)
|
---|
257 | {
|
---|
258 | global::System.Xml.XmlWriterSettings settings = new global::System.Xml.XmlWriterSettings();
|
---|
259 | settings.Indent = true;
|
---|
260 | settings.Encoding = encoding;
|
---|
261 |
|
---|
262 | return settings;
|
---|
263 | }
|
---|
264 |
|
---|
265 | #endregion
|
---|
266 | }
|
---|
267 |
|
---|
268 | /// <summary>
|
---|
269 | /// Helper class for serializing and deserializing HLSim models.
|
---|
270 | /// </summary>
|
---|
271 | public sealed partial class HLSimSerializationHelper : HLSimSerializationHelperBase
|
---|
272 | {
|
---|
273 | #region Constructor
|
---|
274 | /// <summary>
|
---|
275 | /// Private constructor to prevent direct instantiation.
|
---|
276 | /// </summary>
|
---|
277 | private HLSimSerializationHelper() : base () { }
|
---|
278 | #endregion
|
---|
279 |
|
---|
280 | #region Singleton Instance
|
---|
281 | /// <summary>
|
---|
282 | /// Singleton instance.
|
---|
283 | /// </summary>
|
---|
284 | private static HLSimSerializationHelper instance;
|
---|
285 | /// <summary>
|
---|
286 | /// Singleton instance.
|
---|
287 | /// </summary>
|
---|
288 | [global::System.Diagnostics.DebuggerBrowsable(global::System.Diagnostics.DebuggerBrowsableState.Never)] // Will trigger creation otherwise.
|
---|
289 | public static HLSimSerializationHelper Instance
|
---|
290 | {
|
---|
291 | [global::System.Diagnostics.DebuggerStepThrough]
|
---|
292 | get
|
---|
293 | {
|
---|
294 | if (HLSimSerializationHelper.instance == null)
|
---|
295 | HLSimSerializationHelper.instance = new HLSimSerializationHelper();
|
---|
296 | return HLSimSerializationHelper.instance;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | #endregion
|
---|
300 | }
|
---|
301 |
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | namespace HeuristicLab.SimulationCore
|
---|
306 | {
|
---|
307 |
|
---|
308 | partial class HLSimSerializationHelperBase
|
---|
309 | {
|
---|
310 |
|
---|
311 | /// <summary>
|
---|
312 | /// Loads a SimulationModel instance into the default partition of the given store, and ignore serialization result.
|
---|
313 | /// </summary>
|
---|
314 | /// <param name="store">The new SimulationModel instance will be created into the default partition of this store.</param>
|
---|
315 | /// <param name="fileName">Name of the file from which the SimulationModel instance will be deserialized.</param>
|
---|
316 | /// <param name="schemaResolver">
|
---|
317 | /// An ISchemaResolver that allows the serializer to do schema validation on the root element (and everything inside it).
|
---|
318 | /// If null is passed, schema validation will not be performed.
|
---|
319 | /// </param>
|
---|
320 | /// <param name="validationController">
|
---|
321 | /// A ValidationController that will be used to do load-time validation (validations with validation category "Load"). If null
|
---|
322 | /// is passed, load-time validation will not be performed.
|
---|
323 | /// </param>
|
---|
324 | /// <param name="serializerLocator">
|
---|
325 | /// An ISerializerLocator that will be used to locate any additional domain model types required to load the model. Can be null.
|
---|
326 | /// </param>
|
---|
327 | /// <returns>The loaded SimulationModel instance.</returns>
|
---|
328 | public virtual SimulationModel LoadModel(DslModeling::Store store, string fileName, DslModeling::ISchemaResolver schemaResolver, DslValidation::ValidationController validationController, DslModeling::ISerializerLocator serializerLocator)
|
---|
329 | {
|
---|
330 | #region Check Parameters
|
---|
331 | if (store == null)
|
---|
332 | throw new global::System.ArgumentNullException("store");
|
---|
333 | #endregion
|
---|
334 |
|
---|
335 | return this.LoadModel(new DslModeling::SerializationResult(), store.DefaultPartition, fileName, schemaResolver, validationController, serializerLocator);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /// <summary>
|
---|
339 | /// Loads a SimulationModel instance into the default partition of the given store.
|
---|
340 | /// </summary>
|
---|
341 | /// <param name="serializationResult">Stores serialization result from the load operation.</param>
|
---|
342 | /// <param name="store">The new SimulationModel instance will be created into the default partition of this store.</param>
|
---|
343 | /// <param name="fileName">Name of the file from which the SimulationModel instance will be deserialized.</param>
|
---|
344 | /// <param name="schemaResolver">
|
---|
345 | /// An ISchemaResolver that allows the serializer to do schema validation on the root element (and everything inside it).
|
---|
346 | /// If null is passed, schema validation will not be performed.
|
---|
347 | /// </param>
|
---|
348 | /// <param name="validationController">
|
---|
349 | /// A ValidationController that will be used to do load-time validation (validations with validation category "Load"). If null
|
---|
350 | /// is passed, load-time validation will not be performed.
|
---|
351 | /// </param>
|
---|
352 | /// <param name="serializerLocator">
|
---|
353 | /// An ISerializerLocator that will be used to locate any additional domain model types required to load the model. Can be null.
|
---|
354 | /// </param>
|
---|
355 | /// <returns>The loaded SimulationModel instance.</returns>
|
---|
356 | public virtual SimulationModel LoadModel(DslModeling::SerializationResult serializationResult, DslModeling::Store store, string fileName, DslModeling::ISchemaResolver schemaResolver, DslValidation::ValidationController validationController, DslModeling::ISerializerLocator serializerLocator)
|
---|
357 | {
|
---|
358 | #region Check Parameters
|
---|
359 | if (store == null)
|
---|
360 | throw new global::System.ArgumentNullException("store");
|
---|
361 | #endregion
|
---|
362 |
|
---|
363 | return this.LoadModel(serializationResult, store.DefaultPartition, fileName, schemaResolver, validationController, serializerLocator);
|
---|
364 | }
|
---|
365 |
|
---|
366 | /// <summary>
|
---|
367 | /// Loads a SimulationModel instance.
|
---|
368 | /// </summary>
|
---|
369 | /// <param name="serializationResult">Stores serialization result from the load operation.</param>
|
---|
370 | /// <param name="partition">Partition in which the new SimulationModel instance will be created.</param>
|
---|
371 | /// <param name="fileName">Name of the file from which the SimulationModel instance will be deserialized.</param>
|
---|
372 | /// <param name="schemaResolver">
|
---|
373 | /// An ISchemaResolver that allows the serializer to do schema validation on the root element (and everything inside it).
|
---|
374 | /// If null is passed, schema validation will not be performed.
|
---|
375 | /// </param>
|
---|
376 | /// <param name="validationController">
|
---|
377 | /// A ValidationController that will be used to do load-time validation (validations with validation category "Load"). If null
|
---|
378 | /// is passed, load-time validation will not be performed.
|
---|
379 | /// </param>
|
---|
380 | /// <param name="serializerLocator">
|
---|
381 | /// An ISerializerLocator that will be used to locate any additional domain model types required to load the model. Can be null.
|
---|
382 | /// </param>
|
---|
383 | /// <returns>The loaded SimulationModel instance.</returns>
|
---|
384 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability","CA1506:AvoidExcessiveClassCoupling", Justification="Generated code")]
|
---|
385 | public virtual SimulationModel LoadModel(DslModeling::SerializationResult serializationResult, DslModeling::Partition partition, string fileName, DslModeling::ISchemaResolver schemaResolver, DslValidation::ValidationController validationController, DslModeling::ISerializerLocator serializerLocator)
|
---|
386 | {
|
---|
387 | #region Check Parameters
|
---|
388 | if (serializationResult == null)
|
---|
389 | throw new global::System.ArgumentNullException("serializationResult");
|
---|
390 | if (partition == null)
|
---|
391 | throw new global::System.ArgumentNullException("partition");
|
---|
392 | if (string.IsNullOrEmpty(fileName))
|
---|
393 | throw new global::System.ArgumentNullException("fileName");
|
---|
394 | #endregion
|
---|
395 |
|
---|
396 | // Ensure there is a transaction for this model to Load in.
|
---|
397 | if (!partition.Store.TransactionActive)
|
---|
398 | {
|
---|
399 | throw new global::System.InvalidOperationException(HLSimDomainModel.SingletonResourceManager.GetString("MissingTransaction"));
|
---|
400 | }
|
---|
401 |
|
---|
402 | SimulationModel modelRoot = null;
|
---|
403 | DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(partition.Store);
|
---|
404 | DslModeling::DomainClassXmlSerializer modelRootSerializer = directory.GetSerializer(SimulationModel.DomainClassId);
|
---|
405 | global::System.Diagnostics.Debug.Assert(modelRootSerializer != null, "Cannot find serializer for SimulationModel!");
|
---|
406 | if (modelRootSerializer != null)
|
---|
407 | {
|
---|
408 | using (global::System.IO.FileStream fileStream = global::System.IO.File.OpenRead(fileName))
|
---|
409 | {
|
---|
410 | DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(directory, fileStream.Name, serializationResult);
|
---|
411 | this.InitializeSerializationContext(partition, serializationContext, true);
|
---|
412 | DslModeling::TransactionContext transactionContext = new DslModeling::TransactionContext();
|
---|
413 | transactionContext.Add(DslModeling::SerializationContext.TransactionContextKey, serializationContext);
|
---|
414 | using (DslModeling::Transaction t = partition.Store.TransactionManager.BeginTransaction("Load Model from " + fileName, true, transactionContext))
|
---|
415 | {
|
---|
416 | // Ensure there is some content in the file. Blank (or almost blank, to account for encoding header bytes, etc.)
|
---|
417 | // files will cause a new root element to be created and returned.
|
---|
418 | if (fileStream.Length > 5)
|
---|
419 | {
|
---|
420 | try
|
---|
421 | {
|
---|
422 | global::System.Xml.XmlReaderSettings settings = HLSimSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false);
|
---|
423 | using (global::System.Xml.XmlReader reader = global::System.Xml.XmlReader.Create(fileStream, settings))
|
---|
424 | {
|
---|
425 | // Attempt to read the encoding.
|
---|
426 | reader.Read(); // Move to the first node - will be the XmlDeclaration if there is one.
|
---|
427 | global::System.Text.Encoding encoding;
|
---|
428 | if (this.TryGetEncoding(reader, out encoding))
|
---|
429 | {
|
---|
430 | serializationResult.Encoding = encoding;
|
---|
431 | }
|
---|
432 |
|
---|
433 | // Load any additional domain models that are required
|
---|
434 | DslModeling::SerializationUtilities.ResolveDomainModels(reader, serializerLocator, partition.Store);
|
---|
435 |
|
---|
436 | reader.MoveToContent();
|
---|
437 |
|
---|
438 |
|
---|
439 | modelRoot = modelRootSerializer.TryCreateInstance(serializationContext, reader, partition) as SimulationModel;
|
---|
440 | if (modelRoot != null && !serializationResult.Failed)
|
---|
441 | {
|
---|
442 | this.ReadRootElement(serializationContext, modelRoot, reader, schemaResolver);
|
---|
443 | }
|
---|
444 | }
|
---|
445 |
|
---|
446 | }
|
---|
447 | catch (global::System.Xml.XmlException xEx)
|
---|
448 | {
|
---|
449 | DslModeling::SerializationUtilities.AddMessage(
|
---|
450 | serializationContext,
|
---|
451 | DslModeling::SerializationMessageKind.Error,
|
---|
452 | xEx
|
---|
453 | );
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | if(modelRoot == null && !serializationResult.Failed)
|
---|
458 | {
|
---|
459 | // create model root if it doesn't exist.
|
---|
460 | modelRoot = this.CreateModelHelper(partition);
|
---|
461 | }
|
---|
462 | if (t.IsActive)
|
---|
463 | t.Commit();
|
---|
464 | } // End Inner Tx
|
---|
465 |
|
---|
466 | // Do load-time validation if a ValidationController is provided.
|
---|
467 | if (!serializationResult.Failed && validationController != null)
|
---|
468 | {
|
---|
469 | using (new SerializationValidationObserver(serializationResult, validationController))
|
---|
470 | {
|
---|
471 | validationController.Validate(partition, DslValidation::ValidationCategories.Load);
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | }
|
---|
476 | }
|
---|
477 | return modelRoot;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /// <summary>
|
---|
481 | /// Attempts to return the encoding used by the reader.
|
---|
482 | /// </summary>
|
---|
483 | /// <remarks>
|
---|
484 | /// The reader will be positioned at the start of the document when calling this method.
|
---|
485 | /// </remarks>
|
---|
486 | protected virtual bool TryGetEncoding(global::System.Xml.XmlReader reader, out global::System.Text.Encoding encoding)
|
---|
487 | {
|
---|
488 | global::System.Diagnostics.Debug.Assert(reader.NodeType == System.Xml.XmlNodeType.XmlDeclaration, "reader should be at the XmlDeclaration node when calling this method");
|
---|
489 |
|
---|
490 | encoding = null;
|
---|
491 | // Attempt to read and parse the "encoding" attribute from the XML declaration node
|
---|
492 | if (reader.NodeType == global::System.Xml.XmlNodeType.XmlDeclaration)
|
---|
493 | {
|
---|
494 | string encodingName = reader.GetAttribute("encoding");
|
---|
495 | if (!string.IsNullOrWhiteSpace(encodingName))
|
---|
496 | {
|
---|
497 | encoding = global::System.Text.Encoding.GetEncoding(encodingName);
|
---|
498 | return true;
|
---|
499 | }
|
---|
500 | }
|
---|
501 | return false;
|
---|
502 | }
|
---|
503 |
|
---|
504 | /// <summary>
|
---|
505 | /// Saves the given model root to the given file, with default encoding (UTF-8), and optional properties with default value will not
|
---|
506 | /// be written out.
|
---|
507 | /// </summary>
|
---|
508 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
509 | /// <param name="modelRoot">SimulationModel instance to be saved.</param>
|
---|
510 | /// <param name="fileName">Name of the file in which the SimulationModel instance will be saved.</param>
|
---|
511 | public virtual void SaveModel(DslModeling::SerializationResult serializationResult, SimulationModel modelRoot, string fileName)
|
---|
512 | {
|
---|
513 | this.SaveModel(serializationResult, modelRoot, fileName, global::System.Text.Encoding.UTF8, false);
|
---|
514 | }
|
---|
515 |
|
---|
516 | /// <summary>
|
---|
517 | /// Saves the given model to the given file, with default encoding (UTF-8).
|
---|
518 | /// </summary>
|
---|
519 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
520 | /// <param name="modelRoot">SimulationModel instance to be saved.</param>
|
---|
521 | /// <param name="fileName">Name of the file in which the SimulationModel instance will be saved.</param>
|
---|
522 | /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
|
---|
523 | public virtual void SaveModel(DslModeling::SerializationResult serializationResult, SimulationModel modelRoot, string fileName, bool writeOptionalPropertiesWithDefaultValue)
|
---|
524 | {
|
---|
525 | this.SaveModel(serializationResult, modelRoot, fileName, global::System.Text.Encoding.UTF8, writeOptionalPropertiesWithDefaultValue);
|
---|
526 | }
|
---|
527 |
|
---|
528 | /// <summary>
|
---|
529 | /// Saves the given model root to the given file, with specified encoding.
|
---|
530 | /// </summary>
|
---|
531 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
532 | /// <param name="modelRoot">SimulationModel instance to be saved.</param>
|
---|
533 | /// <param name="fileName">Name of the file in which the SimulationModel instance will be saved.</param>
|
---|
534 | /// <param name="encoding">Encoding to use when saving the SimulationModel instance.</param>
|
---|
535 | /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
|
---|
536 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
|
---|
537 | public virtual void SaveModel(DslModeling::SerializationResult serializationResult, SimulationModel modelRoot, string fileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
|
---|
538 | {
|
---|
539 | #region Check Parameters
|
---|
540 | if (serializationResult == null)
|
---|
541 | throw new global::System.ArgumentNullException("serializationResult");
|
---|
542 | if (modelRoot == null)
|
---|
543 | throw new global::System.ArgumentNullException("modelRoot");
|
---|
544 | if (string.IsNullOrEmpty(fileName))
|
---|
545 | throw new global::System.ArgumentNullException("fileName");
|
---|
546 | #endregion
|
---|
547 |
|
---|
548 | if (serializationResult.Failed)
|
---|
549 | return;
|
---|
550 |
|
---|
551 | using (global::System.IO.MemoryStream newFileContent = this.InternalSaveModel(serializationResult, modelRoot, fileName, encoding, writeOptionalPropertiesWithDefaultValue))
|
---|
552 | {
|
---|
553 | if (!serializationResult.Failed && newFileContent != null)
|
---|
554 | { // Only write the content if there's no error encountered during serialization.
|
---|
555 | using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(fileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None))
|
---|
556 | {
|
---|
557 | using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding))
|
---|
558 | {
|
---|
559 | writer.Write(newFileContent.ToArray());
|
---|
560 | }
|
---|
561 | }
|
---|
562 | }
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | /// <summary>
|
---|
567 | /// Saves the given model root as a in-memory stream.
|
---|
568 | /// This is a helper used by SaveModel() and SaveModelAndDiagram(). When saving the model and the diagram together, we want to make sure that
|
---|
569 | /// both can be saved without error before writing the content to disk, so we serialize the model into a in-memory stream first.
|
---|
570 | /// </summary>
|
---|
571 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
572 | /// <param name="modelRoot">SimulationModel instance to be saved.</param>
|
---|
573 | /// <param name="fileName">Name of the file in which the SimulationModel instance will be saved.</param>
|
---|
574 | /// <param name="encoding">Encoding to use when saving the SimulationModel instance.</param>
|
---|
575 | /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
|
---|
576 | /// <returns>In-memory stream containing the serialized SimulationModel instance.</returns>
|
---|
577 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
|
---|
578 | internal global::System.IO.MemoryStream InternalSaveModel(DslModeling::SerializationResult serializationResult, SimulationModel modelRoot, string fileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
|
---|
579 | {
|
---|
580 | #region Check Parameters
|
---|
581 | global::System.Diagnostics.Debug.Assert(serializationResult != null);
|
---|
582 | global::System.Diagnostics.Debug.Assert(modelRoot != null);
|
---|
583 | global::System.Diagnostics.Debug.Assert(!serializationResult.Failed);
|
---|
584 | #endregion
|
---|
585 |
|
---|
586 | serializationResult.Encoding = encoding;
|
---|
587 |
|
---|
588 | DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(modelRoot.Store);
|
---|
589 |
|
---|
590 |
|
---|
591 | global::System.IO.MemoryStream newFileContent = new global::System.IO.MemoryStream();
|
---|
592 |
|
---|
593 | DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(directory, fileName, serializationResult);
|
---|
594 | this.InitializeSerializationContext(modelRoot.Partition, serializationContext, false);
|
---|
595 | // MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here.
|
---|
596 | serializationContext.WriteOptionalPropertiesWithDefaultValue = writeOptionalPropertiesWithDefaultValue;
|
---|
597 | global::System.Xml.XmlWriterSettings settings = HLSimSerializationHelper.Instance.CreateXmlWriterSettings(serializationContext, false, encoding);
|
---|
598 | using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(newFileContent, settings))
|
---|
599 | {
|
---|
600 | this.WriteRootElement(serializationContext, modelRoot, writer);
|
---|
601 | }
|
---|
602 |
|
---|
603 | return newFileContent;
|
---|
604 | }
|
---|
605 | /// <summary>
|
---|
606 | /// Saves the given model root as a in-memory stream.
|
---|
607 | /// This is a helper used by SaveModel() and SaveModelAndDiagram(). When saving the model and the diagram together, we want to make sure that
|
---|
608 | /// both can be saved without error before writing the content to disk, so we serialize the model into a in-memory stream first.
|
---|
609 | /// </summary>
|
---|
610 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
611 | /// <param name="diagram">HLSimDiagram to be saved.</param>
|
---|
612 | /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
|
---|
613 | /// <param name="encoding">Encoding to use when saving the diagram.</param>
|
---|
614 | /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
|
---|
615 | /// <returns>In-memory stream containing the serialized HLSimDiagram instance.</returns>
|
---|
616 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
|
---|
617 | internal global::System.IO.MemoryStream InternalSaveDiagram(DslModeling::SerializationResult serializationResult, HLSimDiagram diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
|
---|
618 | {
|
---|
619 | #region Check Parameters
|
---|
620 | global::System.Diagnostics.Debug.Assert(serializationResult != null);
|
---|
621 | global::System.Diagnostics.Debug.Assert(diagram != null);
|
---|
622 | global::System.Diagnostics.Debug.Assert(!serializationResult.Failed);
|
---|
623 | #endregion
|
---|
624 |
|
---|
625 | DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(diagram.Store);
|
---|
626 |
|
---|
627 |
|
---|
628 | global::System.IO.MemoryStream newFileContent = new global::System.IO.MemoryStream();
|
---|
629 |
|
---|
630 | DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(directory, diagramFileName, serializationResult);
|
---|
631 | this.InitializeSerializationContext(diagram.Partition, serializationContext, false);
|
---|
632 | // MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here.
|
---|
633 | serializationContext.WriteOptionalPropertiesWithDefaultValue = writeOptionalPropertiesWithDefaultValue;
|
---|
634 | global::System.Xml.XmlWriterSettings settings = HLSimSerializationHelper.Instance.CreateXmlWriterSettings(serializationContext, true, encoding);
|
---|
635 | using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(newFileContent, settings))
|
---|
636 | {
|
---|
637 | this.WriteRootElement(serializationContext, diagram, writer);
|
---|
638 | }
|
---|
639 |
|
---|
640 | return newFileContent;
|
---|
641 | }
|
---|
642 |
|
---|
643 | /// <summary>
|
---|
644 | /// Helper method to create and initialize a new SimulationModel.
|
---|
645 | /// </summary>
|
---|
646 | internal protected virtual SimulationModel CreateModelHelper(DslModeling::Partition modelPartition)
|
---|
647 | {
|
---|
648 | SimulationModel model = new SimulationModel(modelPartition);
|
---|
649 | return model;
|
---|
650 | }
|
---|
651 |
|
---|
652 | /// <summary>
|
---|
653 | /// Loads a SimulationModel instance and its associated diagram file into the default partition of the given store, and ignore serialization result.
|
---|
654 | /// </summary>
|
---|
655 | /// <param name="store">The new SimulationModel instance will be created into the default partition of this store.</param>
|
---|
656 | /// <param name="modelFileName">Name of the file from which the SimulationModel instance will be deserialized.</param>
|
---|
657 | /// <param name="diagramFileName">Name of the file from which the HLSimDiagram instance will be deserialized.</param>
|
---|
658 | /// <param name="schemaResolver">
|
---|
659 | /// An ISchemaResolver that allows the serializer to do schema validation on the root element (and everything inside it).
|
---|
660 | /// If null is passed, schema validation will not be performed.
|
---|
661 | /// </param>
|
---|
662 | /// <param name="validationController">
|
---|
663 | /// A ValidationController that will be used to do load-time validation (validations with validation category "Load"). If null
|
---|
664 | /// is passed, load-time validation will not be performed.
|
---|
665 | /// </param>
|
---|
666 | /// <param name="serializerLocator">
|
---|
667 | /// An ISerializerLocator that will be used to locate any additional domain model types required to load the model. Can be null.
|
---|
668 | /// </param>
|
---|
669 | /// <returns>The loaded SimulationModel instance.</returns>
|
---|
670 | public virtual SimulationModel LoadModelAndDiagram(DslModeling::Store store, string modelFileName, string diagramFileName, DslModeling::ISchemaResolver schemaResolver, DslValidation::ValidationController validationController, DslModeling::ISerializerLocator serializerLocator)
|
---|
671 | {
|
---|
672 | return this.LoadModelAndDiagram(new DslModeling::SerializationResult(), store, modelFileName, diagramFileName, schemaResolver, validationController, serializerLocator);
|
---|
673 | }
|
---|
674 |
|
---|
675 | /// <summary>
|
---|
676 | /// Loads a SimulationModel instance and its associated diagram file into the default partition of the given store.
|
---|
677 | /// </summary>
|
---|
678 | /// <param name="serializationResult">Stores serialization result from the load operation.</param>
|
---|
679 | /// <param name="store">The new SimulationModel instance will be created into the default partition of this store.</param>
|
---|
680 | /// <param name="modelFileName">Name of the file from which the SimulationModel instance will be deserialized.</param>
|
---|
681 | /// <param name="diagramFileName">Name of the file from which the HLSimDiagram instance will be deserialized.</param>
|
---|
682 | /// <param name="schemaResolver">
|
---|
683 | /// An ISchemaResolver that allows the serializer to do schema validation on the root element (and everything inside it).
|
---|
684 | /// If null is passed, schema validation will not be performed.
|
---|
685 | /// </param>
|
---|
686 | /// <param name="validationController">
|
---|
687 | /// A ValidationController that will be used to do load-time validation (validations with validation category "Load"). If null
|
---|
688 | /// is passed, load-time validation will not be performed.
|
---|
689 | /// </param>
|
---|
690 | /// <param name="serializerLocator">
|
---|
691 | /// An ISerializerLocator that will be used to locate any additional domain model types required to load the model. Can be null.
|
---|
692 | /// </param>
|
---|
693 | /// <returns>The loaded SimulationModel instance.</returns>
|
---|
694 | public virtual SimulationModel LoadModelAndDiagram(DslModeling::SerializationResult serializationResult, DslModeling::Store store, string modelFileName, string diagramFileName, DslModeling::ISchemaResolver schemaResolver, DslValidation::ValidationController validationController, DslModeling::ISerializerLocator serializerLocator)
|
---|
695 | {
|
---|
696 | #region Check Parameters
|
---|
697 | if (store == null)
|
---|
698 | throw new global::System.ArgumentNullException("store");
|
---|
699 | #endregion
|
---|
700 |
|
---|
701 | DslModeling::Partition diagramPartition = new DslModeling::Partition(store);
|
---|
702 | return this.LoadModelAndDiagram(serializationResult, store.DefaultPartition, modelFileName, diagramPartition, diagramFileName, schemaResolver, validationController, serializerLocator);
|
---|
703 | }
|
---|
704 |
|
---|
705 | /// <summary>
|
---|
706 | /// Loads a SimulationModel instance and its associated diagram file.
|
---|
707 | /// </summary>
|
---|
708 | /// <param name="serializationResult">Stores serialization result from the load operation.</param>
|
---|
709 | /// <param name="modelPartition">Partition in which the new SimulationModel instance will be created.</param>
|
---|
710 | /// <param name="modelFileName">Name of the file from which the SimulationModel instance will be deserialized.</param>
|
---|
711 | /// <param name="diagramPartition">Partition in which the new HLSimDiagram instance will be created.</param>
|
---|
712 | /// <param name="diagramFileName">Name of the file from which the HLSimDiagram instance will be deserialized.</param>
|
---|
713 | /// <param name="schemaResolver">
|
---|
714 | /// An ISchemaResolver that allows the serializer to do schema validation on the root element (and everything inside it).
|
---|
715 | /// If null is passed, schema validation will not be performed.
|
---|
716 | /// </param>
|
---|
717 | /// <param name="validationController">
|
---|
718 | /// A ValidationController that will be used to do load-time validation (validations with validation category "Load"). If null
|
---|
719 | /// is passed, load-time validation will not be performed.
|
---|
720 | /// </param>
|
---|
721 | /// <param name="serializerLocator">
|
---|
722 | /// An ISerializerLocator that will be used to locate any additional domain model types required to load the model. Can be null.
|
---|
723 | /// </param>
|
---|
724 | /// <returns>The loaded SimulationModel instance.</returns>
|
---|
725 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Generated code.")]
|
---|
726 | public virtual SimulationModel LoadModelAndDiagram(DslModeling::SerializationResult serializationResult, DslModeling::Partition modelPartition, string modelFileName, DslModeling::Partition diagramPartition, string diagramFileName, DslModeling::ISchemaResolver schemaResolver, DslValidation::ValidationController validationController, DslModeling::ISerializerLocator serializerLocator)
|
---|
727 | {
|
---|
728 | #region Check Parameters
|
---|
729 | if (serializationResult == null)
|
---|
730 | throw new global::System.ArgumentNullException("serializationResult");
|
---|
731 | if (modelPartition == null)
|
---|
732 | throw new global::System.ArgumentNullException("modelPartition");
|
---|
733 | if (diagramPartition == null)
|
---|
734 | throw new global::System.ArgumentNullException("diagramPartition");
|
---|
735 | if (string.IsNullOrEmpty(diagramFileName))
|
---|
736 | throw new global::System.ArgumentNullException("diagramFileName");
|
---|
737 | #endregion
|
---|
738 |
|
---|
739 | SimulationModel modelRoot;
|
---|
740 |
|
---|
741 | // Ensure there is an outer transaction spanning both model and diagram load, so moniker resolution works properly.
|
---|
742 | if (!diagramPartition.Store.TransactionActive)
|
---|
743 | {
|
---|
744 | throw new global::System.InvalidOperationException(HLSimDomainModel.SingletonResourceManager.GetString("MissingTransaction"));
|
---|
745 | }
|
---|
746 |
|
---|
747 | modelRoot = this.LoadModel(serializationResult, modelPartition, modelFileName, schemaResolver, validationController, serializerLocator);
|
---|
748 |
|
---|
749 | if (serializationResult.Failed)
|
---|
750 | {
|
---|
751 | // don't try to deserialize diagram data if model load failed.
|
---|
752 | return modelRoot;
|
---|
753 | }
|
---|
754 |
|
---|
755 | HLSimDiagram diagram = null;
|
---|
756 | DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(diagramPartition.Store);
|
---|
757 | DslModeling::DomainClassXmlSerializer diagramSerializer = directory.GetSerializer(HLSimDiagram.DomainClassId);
|
---|
758 | global::System.Diagnostics.Debug.Assert(diagramSerializer != null, "Cannot find serializer for HLSimDiagram");
|
---|
759 | if (diagramSerializer != null)
|
---|
760 | {
|
---|
761 | if(!global::System.IO.File.Exists(diagramFileName))
|
---|
762 | {
|
---|
763 | // missing diagram file indicates we should create a new diagram.
|
---|
764 | diagram = this.CreateDiagramHelper(diagramPartition, modelRoot);
|
---|
765 | }
|
---|
766 | else
|
---|
767 | {
|
---|
768 | using (global::System.IO.FileStream fileStream = global::System.IO.File.OpenRead(diagramFileName))
|
---|
769 | {
|
---|
770 | DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(directory, fileStream.Name, serializationResult);
|
---|
771 | this.InitializeSerializationContext(diagramPartition, serializationContext, true);
|
---|
772 | DslModeling::TransactionContext transactionContext = new DslModeling::TransactionContext();
|
---|
773 | transactionContext.Add(DslModeling::SerializationContext.TransactionContextKey, serializationContext);
|
---|
774 |
|
---|
775 | using (DslModeling::Transaction t = diagramPartition.Store.TransactionManager.BeginTransaction("LoadDiagram", true, transactionContext))
|
---|
776 | {
|
---|
777 | // Ensure there is some content in the file. Blank (or almost blank, to account for encoding header bytes, etc.)
|
---|
778 | // files will cause a new diagram to be created and returned
|
---|
779 | if (fileStream.Length > 5)
|
---|
780 | {
|
---|
781 | global::System.Xml.XmlReaderSettings settings = HLSimSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false);
|
---|
782 | try
|
---|
783 | {
|
---|
784 | using (global::System.Xml.XmlReader reader = global::System.Xml.XmlReader.Create(fileStream, settings))
|
---|
785 | {
|
---|
786 | reader.MoveToContent();
|
---|
787 | diagram = diagramSerializer.TryCreateInstance(serializationContext, reader, diagramPartition) as HLSimDiagram;
|
---|
788 | if (diagram != null)
|
---|
789 | {
|
---|
790 | this.ReadRootElement(serializationContext, diagram, reader, schemaResolver);
|
---|
791 | }
|
---|
792 | }
|
---|
793 | }
|
---|
794 | catch (global::System.Xml.XmlException xEx)
|
---|
795 | {
|
---|
796 | DslModeling::SerializationUtilities.AddMessage(
|
---|
797 | serializationContext,
|
---|
798 | DslModeling::SerializationMessageKind.Error,
|
---|
799 | xEx
|
---|
800 | );
|
---|
801 | }
|
---|
802 | if (serializationResult.Failed)
|
---|
803 | {
|
---|
804 | // Serialization error encountered, rollback the transaction.
|
---|
805 | diagram = null;
|
---|
806 | t.Rollback();
|
---|
807 | }
|
---|
808 | }
|
---|
809 |
|
---|
810 | if(diagram == null && !serializationResult.Failed)
|
---|
811 | {
|
---|
812 | // Create diagram if it doesn't exist
|
---|
813 | diagram = this.CreateDiagramHelper(diagramPartition, modelRoot);
|
---|
814 | }
|
---|
815 |
|
---|
816 | if (t.IsActive)
|
---|
817 | t.Commit();
|
---|
818 | } // End inner Tx
|
---|
819 |
|
---|
820 | // Do load-time validation if a ValidationController is provided.
|
---|
821 | if (!serializationResult.Failed && validationController != null)
|
---|
822 | {
|
---|
823 | using (new SerializationValidationObserver(serializationResult, validationController))
|
---|
824 | {
|
---|
825 | validationController.Validate(diagramPartition, DslValidation::ValidationCategories.Load);
|
---|
826 | }
|
---|
827 | }
|
---|
828 | }
|
---|
829 | }
|
---|
830 |
|
---|
831 | if (diagram != null)
|
---|
832 | {
|
---|
833 | if (!serializationResult.Failed)
|
---|
834 | { // Succeeded.
|
---|
835 | diagram.ModelElement = modelRoot;
|
---|
836 | diagram.PostDeserialization(true);
|
---|
837 | this.CheckForOrphanedShapes(diagram, serializationResult);
|
---|
838 | }
|
---|
839 | else
|
---|
840 | { // Failed.
|
---|
841 | diagram.PostDeserialization(false);
|
---|
842 | }
|
---|
843 | }
|
---|
844 | }
|
---|
845 | return modelRoot;
|
---|
846 | }
|
---|
847 |
|
---|
848 | /// <summary>
|
---|
849 | /// Helper method to create and initialize a new HLSimDiagram.
|
---|
850 | /// </summary>
|
---|
851 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId="modelRoot", Justification = "Signature enforced by caller.")]
|
---|
852 | internal protected virtual HLSimDiagram CreateDiagramHelper(DslModeling::Partition diagramPartition, DslModeling::ModelElement modelRoot)
|
---|
853 | {
|
---|
854 | HLSimDiagram diagram = new HLSimDiagram(diagramPartition);
|
---|
855 | return diagram;
|
---|
856 | }
|
---|
857 |
|
---|
858 |
|
---|
859 | /// <summary>
|
---|
860 | /// Saves the given diagram to the given file, with default encoding (UTF-8), and optional properties with default value will not
|
---|
861 | /// be written out.
|
---|
862 | /// </summary>
|
---|
863 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
864 | /// <param name="modelRoot">SimulationModel instance to be saved.</param>
|
---|
865 | /// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param>
|
---|
866 | /// <param name="diagram">HLSimDiagram to be saved.</param>
|
---|
867 | /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
|
---|
868 | public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, SimulationModel modelRoot, string modelFileName, HLSimDiagram diagram, string diagramFileName)
|
---|
869 | {
|
---|
870 | this.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, global::System.Text.Encoding.UTF8, false);
|
---|
871 | }
|
---|
872 |
|
---|
873 | /// <summary>
|
---|
874 | /// Saves the given diagram to the given file, with default encoding (UTF-8).
|
---|
875 | /// </summary>
|
---|
876 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
877 | /// <param name="modelRoot">SimulationModel instance to be saved.</param>
|
---|
878 | /// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param>
|
---|
879 | /// <param name="diagram">HLSimDiagram to be saved.</param>
|
---|
880 | /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
|
---|
881 | /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
|
---|
882 | public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, SimulationModel modelRoot, string modelFileName, HLSimDiagram diagram, string diagramFileName, bool writeOptionalPropertiesWithDefaultValue)
|
---|
883 | {
|
---|
884 | this.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, global::System.Text.Encoding.UTF8, writeOptionalPropertiesWithDefaultValue);
|
---|
885 | }
|
---|
886 |
|
---|
887 | /// <summary>
|
---|
888 | /// Saves the given SimulationModel and HLSimDiagram to the given files, with specified encoding.
|
---|
889 | /// </summary>
|
---|
890 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
891 | /// <param name="modelRoot">SimulationModel instance to be saved.</param>
|
---|
892 | /// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param>
|
---|
893 | /// <param name="diagram">HLSimDiagram to be saved.</param>
|
---|
894 | /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
|
---|
895 | /// <param name="encoding">Encoding to use when saving the diagram.</param>
|
---|
896 | /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
|
---|
897 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
|
---|
898 | public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, SimulationModel modelRoot, string modelFileName, HLSimDiagram diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
|
---|
899 | {
|
---|
900 | #region Check Parameters
|
---|
901 | if (serializationResult == null)
|
---|
902 | throw new global::System.ArgumentNullException("serializationResult");
|
---|
903 | if (string.IsNullOrEmpty(modelFileName))
|
---|
904 | throw new global::System.ArgumentNullException("modelFileName");
|
---|
905 | if (diagram == null)
|
---|
906 | throw new global::System.ArgumentNullException("diagram");
|
---|
907 | if (string.IsNullOrEmpty(diagramFileName))
|
---|
908 | throw new global::System.ArgumentNullException("diagramFileName");
|
---|
909 | #endregion
|
---|
910 |
|
---|
911 | if (serializationResult.Failed)
|
---|
912 | return;
|
---|
913 |
|
---|
914 | // Save the model file first
|
---|
915 | using (global::System.IO.MemoryStream modelFileContent = this.InternalSaveModel(serializationResult, modelRoot, modelFileName, encoding, writeOptionalPropertiesWithDefaultValue))
|
---|
916 | {
|
---|
917 | if (serializationResult.Failed)
|
---|
918 | return;
|
---|
919 |
|
---|
920 | using (global::System.IO.MemoryStream diagramFileContent = this.InternalSaveDiagram(serializationResult, diagram, diagramFileName, encoding, writeOptionalPropertiesWithDefaultValue))
|
---|
921 | {
|
---|
922 | if (!serializationResult.Failed)
|
---|
923 | {
|
---|
924 | // Only write the contents if there's no error encountered during serialization.
|
---|
925 | if (modelFileContent != null)
|
---|
926 | {
|
---|
927 | using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(modelFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None))
|
---|
928 | {
|
---|
929 | using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding))
|
---|
930 | {
|
---|
931 | writer.Write(modelFileContent.ToArray());
|
---|
932 | }
|
---|
933 | }
|
---|
934 | }
|
---|
935 | if (diagramFileContent != null)
|
---|
936 | {
|
---|
937 | using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(diagramFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None))
|
---|
938 | {
|
---|
939 | using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding))
|
---|
940 | {
|
---|
941 | writer.Write(diagramFileContent.ToArray());
|
---|
942 | }
|
---|
943 | }
|
---|
944 | }
|
---|
945 | }
|
---|
946 | }
|
---|
947 | }
|
---|
948 | }
|
---|
949 |
|
---|
950 | /// <summary>
|
---|
951 | /// Saves the given diagram to the given file, with default encoding (UTF-8), and optional properties with default value will not
|
---|
952 | /// be written out.
|
---|
953 | /// </summary>
|
---|
954 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
955 | /// <param name="diagram">HLSimDiagram to be saved.</param>
|
---|
956 | /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
|
---|
957 | public virtual void SaveDiagram(DslModeling::SerializationResult serializationResult, HLSimDiagram diagram, string diagramFileName)
|
---|
958 | {
|
---|
959 | this.SaveDiagram(serializationResult, diagram, diagramFileName, global::System.Text.Encoding.UTF8, false);
|
---|
960 | }
|
---|
961 |
|
---|
962 | /// <summary>
|
---|
963 | /// Saves the given diagram to the given file, with default encoding (UTF-8).
|
---|
964 | /// </summary>
|
---|
965 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
966 | /// <param name="diagram">HLSimDiagram to be saved.</param>
|
---|
967 | /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
|
---|
968 | /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
|
---|
969 | public virtual void SaveDiagram(DslModeling::SerializationResult serializationResult, HLSimDiagram diagram, string diagramFileName, bool writeOptionalPropertiesWithDefaultValue)
|
---|
970 | {
|
---|
971 | this.SaveDiagram(serializationResult, diagram, diagramFileName, global::System.Text.Encoding.UTF8, writeOptionalPropertiesWithDefaultValue);
|
---|
972 | }
|
---|
973 |
|
---|
974 | /// <summary>
|
---|
975 | /// Saves the given HLSimDiagram to the given file, with specified encoding.
|
---|
976 | /// </summary>
|
---|
977 | /// <param name="serializationResult">Stores serialization result from the save operation.</param>
|
---|
978 | /// <param name="diagram">HLSimDiagram to be saved.</param>
|
---|
979 | /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param>
|
---|
980 | /// <param name="encoding">Encoding to use when saving the diagram.</param>
|
---|
981 | /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param>
|
---|
982 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
|
---|
983 | public virtual void SaveDiagram(DslModeling::SerializationResult serializationResult, HLSimDiagram diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
|
---|
984 | {
|
---|
985 | #region Check Parameters
|
---|
986 | if (serializationResult == null)
|
---|
987 | throw new global::System.ArgumentNullException("serializationResult");
|
---|
988 | if (diagram == null)
|
---|
989 | throw new global::System.ArgumentNullException("diagram");
|
---|
990 | if (string.IsNullOrEmpty(diagramFileName))
|
---|
991 | throw new global::System.ArgumentNullException("diagramFileName");
|
---|
992 | #endregion
|
---|
993 |
|
---|
994 | if (serializationResult.Failed)
|
---|
995 | return;
|
---|
996 |
|
---|
997 | using (global::System.IO.MemoryStream diagramFileContent = this.InternalSaveDiagram(serializationResult, diagram, diagramFileName, encoding, writeOptionalPropertiesWithDefaultValue))
|
---|
998 | {
|
---|
999 | if (!serializationResult.Failed)
|
---|
1000 | {
|
---|
1001 | // Only write the contents if there's no error encountered during serialization.
|
---|
1002 | if (diagramFileContent != null)
|
---|
1003 | {
|
---|
1004 | using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(diagramFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None))
|
---|
1005 | {
|
---|
1006 | using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding))
|
---|
1007 | {
|
---|
1008 | writer.Write(diagramFileContent.ToArray());
|
---|
1009 | }
|
---|
1010 | }
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | /// <summary>
|
---|
1017 | /// Read an element from the root of XML.
|
---|
1018 | /// </summary>
|
---|
1019 | /// <param name="serializationContext">Serialization context.</param>
|
---|
1020 | /// <param name="rootElement">In-memory element instance that will get the deserialized data.</param>
|
---|
1021 | /// <param name="reader">XmlReader to read serialized data from.</param>
|
---|
1022 | /// <param name="schemaResolver">An ISchemaResolver that allows the serializer to do schema validation on the root element (and everything inside it).</param>
|
---|
1023 | protected virtual void ReadRootElement(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement rootElement, global::System.Xml.XmlReader reader, DslModeling::ISchemaResolver schemaResolver)
|
---|
1024 | {
|
---|
1025 | #region Check Parameters
|
---|
1026 | global::System.Diagnostics.Debug.Assert(serializationContext != null);
|
---|
1027 | if (serializationContext == null)
|
---|
1028 | throw new global::System.ArgumentNullException("serializationContext");
|
---|
1029 | global::System.Diagnostics.Debug.Assert(rootElement != null);
|
---|
1030 | if (rootElement == null)
|
---|
1031 | throw new global::System.ArgumentNullException("rootElement");
|
---|
1032 | global::System.Diagnostics.Debug.Assert(reader != null);
|
---|
1033 | if (reader == null)
|
---|
1034 | throw new global::System.ArgumentNullException("reader");
|
---|
1035 | #endregion
|
---|
1036 |
|
---|
1037 | DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(rootElement.Store);
|
---|
1038 |
|
---|
1039 | DslModeling::DomainClassXmlSerializer rootSerializer = directory.GetSerializer(rootElement.GetDomainClass().Id);
|
---|
1040 | global::System.Diagnostics.Debug.Assert(rootSerializer != null, "Cannot find serializer for " + rootElement.GetDomainClass().Name + "!");
|
---|
1041 |
|
---|
1042 | // Version check.
|
---|
1043 | this.CheckVersion(serializationContext, reader);
|
---|
1044 |
|
---|
1045 | if (!serializationContext.Result.Failed)
|
---|
1046 | {
|
---|
1047 | // Use a validating reader if possible
|
---|
1048 | using (reader = TryCreateValidatingReader(schemaResolver, reader, serializationContext))
|
---|
1049 | {
|
---|
1050 | rootSerializer.Read(serializationContext, rootElement, reader);
|
---|
1051 | }
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | /// <summary>
|
---|
1057 | /// Write an element as the root of XML.
|
---|
1058 | /// </summary>
|
---|
1059 | /// <param name="serializationContext">Serialization context.</param>
|
---|
1060 | /// <param name="rootElement">Root element instance that will be serialized.</param>
|
---|
1061 | /// <param name="writer">XmlWriter to write serialized data to.</param>
|
---|
1062 | public virtual void WriteRootElement(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement rootElement, global::System.Xml.XmlWriter writer)
|
---|
1063 | {
|
---|
1064 | #region Check Parameters
|
---|
1065 | global::System.Diagnostics.Debug.Assert(serializationContext != null);
|
---|
1066 | if (serializationContext == null)
|
---|
1067 | throw new global::System.ArgumentNullException("serializationContext");
|
---|
1068 | global::System.Diagnostics.Debug.Assert(rootElement != null);
|
---|
1069 | if (rootElement == null)
|
---|
1070 | throw new global::System.ArgumentNullException("rootElement");
|
---|
1071 | global::System.Diagnostics.Debug.Assert(writer != null);
|
---|
1072 | if (writer == null)
|
---|
1073 | throw new global::System.ArgumentNullException("writer");
|
---|
1074 | #endregion
|
---|
1075 |
|
---|
1076 | DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(rootElement.Store);
|
---|
1077 |
|
---|
1078 | DslModeling::DomainClassXmlSerializer rootSerializer = directory.GetSerializer(rootElement.GetDomainClass().Id);
|
---|
1079 | global::System.Diagnostics.Debug.Assert(rootSerializer != null, "Cannot find serializer for " + rootElement.GetDomainClass().Name + "!");
|
---|
1080 |
|
---|
1081 | // Set up root element settings
|
---|
1082 | DslModeling::RootElementSettings rootElementSettings = new DslModeling::RootElementSettings();
|
---|
1083 | if (!(rootElement is DslDiagrams::Diagram))
|
---|
1084 | {
|
---|
1085 | // Only model has schema, diagram has no schema.
|
---|
1086 | rootElementSettings.SchemaTargetNamespace = "http://schemas.microsoft.com/dsltools/HLSim";
|
---|
1087 | }
|
---|
1088 | rootElementSettings.Version = new global::System.Version("1.0.0.0");
|
---|
1089 |
|
---|
1090 | // Carry out the normal serialization.
|
---|
1091 | rootSerializer.Write(serializationContext, rootElement, writer, rootElementSettings);
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | /// <summary>
|
---|
1095 | /// Checks the version of the file being read.
|
---|
1096 | /// </summary>
|
---|
1097 | /// <param name="serializationContext">Serialization context.</param>
|
---|
1098 | /// <param name="reader">Reader for the file being read. The reader is positioned at the open tag of the root element being read.</param>
|
---|
1099 | protected virtual void CheckVersion(DslModeling::SerializationContext serializationContext, global::System.Xml.XmlReader reader)
|
---|
1100 | {
|
---|
1101 | #region Check Parameters
|
---|
1102 | global::System.Diagnostics.Debug.Assert(serializationContext != null);
|
---|
1103 | if (serializationContext == null)
|
---|
1104 | throw new global::System.ArgumentNullException("serializationContext");
|
---|
1105 | global::System.Diagnostics.Debug.Assert(reader != null);
|
---|
1106 | if (reader == null)
|
---|
1107 | throw new global::System.ArgumentNullException("reader");
|
---|
1108 | #endregion
|
---|
1109 |
|
---|
1110 | global::System.Version expectedVersion = new global::System.Version("1.0.0.0");
|
---|
1111 | string dslVersionStr = reader.GetAttribute("dslVersion");
|
---|
1112 | if (dslVersionStr != null)
|
---|
1113 | {
|
---|
1114 | try
|
---|
1115 | {
|
---|
1116 | global::System.Version actualVersion = new global::System.Version(dslVersionStr);
|
---|
1117 | if (actualVersion != expectedVersion)
|
---|
1118 | {
|
---|
1119 | HLSimSerializationBehaviorSerializationMessages.VersionMismatch(serializationContext, reader, expectedVersion, actualVersion);
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | catch (global::System.ArgumentException)
|
---|
1123 | {
|
---|
1124 | HLSimSerializationBehaviorSerializationMessages.InvalidPropertyValue(serializationContext, reader, "dslVersion", typeof(global::System.Version), dslVersionStr);
|
---|
1125 | }
|
---|
1126 | catch (global::System.FormatException)
|
---|
1127 | {
|
---|
1128 | HLSimSerializationBehaviorSerializationMessages.InvalidPropertyValue(serializationContext, reader, "dslVersion", typeof(global::System.Version), dslVersionStr);
|
---|
1129 | }
|
---|
1130 | catch (global::System.OverflowException)
|
---|
1131 | {
|
---|
1132 | HLSimSerializationBehaviorSerializationMessages.InvalidPropertyValue(serializationContext, reader, "dslVersion", typeof(global::System.Version), dslVersionStr);
|
---|
1133 | }
|
---|
1134 | }
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 |
|
---|
1138 | /// <summary>
|
---|
1139 | /// Attempts to return a validating XML reader
|
---|
1140 | /// </summary>
|
---|
1141 | /// <returns>If all of the schema for registered serializers and all of the schema referenced in the file can be resolved,
|
---|
1142 | /// then a validating reader is returned. Otherwise, the supplied reader is returned.
|
---|
1143 | /// The serialization context will contain warning messages for all schema that could not be resolved.</returns>
|
---|
1144 | protected virtual global::System.Xml.XmlReader TryCreateValidatingReader(DslModeling::ISchemaResolver schemaResolver, global::System.Xml.XmlReader reader, DslModeling::SerializationContext serializationContext)
|
---|
1145 | {
|
---|
1146 | System.Diagnostics.Debug.Assert(serializationContext != null, "Serialization context should not be null");
|
---|
1147 |
|
---|
1148 | // Can't do anything without a schema resolver
|
---|
1149 | if (schemaResolver == null)
|
---|
1150 | {
|
---|
1151 | return reader;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | global::System.Xml.Schema.XmlSchemaSet schemaSet = new global::System.Xml.Schema.XmlSchemaSet(reader.NameTable);
|
---|
1155 |
|
---|
1156 | // Combine the list of namespaces for models in the store with those referred to in the current node
|
---|
1157 | System.Collections.Generic.IEnumerable<string> namespaces = serializationContext.Directory.Namespaces.Select(n => n.TargetNamespace);
|
---|
1158 | namespaces = namespaces.Concat(DslModeling::SerializationUtilities.GetNamespacesFromCurrentNode(reader));
|
---|
1159 |
|
---|
1160 | bool success = true;
|
---|
1161 | foreach (string ns in namespaces.Distinct())
|
---|
1162 | {
|
---|
1163 | // Try to resolve all namespaces so warnings are shown for all missing namespaces
|
---|
1164 | success = ResolveSchema(ns, schemaSet, schemaResolver, reader, serializationContext) & success;
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | if (success && schemaSet.Count > 0)
|
---|
1168 | {
|
---|
1169 | global::System.Xml.XmlReaderSettings readerSettings = reader.Settings.Clone();
|
---|
1170 | readerSettings.ConformanceLevel = global::System.Xml.ConformanceLevel.Auto;
|
---|
1171 | readerSettings.ValidationType = global::System.Xml.ValidationType.Schema;
|
---|
1172 | readerSettings.Schemas = schemaSet;
|
---|
1173 | HLSimSerializationBehaviorSchemaValidationCallback validationCallback = new HLSimSerializationBehaviorSchemaValidationCallback(serializationContext);
|
---|
1174 | readerSettings.ValidationEventHandler += new global::System.Xml.Schema.ValidationEventHandler(validationCallback.Handler);
|
---|
1175 |
|
---|
1176 |
|
---|
1177 | // Wrap the given reader as a validating reader and carry out the normal deserialization.
|
---|
1178 | global::System.Xml.XmlReader validatingReader = global::System.Xml.XmlReader.Create(reader, readerSettings);
|
---|
1179 |
|
---|
1180 | validationCallback.Reader = validatingReader;
|
---|
1181 | return validatingReader;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | return reader;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | /// <summary>
|
---|
1188 | /// Attempts to resolve the supplied schema namespace
|
---|
1189 | /// </summary>
|
---|
1190 | /// <remarks>If the schema can be resolved it is added to the supplied schema set. Otherwise, a
|
---|
1191 | /// warning will be written to serializationContext.
|
---|
1192 | /// </remarks>
|
---|
1193 | /// <returns>A flag indicating whether the schema was resolved or not</returns>
|
---|
1194 | protected static bool ResolveSchema(string targetNamespace, global::System.Xml.Schema.XmlSchemaSet schemaSet, DslModeling::ISchemaResolver schemaResolver, global::System.Xml.XmlReader reader, DslModeling::SerializationContext serializationContext)
|
---|
1195 | {
|
---|
1196 | global::System.Collections.Generic.IList<string> schemas = schemaResolver.ResolveSchema(targetNamespace);
|
---|
1197 | if (schemas != null && schemas.Count > 0)
|
---|
1198 | {
|
---|
1199 | if (schemas.Count > 1)
|
---|
1200 | {
|
---|
1201 | HLSimSerializationBehaviorSerializationMessages.AmbiguousSchema(serializationContext, reader, targetNamespace, schemas[0]);
|
---|
1202 | }
|
---|
1203 | schemaSet.Add(targetNamespace, schemas[0]);
|
---|
1204 | return true;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | HLSimSerializationBehaviorSerializationMessages.NoSchema(serializationContext, reader, targetNamespace);
|
---|
1208 | return false;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 |
|
---|
1212 | /// <summary>
|
---|
1213 | /// A utility class to handle schema validation warning/error
|
---|
1214 | /// </summary>
|
---|
1215 | private sealed class HLSimSerializationBehaviorSchemaValidationCallback
|
---|
1216 | {
|
---|
1217 | #region Member Variables
|
---|
1218 | /// <summary>
|
---|
1219 | /// SerializationContext to store schema validation warning/error.
|
---|
1220 | /// </summary>
|
---|
1221 | DslModeling::SerializationContext serializationContext;
|
---|
1222 | /// <summary>
|
---|
1223 | /// Reader that generates the schema warning/error.
|
---|
1224 | /// </summary>
|
---|
1225 | global::System.Xml.XmlReader reader;
|
---|
1226 | #endregion
|
---|
1227 |
|
---|
1228 | #region Constructor
|
---|
1229 | /// <summary>
|
---|
1230 | /// Constructor
|
---|
1231 | /// </summary>
|
---|
1232 | /// <param name="serializationContext">SerializationContext to be used to store schema validation warning/error.</param>
|
---|
1233 | internal HLSimSerializationBehaviorSchemaValidationCallback(DslModeling::SerializationContext serializationContext)
|
---|
1234 | {
|
---|
1235 | global::System.Diagnostics.Debug.Assert(serializationContext != null);
|
---|
1236 | this.serializationContext = serializationContext;
|
---|
1237 | }
|
---|
1238 | #endregion
|
---|
1239 |
|
---|
1240 | #region Accessor
|
---|
1241 | /// <summary>
|
---|
1242 | /// Sets the reader that generates the schema validation warning/error.
|
---|
1243 | /// </summary>
|
---|
1244 | internal global::System.Xml.XmlReader Reader
|
---|
1245 | {
|
---|
1246 | [global::System.Diagnostics.DebuggerStepThrough]
|
---|
1247 | set { this.reader = value; }
|
---|
1248 | }
|
---|
1249 | #endregion
|
---|
1250 |
|
---|
1251 | #region Callback method
|
---|
1252 | /// <summary>
|
---|
1253 | /// Callback to handler schema validation warning/error.
|
---|
1254 | /// </summary>
|
---|
1255 | internal void Handler(object sender, global::System.Xml.Schema.ValidationEventArgs e)
|
---|
1256 | {
|
---|
1257 | global::System.Diagnostics.Debug.Assert(this.serializationContext != null);
|
---|
1258 | if (this.serializationContext != null)
|
---|
1259 | HLSimSerializationBehaviorSerializationMessages.SchemaValidationError(this.serializationContext, this.reader, e.Message);
|
---|
1260 | }
|
---|
1261 | #endregion
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | /// <summary>
|
---|
1265 | /// Return the model in XML format
|
---|
1266 | /// </summary>
|
---|
1267 | /// <param name="modelRoot">Root instance to be saved.</param>
|
---|
1268 | /// <param name="encoding">Encoding to use when saving the root instance.</param>
|
---|
1269 | /// <returns>Model in XML form</returns>
|
---|
1270 | public virtual string GetSerializedModelString(global::HeuristicLab.SimulationCore.SimulationModel modelRoot, global::System.Text.Encoding encoding)
|
---|
1271 | {
|
---|
1272 | string result = string.Empty;
|
---|
1273 | if (modelRoot == null)
|
---|
1274 | {
|
---|
1275 | return result;
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | DslModeling::SerializationResult serializationResult = new DslModeling::SerializationResult();
|
---|
1279 | using (global::System.IO.MemoryStream modelFileContent = this.InternalSaveModel(serializationResult, modelRoot, string.Empty, encoding, false))
|
---|
1280 | {
|
---|
1281 | if (!serializationResult.Failed && modelFileContent != null)
|
---|
1282 | {
|
---|
1283 | char[] chars = encoding.GetChars(modelFileContent.GetBuffer());
|
---|
1284 |
|
---|
1285 | // search the open angle bracket and trim off the Byte Of Mark.
|
---|
1286 | result = new string( chars);
|
---|
1287 | int indexPos = result.IndexOf('<');
|
---|
1288 | if (indexPos > 0)
|
---|
1289 | {
|
---|
1290 | // strip off the leading Byte Of Mark.
|
---|
1291 | result = result.Substring(indexPos);
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | // trim off trailing 0s.
|
---|
1295 | result = result.TrimEnd( '\0');
|
---|
1296 | }
|
---|
1297 | }
|
---|
1298 | return result;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 |
|
---|
1302 | #region Private/Helper Methods/Properties
|
---|
1303 |
|
---|
1304 | #region Class SerializationValidationObserver
|
---|
1305 | /// <summary>
|
---|
1306 | /// An utility class to collect validation messages during serialization, and store them in serialization result.
|
---|
1307 | /// </summary>
|
---|
1308 | protected sealed class SerializationValidationObserver : DslValidation::ValidationMessageObserver, global::System.IDisposable
|
---|
1309 | {
|
---|
1310 | #region Member Variables
|
---|
1311 | /// <summary>
|
---|
1312 | /// SerializationResult to store the messages.
|
---|
1313 | /// </summary>
|
---|
1314 | private DslModeling::SerializationResult serializationResult;
|
---|
1315 | /// <summary>
|
---|
1316 | /// ValidationController to get messages from.
|
---|
1317 | /// </summary>
|
---|
1318 | private DslValidation::ValidationController validationController;
|
---|
1319 | #endregion
|
---|
1320 |
|
---|
1321 | #region Constructor
|
---|
1322 | /// <summary>
|
---|
1323 | /// Constructor
|
---|
1324 | /// </summary>
|
---|
1325 | internal SerializationValidationObserver(DslModeling::SerializationResult serializationResult, DslValidation::ValidationController validationController)
|
---|
1326 | {
|
---|
1327 | #region Check Parameters
|
---|
1328 | global::System.Diagnostics.Debug.Assert(serializationResult != null);
|
---|
1329 | global::System.Diagnostics.Debug.Assert(validationController != null);
|
---|
1330 | #endregion
|
---|
1331 |
|
---|
1332 | this.serializationResult = serializationResult;
|
---|
1333 | this.validationController = validationController;
|
---|
1334 |
|
---|
1335 | // Subscribe to validation messages.
|
---|
1336 | this.validationController.AddObserver(this);
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | /// <summary>
|
---|
1340 | /// Destructor
|
---|
1341 | /// </summary>
|
---|
1342 | ~SerializationValidationObserver()
|
---|
1343 | {
|
---|
1344 | this.Dispose(false);
|
---|
1345 | }
|
---|
1346 | #endregion
|
---|
1347 |
|
---|
1348 | #region Base Overrides
|
---|
1349 | /// <summary>
|
---|
1350 | /// Called with validation messages are added.
|
---|
1351 | /// </summary>
|
---|
1352 | protected override void OnValidationMessageAdded(DslValidation::ValidationMessage addedMessage)
|
---|
1353 | {
|
---|
1354 | #region Check Parameters
|
---|
1355 | global::System.Diagnostics.Debug.Assert(addedMessage != null);
|
---|
1356 | #endregion
|
---|
1357 |
|
---|
1358 | if (addedMessage != null && this.serializationResult != null)
|
---|
1359 | { // Record the validation message as a serialization message.
|
---|
1360 | DslModeling::SerializationUtilities.AddValidationMessage(this.serializationResult, addedMessage);
|
---|
1361 | }
|
---|
1362 | base.OnValidationMessageAdded(addedMessage);
|
---|
1363 | }
|
---|
1364 | #endregion
|
---|
1365 |
|
---|
1366 | #region IDisposable Members
|
---|
1367 | /// <summary>
|
---|
1368 | /// IDisposable.Dispose().
|
---|
1369 | /// </summary>
|
---|
1370 | public void Dispose()
|
---|
1371 | {
|
---|
1372 | this.Dispose(true);
|
---|
1373 | global::System.GC.SuppressFinalize(this);
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | /// <summary>
|
---|
1377 | /// Unregister the observer on dispose.
|
---|
1378 | /// </summary>
|
---|
1379 | private void Dispose(bool disposing)
|
---|
1380 | {
|
---|
1381 | global::System.Diagnostics.Debug.Assert(disposing, "SerializationValidationObserver finalized without being disposed!");
|
---|
1382 | if (disposing && this.validationController != null)
|
---|
1383 | {
|
---|
1384 | this.validationController.RemoveObserver(this);
|
---|
1385 | this.validationController = null;
|
---|
1386 | }
|
---|
1387 | this.serializationResult = null;
|
---|
1388 | }
|
---|
1389 | #endregion
|
---|
1390 | }
|
---|
1391 | #endregion
|
---|
1392 |
|
---|
1393 | /// <summary>
|
---|
1394 | /// Go through the diagram to find all shapes that are not connected to a model element. Some of them may be by design, the rest are out-of-sync shapes.
|
---|
1395 | /// We want to make sure that all out-of-sync shapes are given a chance to fix themselves up, or post proper warning/error messages.
|
---|
1396 | /// </summary>
|
---|
1397 | /// <param name="diagram">The diagram of which the shapes are being checked.</param>
|
---|
1398 | /// <param name="serializationResult">SerializationResult to store warning/error in case an orphaned shape choose to do so.</param>
|
---|
1399 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
|
---|
1400 | protected virtual void CheckForOrphanedShapes(DslDiagrams::Diagram diagram, DslModeling::SerializationResult serializationResult)
|
---|
1401 | {
|
---|
1402 | global::System.Collections.Generic.List<DslDiagrams::ShapeElement> orphanedShapes = new global::System.Collections.Generic.List<DslDiagrams::ShapeElement>();
|
---|
1403 | diagram.IterateShapes(new OrphanedShapeIterator(orphanedShapes, diagram));
|
---|
1404 | foreach (DslDiagrams::ShapeElement orphanedShape in orphanedShapes)
|
---|
1405 | {
|
---|
1406 | if (serializationResult.Failed)
|
---|
1407 | break;
|
---|
1408 |
|
---|
1409 | orphanedShape.OnOrphaned(serializationResult);
|
---|
1410 | }
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | #region Class OrphanedShapeIterator
|
---|
1414 | /// <summary>
|
---|
1415 | /// An iterator to collect all the orphaned shapes.
|
---|
1416 | /// </summary>
|
---|
1417 | private class OrphanedShapeIterator : DslDiagrams::IShapeIterator
|
---|
1418 | {
|
---|
1419 | #region Member Variables
|
---|
1420 | /// <summary>
|
---|
1421 | /// Stores all iterated orphaned shapes.
|
---|
1422 | /// </summary>
|
---|
1423 | private global::System.Collections.Generic.List<DslDiagrams::ShapeElement> orphanedShapes;
|
---|
1424 |
|
---|
1425 | /// <summary>
|
---|
1426 | /// Diagram that contains the shapes to be iterated.
|
---|
1427 | /// </summary>
|
---|
1428 | private DslDiagrams::Diagram diagram;
|
---|
1429 | #endregion
|
---|
1430 |
|
---|
1431 | #region Constructor
|
---|
1432 | /// <summary>
|
---|
1433 | /// Constructor.
|
---|
1434 | /// </summary>
|
---|
1435 | /// <param name="orphanedShapes">Storage for all iterated orphaned shapes.</param>
|
---|
1436 | /// <param name="diagram">Diagram that contains the shapes to be iterated.</param>
|
---|
1437 | internal OrphanedShapeIterator(global::System.Collections.Generic.List<DslDiagrams::ShapeElement> orphanedShapes, DslDiagrams::Diagram diagram)
|
---|
1438 | {
|
---|
1439 | #region Check Parameters
|
---|
1440 | global::System.Diagnostics.Debug.Assert(orphanedShapes != null);
|
---|
1441 | global::System.Diagnostics.Debug.Assert(diagram != null);
|
---|
1442 | #endregion
|
---|
1443 |
|
---|
1444 | this.orphanedShapes = orphanedShapes;
|
---|
1445 | this.diagram = diagram;
|
---|
1446 | }
|
---|
1447 | #endregion
|
---|
1448 |
|
---|
1449 | #region IShapeIterator Members
|
---|
1450 | /// <summary>
|
---|
1451 | /// Called when a shape is encountered during iteration. The shape will be stored if it's orphaned.
|
---|
1452 | /// A shape is considered "orphaned" if:
|
---|
1453 | /// 1) It has a PresentationViewsSubject link out of it, but the Subject cannot be resolved.
|
---|
1454 | /// 2) It is a connector, and the Subject is null (regardless of the existence of PresentationViewsSubject relationship).
|
---|
1455 | /// </summary>
|
---|
1456 | /// <param name="shape">Shape encountered.</param>
|
---|
1457 | public void OnShape(DslDiagrams::ShapeElement shape)
|
---|
1458 | {
|
---|
1459 | if (this.diagram.IsOrphaned(shape))
|
---|
1460 | this.orphanedShapes.Add(shape); // Orphaned.
|
---|
1461 | }
|
---|
1462 | #endregion
|
---|
1463 | }
|
---|
1464 | #endregion
|
---|
1465 | #endregion
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | namespace HeuristicLab.SimulationCore
|
---|
1471 | {
|
---|
1472 | [DslValidation::ValidationState(DslValidation::ValidationState.Enabled)]
|
---|
1473 | public partial class SimulationModel
|
---|
1474 | {
|
---|
1475 | /// <summary>
|
---|
1476 | /// Check to make sure all elements in the model will have unambiguous monikers when serialized.
|
---|
1477 | /// </summary>
|
---|
1478 | [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Generated code.")]
|
---|
1479 | [DslValidation::ValidationMethod(DslValidation::ValidationCategories.Load | DslValidation::ValidationCategories.Save | DslValidation::ValidationCategories.Menu)]
|
---|
1480 | private void ValidateMonikerAmbiguity(DslValidation::ValidationContext context)
|
---|
1481 | {
|
---|
1482 | // Don't run this rule when deserializing - any ambiguous monikers will
|
---|
1483 | // already have stopped the file from loading.
|
---|
1484 | if (this.Store.InSerializationTransaction)
|
---|
1485 | {
|
---|
1486 | return;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | global::System.Collections.Generic.IDictionary<global::System.Guid, DslModeling::IMonikerResolver> monikerResolvers = HLSimSerializationHelper.Instance.GetMonikerResolvers(this.Store);
|
---|
1490 | foreach (DslModeling::ModelElement element in this.Store.ElementDirectory.AllElements)
|
---|
1491 | {
|
---|
1492 | global::System.Guid domainModelId = element.GetDomainClass().DomainModel.Id;
|
---|
1493 | DslModeling::IMonikerResolver monikerResolver = null;
|
---|
1494 | if (monikerResolvers.TryGetValue(domainModelId, out monikerResolver) && monikerResolver != null)
|
---|
1495 | {
|
---|
1496 | DslModeling::SimpleMonikerResolver simpleMonikerResolver = monikerResolver as DslModeling::SimpleMonikerResolver;
|
---|
1497 | if (simpleMonikerResolver != null)
|
---|
1498 | {
|
---|
1499 | try
|
---|
1500 | {
|
---|
1501 | simpleMonikerResolver.ProcessAddedElement(element);
|
---|
1502 | }
|
---|
1503 | catch (DslModeling::AmbiguousMonikerException amEx)
|
---|
1504 | { // Ambiguous moniker detected.
|
---|
1505 | context.LogError(
|
---|
1506 | string.Format(
|
---|
1507 | global::System.Globalization.CultureInfo.CurrentCulture,
|
---|
1508 | HLSimDomainModel.SingletonResourceManager.GetString("AmbiguousMoniker"),
|
---|
1509 | amEx.Moniker,
|
---|
1510 | DslModeling::SerializationUtilities.GetElementName(element),
|
---|
1511 | DslModeling::SerializationUtilities.GetElementName(amEx.Element)
|
---|
1512 | ),
|
---|
1513 | "AmbiguousMoniker",
|
---|
1514 | this,
|
---|
1515 | amEx.Element
|
---|
1516 | );
|
---|
1517 | }
|
---|
1518 | }
|
---|
1519 | }
|
---|
1520 | }
|
---|
1521 | // Clean up the created moniker resolvers after the check.
|
---|
1522 | foreach (DslModeling::IMonikerResolver monikerResolver in monikerResolvers.Values)
|
---|
1523 | {
|
---|
1524 | DslModeling::SimpleMonikerResolver simpleMonikerResolver = monikerResolver as DslModeling::SimpleMonikerResolver;
|
---|
1525 | if (simpleMonikerResolver != null)
|
---|
1526 | simpleMonikerResolver.Reset();
|
---|
1527 | }
|
---|
1528 | monikerResolvers.Clear();
|
---|
1529 | }
|
---|
1530 | }
|
---|
1531 | }
|
---|