예제 #1
0
        public static void Load(string path)
        {
            string[] files = Directory.GetFiles(path, "*" + Res.SiteReaderExt, SearchOption.TopDirectoryOnly);

            SiteReaders = new ObservableCollection<SiteReader>();

            if (!Directory.Exists(Environment.CurrentDirectory + @"\CacheDir"))
            {
                Directory.CreateDirectory(Environment.CurrentDirectory + @"\CacheDir");
            }
            else
            {
                var cacheFiles = Directory.GetFiles(
                    Environment.CurrentDirectory + @"\CacheDir",
                    "*.dll",
                    SearchOption.TopDirectoryOnly
                );

                try
                {
                    foreach (var f in cacheFiles) File.Delete(f);
                }
                catch (Exception error)
                {
                    ErrorLog.LogException(error);
                }
            }

            foreach (string file in files)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    SiteReader sr;

                    doc.Load(file);

                    var nodes = doc.SelectNodes("//Section");
                    var root = doc.SelectSingleNode("//SiteReader");
                    var sections = new Dictionary<string, int>(nodes.Count);

                    foreach (XmlNode n in nodes)
                    {
                        var key = n.Attributes["title"].InnerText;
                        var id = int.Parse(n.Attributes["id"].InnerText);

                        /*if (sections.ContainsKey(key))
                        {
                            sections.Add(key, id);
                        }*/

                        while (sections.ContainsKey(key)) key += ' ';

                        sections.Add(key, id);
                    }

                    var name = doc.SelectSingleNode("//SiteName").InnerText.Trim();
                    var url = doc.SelectSingleNode("//BaseUrl").InnerText.Trim();
                    var topicsPp = int.Parse(doc.SelectSingleNode("//TopicsPerPage").InnerText.Trim());
                    var type = doc.SelectSingleNode("//Type").InnerText.Trim();
                    var encoding = doc.SelectSingleNode("//DefaultEncoding").InnerText.Trim();
                    var redirects = bool.Parse(doc.SelectSingleNode("//AllowRedirects").InnerText.Trim());
                    var friendlyUrls = bool.Parse(doc.SelectSingleNode("//UseFriendlyLinks").InnerText.Trim());
                    var code = doc.SelectSingleNode("//Code");

                    if (code != null && code.InnerText.Trim().Length > 0)
                    {
                        Type compiled = Compile(
                            code.InnerText,
                            name,
                            root.Attributes["pluginVersion"].InnerText,
                            root.Attributes["pluginAuthor"].InnerText
                        );
                        Object[] args = new Object[] {
                            name,
                            url,
                            type,
                            topicsPp,
                            sections,
                            encoding,
                            redirects,
                            friendlyUrls
                        };

                        sr = (SiteReader)Activator.CreateInstance(compiled, args);
                    }
                    else
                    {
                        sr = new SiteReader(name, url, type, topicsPp, sections, encoding, redirects, friendlyUrls);
                    }

                    SiteReaders.Add(sr);
                }
                catch (Exception error)
                {
                    ErrorLog.LogException(error);
                }
            }
        }
예제 #2
0
        public static void Load(string path)
        {
            string[] files = Directory.GetFiles(path, "*" + Res.SiteReaderExt, SearchOption.TopDirectoryOnly);

            SiteReaders = new ObservableCollection <SiteReader>();

            if (!Directory.Exists(Environment.CurrentDirectory + @"\CacheDir"))
            {
                Directory.CreateDirectory(Environment.CurrentDirectory + @"\CacheDir");
            }
            else
            {
                var cacheFiles = Directory.GetFiles(
                    Environment.CurrentDirectory + @"\CacheDir",
                    "*.dll",
                    SearchOption.TopDirectoryOnly
                    );

                try
                {
                    foreach (var f in cacheFiles)
                    {
                        File.Delete(f);
                    }
                }
                catch (Exception error)
                {
                    ErrorLog.LogException(error);
                }
            }

            foreach (string file in files)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    SiteReader  sr;

                    doc.Load(file);

                    var nodes    = doc.SelectNodes("//Section");
                    var root     = doc.SelectSingleNode("//SiteReader");
                    var sections = new Dictionary <string, int>(nodes.Count);

                    foreach (XmlNode n in nodes)
                    {
                        var key = n.Attributes["title"].InnerText;
                        var id  = int.Parse(n.Attributes["id"].InnerText);

                        /*if (sections.ContainsKey(key))
                         * {
                         *      sections.Add(key, id);
                         * }*/

                        while (sections.ContainsKey(key))
                        {
                            key += ' ';
                        }

                        sections.Add(key, id);
                    }

                    var name         = doc.SelectSingleNode("//SiteName").InnerText.Trim();
                    var url          = doc.SelectSingleNode("//BaseUrl").InnerText.Trim();
                    var topicsPp     = int.Parse(doc.SelectSingleNode("//TopicsPerPage").InnerText.Trim());
                    var type         = doc.SelectSingleNode("//Type").InnerText.Trim();
                    var encoding     = doc.SelectSingleNode("//DefaultEncoding").InnerText.Trim();
                    var redirects    = bool.Parse(doc.SelectSingleNode("//AllowRedirects").InnerText.Trim());
                    var friendlyUrls = bool.Parse(doc.SelectSingleNode("//UseFriendlyLinks").InnerText.Trim());
                    var code         = doc.SelectSingleNode("//Code");

                    if (code != null && code.InnerText.Trim().Length > 0)
                    {
                        Type compiled = Compile(
                            code.InnerText,
                            name,
                            root.Attributes["pluginVersion"].InnerText,
                            root.Attributes["pluginAuthor"].InnerText
                            );
                        Object[] args = new Object[] {
                            name,
                            url,
                            type,
                            topicsPp,
                            sections,
                            encoding,
                            redirects,
                            friendlyUrls
                        };

                        sr = (SiteReader)Activator.CreateInstance(compiled, args);
                    }
                    else
                    {
                        sr = new SiteReader(name, url, type, topicsPp, sections, encoding, redirects, friendlyUrls);
                    }

                    SiteReaders.Add(sr);
                }
                catch (Exception error)
                {
                    ErrorLog.LogException(error);
                }
            }
        }