Exemplo n.º 1
0
 public static XmlReader getXmlReader(string filePath, out Stream stream)
 {
     return(getXmlReader(filePath, ProjectFileExtensions.GetProjectFileType(System.IO.Path.GetExtension(filePath)), out stream));
 }
Exemplo n.º 2
0
        public ProjectBase(string fileName)
            : this()
        {
            SpectraContainerBase container;

            object[] args = new object[3];
            args[0] = this;
            Stream stream;

            //jesli rozszerzeniu pliku projektu to evpc projekt zostal zapisany w plikach skompresowanych i
            //taki rodzaj strumienia musi byc wykorzystany
            //if (this._compressed = System.IO.Path.GetExtension(fileName) == ".evpc")
            this._fileType = ProjectFileExtensions.GetProjectFileType(System.IO.Path.GetExtension(fileName));
            if (this._fileType == ProjectFileType.Normal)
            {
                stream = new FileStream(fileName, FileMode.Open);
            }
            else
            {
                stream = new GZipStream(new FileStream(fileName, FileMode.Open), CompressionMode.Decompress);
            }

            try {
                int spectraCount = -1;
                //this._path = System.IO.Path.GetDirectoryName(fileName);
                this._projectFile = fileName;
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreWhitespace = true;
                using (XmlReader reader = XmlReader.Create(stream, settings)) {
                    reader.Read(); //declaration
                    reader.Read(); //project
                    if (reader.HasAttributes)
                    {
                        while (reader.MoveToNextAttribute())
                        {
                            switch (reader.Name)
                            {
                            case "caption": this._caption = reader.Value; break;

                            case "calculatedValues": this._calculatedValues = Boolean.Parse(reader.Value); break;

                            case "spectraCount": spectraCount = int.Parse(reader.Value); break;
                            }
                        }
                        reader.MoveToElement();
                    }
                    //List<IParameter> bindingParameters = new List<IParameter>();
                    //reader.ReadStartElement("project");
                    while (reader.Read())
                    {
                        switch (reader.Name)
                        {
                        case "models":
                            bool includedModelsData = reader.GetAttribute("defs") == "included";
                            while (reader.Read())
                            {
                                if (reader.Name == "model")
                                {
                                    BindingFlags bindingFlags = BindingFlags.CreateInstance;
                                    Assembly     ass          = Assembly.GetAssembly(this.GetSpectraContainerType());
                                    string       typeName     = this.GetSpectraContainerType().ToString();

                                    XmlReader containerReader;

                                    if (includedModelsData)
                                    {
                                        containerReader = reader.ReadSubtree();
                                        args[1]         = containerReader;
                                        args[2]         = null;
                                        try {
                                            container = (SpectraContainerBase)ass.CreateInstance(typeName, true, bindingFlags, null, args, null, null);
                                            if (container != null)
                                            {
                                                AddSpectraContainer(container);
                                            }
                                        } finally {
                                            containerReader.Close();
                                        }
                                    }
                                    else
                                    {
                                        Stream conStream;
                                        reader.MoveToFirstAttribute();
                                        string containerFileName = reader.Value;     // containerElement.Attributes["file"].Value;
                                        containerFileName = System.IO.Path.Combine(this.Path, containerFileName);
                                        containerReader   = getXmlReader(containerFileName, this._fileType, out conStream);
                                        try {
                                            //args = new object[] { this, containerReader, System.IO.Path.GetDirectoryName(containerFileName) };
                                            args[1]   = containerReader;
                                            args[2]   = System.IO.Path.GetDirectoryName(containerFileName);
                                            container = (SpectraContainerBase)ass.CreateInstance(typeName, true, bindingFlags, null, args, null, null);
                                            if (container != null)
                                            {
                                                AddSpectraContainer(container);
                                            }
                                        } finally {
                                            containerReader.Close();
                                            conStream.Close();
                                        }
                                        reader.MoveToElement();
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            break;

                        case "bindings":
                            //if (reader.ReadToFollowing("bindings")) {
                            while (reader.Read())
                            {
                                if (reader.Name == "binding")
                                {
                                    string name = reader.GetAttribute("name");
                                    switch (reader.GetAttribute("type"))
                                    {
                                    case "parameter":
                                        _bindingsManager.Add(new ParameterBinding(reader, this, name));
                                        break;

                                    case "group":
                                        _bindingsManager.Add(new GroupBinding(reader, this, name));
                                        break;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
            } finally {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }