PageRenderTime 608ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Releases/1.5.1/Source/Dsl/CustomCode/ConfigurationPropertySerializer.cs

#
C# | 226 lines | 177 code | 37 blank | 12 comment | 17 complexity | bcbf700ee0b9530d5e358e24a42214ac 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. using System.Xml;
  7. using System.Text.RegularExpressions;
  8. namespace ConfigurationSectionDesigner
  9. {
  10. public partial class ConfigurationPropertySerializer
  11. {
  12. /// <summary>
  13. /// Custom deserialization of ConfigurationProperty's elements. This is required because we're
  14. /// storing the CustomAttributes as elements.
  15. /// </summary>
  16. private static void CustomReadPropertiesFromElements( SerializationContext serializationContext, ModelElement element, XmlReader reader )
  17. {
  18. ConfigurationProperty property = (ConfigurationProperty)element;
  19. if( reader.Name != "customAttributes" )
  20. {
  21. ConfigurationSectionDesignerSerializationBehaviorSerializationMessages.UnexpectedXmlElement( serializationContext, reader );
  22. return;
  23. }
  24. XmlReader caReader = reader.ReadSubtree();
  25. AttributeList attributeList = new AttributeList();
  26. while( caReader.Read() )
  27. {
  28. switch( caReader.NodeType )
  29. {
  30. case XmlNodeType.Element:
  31. if( caReader.Name == "customAttributes" )
  32. continue;
  33. if( caReader.Name != "attribute" )
  34. {
  35. ConfigurationSectionDesignerSerializationBehaviorSerializationMessages.UnexpectedXmlElement( serializationContext, caReader );
  36. continue;
  37. }
  38. Attribute attribute = new Attribute();
  39. attributeList.AddLast( attribute );
  40. while( caReader.MoveToNextAttribute() )
  41. {
  42. switch( caReader.Name )
  43. {
  44. case "name":
  45. attribute.Name = caReader.Value;
  46. break;
  47. default:
  48. ConfigurationSectionDesignerSerializationBehaviorSerializationMessages.IgnoredPropertyValue( serializationContext, caReader, caReader.Name, caReader.ValueType, caReader.Value );
  49. break;
  50. }
  51. }
  52. if( string.IsNullOrEmpty( attribute.Name.Trim() ) )
  53. attribute.Name = "INVALID";
  54. caReader.Read();
  55. while( caReader.NodeType != XmlNodeType.Element )
  56. caReader.Read();
  57. if( caReader.Name != "parameters" )
  58. {
  59. ConfigurationSectionDesignerSerializationBehaviorSerializationMessages.UnexpectedXmlElement( serializationContext, reader );
  60. continue;
  61. }
  62. XmlReader paReader = caReader.ReadSubtree();
  63. while( paReader.Read() )
  64. {
  65. switch( paReader.NodeType )
  66. {
  67. case XmlNodeType.Element:
  68. if( paReader.Name == "parameters" ) continue;
  69. if( paReader.Name != "parameter" )
  70. {
  71. ConfigurationSectionDesignerSerializationBehaviorSerializationMessages.UnexpectedXmlElement( serializationContext, paReader );
  72. continue;
  73. }
  74. AttributeParameter parameter = new AttributeParameter();
  75. attribute.Parameters.AddLast( parameter );
  76. while( paReader.MoveToNextAttribute() )
  77. {
  78. switch( caReader.Name )
  79. {
  80. case "name":
  81. parameter.Name = paReader.Value;
  82. break;
  83. case "value":
  84. parameter.Value = paReader.Value;
  85. break;
  86. default:
  87. ConfigurationSectionDesignerSerializationBehaviorSerializationMessages.IgnoredPropertyValue( serializationContext, paReader, paReader.Name, paReader.ValueType, paReader.Value );
  88. break;
  89. }
  90. }
  91. if( string.IsNullOrEmpty( parameter.Value.Trim() ) )
  92. parameter.Value = "INVALID";
  93. break;
  94. }
  95. }
  96. break;
  97. }
  98. }
  99. property.CustomAttributes = attributeList;
  100. reader.Skip();
  101. while( reader.NodeType != XmlNodeType.Element )
  102. reader.Skip();
  103. }
  104. /// <summary>
  105. /// Custom serialization of ConfigurationProperty's elements. This is required because we're
  106. /// storing the CustomAttributes as elements.
  107. /// </summary>
  108. private static void CustomWritePropertiesAsElements( SerializationContext serializationContext, ModelElement element, XmlWriter writer )
  109. {
  110. ConfigurationProperty property = (ConfigurationProperty)element;
  111. writer.WriteStartElement( "customAttributes" );
  112. foreach( Attribute attribute in property.CustomAttributes )
  113. {
  114. writer.WriteStartElement( "attribute" );
  115. writer.WriteStartAttribute( "name" );
  116. writer.WriteString( attribute.Name );
  117. writer.WriteEndAttribute();
  118. writer.WriteStartElement( "parameters" );
  119. foreach( AttributeParameter parameter in attribute.Parameters )
  120. {
  121. writer.WriteStartElement( "parameter" );
  122. if( !string.IsNullOrEmpty( parameter.Name ) )
  123. {
  124. writer.WriteStartAttribute( "name" );
  125. writer.WriteString( parameter.Name );
  126. writer.WriteEndAttribute();
  127. }
  128. writer.WriteStartAttribute( "value" );
  129. writer.WriteString( parameter.Value );
  130. writer.WriteEndAttribute();
  131. writer.WriteEndElement();
  132. }
  133. writer.WriteEndElement();
  134. writer.WriteEndElement();
  135. }
  136. writer.WriteEndElement();
  137. }
  138. #region Use default implementations
  139. private void CustomConstructor()
  140. {
  141. DefaultConstructor();
  142. }
  143. private void CustomReadPropertiesFromAttributes( SerializationContext serializationContext, ModelElement element, XmlReader reader )
  144. {
  145. DefaultReadPropertiesFromAttributes( serializationContext, element, reader );
  146. }
  147. private void CustomWritePropertiesAsAttributes( SerializationContext serializationContext, ModelElement element, XmlWriter writer )
  148. {
  149. DefaultWritePropertiesAsAttributes( serializationContext, element, writer );
  150. }
  151. private ModelElement CustomTryCreateInstance( SerializationContext serializationContext, XmlReader reader, Partition partition )
  152. {
  153. return DefaultTryCreateInstance( serializationContext, reader, partition );
  154. }
  155. private ModelElement CustomCreateInstance( SerializationContext serializationContext, XmlReader reader, Partition partition )
  156. {
  157. return DefaultCreateInstance( serializationContext, reader, partition );
  158. }
  159. private Moniker CustomTryCreateMonikerInstance( SerializationContext serializationContext, XmlReader reader, ModelElement sourceRolePlayer, Guid relDomainClassId, Partition partition )
  160. {
  161. return DefaultTryCreateMonikerInstance( serializationContext, reader, sourceRolePlayer, relDomainClassId, partition );
  162. }
  163. private Moniker CustomCreateMonikerInstance( SerializationContext serializationContext, XmlReader reader, ModelElement sourceRolePlayer, Guid relDomainClassId, Partition partition )
  164. {
  165. return DefaultCreateMonikerInstance( serializationContext, reader, sourceRolePlayer, relDomainClassId, partition );
  166. }
  167. private string CustomCalculateQualifiedName( DomainXmlSerializerDirectory directory, ModelElement element )
  168. {
  169. return DefaultCalculateQualifiedName( directory, element );
  170. }
  171. private string CustomGetMonikerQualifier( DomainXmlSerializerDirectory directory, ModelElement element )
  172. {
  173. return DefaultGetMonikerQualifier( directory, element );
  174. }
  175. private void CustomReadElements( SerializationContext serializationContext, ModelElement element, XmlReader reader )
  176. {
  177. DefaultReadElements( serializationContext, element, reader );
  178. }
  179. private void CustomWriteElements( SerializationContext serializationContext, ModelElement element, XmlWriter writer )
  180. {
  181. // IsCustom == true, calling CustomWriteElements().
  182. // Provide your custom implementation with following signature:
  183. // private static void CustomWriteElements(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlWriter writer);
  184. // You can call DefaultWriteElements() for the default implementation.
  185. DefaultWriteElements( serializationContext, element, writer );
  186. }
  187. #endregion
  188. }
  189. }