/// <summary>
 /// Load from file and verify namespace
 /// </summary>
 /// <param name="ns"></param>
 /// <param name="filePath"></param>
 /// <param name="model"></param>
 /// <returns></returns>
 private bool TryLoadDesign(Namespace ns, string filePath,
                            out IModelDesign model)
 {
     if (!TryLoad(filePath, out model))
     {
         return(false);
     }
     // verify namespace.
     if (!string.IsNullOrEmpty(ns.Value) &&
         ns.Value != model.Namespace)
     {
         return(false);
     }
     // verify version.
     if (!string.IsNullOrEmpty(ns.Version) &&
         ns.Version != model.Version)
     {
         return(false);
     }
     if (!(model is INodeResolver))
     {
         return(false);
     }
     return(true);
 }
        /// <summary>
        /// Generates a single file containing all of the classes.
        /// </summary>
        public static void Save(this IModelDesign _model, string filePath)
        {
            var context = new SystemContext {
                NamespaceUris = new NamespaceTable()
            };
            var nodes      = _model.GetNodes(context);
            var collection = nodes.ToNodeStateCollection(context);

            // open the output file.
            var outputFile = string.Format(@"{0}\{1}.PredefinedNodes.xml", filePath, _model.Name);

            using (var ostrm = File.Open(outputFile, FileMode.Create)) {
                collection.SaveAsXml(context, ostrm);
            }

            // save as nodeset.
            var outputFile2 = string.Format(@"{0}\{1}.NodeSet.xml", filePath, _model.Name);

            using (var ostrm = File.Open(outputFile2, FileMode.Create)) {
                collection.SaveAsNodeSet(ostrm, context);
            }

            // save as nodeset2.
            var outputFile3 = string.Format(@"{0}\{1}.NodeSet2.xml", filePath, _model.Name);

            using (Stream ostrm = File.Open(outputFile3, FileMode.Create)) {
                var model = new ModelTableEntry {
                    ModelUri                 = _model.Namespace,
                    Version                  = _model.Version,
                    PublicationDate          = _model.PublicationDate ?? DateTime.MinValue,
                    PublicationDateSpecified = _model.PublicationDate != null
                };
                // if (_model.Dependencies != null) {
                //     model.RequiredModel = new List<ModelTableEntry>(_model.Dependencies.Values).ToArray();
                // }
                NodeSet2.Create(nodes, model, _model.PublicationDate, context).Save(ostrm);
            }

            // save as json.
            var outputFile4 = string.Format(@"{0}\{1}.NodeSet.json", filePath, _model.Name);

            using (var ostrm = File.Open(outputFile4, FileMode.Create)) {
                collection.SaveAsJson(ostrm, Formatting.Indented, context);
            }
        }
 /// <summary>
 /// Try loading model or types from file
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="model"></param>
 /// <returns></returns>
 private bool TryLoad(string fileName, out IModelDesign model)
 {
     model = null;
     if (!File.Exists(fileName))
     {
         return(false);
     }
     try {
         model = Model.Load(fileName, this);
         return(true);
     }
     catch {
         try {
             model = Model.LoadTypeDictionary(fileName, this);
             return(true);
         }
         catch {
             return(false);
         }
     }
 }