public ActiveWriterExtensibleObject(Model model) { this._model = model; }
/// <summary> /// This method deserializes all child model elements. /// </summary> /// <remarks> /// The caller will position the reader at the open tag of the first child XML element to deserialized. /// This method will read as many child elements as it can. It returns under three circumstances: /// 1) When an unknown child XML element is encountered. In this case, this method will position the reader at the /// open tag of the unknown element. This implies the if the first child XML element is unknown, this method /// should return immediately and do nothing. /// 2) When all child XML elemnets are read. In this case, the reader will be positioned at the end tag of the parent element. /// 3) EOF. /// </remarks> /// <param name="serializationContext">Serialization context.</param> /// <param name="reader">XmlReader to read serialized data from.</param> /// <param name="element">In-memory Model instance that will get the deserialized data.</param> private static void ReadChildElements(DslModeling::SerializationContext serializationContext, Model element, global::System.Xml.XmlReader reader) { while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element) { switch (reader.LocalName) { case "classes": // Relationship "ModelHasClass" if (reader.IsEmptyElement) { // No instance of this relationship, just skip DslModeling::SerializationUtilities.Skip(reader); } else { DslModeling::SerializationUtilities.SkipToFirstChild(reader); // Skip the open tag of <classes> ReadModelHasClassInstances(serializationContext, element, reader); DslModeling::SerializationUtilities.Skip(reader); // Skip the close tag of </classes> } break; case "nestedClasses": // Relationship "ModelHasNestedClasses" if (reader.IsEmptyElement) { // No instance of this relationship, just skip DslModeling::SerializationUtilities.Skip(reader); } else { DslModeling::SerializationUtilities.SkipToFirstChild(reader); // Skip the open tag of <nestedClasses> ReadModelHasNestedClassesInstances(serializationContext, element, reader); DslModeling::SerializationUtilities.Skip(reader); // Skip the close tag of </nestedClasses> } break; default: return; // Don't know this element. } } }
/// <summary> /// Reads all instances of relationship ModelHasNestedClasses. /// </summary> /// <remarks> /// The caller will position the reader at the open tag of the first XML element inside the relationship tag, so it can be /// either the first instance, or a bogus tag. This method will deserialize all instances and ignore all bogus tags. When the /// method returns, the reader will be positioned at the end tag of the relationship (or EOF if somehow that happens). /// </remarks> /// <param name="serializationContext">Serialization context.</param> /// <param name="element">In-memory Model instance that will get the deserialized data.</param> /// <param name="reader">XmlReader to read serialized data from.</param> private static void ReadModelHasNestedClassesInstances(DslModeling::SerializationContext serializationContext, Model element, global::System.Xml.XmlReader reader) { while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element) { DslModeling::DomainClassXmlSerializer newNestedClassOfModelHasNestedClassesSerializer = serializationContext.Directory.GetSerializer(NestedClass.DomainClassId); global::System.Diagnostics.Debug.Assert(newNestedClassOfModelHasNestedClassesSerializer != null, "Cannot find serializer for NestedClass!"); NestedClass newNestedClassOfModelHasNestedClasses = newNestedClassOfModelHasNestedClassesSerializer.TryCreateInstance(serializationContext, reader, element.Partition) as NestedClass; if (newNestedClassOfModelHasNestedClasses != null) { element.NestedClasses.Add(newNestedClassOfModelHasNestedClasses); DslModeling::DomainClassXmlSerializer targetSerializer = serializationContext.Directory.GetSerializer (newNestedClassOfModelHasNestedClasses.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert (targetSerializer != null, "Cannot find serializer for " + newNestedClassOfModelHasNestedClasses.GetDomainClass().Name + "!"); targetSerializer.Read(serializationContext, newNestedClassOfModelHasNestedClasses, reader); } else { global::System.Type typeofModelHasNestedClasses = typeof(ModelHasNestedClasses); DslModeling::DomainRelationshipXmlSerializer newModelHasNestedClassesSerializer = serializationContext.Directory.GetSerializer(ModelHasNestedClasses.DomainClassId) as DslModeling::DomainRelationshipXmlSerializer; global::System.Diagnostics.Debug.Assert(newModelHasNestedClassesSerializer != null, "Cannot find serializer for ModelHasNestedClasses!"); ModelHasNestedClasses newModelHasNestedClasses = newModelHasNestedClassesSerializer.TryCreateInstance (serializationContext, reader, element.Partition) as ModelHasNestedClasses; if (newModelHasNestedClasses != null) { if (newModelHasNestedClasses.GetType() == typeofModelHasNestedClasses) { // The relationship should be serialized in short-form. ActiveWriterSerializationBehaviorSerializationMessages.ExpectingShortFormRelationship(serializationContext, reader, typeof(ModelHasNestedClasses)); } DslModeling::DomainRoleInfo.SetRolePlayer (newModelHasNestedClasses, ModelHasNestedClasses.ModelDomainRoleId, element); DslModeling::DomainClassXmlSerializer targetSerializer = serializationContext.Directory.GetSerializer (newModelHasNestedClasses.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert (targetSerializer != null, "Cannot find serializer for " + newModelHasNestedClasses.GetDomainClass().Name + "!"); targetSerializer.Read(serializationContext, newModelHasNestedClasses, reader); } else { // Unknown element, skip DslModeling::SerializationUtilities.Skip(reader); } } } }
private static void WriteChildElements(DslModeling::SerializationContext serializationContext, Model element, global::System.Xml.XmlWriter writer) { // ModelHasClass global::System.Collections.ObjectModel.ReadOnlyCollection<ModelHasClass> allModelHasClassInstances = ModelHasClass.GetLinksToClasses(element); if (!serializationContext.Result.Failed && allModelHasClassInstances.Count > 0) { writer.WriteStartElement("classes"); global::System.Type typeofModelHasClass = typeof(ModelHasClass); foreach (ModelHasClass eachModelHasClassInstance in allModelHasClassInstances) { if (serializationContext.Result.Failed) break; if (eachModelHasClassInstance.GetType() != typeofModelHasClass) { // Derived relationships will be serialized in full-form. DslModeling::DomainClassXmlSerializer derivedRelSerializer = serializationContext.Directory.GetSerializer(eachModelHasClassInstance.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(derivedRelSerializer != null, "Cannot find serializer for " + eachModelHasClassInstance.GetDomainClass().Name + "!"); derivedRelSerializer.Write(serializationContext, eachModelHasClassInstance, writer); } else { // No need to serialize the relationship itself, just serialize the role-player directly. DslModeling::ModelElement targetElement = eachModelHasClassInstance.Class; DslModeling::DomainClassXmlSerializer targetSerializer = serializationContext.Directory.GetSerializer(targetElement.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(targetSerializer != null, "Cannot find serializer for " + targetElement.GetDomainClass().Name + "!"); targetSerializer.Write(serializationContext, targetElement, writer); } } writer.WriteEndElement(); } // ModelHasNestedClasses global::System.Collections.ObjectModel.ReadOnlyCollection<ModelHasNestedClasses> allModelHasNestedClassesInstances = ModelHasNestedClasses.GetLinksToNestedClasses(element); if (!serializationContext.Result.Failed && allModelHasNestedClassesInstances.Count > 0) { writer.WriteStartElement("nestedClasses"); global::System.Type typeofModelHasNestedClasses = typeof(ModelHasNestedClasses); foreach (ModelHasNestedClasses eachModelHasNestedClassesInstance in allModelHasNestedClassesInstances) { if (serializationContext.Result.Failed) break; if (eachModelHasNestedClassesInstance.GetType() != typeofModelHasNestedClasses) { // Derived relationships will be serialized in full-form. DslModeling::DomainClassXmlSerializer derivedRelSerializer = serializationContext.Directory.GetSerializer(eachModelHasNestedClassesInstance.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(derivedRelSerializer != null, "Cannot find serializer for " + eachModelHasNestedClassesInstance.GetDomainClass().Name + "!"); derivedRelSerializer.Write(serializationContext, eachModelHasNestedClassesInstance, writer); } else { // No need to serialize the relationship itself, just serialize the role-player directly. DslModeling::ModelElement targetElement = eachModelHasNestedClassesInstance.NestedClass; DslModeling::DomainClassXmlSerializer targetSerializer = serializationContext.Directory.GetSerializer(targetElement.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(targetSerializer != null, "Cannot find serializer for " + targetElement.GetDomainClass().Name + "!"); targetSerializer.Write(serializationContext, targetElement, writer); } } writer.WriteEndElement(); } }
private static void ReadPropertiesFromElements(DslModeling::SerializationContext serializationContext, Model element, global::System.Xml.XmlReader reader) { while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element) { switch (reader.LocalName) { case "additionalImports": // AdditionalImports if (reader.IsEmptyElement) { // No serialized value, must be default one. DslModeling::SerializationUtilities.Skip(reader); // Skip this tag. } else { string strAdditionalImports = reader.ReadInnerXml(); global::System.Collections.Generic.List<Altinoren.ActiveWriter.Import> valueOfAdditionalImports; if (DslModeling::SerializationUtilities.TryGetValue<global::System.Collections.Generic.List<Altinoren.ActiveWriter.Import>>(DslModeling::SerializationUtilities.UnescapeXmlString(strAdditionalImports), out valueOfAdditionalImports)) { element.AdditionalImports = valueOfAdditionalImports; } else { // Invalid property value, ignored. ActiveWriterSerializationBehaviorSerializationMessages.IgnoredPropertyValue(serializationContext, reader, "additionalImports", typeof(global::System.Collections.Generic.List<Altinoren.ActiveWriter.Import>), strAdditionalImports); } DslModeling::SerializationUtilities.SkipToNextElement(reader); } break; default: return; // Don't know this element. } } }
public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, Model modelRoot, string modelFileName, ActiveRecordMapping diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue) { #region Check Parameters if (serializationResult == null) throw new global::System.ArgumentNullException("serializationResult"); if (string.IsNullOrEmpty(modelFileName)) throw new global::System.ArgumentNullException("modelFileName"); if (diagram == null) throw new global::System.ArgumentNullException("diagram"); if (string.IsNullOrEmpty(diagramFileName)) throw new global::System.ArgumentNullException("diagramFileName"); #endregion if (serializationResult.Failed) return; // Save the model file first using (global::System.IO.MemoryStream modelFileContent = this.InternalSaveModel(serializationResult, modelRoot, modelFileName, encoding, writeOptionalPropertiesWithDefaultValue)) { if (serializationResult.Failed) return; using (global::System.IO.MemoryStream diagramFileContent = new global::System.IO.MemoryStream()) { DslModeling::DomainClassXmlSerializer diagramSerializer = this.Directory.GetSerializer(diagram.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(diagramSerializer != null, "Cannot find serializer for " + diagram.GetDomainClass().Name + "!"); if (diagramSerializer != null) { DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(this.Directory, diagramFileName, serializationResult); // MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here. serializationContext.WriteOptionalPropertiesWithDefaultValue = writeOptionalPropertiesWithDefaultValue; global::System.Xml.XmlWriterSettings settings = new global::System.Xml.XmlWriterSettings(); settings.Indent = true; settings.Encoding = encoding; using (global::System.IO.StreamWriter streamWriter = new global::System.IO.StreamWriter(diagramFileContent, encoding)) { using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(streamWriter, settings)) { diagramSerializer.WriteRootElement(serializationContext, diagram, writer); } } } if (!serializationResult.Failed) { // Only write the contents if there's no error encountered during serialization. if (modelFileContent != null) { 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)) { using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding)) { writer.Write(modelFileContent.ToArray()); } } } if (diagramFileContent != null) { 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)) { using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding)) { writer.Write(diagramFileContent.ToArray()); } } } } } } }
private static void WritePropertiesAsElements(DslModeling::SerializationContext serializationContext, Model element, global::System.Xml.XmlWriter writer) { // AdditionalImports if (!serializationContext.Result.Failed) { global::System.Collections.Generic.List<Altinoren.ActiveWriter.Import> propValue = element.AdditionalImports; string serializedPropValue = DslModeling::SerializationUtilities.GetString<global::System.Collections.Generic.List<Altinoren.ActiveWriter.Import>>(serializationContext, propValue); if (!serializationContext.Result.Failed) { writer.WriteElementString("additionalImports", serializedPropValue); } } }
/// <summary> /// Saves the given diagram to the given file, with default encoding (UTF-8), and optional properties with default value will not /// be written out. /// </summary> /// <param name="serializationResult">Stores serialization result from the save operation.</param> /// <param name="modelRoot">Model instance to be saved.</param> /// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param> /// <param name="diagram">ActiveRecordMapping to be saved.</param> /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param> public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, Model modelRoot, string modelFileName, ActiveRecordMapping diagram, string diagramFileName) { this.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, global::System.Text.Encoding.UTF8, false); }
/// <summary> /// Saves the given diagram to the given file, with default encoding (UTF-8). /// </summary> /// <param name="serializationResult">Stores serialization result from the save operation.</param> /// <param name="modelRoot">Model instance to be saved.</param> /// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param> /// <param name="diagram">ActiveRecordMapping to be saved.</param> /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param> /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param> public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, Model modelRoot, string modelFileName, ActiveRecordMapping diagram, string diagramFileName, bool writeOptionalPropertiesWithDefaultValue) { this.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, global::System.Text.Encoding.UTF8, writeOptionalPropertiesWithDefaultValue); }
/// <summary> /// Helper method to create and initialize a new Model. /// </summary> internal protected virtual Model CreateModelHelper(DslModeling::Partition modelPartition) { Model model = new Model(modelPartition); return model; }
protected virtual global::System.IO.MemoryStream InternalSaveModel(DslModeling::SerializationResult serializationResult, Model modelRoot, string fileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue) { #region Check Parameters global::System.Diagnostics.Debug.Assert(serializationResult != null); global::System.Diagnostics.Debug.Assert(modelRoot != null); global::System.Diagnostics.Debug.Assert(!serializationResult.Failed); #endregion global::System.IO.MemoryStream newFileContent = new global::System.IO.MemoryStream(); DslModeling::DomainClassXmlSerializer modelRootSerializer = this.Directory.GetSerializer(modelRoot.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(modelRootSerializer != null, "Cannot find serializer for " + modelRoot.GetDomainClass().Name + "!"); if (modelRootSerializer != null) { DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(this.Directory, fileName, serializationResult); // MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here. serializationContext.WriteOptionalPropertiesWithDefaultValue = writeOptionalPropertiesWithDefaultValue; global::System.Xml.XmlWriterSettings settings = new global::System.Xml.XmlWriterSettings(); settings.Indent = true; settings.Encoding = encoding; using (global::System.IO.StreamWriter streamWriter = new global::System.IO.StreamWriter(newFileContent, encoding)) { using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(streamWriter, settings)) { modelRootSerializer.WriteRootElement(serializationContext, modelRoot, writer); } } } return newFileContent; }
public virtual void SaveModel(DslModeling::SerializationResult serializationResult, Model modelRoot, string fileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue) { #region Check Parameters if (serializationResult == null) throw new global::System.ArgumentNullException("serializationResult"); if (modelRoot == null) throw new global::System.ArgumentNullException("modelRoot"); if (string.IsNullOrEmpty(fileName)) throw new global::System.ArgumentNullException("fileName"); #endregion if (serializationResult.Failed) return; using (global::System.IO.MemoryStream newFileContent = this.InternalSaveModel(serializationResult, modelRoot, fileName, encoding, writeOptionalPropertiesWithDefaultValue)) { if (!serializationResult.Failed && newFileContent != null) { // Only write the content if there's no error encountered during serialization. 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)) { using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding)) { writer.Write(newFileContent.ToArray()); } } } } }
/// <summary> /// Saves the given model to the given file, with default encoding (UTF-8). /// </summary> /// <param name="serializationResult">Stores serialization result from the save operation.</param> /// <param name="modelRoot">Model instance to be saved.</param> /// <param name="fileName">Name of the file in which the Model instance will be saved.</param> /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param> public virtual void SaveModel(DslModeling::SerializationResult serializationResult, Model modelRoot, string fileName, bool writeOptionalPropertiesWithDefaultValue) { this.SaveModel(serializationResult, modelRoot, fileName, global::System.Text.Encoding.UTF8, writeOptionalPropertiesWithDefaultValue); }
/// <summary> /// Saves the given model root to the given file, with default encoding (UTF-8), and optional properties with default value will not /// be written out. /// </summary> /// <param name="serializationResult">Stores serialization result from the save operation.</param> /// <param name="modelRoot">Model instance to be saved.</param> /// <param name="fileName">Name of the file in which the Model instance will be saved.</param> public virtual void SaveModel(DslModeling::SerializationResult serializationResult, Model modelRoot, string fileName) { this.SaveModel(serializationResult, modelRoot, fileName, global::System.Text.Encoding.UTF8, false); }