private void Load(string xmlFilePath)
        {
            string xmlDirectory = Path.GetDirectoryName(xmlFilePath);

            Debug.WriteLine("Loading {0}", xmlFilePath);

#if DEBUG
            if (fsw == null)
            {
                fsw          = new FileSystemWatcher(xmlDirectory, ModulesFile);
                fsw.Changed += (sender, e) =>
                {
                    moduleDefs.Clear();
                    boardDefs.Clear();
                    Load(e.FullPath);
                };
                fsw.EnableRaisingEvents = true;
            }
#endif


            Action <Exception> logError = (e) => Log.WriteError("Error loading {0}:\n{1}", xmlFilePath, e.ToString());
            try
            {
                using (var fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read))
                {
                    bool schemaErrorsFound = false;

                    //Do schema validation when reading the xml files ...
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.ValidationType = ValidationType.Schema;
                    settings.Schemas.Add(schema);
                    settings.ValidationEventHandler += (sender, e) =>
                    {
                        if (e.Severity == XmlSeverityType.Error)
                        {
                            Log.WriteError("Error loading '{0}': {1}", xmlFilePath, e.Message);
                            schemaErrorsFound = true;
                        }
                        else
                        {
                            Log.WriteInfo("Warning loading '{0}': {1}", xmlFilePath, e.Message);
                        }
                    };
                    XmlReader reader = XmlReader.Create(fs, settings);

                    definitions = serializer.Deserialize(reader) as GadgeteerDefinitions;
                    if (definitions == null)
                    {
                        Log.WriteError("Could not deserialize {0}", xmlFilePath);
                        return;
                    }
                    if (schemaErrorsFound)
                    {
                        return;
                    }

                    foreach (var md in definitions.ModuleDefinitions)
                    {
                        if (IsNewerVersionRegistered(moduleDefs, md.UniqueId, md) ||
                            !IsValidPart(md))
                        {
                            continue;
                        }

                        SetFullPaths(xmlDirectory, md);

                        //Make sure instance names start with lower case
                        md.InstanceName = string.Concat(Char.ToLower(md.InstanceName[0], CultureInfo.CurrentCulture),
                                                        md.InstanceName.Substring(1));

                        moduleDefs[md.UniqueId] = md;
                    }


                    foreach (var md in definitions.MainboardDefinitions)
                    {
                        if (IsNewerVersionRegistered(boardDefs, md.Name, md) ||
                            !IsValidPart(md))
                        {
                            continue;
                        }

                        SetFullPaths(xmlDirectory, md);

                        boardDefs[md.Name] = md;
                    }
                }
            }
            catch (IOException e) { logError(e); }
            catch (UnauthorizedAccessException e) { logError(e); }
            catch (XmlException e) { logError(e); }
            catch (FormatException e) { logError(e); }
            catch (InvalidOperationException e) { logError(e); }
        }
Пример #2
0
        private void Load(string xmlFilePath)
        {
            string xmlDirectory = Path.GetDirectoryName(xmlFilePath);

            Debug.WriteLine("Loading {0}", xmlFilePath);

#if DEBUG
            if (fsw == null)
            {
                fsw          = new FileSystemWatcher(xmlDirectory, ModulesFile);
                fsw.Changed += (sender, e) =>
                {
                    moduleDefs.Clear();
                    boardDefs.Clear();
                    Load(e.FullPath);
                };
                fsw.EnableRaisingEvents = true;
            }
#endif


            Action <Exception> logError = (e) => Log.WriteError("Error loading {0}:\n{1}", xmlFilePath, e.ToString());
            try
            {
                using (var fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read))
                {
                    XmlReader reader = XmlReader.Create(fs);

                    definitions = serializer.Deserialize(reader) as GadgeteerDefinitions;
                    if (definitions == null)
                    {
                        Log.WriteError("Could not deserialize {0}.", xmlFilePath);
                        return;
                    }

                    foreach (var md in definitions.ModuleDefinitions)
                    {
                        if (!md.HasErrors)
                        {
                            if (IsNewerVersionRegistered(moduleDefs, md.UniqueId, md))
                            {
                                continue;
                            }

                            SetFullPaths(xmlDirectory, md);

                            //Make sure instance names start with lower case
                            md.InstanceName = ToCamelCase(md.InstanceName);
                        }
                        moduleDefs[md.UniqueId] = md;
                    }

                    foreach (var md in definitions.MainboardDefinitions)
                    {
                        if (!md.HasErrors)
                        {
                            if (IsNewerVersionRegistered(boardDefs, md.Name, md))
                            {
                                continue;
                            }

                            SetFullPaths(xmlDirectory, md);
                        }
                        boardDefs[md.Name] = md;
                    }
                }
            }
            catch (IOException e) { logError(e); }
            catch (UnauthorizedAccessException e) { logError(e); }
            catch (XmlException e) { logError(e); }
            catch (FormatException e) { logError(e); }
            catch (InvalidOperationException e) { logError(e); }
        }