PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Tum.PDE.ToolFramework/Tum.PDE.ToolFramework.Modeling.Diagrams/CustomCode/Serialization/LinkShapeSerializer.cs

#
C# | 197 lines | 133 code | 17 blank | 47 comment | 26 complexity | 7e00d72d832fe99f3e7016bbd1da9390 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.Modeling;
  6. namespace Tum.PDE.ToolFramework.Modeling.Diagrams
  7. {
  8. /// <summary>
  9. /// Serializer LinkShapeSerializer for DomainClass LinkShape.
  10. /// </summary>
  11. public partial class LinkShapeSerializer : LinkShapeSerializerBase
  12. {
  13. /// <summary>
  14. /// This method creates an instance of AblaufbausteinpunktRAblaufbausteinspezShape based on the tag currently pointed by the reader. The reader is guaranteed (by the caller)
  15. /// to be pointed at a serialized instance of AblaufbausteinpunktRAblaufbausteinspezShape.
  16. /// </summary>
  17. /// <remarks>
  18. /// The caller will guarantee that the reader is positioned at open XML tag of the ModelRoot instance being read. This method should
  19. /// not move the reader; the reader should remain at the same position when this method returns.
  20. /// </remarks>
  21. /// <param name="serializationContext">Serialization context.</param>
  22. /// <param name="reader">XmlReader to read serialized data from.</param>
  23. /// <param name="partition">Partition in which new AblaufbausteinpunktRAblaufbausteinspezShape instance should be created.</param>
  24. /// <returns>Created AblaufbausteinpunktRAblaufbausteinspezShape instance.</returns>
  25. protected override ModelElement CreateInstance(SerializationContext serializationContext, global::System.Xml.XmlReader reader, Partition partition)
  26. {
  27. string idStr = reader.GetAttribute("Id");
  28. try
  29. {
  30. global::System.Guid id;
  31. if (string.IsNullOrEmpty(idStr))
  32. { // Create a default Id.
  33. id = global::System.Guid.NewGuid();
  34. TestDslDefinitionSerializationBehaviorSerializationMessages.MissingId(serializationContext, reader, id);
  35. }
  36. else
  37. {
  38. id = new global::System.Guid(idStr);
  39. }
  40. string elementIdStr = reader.GetAttribute("internalElementId");
  41. if (string.IsNullOrEmpty(elementIdStr))
  42. {
  43. return null;
  44. }
  45. else
  46. {
  47. global::System.Guid elementId = new global::System.Guid(elementIdStr);
  48. ModelElement element = null;
  49. if (elementId != System.Guid.Empty)
  50. element = partition.ElementDirectory.FindElement(elementId);
  51. string sourceShapeIdStr = reader.GetAttribute("sourceShapeId");
  52. string targetShapeIdStr = reader.GetAttribute("targetShapeId");
  53. if (string.IsNullOrEmpty(sourceShapeIdStr) || string.IsNullOrEmpty(targetShapeIdStr))
  54. {
  55. return null;
  56. }
  57. else
  58. {
  59. global::System.Guid sourceShapeId = new global::System.Guid(sourceShapeIdStr);
  60. global::System.Guid targetShapeId = new global::System.Guid(targetShapeIdStr);
  61. NodeShape source = partition.ElementDirectory.FindElement(sourceShapeId) as NodeShape;
  62. NodeShape target = partition.ElementDirectory.FindElement(targetShapeId) as NodeShape;
  63. if (source == null || target == null)
  64. {
  65. return null;
  66. }
  67. if (element == null)
  68. element = CanAssignRelationship(source.Element, target.Element);
  69. if (element == null || !(element is IDomainModelOwnable))
  70. return null;
  71. return (element as IDomainModelOwnable).GetDomainModelServices().TopMostService.ShapeProvider.CreateShapeForElementLink(this.GetShapeClassId(), element, new PropertyAssignment[]{ new PropertyAssignment(ElementFactory.IdPropertyAssignment, id)});
  72. }
  73. }
  74. }
  75. catch (global::System.ArgumentNullException /* anEx */)
  76. {
  77. TestDslDefinitionSerializationBehaviorSerializationMessages.InvalidPropertyValue(serializationContext, reader, "Id", typeof(global::System.Guid), idStr);
  78. }
  79. catch (global::System.FormatException /* fEx */)
  80. {
  81. TestDslDefinitionSerializationBehaviorSerializationMessages.InvalidPropertyValue(serializationContext, reader, "Id", typeof(global::System.Guid), idStr);
  82. }
  83. catch (global::System.OverflowException /* ofEx */)
  84. {
  85. TestDslDefinitionSerializationBehaviorSerializationMessages.InvalidPropertyValue(serializationContext, reader, "Id", typeof(global::System.Guid), idStr);
  86. }
  87. return null;
  88. }
  89. /// <summary>
  90. /// Gets the shape class Id.
  91. /// </summary>
  92. /// <returns></returns>
  93. public virtual Guid GetShapeClassId()
  94. {
  95. return LinkShape.DomainClassId;
  96. }
  97. /// <summary>
  98. /// Finds out if a relationship can be assigned automatically between the given source
  99. /// and target element.
  100. /// </summary>
  101. /// <remarks>
  102. /// This method is required becase reference relationship do not need to be serialized
  103. /// with an id. But shapes still need to be created for them if a visual information
  104. /// is provided in the diagrams model.
  105. /// </remarks>
  106. protected virtual ModelElement CanAssignRelationship(ModelElement source, ModelElement target)
  107. {
  108. return null;
  109. }
  110. /// <summary>
  111. /// This method deserializes all properties that are serialized as XML attributes.
  112. /// </summary>
  113. /// <remarks>
  114. /// Because this method only handles properties serialized as XML attributes, the passed-in reader shouldn't be moved inside this method.
  115. /// The caller will guarantee that the reader is positioned on the open XML tag of the current element being deserialized.
  116. /// </remarks>
  117. /// <param name="serializationContext">Serialization context.</param>
  118. /// <param name="element">In-memory LinkShape instance that will get the deserialized data.</param>
  119. /// <param name="reader">XmlReader to read serialized data from.</param>
  120. protected override void ReadPropertiesFromAttributes(SerializationContext serializationContext, ModelElement element, System.Xml.XmlReader reader)
  121. {
  122. LinkShape instanceOfLinkShape = element as LinkShape;
  123. global::System.Diagnostics.Debug.Assert(instanceOfLinkShape != null, "Expecting an instance of LinkShape");
  124. // DummyProperty
  125. if (!serializationContext.Result.Failed)
  126. {
  127. string attribDummyProperty = DiagramsDSLSerializationHelper.Instance.ReadAttribute(serializationContext, element, reader, "dummyProperty");
  128. if (attribDummyProperty != null)
  129. {
  130. global::System.String valueOfDummyProperty;
  131. if (SerializationUtilities.TryGetValue<global::System.String>(serializationContext, attribDummyProperty, out valueOfDummyProperty))
  132. {
  133. instanceOfLinkShape.DummyProperty = valueOfDummyProperty;
  134. }
  135. else
  136. { // Invalid property value, ignored.
  137. TestDslDefinitionSerializationBehaviorSerializationMessages.IgnoredPropertyValue(serializationContext, reader, "dummyProperty", typeof(global::System.String), attribDummyProperty);
  138. }
  139. }
  140. }
  141. // RoutingMode
  142. if (!serializationContext.Result.Failed)
  143. {
  144. string attribRoutingMode = DiagramsDSLSerializationHelper.Instance.ReadAttribute(serializationContext, element, reader, "routingMode");
  145. if (attribRoutingMode != null)
  146. {
  147. RoutingMode valueOfRoutingMode;
  148. if (SerializationUtilities.TryGetValue<RoutingMode>(serializationContext, attribRoutingMode, out valueOfRoutingMode))
  149. {
  150. instanceOfLinkShape.RoutingMode = valueOfRoutingMode;
  151. }
  152. else
  153. { // Invalid property value, ignored.
  154. TestDslDefinitionSerializationBehaviorSerializationMessages.IgnoredPropertyValue(serializationContext, reader, "routingMode", typeof(RoutingMode), attribRoutingMode);
  155. }
  156. }
  157. }
  158. //base.ReadPropertiesFromAttributes(serializationContext, element, reader);
  159. }
  160. /// <summary>
  161. /// Write all properties that need to be serialized as XML attributes.
  162. /// </summary>
  163. /// <param name="serializationContext">Serialization context.</param>
  164. /// <param name="element">LinkShape instance to be serialized.</param>
  165. /// <param name="writer">XmlWriter to write serialized data to.</param>
  166. protected override void WritePropertiesAsAttributes(Microsoft.VisualStudio.Modeling.SerializationContext serializationContext, Microsoft.VisualStudio.Modeling.ModelElement element, System.Xml.XmlWriter writer)
  167. {
  168. base.WritePropertiesAsAttributes(serializationContext, element, writer);
  169. LinkShape instanceOfLinkShape = element as LinkShape;
  170. global::System.Diagnostics.Debug.Assert(instanceOfLinkShape != null, "Expecting an instance of LinkShape");
  171. if (!serializationContext.Result.Failed)
  172. {
  173. string serializedPropValue = SerializationUtilities.GetString<Guid>(serializationContext, instanceOfLinkShape.SourceAnchor.FromShape.Id);
  174. DiagramsDSLSerializationHelper.Instance.WriteAttributeString(serializationContext, element, writer, "sourceShapeId", serializedPropValue);
  175. serializedPropValue = SerializationUtilities.GetString<Guid>(serializationContext, instanceOfLinkShape.TargetAnchor.ToShape.Id);
  176. DiagramsDSLSerializationHelper.Instance.WriteAttributeString(serializationContext, element, writer, "targetShapeId", serializedPropValue);
  177. }
  178. }
  179. }
  180. }