Esempio n. 1
0
        /// <summary>
        /// Loads the pattern model file, and returns the schema.
        /// </summary>
        /// <param name="modelFilePath">Full path to the pattern model file.</param>
        public static PatternModelSchema LoadPatternModelFromFile(string modelFilePath)
        {
            var patternModel = (PatternModelSchema)null;
            var store        = new DslModeling.Store(null, new[] { typeof(PatternModelDomainModel) });

            DslModeling.Transaction tx = null;

            try
            {
                var serializationResult = new DslModeling.SerializationResult();
                tx           = store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingModel, true);
                patternModel = Instance.LoadModel(serializationResult, store, modelFilePath, null, null, null);
                if (serializationResult.Failed)
                {
                    throw new DslModeling.SerializationException(serializationResult);
                }

                tx.Commit();
            }
            finally
            {
                if ((tx != null))
                {
                    tx.Dispose();
                }
            }

            return(patternModel);
        }
        /// <summary>
        /// Loads the pattern model file, and returns the schema.
        /// </summary>
        /// <param name="modelFilePath">Full path to the pattern model file.</param>
        public static PatternModelSchema LoadPatternModelFromFile(string modelFilePath)
        {
            var patternModel = (PatternModelSchema)null;
            var store = new DslModeling.Store(null, new[] { typeof(PatternModelDomainModel) });
            DslModeling.Transaction tx = null;

            try
            {
                var serializationResult = new DslModeling.SerializationResult();
                tx = store.TransactionManager.BeginTransaction(Resources.PatternModelSerializationHelper_TransactionDescriptionLoadingModel, true);
                patternModel = Instance.LoadModel(serializationResult, store, modelFilePath, null, null, null);
                if (serializationResult.Failed)
                {
                    throw new DslModeling.SerializationException(serializationResult);
                }

                tx.Commit();
            }
            finally
            {
                if ((tx != null))
                {
                    tx.Dispose();
                }
            }

            return patternModel;
        }
Esempio n. 3
0
        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);
        }