/// <summary> /// This methods deserializes nested XML elements inside the passed-in element. /// </summary> /// <remarks> /// The caller will guarantee that the current element does have nested XML elements, and the call will position the /// reader at the open tag of the first child XML element. /// This method will read as many child XML 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 that 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="element">In-memory NodeShape instance that will get the deserialized data.</param> /// <param name="reader">XmlReader to read serialized data from.</param> protected override void ReadElements(DslModeling.SerializationContext serializationContext, DslModeling.ModelElement element, System.Xml.XmlReader reader) { base.ReadElements(serializationContext, element, reader); while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element) { switch (reader.LocalName) { default: return; // Don't know this element. } } }
/// <summary> /// Write extension element data inside the current XML element. /// </summary> /// <param name="serializationContext">The current serialization context instance.</param> /// <param name="element">The element whose attributes have just been written.</param> /// <param name="writer">XmlWriter to write serialized data to.</param> /// <remarks>The default implemenation is to write out all non-embedded extension elements, /// regardless of whether they relate to the current element or not. /// The additional data should be written as a series of one or more /// XML elements.</remarks> protected internal override void WriteExtensions(DslModeling.SerializationContext serializationContext, DslModeling.ModelElement element, XmlWriter writer) { Guard.NotNull(() => serializationContext, serializationContext); Guard.NotNull(() => element, element); Guard.NotNull(() => writer, writer); if (!(element is PatternModelSchemaDiagram)) { var allExtensionElements = element.Partition.ElementDirectory.FindElements(DslModeling.ExtensionElement.DomainClassId, true); var nonEmbeddedExtensionsElements = allExtensionElements.Where(e => DslModeling.DomainClassInfo.FindEmbeddingElementLink(e) == null).OfType <DslModeling.ExtensionElement>(); DslModeling.SerializationUtilities.WriteExtensions(serializationContext, writer, nonEmbeddedExtensionsElements); } }
/// <summary> /// Saves the model. /// </summary> /// <param name="modelRoot">The model root.</param> /// <param name="stream">The stream.</param> internal void SaveModel(PatternModelSchema modelRoot, Stream stream) { Guard.NotNull(() => modelRoot, modelRoot); Guard.NotNull(() => stream, stream); var context = new DslModeling.SerializationContext(GetDirectory(modelRoot.Store)); this.InitializeSerializationContext(modelRoot.Partition, context, false); var settings = CreateXmlWriterSettings(context, false, Encoding.UTF8); using (var writer = XmlWriter.Create(stream, settings)) { this.WriteRootElement(context, modelRoot, writer); } }
/// <summary> /// This methods serializes 1) properties serialized as nested XML elements and 2) child model elements into XML. /// </summary> /// <param name="serializationContext">Serialization context.</param> /// <param name="element">NodeShape instance to be serialized.</param> /// <param name="writer">XmlWriter to write serialized data to.</param> protected override void WriteElements(DslModeling.SerializationContext serializationContext, DslModeling.ModelElement element, System.Xml.XmlWriter writer) { base.WriteElements(serializationContext, element, writer); }
/// <summary> /// This method deserializes all properties that are serialized as XML attributes. /// </summary> /// <remarks> /// Because this method only handles properties serialized as XML attributes, the passed-in reader shouldn't be moved inside this method. /// The caller will guarantee that the reader is positioned on the open XML tag of the current element being deserialized. /// </remarks> /// <param name="serializationContext">Serialization context.</param> /// <param name="element">In-memory NodeShape instance that will get the deserialized data.</param> /// <param name="reader">XmlReader to read serialized data from.</param> protected override void ReadPropertiesFromAttributes(DslModeling.SerializationContext serializationContext, DslModeling.ModelElement element, System.Xml.XmlReader reader) { base.ReadPropertiesFromAttributes(serializationContext, element, reader); }
private void LoadDiagram( DslModeling.SerializationResult serializationResult, DslModeling.Partition diagramPartition, PatternModelSchema modelRoot, DslModeling.DomainXmlSerializerDirectory directory, DslModeling.DomainClassXmlSerializer diagramSerializer, string diagramFileName, DslModeling.ISchemaResolver schemaResolver, ValidationController validationController) { if (diagramSerializer != null) { PatternModelSchemaDiagram diagram = null; using (FileStream fileStream = File.OpenRead(diagramFileName)) { var serializationContext = new DslModeling.SerializationContext(directory, fileStream.Name, serializationResult); this.InitializeSerializationContext(diagramPartition, serializationContext, true); var transactionContext = new DslModeling.TransactionContext(); transactionContext.Add(DslModeling.SerializationContext.TransactionContextKey, serializationContext); using (var transaction = diagramPartition.Store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingDiagram, true, transactionContext)) { // Ensure there is some content in the file. Blank (or almost blank, to account for encoding header bytes, etc.) // files will cause a new diagram to be created and returned if (fileStream.Length > 5) { var settings = PatternModelSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false); try { using (var reader = XmlReader.Create(fileStream, settings)) { reader.MoveToContent(); diagram = diagramSerializer.TryCreateInstance(serializationContext, reader, diagramPartition) as PatternModelSchemaDiagram; if (diagram != null) { this.ReadRootElement(serializationContext, diagram, reader, schemaResolver); } } } catch (XmlException ex) { DslModeling.SerializationUtilities.AddMessage(serializationContext, DslModeling.SerializationMessageKind.Error, ex); } if (serializationResult.Failed) { // Serialization error encountered, rollback the transaction. diagram = null; transaction.Rollback(); } } if (diagram == null && !serializationResult.Failed) { // Create diagram if it doesn't exist if (diagramFileName.EndsWith(DesignerConstants.DiagramFileExtension, StringComparison.OrdinalIgnoreCase)) { diagram = new PatternModelSchemaDiagram( diagramPartition.Store, new DslModeling.PropertyAssignment(DslDiagrams.Diagram.NameDomainPropertyId, Guid.NewGuid())); } else { diagram = this.CreateDiagramHelper(diagramPartition, modelRoot); } } if (transaction.IsActive) { transaction.Commit(); } } // End inner Tx // Do load-time validation if a ValidationController is provided. if (!serializationResult.Failed && validationController != null) { using (new SerializationValidationObserver(serializationResult, validationController)) { validationController.Validate(diagramPartition, ValidationCategories.Load); } } } if (diagram != null) { if (!serializationResult.Failed) { // Succeeded. diagram.ModelElement = modelRoot; diagram.PostDeserialization(true); this.CheckForOrphanedShapes(diagram, serializationResult); } else { // Failed. diagram.PostDeserialization(false); } } } }
public PatternModelSchema LoadModel(DslModeling.Store store, Stream stream, DslModeling.ISchemaResolver schemaResolver, DslModeling.ISerializerLocator serializerLocator) { Guard.NotNull(() => store, store); Guard.NotNull(() => stream, stream); if (!store.DefaultPartition.Store.TransactionActive) { throw new InvalidOperationException(PatternModelDomainModel.SingletonResourceManager.GetString("MissingTransaction")); } var directory = this.GetDirectory(store.DefaultPartition.Store); var modelRootSerializer = directory.GetSerializer(PatternModelSchema.DomainClassId); var serializationResult = new DslModeling.SerializationResult(); Debug.Assert(modelRootSerializer != null, @"Cannot find serializer for PatternModel!"); if (modelRootSerializer != null) { PatternModelSchema modelRoot = null; var serializationContext = new DslModeling.SerializationContext(directory, string.Empty, serializationResult); this.InitializeSerializationContext(store.DefaultPartition, serializationContext, true); var transactionContext = new DslModeling.TransactionContext(); transactionContext.Add(DslModeling.SerializationContext.TransactionContextKey, serializationContext); using (var tx = store.DefaultPartition.Store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingModel, true, transactionContext)) { try { var settings = PatternModelSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false); using (var reader = XmlReader.Create(stream, settings)) { reader.Read(); Encoding encoding; if (this.TryGetEncoding(reader, out encoding)) { serializationResult.Encoding = encoding; } DslModeling.SerializationUtilities.ResolveDomainModels(reader, serializerLocator, store.DefaultPartition.Store); reader.MoveToContent(); modelRoot = modelRootSerializer.TryCreateInstance(serializationContext, reader, store.DefaultPartition) as PatternModelSchema; if (modelRoot != null && !serializationResult.Failed) { this.ReadRootElement(serializationContext, modelRoot, reader, schemaResolver); } } } catch (XmlException ex) { DslModeling.SerializationUtilities.AddMessage(serializationContext, DslModeling.SerializationMessageKind.Error, ex); } if (modelRoot == null && !serializationResult.Failed) { modelRoot = this.CreateModelHelper(store.DefaultPartition); } if (tx.IsActive) { tx.Commit(); } } // Set tracking properties ResetTrackingProperties(store); return modelRoot; } return null; }
public PatternModelSchema LoadModel(DslModeling.Store store, Stream stream, DslModeling.ISchemaResolver schemaResolver, DslModeling.ISerializerLocator serializerLocator) { Guard.NotNull(() => store, store); Guard.NotNull(() => stream, stream); if (!store.DefaultPartition.Store.TransactionActive) { throw new InvalidOperationException(PatternModelDomainModel.SingletonResourceManager.GetString("MissingTransaction")); } var directory = this.GetDirectory(store.DefaultPartition.Store); var modelRootSerializer = directory.GetSerializer(PatternModelSchema.DomainClassId); var serializationResult = new DslModeling.SerializationResult(); Debug.Assert(modelRootSerializer != null, @"Cannot find serializer for PatternModel!"); if (modelRootSerializer != null) { PatternModelSchema modelRoot = null; var serializationContext = new DslModeling.SerializationContext(directory, string.Empty, serializationResult); this.InitializeSerializationContext(store.DefaultPartition, serializationContext, true); var transactionContext = new DslModeling.TransactionContext(); transactionContext.Add(DslModeling.SerializationContext.TransactionContextKey, serializationContext); using (var tx = store.DefaultPartition.Store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingModel, true, transactionContext)) { try { var settings = PatternModelSerializationHelper.Instance.CreateXmlReaderSettings(serializationContext, false); using (var reader = XmlReader.Create(stream, settings)) { reader.Read(); Encoding encoding; if (this.TryGetEncoding(reader, out encoding)) { serializationResult.Encoding = encoding; } DslModeling.SerializationUtilities.ResolveDomainModels(reader, serializerLocator, store.DefaultPartition.Store); reader.MoveToContent(); modelRoot = modelRootSerializer.TryCreateInstance(serializationContext, reader, store.DefaultPartition) as PatternModelSchema; if (modelRoot != null && !serializationResult.Failed) { this.ReadRootElement(serializationContext, modelRoot, reader, schemaResolver); } } } catch (XmlException ex) { DslModeling.SerializationUtilities.AddMessage(serializationContext, DslModeling.SerializationMessageKind.Error, ex); } if (modelRoot == null && !serializationResult.Failed) { modelRoot = this.CreateModelHelper(store.DefaultPartition); } if (tx.IsActive) { tx.Commit(); } } // Set tracking properties ResetTrackingProperties(store); return(modelRoot); } return(null); }