示例#1
0
        /// <exception cref="IOException">
        /// Could not load the project.
        /// </exception>
        /// <exception cref="InvalidDataException">
        /// The save file is corrupt and could not be loaded.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="fileName"/> is empty string.
        /// </exception>
        public static Project Load(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentException(Strings.ErrorBlankFilename, "fileName");

            if (!File.Exists(fileName))
                throw new FileNotFoundException(Strings.ErrorFileNotFound);

            XmlDocument document = new XmlDocument();
            try
            {
                document.Load(fileName);
            }
            catch (Exception ex)
            {
                throw new IOException(Strings.ErrorCouldNotLoadFile, ex);
            }

            XmlElement root = document["Project"];
            if (root == null)
                throw new InvalidDataException(Strings.ErrorCorruptSaveFile);

            Project project = new Project();
            project.loading = true;
            try
            {
                DeserializeContext context = new DeserializeContext();
                project.Deserialize(context, root);
                foreach (var p in context.Models)
                {
                    p.Key.Deserialize(p.Value);
                    ((IModifiable)p.Key).Clean();
                }
            }
            catch (Exception ex)
            {
                throw new InvalidException(Strings.ErrorCorruptSaveFile + "\r\n" + ex.ToString(), ex);
            }
            project.loading = false;
            project.FilePath = fileName;
            project.isReadOnly = project.projectFile.IsReadOnly;
            project.Clean();
            return project;
        }
示例#2
0
        /// <exception cref="InvalidException">
        /// The save format is corrupt and could not be loaded.
        /// </exception>
        public override void Deserialize(DeserializeContext context, XmlElement root)
        {
            try
            {
                Language language = Language.GetLanguage(root.GetAttributeValue("Language", "UML"));
                if (language == null)
                    throw new InvalidDataException("Invalid project language.");

                this.language = language;
            }
            catch (Exception ex)
            {
                throw new InvalidException("Invalid project language.", ex);
            }
            base.Deserialize(context, root);
        }
示例#3
0
        public virtual void Deserialize(DeserializeContext context, XmlElement root)
        {
            if (root == null)
                throw new ArgumentNullException("root");

            Id = root["Id"].GetValue(Guid.Empty);
            name = root["Name"].GetValue(Strings.Untitled);
            Collapsed = root["Collapsed"].GetValue(false);

            XmlNodeList nodeList = root.SelectNodes("ProjectItems/ProjectItem");

            foreach (XmlElement itemElement in nodeList)
            {
                XmlAttribute typeAttribute = itemElement.Attributes["type"];
                XmlAttribute assemblyAttribute = itemElement.Attributes["assembly"];


                if (assemblyAttribute == null)
                {
                    string typeName = typeAttribute.InnerText;
                    IProjectItem projectItem = null;
                    if (typeName == ProjectItemType.Package.ToString())
                        projectItem = new Package();
                    else if (typeName == ProjectItemType.Model.ToString())
                        projectItem = Model.CreateProjectItem();
                    else if(typeName == EntityType.Class.ToString())
                        projectItem = ProjectInfo.Language.CreateClass("Class");
                    else if (typeName == EntityType.Delegate.ToString())
                        projectItem = ProjectInfo.Language.CreateDelegate("Delegate");
                    else if (typeName == EntityType.Enum.ToString())
                        projectItem = ProjectInfo.Language.CreateEnum("Enum");
                    else if (typeName == EntityType.Interface.ToString())
                        projectItem = ProjectInfo.Language.CreateInterface("Interface");
                    else if (typeName == EntityType.Structure.ToString())
                        projectItem = ProjectInfo.Language.CreateStructure("Structure");

                    if(projectItem == null)
                        throw new InvalidException("Invalid type [{0}] of ProjectItem.".FormatArgs(typeName));
                    projectItem.ProjectInfo = ProjectInfo;
                    projectItem.Deserialize(context, itemElement);
                    Add(projectItem);
                }
                else
                {

                    string typeName = typeAttribute.InnerText;
                    string assemblyName = assemblyAttribute.InnerText;

                    try
                    {
                        Assembly assembly = Assembly.Load(assemblyName);
                        IProjectItem projectItem = (IProjectItem)assembly.CreateInstance(
                            typeName, false,
                            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                            null, null, null, null);
                        projectItem.ProjectInfo = ProjectInfo;
                        projectItem.Deserialize(context, itemElement);
                        Add(projectItem);
                    }
                    catch (InvalidException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidException("Invalid type or assembly of ProjectItem.", ex);
                    }
                }
            }
            OnDeserializing(new SerializeEventArgs(root));
        }