public void MergeAndSave ()
		{
			string fileName = "NiniConfig.ini";

			StreamWriter fileWriter = new StreamWriter (fileName);
			fileWriter.WriteLine ("[Pets]");
			fileWriter.WriteLine ("cat = Muffy"); // overwrite
			fileWriter.WriteLine ("dog = Rover"); // new
			fileWriter.WriteLine ("bird = Tweety");
			fileWriter.Close ();
			
			StringWriter writer = new StringWriter ();
			writer.WriteLine ("[Pets]");
			writer.WriteLine ("cat = Becky"); // overwrite
			writer.WriteLine ("lizard = Saurus"); // new
			writer.WriteLine ("[People]");
			writer.WriteLine (" woman = Jane");
			writer.WriteLine (" man = John");
			IniConfigSource iniSource = new IniConfigSource 
									(new StringReader (writer.ToString ()));

			IniConfigSource source = new IniConfigSource (fileName);

			source.Merge (iniSource);
			
			IConfig config = source.Configs["Pets"];
			Assert.AreEqual (4, config.GetKeys ().Length);
			Assert.AreEqual ("Becky", config.Get ("cat"));
			Assert.AreEqual ("Rover", config.Get ("dog"));
			Assert.AreEqual ("Saurus", config.Get ("lizard"));
		
			config = source.Configs["People"];
			Assert.AreEqual (2, config.GetKeys ().Length);
			Assert.AreEqual ("Jane", config.Get ("woman"));
			Assert.AreEqual ("John", config.Get ("man"));
			
			config.Set ("woman", "Tara");
			config.Set ("man", "Quentin");
			
			source.Save ();
			
			source = new IniConfigSource (fileName);
			
			config = source.Configs["Pets"];
			Assert.AreEqual (4, config.GetKeys ().Length);
			Assert.AreEqual ("Becky", config.Get ("cat"));
			Assert.AreEqual ("Rover", config.Get ("dog"));
			Assert.AreEqual ("Saurus", config.Get ("lizard"));
			
			config = source.Configs["People"];
			Assert.AreEqual (2, config.GetKeys ().Length);
			Assert.AreEqual ("Tara", config.Get ("woman"));
			Assert.AreEqual ("Quentin", config.Get ("man"));
			
			File.Delete  (fileName);
		}
		public void MergeItself ()
		{
			StringWriter writer = new StringWriter ();
			writer.WriteLine ("[People]");
			writer.WriteLine (" woman = Jane");
			writer.WriteLine (" man = John");
			IniConfigSource iniSource = 
					new IniConfigSource (new StringReader (writer.ToString ()));
			
			iniSource.Merge (iniSource); // exception
		}
Exemple #3
0
        public BaseConfig(string iniFileName)
        {
            source = new IniConfigSource(iniFileName);

            string mergedPath = string.Format("{0}.local", iniFileName);

            if (File.Exists(mergedPath))
            {
                var forMerge = new IniConfigSource(mergedPath);
                source.Merge(forMerge);
            }
        }
        // We call this from our plugin module to get our configuration
        public IConfig GetConfig()
        { 
            IConfig config = null;
            config = ServerUtils.GetConfig(ConfigFile, ConfigName);

            // Our file is not here? We can get one to bootstrap our plugin module
            if ( config == null )
            {
                IConfigSource remotesource = GetConfigSource();

                if (remotesource != null)
                {
                    IniConfigSource initialconfig = new IniConfigSource();
                    initialconfig.Merge (remotesource);
                    initialconfig.Save(ConfigFile);
                }

                config = remotesource.Configs[ConfigName];
            }

            return config;
        }
        private IniConfigSource LoadConfig(string location)
        {
            IniConfigSource currentConfig = new IniConfigSource();
            List<string> currentConfigLines = new List<string>();
            string[] configLines = null;

            if (IsUrl(location))
            {
                // Web-based loading
                string responseStr;
                if (WebUtil.TryGetUrl(location, out responseStr))
                {
                    configLines = responseStr.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    m_log.Error("Failed to load web config file " + location + ": " + responseStr);
                }
            }
            else
            {
                // Local file loading
                try
                {
                    configLines = new List<string>(File.ReadAllLines(location)).ToArray();
                }
                catch (Exception ex)
                {
                    m_log.Error("Failed to load config file " + location + ": " + ex.Message);
                }
            }

            if (configLines != null)
            {
                for (int i = 0; i < configLines.Length; i++)
                {
                    string line = configLines[i].Trim();

                    if (line.StartsWith("Include "))
                    {
                        // Compile the current config lines, compile the included config file, and combine them
                        currentConfig.Merge(CompileConfig(currentConfigLines));
                        currentConfigLines.Clear();

                        string includeLocation = line.Substring(8).Trim().Trim(new char[] { '"' });

                        if (IsUrl(includeLocation))
                        {
                            IniConfigSource includeConfig = LoadConfig(includeLocation);
                            currentConfig.Merge(includeConfig);
                        }
                        else
                        {
                            string basepath = Path.GetFullPath(Util.configDir());

                            // Resolve relative paths with wildcards
                            string chunkWithoutWildcards = includeLocation;
                            string chunkWithWildcards = string.Empty;
                            int wildcardIndex = includeLocation.IndexOfAny(new char[] { '*', '?' });
                            if (wildcardIndex != -1)
                            {
                                chunkWithoutWildcards = includeLocation.Substring(0, wildcardIndex);
                                chunkWithWildcards = includeLocation.Substring(wildcardIndex);
                            }
                            string path = Path.Combine(basepath, chunkWithoutWildcards);
                            path = Path.GetFullPath(path) + chunkWithWildcards;
                            
                            string[] paths = Util.Glob(path);
                            foreach (string p in paths)
                            {
                                IniConfigSource includeConfig = LoadConfig(p);
                                currentConfig.Merge(includeConfig);
                            }
                        }
                    }
                    else if (!String.IsNullOrEmpty(line) && !line.StartsWith(";"))
                    {
                        currentConfigLines.Add(line);
                    }
                }

                currentConfig.Merge(CompileConfig(currentConfigLines));
            }

            return currentConfig;
        }
        /// <summary>
        ///     Loads the region configuration
        /// </summary>
        /// <param name="argvSource">Parameters passed into the process when started</param>
        /// <returns>A configuration that gets passed to modules</returns>
        public IConfigSource LoadConfigSettings(IConfigSource argvSource)
        {
            iniFilePath = "";
            bool iniFileExists = false;
            bool oldoptions = false;

            string mainIniDirectory = Constants.DEFAULT_CONFIG_DIR;
            string mainIniFileName = defaultIniFile;
            string secondaryIniFileName = "";
            string worldIniFileName = "MyWorld.ini";

            List<string> sources = new List<string>();
            string basePath = Util.configDir();

            if (argvSource != null)
            {
                IConfig startupConfig = argvSource.Configs["Startup"];

                oldoptions =
                    startupConfig.GetBoolean("oldoptions", false);

                inidbg =
                    startupConfig.GetBoolean("inidbg", inidbg);

                showIniLoading =
                    startupConfig.GetBoolean("inishowfileloading", showIniLoading);

                if (oldoptions)
                {
                    string masterFileName =
                        startupConfig.GetString("inimaster", String.Empty);

                    string iniGridName =
                        startupConfig.GetString("inigrid", String.Empty);

                    if (iniGridName == string.Empty) //Read the old name then
                        iniGridName =
                            startupConfig.GetString("inifile", String.Empty);

                    string iniSimName =
                        startupConfig.GetString("inisim", defaultIniFile);

                    //Be mindful of these when modifying...
                    //1) When file A includes file B, if the same directive is found in both, that the value in file B wins.
                    //2) That inifile may be used with or without inimaster being used.
                    //3) That any values for directives pulled in via inifile (Config Set 2) override directives of the same name found in
                    //    the directive set (Config Set 1) created by reading in bin/Universe.ini and its subsequently included files
                    //    or that created by reading in whatever file inimaster points to and its subsequently included files.

                    if (IsUri(masterFileName))
                    {
                        if (!sources.Contains(masterFileName))
                            sources.Add(masterFileName);
                    }
                    else
                    {
                        string masterFilePath = Util.BasePathCombine(masterFileName);

                        if (masterFileName != String.Empty &&
                            File.Exists(masterFilePath) &&
                            (!sources.Contains(masterFilePath)))
                            sources.Add(masterFilePath);
                        if (iniGridName == "") //Then it doesn't exist and we need to set this
                            iniFilePath = masterFilePath;
                        if (iniSimName == "") //Then it doesn't exist and we need to set this
                            iniFilePath = masterFilePath;
                    }

                    if (iniGridName != "")
                    {
                        if (IsUri(iniGridName))
                        {
                            if (!sources.Contains(iniGridName))
                                sources.Add(iniGridName);
                            iniFilePath = iniGridName;
                        }
                        else
                        {
                            iniFilePath = Util.BasePathCombine(iniGridName);

                            if (File.Exists(iniFilePath))
                            {
                                if (!sources.Contains(iniFilePath))
                                    sources.Add(iniFilePath);
                            }
                        }
                    }

                    if (iniSimName != "")
                    {
                        if (IsUri(iniSimName))
                        {
                            if (!sources.Contains(iniSimName))
                                sources.Add(iniSimName);
                            iniFilePath = iniSimName;
                        }
                        else
                        {
                            iniFilePath = Util.BasePathCombine(iniSimName);

                            if (File.Exists(iniFilePath))
                            {
                                if (!sources.Contains(iniFilePath))
                                    sources.Add(iniFilePath);
                            }
                        }
                    }

                    string iniDirName =
                        startupConfig.GetString("inidirectory", "");

                    if (iniDirName != "" && Directory.Exists(iniDirName))
                    {
                        Console.WriteLine(string.Format("Searching folder {0} for config ini files",
                                                        iniDirName));

                        string[] fileEntries = Directory.GetFiles(iniDirName);

                        foreach (string filePath in fileEntries.Where(filePath =>
                                                                          {
                                                                              var extension = Path.GetExtension(filePath);
                                                                              return extension != null &&
                                                                                     extension.ToLower() == ".ini";
                                                                          })
                                                               .Where(
                                                                   filePath =>
                                                                   !sources.Contains(Path.Combine(iniDirName, filePath)))
                            )
                        {
                            sources.Add(Path.Combine(iniDirName, filePath));
                        }
                    }
                }
                else
                {
                    mainIniDirectory = startupConfig.GetString("mainIniDirectory", mainIniDirectory);
                    mainIniFileName = startupConfig.GetString("mainIniFileName", defaultIniFile);
                    secondaryIniFileName = startupConfig.GetString("secondaryIniFileName", secondaryIniFileName);
                }
            }

            if (!oldoptions)
            {
                if (mainIniDirectory != "")
                    basePath = mainIniDirectory;
                if (mainIniFileName != "")
                {
                    if (IsUri(mainIniFileName))
                    {
                        if (!sources.Contains(mainIniFileName))
                            sources.Add(mainIniFileName);
                    }
                    else
                    {
                        string mainIniFilePath = Path.Combine(mainIniDirectory, mainIniFileName);
                        if (!sources.Contains(mainIniFilePath))
                            sources.Add(mainIniFilePath);
                    }
                }

                if (secondaryIniFileName != "")
                {
                    if (IsUri(secondaryIniFileName))
                    {
                        if (!sources.Contains(secondaryIniFileName))
                            sources.Add(secondaryIniFileName);
                    }
                    else
                    {
                        string secondaryIniFilePath = Path.Combine(mainIniDirectory, secondaryIniFileName);
                        if (!sources.Contains(secondaryIniFilePath))
                            sources.Add(secondaryIniFilePath);
                    }
                }
            }

            IConfigSource m_config = new IniConfigSource();
            IConfigSource m_fakeconfig = new IniConfigSource();

            if (sources.Count == 0)
            {
                Console.WriteLine(string.Format("[CONFIG]: Could not load any configuration"));
                Console.WriteLine(
                    string.Format("[CONFIG]: Did you copy the " + defaultIniFile + ".example file to " + defaultIniFile +
                                  "?"));
                throw new NotSupportedException();
            }

            List<string> triedPaths = new List<string>();
            for (int i = 0; i < sources.Count; i++)
            {
                //Read all non .example files first, then read all the example ones

                if (File.Exists(sources[i]) &&
                    ReadConfig(sources[i], i, m_fakeconfig))
                    iniFileExists = true;
                else if (File.Exists(sources[i] + ".example") &&
                         ReadConfig(sources[i] + ".example", i, m_fakeconfig))
                    iniFileExists = true;
                AddIncludes(sources, basePath, ref i, ref triedPaths, m_fakeconfig);
            }

            //
            sources.Reverse();
            for (int i = 0; i < sources.Count; i++)
            {
                //Read all non .example files first, then read all the example ones

                if (File.Exists(sources[i]))
                    ReadConfig(sources[i], i, m_config);
                else if (File.Exists(sources[i] + ".example"))
                    ReadConfig(sources[i] + ".example", i, m_config);
            }

            // add override paramteres if they exist ONLY for standalone operation
            if (! mainIniFileName.Contains("GridServer"))
            {
                string  worldIniFilePath = Path.Combine(mainIniDirectory, worldIniFileName);
                if (File.Exists(worldIniFilePath))
                    ReadConfig(worldIniFilePath, 0, m_config);
            }

            FixDefines(ref m_config);

            if (!iniFileExists)
            {
                Console.WriteLine(string.Format("[CONFIG]: Could not load any configuration"));
                Console.WriteLine(string.Format("[CONFIG]: .. or Configuration possibly exists, but there was an error loading it!"));
                Console.WriteLine(string.Format("[CONFIG]: Configuration : " + mainIniDirectory+", "+mainIniFileName));
                throw new NotSupportedException();
            }
            // Make sure command line options take precedence
            if (argvSource != null)
                m_config.Merge(argvSource);

            return m_config;
        }
Exemple #7
0
        static int Main(string[] args)
        {
            ResultCode resultCode = 0;

            // use a configuration file to list all the sites you want to check
            // provide SMTP server information too

            var version = Assembly.GetEntryAssembly().GetName().Version;
            string startup = "LINKCHECKER " + version + " (c) Ian Mercer 2010 http://blog.abodit.com";

            Console.WriteLine(startup);
            Console.WriteLine("".PadLeft(startup.Length, '-'));
            Console.WriteLine();

            if (!File.Exists("LinkChecker.ini"))
            {
                Console.WriteLine("Please reinstall, you need both the .EXE and the .INI file");
                Console.WriteLine("See http://blog.abodit.com");
            }

            IConfigSource mainSource = new IniConfigSource("LinkChecker.ini");

            ArgvConfigSource argSource = new ArgvConfigSource(args);
            //argSource.AddSwitch("SMTP", "Server", "smtp");
            //argSource.AddSwitch("SMTP", "Port", "port");
            //argSource.AddSwitch("SMTP", "Username", "user");
            //argSource.AddSwitch("SMTP", "Password", "password");

            argSource.AddSwitch("Settings", "Domain", "d");                // a single domain

            argSource.AddSwitch("Settings", "Delay", "de");             //

            argSource.AddSwitch("Settings", "IncludeSelectors", "is");     // Exclude pages option
            argSource.AddSwitch("Settings", "ExcludeSelectors", "xs");     // Exclude pages option

            argSource.AddSwitch("Settings", "ExcludePages", "xp");         // Exclude pages option
            argSource.AddSwitch("Settings", "All", "a");                 //
            argSource.AddSwitch("Settings", "Pages", "pa");             //
            argSource.AddSwitch("Settings", "Seo", "s");                 //
            argSource.AddSwitch("Settings", "Changes", "c");         //
            argSource.AddSwitch("Settings", "Limit", "li");             //
            argSource.AddSwitch("Settings", "Dump", "du");               //  dump all text found
            argSource.AddSwitch("Settings", "Suggestions", "su");

            mainSource.Merge(argSource);

            //var SMTPConfig = mainSource.Configs["SMTP"];
            var config = mainSource.Configs["Settings"];

            //string SMTPServer = SMTPConfig.GetString("Server", "");
            //int SMTPPort = SMTPConfig.GetInt("Port", 25);
            //string SMTPUsername = SMTPConfig.GetString("Username", "");
            //string SMTPPassword = SMTPConfig.GetString("Password", "");

            //SMTPConfig.Set("Server", SMTPServer);
            //SMTPConfig.Set("Port", SMTPPort);
            //SMTPConfig.Set("Username", SMTPUsername);
            //SMTPConfig.Set("Password", SMTPPassword);

            int delayBetween = config.GetInt("Delay", 10);
            config.Set("Delay", delayBetween);

            string excludedPathSetting = config.GetString("ExcludePages", ""); //"");       // common duplicate pages / comments
            string excludedPaths = excludedPathSetting;
            config.Set("ExcludePages", excludedPaths);

            string includeSelector = config.GetString("IncludeSelectors", "head title,body");
            string excludeSelector = config.GetString("ExcludeSelectors", ".pageHeader,#header,.header,#footer,.footer,#sidebar,.sidebar," +                                                                        "#feedbackTab,#feedback,.feedback,#feedbackdialog," +
                                            "#smalltype,.share-links-footer,block-ec_blogs");
            config.Set("IncludeSelector", includeSelector);
            config.Set("ExcludeSelector", excludeSelector);

            string listPages = config.GetString("Pages", "").ToLower();
            if (listPages != "none" && listPages != "list") listPages = "error";
            config.Set("Pages", listPages);

            string seo = config.GetString("Seo", "").ToLower();
            if (seo != "none" && seo != "list") seo = "error";
            config.Set("Seo", seo);

            string changes = config.GetString("Changes", "").ToLower();
            if (changes != "none" && changes != "error") changes = "list";
            config.Set("Changes", changes);

            int limit = config.GetInt("Limit", 3000);               // 3000 pages limit by default

            string dumpFilePath = config.GetString("Dump");
            if (dumpFilePath != null)
                config.Set("Dump", dumpFilePath);

            bool showSuggestedLinks = !string.IsNullOrWhiteSpace(config.Get("Suggestions", ""));
            config.Set("Suggestions", showSuggestedLinks);

            string domainSingle = config.GetString("Domain", "");

            // Save any changes back to the config file
            // Don't do this because then they affect everyone after that ...
            //mainSource.Save();

            if (string.IsNullOrWhiteSpace(domainSingle))
            {
                Console.WriteLine("  Usage:    linkchecker -d example.com");
                Console.WriteLine("  Parameters:");
                Console.WriteLine("     -d:example.com               The domain or starting Url you wish to check");
                Console.WriteLine("                                      all pages at the same level or below will be scanned");
                Console.WriteLine("       -xp:path1,path2            Exclude any paths that include path1, path2, ...");
                Console.WriteLine("                                      e.g. -xp:comment,/recommend,/email,/print");
                Console.WriteLine();
                Console.WriteLine("     -all                         Complete dump including pages, changes and errors (default)");
                Console.WriteLine("       -pages:none|list|error       Detailed information about page and links (default = error)");
                Console.WriteLine("       -seo:none|list|error         Information about any SEO issues (default = error)");
                Console.WriteLine("       -changes:none|list|error     Changes to pages or links since last run (default=list)");
                Console.WriteLine();
                Console.WriteLine("     -delay 10                    Delay 10 seconds between pages (less load on server being tested)");
                Console.WriteLine("     -limit 3000                  Limit how many pages (3000 default)");
                Console.WriteLine();
                Console.WriteLine("     -dump filename               Dump all content from pages to a file");
                Console.WriteLine("       -xs:domSelector2,...         Exclude DOM elements that match jQuery style selectors");
                Console.WriteLine("       -is:domSelector1,...         Include DOM elements that match jQuery style selectors");
                Console.WriteLine("                                       e.g. -is:head title,body -xs:footer,#header,#sidebar,.comment");
                Console.WriteLine();
                Console.WriteLine("                                    -xs and -is are applied throughout the DOM tree to select elements");
                Console.WriteLine();
                Console.WriteLine("     You can put <meta linkedpages=\"\\url,\\url2,...\"> on any page to check that those links");
                Console.WriteLine("     are present on the page somewhere.  In effect a 'link contract' that the page must meet.");
                Console.WriteLine("     This allows you to check that none of the key links on your site are broken.");
                Console.WriteLine();
                Console.WriteLine("     -suggestions                 Lists all the links that are on a page but not in a linkedpages meta tag");
                Console.WriteLine("                                    e.g. <meta linkedpages=\"\\login,\\logout,\\home\">");
                Console.WriteLine();

                Console.WriteLine();

                Console.WriteLine("     RESULT CODE is non-zero if there are any errors and you specified error on any element above");

                //Console.WriteLine("     -smtp    An SMTP server to email the results to");
                //Console.WriteLine("     -port    SMTP Server port (25)");
                //Console.WriteLine("     -user    SMTP Server user name");
                //Console.WriteLine("     -password SMTP Server password");
                Console.WriteLine();
                Console.WriteLine("  Settings may also be placed in a file LinkChecker.ini");
                Console.WriteLine();
                Console.WriteLine("  An XML dump of your web site will be placed in a subdirectory and used for subsequent runs as a comparison");
                Console.WriteLine("  to alert you when new pages appear or links get broken between pages on your site.");
                Console.WriteLine();
                return 1;
            }

            int failed = 0;

            Console.WriteLine("Domain = " + (domainSingle ?? "null"));

            // Display the config settings to the user
            //if (!string.IsNullOrWhiteSpace(SMTPServer))
            //    Console.WriteLine("SMTP Server " + SMTPServer);

            string cleanDomain = domainSingle.Replace("http://", "");
            Uri urlRoot = new Uri("http://" + cleanDomain);

            string directoryPath = System.IO.Path.Combine(Environment.CurrentDirectory, SafeFileName(cleanDomain));
            if (!System.IO.Directory.Exists(directoryPath))
                System.IO.Directory.CreateDirectory(directoryPath);

            string fileName = DateTime.Now.ToString("yyyy-MM-dd-HH;mm") + ".xml";
            string filePath = Path.Combine(directoryPath, fileName);

            // A web site or part of a web site, e.g. www.microsoft.com/presspass
            WebSite webSite = new WebSite(cleanDomain, delayBetween*1000)
                .Except(excludedPaths);

            // Collect error messages
            List<string> errorMessages = new List<string>();

            // Collect SEO warnings
            List<string> seoWarnings = new List<string>();

            // Look for duplicate titles
            Dictionary<string, int> titleCounts = new Dictionary<string, int>();

            Console.WriteLine(" _______________________________________________________________________________________ ");

            int countdown = limit;

            foreach (var webPage in webSite.WebPages)
            {
                // Count only errors and good pages ...
                if ((!(webPage is WebPage.External) && !(webPage is WebPage.OtherContent)) && countdown-- < 0)
                {
                    Console.WriteLine("Limit reached, use something more than  -limit " + limit + "");
                    break;
                }

                Uri url = webPage.Url;

                if (webPage is WebPage.Internal)
                {
                    WebPage.Internal webPageInternal = webPage as WebPage.Internal;
                    if (listPages != "none")
                    {
                        Console.WriteLine(url);
                        Console.WriteLine("  title       = " + webPageInternal.Title);
                        Console.WriteLine("  description = " + webPageInternal.MetaDescription);

                        foreach (var message in webPageInternal.ErrorMessages)
                        {
                            string err = "  ERROR ON PAGE = " + message;
                            Console.WriteLine(err);
                        }
                        errorMessages.AddRange(webPageInternal.ErrorMessages);

                        foreach (var message in webPageInternal.SeoWarnings)
                        {
                            string err = "  WARNING = " + message;
                            Console.WriteLine(err);
                        }
                    }

                    errorMessages.AddRange(webPageInternal.ErrorMessages);
                    seoWarnings.AddRange(webPageInternal.SeoWarnings);

                    // Check for duplicates too
                    string title = webPageInternal.Title ?? "";

                    if (titleCounts.ContainsKey(title))
                        titleCounts[title]++;
                    else
                        titleCounts.Add(title, 1);

                    if (!string.IsNullOrWhiteSpace(dumpFilePath))
                    {
                        var documentNode = webPageInternal.HtmlDocument.DocumentNode;

                        //<td class="itm" style="text-align:left" nowrap><a href="/ESIG">ESIG</a></td>
                        //<td class="dsc">Engineering Science Interactive Graphics</td>

                        var includeSelected =  documentNode.QuerySelectorAll(includeSelector).ToList();
                        var excludeSelected = documentNode.QuerySelectorAll(excludeSelector).ToList();

                        Func<HtmlNode, bool> includeFilter = node => includeSelected.Contains(node);
                        Func<HtmlNode, bool> excludeFilter = node => excludeSelected.Contains(node);

                        using (FileStream file = File.Open(dumpFilePath, FileMode.Append, FileAccess.Write, FileShare.Read))
                        using (StreamWriter sw = new StreamWriter(file))
                        {
                            sw.WriteLine(url);
                            sw.WriteLine("  title       = " + webPageInternal.Title);
                            sw.WriteLine("  description = " + webPageInternal.MetaDescription);

                            var selected = documentNode.InnerTextButJustTheTextBits(includeFilter, excludeFilter, false);
                            var lines = selected.Split('\n', '\r')
                                                .Select(s => s.Replace('\t', ' '))
                                                .Where(s => !string.IsNullOrWhiteSpace(s) && s.Length > 1);           // ignore single character lines
                            foreach (var line in lines)
                                sw.WriteLine(line.Trim());

                            //var lines = webPageInternal.HtmlDocument.DocumentNode.InnerTextButJustTheTextBits(filterPredicate).Split('\n', '\r')
                            //                            .Select(s => s.Replace('\t', ' '))
                            //                            .Where(s => !string.IsNullOrWhiteSpace(s) && s.Length>1);           // ignore single character lines
                            //foreach (var line in lines)
                            //    sw.WriteLine(line.Trim());

                            sw.WriteLine(" _______________________________________________________________________________________ ");
                        }
                    }

                }
                else if (webPage is WebPage.External || webPage is WebPage.OtherContent)
                {
                    if (listPages != "none")
                    {
                        Console.WriteLine(webPage.ToString());
                    }
                }
                else if (webPage is WebPage.Error)
                {
                    Console.WriteLine(webPage.ToString());
                    failed++;
                    errorMessages.Add(webPage.ToString());
                }
                else
                {
                    throw new ApplicationException("Unexpected type " + webPage);
                }

                //if (showSuggestedLinks)
                //foreach (var suggestion in webPageInternal.SuggestedLinks)
                //{
                //    Console.WriteLine(@"Consider adding [LinkedPage(""" + suggestion + @""")]");
                //}

                //int count = requiredLinks.Count(x => x.Value > 0);

                //if (count == 0 && requiredLinks.Count > 0)
                //{
                //    Console.WriteLine("  +++ all required links found on page");
                //}
                //else
                //{
                //    foreach (var missingLink in requiredLinks.Where(x => x.Value > 0))
                //    {
                //        Console.WriteLine("  **** MISSING LINK FROM " + url + " to " + missingLink);
                //        failed++;
                //    }
                //}

                if (listPages != "none")
                    Console.WriteLine("|---------------------------------------------------------------------------------------|");

            }

            // Save the results ...

            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;
            //xmlWriterSettings.CheckCharacters = false;
            xmlWriterSettings.NewLineHandling = NewLineHandling.Entitize;

            // Serialize to disk
            DataContractSerializer serializer = new DataContractSerializer(typeof(WebSite), new[]
                    {typeof(WebPage)
                    , typeof(WebPage.Error)
                    , typeof(WebPage.ExceptionError)
                    , typeof(WebPage.External)
                    , typeof(WebPage.Internal)
                    , typeof(WebPage.LoadError)
                    , typeof(WebPage.OtherContent)
                    , typeof(WebPage.Timeout)
                    , typeof(System.UriFormatException)             // ick!
                    ,typeof(WebLink)

                    });

            // Now compare that against the previous version
            var lastFile = Directory.EnumerateFiles(directoryPath).Select(f => new FileInfo(f)).OrderByDescending(d => d.CreationTimeUtc).FirstOrDefault();

            WebSite previous = null;
            // Read the previous last file in
            if (lastFile != null)
            {
                try
                {
                    using (XmlReader xmlReader = XmlReader.Create(new FileStream(lastFile.FullName, FileMode.Open, FileAccess.Read)))
                    {
                        previous = (WebSite)serializer.ReadObject(xmlReader);
                    }
                }
                catch (System.Runtime.Serialization.SerializationException ex)  // the one we expect
                {
                    previous = null;
                    Console.WriteLine(ex);
                    resultCode = resultCode | ResultCode.PROGRAM_ERROR;
                }
                catch (Exception ex)        // and for now, catch all others ...
                {
                    previous = null;
                    Console.WriteLine(ex);
                    resultCode = resultCode | ResultCode.PROGRAM_ERROR;
                }
            }

            if (changes != "none")
            {
                Console.WriteLine(@"|------------------------------------COMPARISON-----------------------------------------|");

                bool writeXML = true; // unless ...

                try
                {
                    if (previous != null)
                    {
                        var changeLines = previous.StructureCompare(0, webSite).ToArray();

                        if (changeLines.Any())
                        {
                            resultCode = resultCode | ResultCode.CHANGE_DETECTED;
                            foreach (var s in changeLines)
                            {
                                Console.WriteLine(s);
                            }
                        }
                        else
                        {
                            writeXML = false;
                            Console.WriteLine("NO CHANGES DETECTED SINCE " + lastFile.CreationTime.ToShortDateString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Please report: Error comparing " + ex.ToString());
                }

                if (writeXML)
                {
                    try
                    {
                        // And write the updates to a new file
                        using (XmlWriter xmlWriter = XmlWriter.Create(new FileStream(filePath, FileMode.CreateNew), xmlWriterSettings))
                        {
                            serializer.WriteObject(xmlWriter, webSite);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Failed to write file " + ex.Message);
                    }
                }
            }

            if (errorMessages.Count > 0)
            {
                resultCode = resultCode | ResultCode.PAGE_ERROR;

                Console.WriteLine(@"|-------------------------------------ERRORS " + errorMessages.Count() + "-----------------------------------------|");
                foreach (var err in errorMessages)
                {
                    Console.WriteLine(err);
                }
            }
            else
            {
                Console.WriteLine("No errors");
            }

            // Find duplicate page titles
            var duplicatePageTitles = titleCounts.Where(x => x.Value > 1).ToList();
            foreach (var duplicatePageTitle in duplicatePageTitles)
            {
                seoWarnings.Add("Duplicate title '" + duplicatePageTitle.Key + "' on " + duplicatePageTitle.Value + " pages");
            }

            if (seo != "none")
            {
                if (seoWarnings.Any())
                {
                    if (seo == "error")
                        resultCode = resultCode | ResultCode.SEO_ERROR;

                    Console.WriteLine(@"|------------------------------------SEO WARNINGS (" + seoWarnings.Count() + ")------------------------------------|");
                    foreach (var err in seoWarnings)
                    {
                        Console.WriteLine(err);
                    }
                }
            }

            Console.WriteLine(@"\_______________________________________________________________________________________/");
            Console.WriteLine();

            //Console.ReadKey();

            // Now decide whether to fail or not ...

            if (resultCode > 0)
            {
                Console.WriteLine("**** FAILED WITH ERROR CODE ****");
                if ((resultCode & ResultCode.CHANGE_DETECTED) != 0) Console.WriteLine("  Changes detected");
                if ((resultCode & ResultCode.LINK_ERROR) != 0) Console.WriteLine("  Link error");
                if ((resultCode & ResultCode.PAGE_ERROR) != 0) Console.WriteLine("  Page error");
                if ((resultCode & ResultCode.PROGRAM_ERROR) != 0) Console.WriteLine("  Program error");
                if ((resultCode & ResultCode.SEO_ERROR) != 0) Console.WriteLine("  SEO error");
            }

            if (Environment.MachineName == "XPS")
                Thread.Sleep(20000);

            return (int)resultCode;
        }
        public void ExpandKeyValuesMerge()
        {
            StringWriter writer = new StringWriter ();
            writer.WriteLine ("[web]");
            writer.WriteLine (" protocol = http");
            writer.WriteLine ("[server]");
            writer.WriteLine (" domain1 = ${web|protocol}://nini.sf.net/");
            IniConfigSource source = new IniConfigSource
                                    (new StringReader (writer.ToString ()));

            StringWriter newWriter = new StringWriter ();
            newWriter.WriteLine ("[web]");
            newWriter.WriteLine (" apache = Apache implements ${protocol}");
            newWriter.WriteLine ("[server]");
            newWriter.WriteLine (" domain2 = ${web|protocol}://nini.sf.net/");
            IniConfigSource newSource = new IniConfigSource
                                    (new StringReader (newWriter.ToString ()));
            source.Merge (newSource);
            source.ExpandKeyValues ();

            IConfig config = source.Configs["web"];
            Assert.AreEqual ("http", config.Get ("protocol"));
            Assert.AreEqual ("Apache implements http", config.Get ("apache"));
            config = source.Configs["server"];
            Assert.AreEqual ("http://nini.sf.net/", config.Get ("domain1"));
            Assert.AreEqual ("http://nini.sf.net/", config.Get ("domain2"));
        }
Exemple #9
0
        /// <summary>
        /// Load in all the configuration values from the XMRM section of various configuration sources.
        /// 
        /// The order is:
        /// 1 - the Opensim application config file.
        /// 2 - opensim.ini
        /// 3 - The parameters passed in in the body of the text are parsed
        /// [4] - any application config file pointed to by one of the previous 3 configs.
        /// 
        /// Each set of config values overrides the previous so if "ShadowCopy" is set to "true" in the opensim config file, opensim.ini and
        /// in the script but "false" in the config file the final value will be false.
        /// </summary>
        /// <param name="script">The script to parse for information.</param>
        /// <returns>A config object with the values taken from the script.</returns>
        private IConfig LoadConfigValues(out string[] scriptArguments)
        {
            //Load in the arguments as command line arguments
            CommandLineConfig argConfig = new CommandLineConfig(ScriptText, true, "\n", " ");

            argConfig.AddSetting(XMRM, CONFIG_FILE, "f");
            argConfig.AddSetting(XMRM, CONFIG_FILE, "F");

            argConfig.AddSetting(XMRM, ASSEMBLY, "a");
            argConfig.AddSetting(XMRM, ASSEMBLY, "A");

            argConfig.AddSetting(XMRM, CLASS, "c");
            argConfig.AddSetting(XMRM, CLASS, "C");

            argConfig.AddSetting(XMRM, BASE_FOLDER, "b");
            argConfig.AddSetting(XMRM, BASE_FOLDER, "B");

            argConfig.AddFlag(XMRM, SHADOW_COPY, "s", false);
            argConfig.AddFlag(XMRM, SHADOW_COPY, "S", false);

            argConfig.AddFlag(XMRM, GOD, "g", false);
            argConfig.AddFlag(XMRM, GOD, "G", false);

            scriptArguments = argConfig.Argument.Length == 0 ? new string[0] : argConfig.Argument.Split(' ', '\n');

            //Merge the three guaranteed config sources
            IConfigSource config = new IniConfigSource();
            config.Merge(m_appConfig);
            config.Merge(m_config);
            config.Merge(argConfig);

            IConfig mrmConfig = config.Configs[XMRM];

            //If a config file is specified merge those values in
            if (mrmConfig.Contains(CONFIG_FILE)) {
                try {
                    IConfigSource fileSource = new DotNetConfigSource(mrmConfig.Get(CONFIG_FILE));
                    config.Merge(fileSource);
                } catch (Exception e) {
                    ErrorString = ("Unable to load config file from '" + mrmConfig.Get(CONFIG_FILE) + "'. " + e.Message);
                }
            }
            return config.Configs[XMRM];
        }
Exemple #10
0
        private static IniConfigSource LoadConfig(string location)
        {
            IniConfigSource currentConfig = new IniConfigSource();
            List<string> currentConfigLines = new List<string>();
            string[] configLines = null;

            if (IsUrl(location))
            {
                // Web-based loading
                string responseStr;
                if (WebUtil.TryGetUrl(location, out responseStr))
                {
                    configLines = responseStr.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    m_log.Error("Failed to load web config file " + location + ": " + responseStr);
                }
            }
            else
            {
                // Local file loading
                try
                {
                    configLines = new List<string>(File.ReadAllLines(location)).ToArray();
                }
                catch (Exception ex)
                {
                    m_log.Error("Failed to load config file " + location + ": " + ex.Message);
                }
            }

            if (configLines != null)
            {
                for (int i = 0; i < configLines.Length; i++)
                {
                    string line = configLines[i].Trim();

                    if (line.StartsWith("Include "))
                    {
                        // Compile the current config lines, compile the included config file, and combine them
                        currentConfig.Merge(CompileConfig(currentConfigLines));
                        currentConfigLines.Clear();

                        // Compile the included config file
                        string includeFilename = GetConfigPath(line.Substring(8).Trim());
                        IniConfigSource includeConfig = LoadConfig(includeFilename);

                        // Merge the included config with the curent config
                        currentConfig.Merge(includeConfig);
                    }
                    else if (!String.IsNullOrEmpty(line) && !line.StartsWith(";"))
                    {
                        currentConfigLines.Add(line);
                    }
                }

                currentConfig.Merge(CompileConfig(currentConfigLines));
            }

            return currentConfig;
        }
Exemple #11
0
        /// <summary>
        /// Applies any migrational changes from older to newer versions
        /// </summary>
        void migrate()
        {
            var newConfigSource = new IniConfigSource();
            newConfigSource.Merge(UserSettings);

            // For when user setting names were forced to lowercase
            foreach (IConfig config in newConfigSource.Configs)
                UserSettings.Configs[config.Name].Name = config.Name.ToLower();
        }
Exemple #12
0
        /*
        public static Form StartGui(IConfig config, ViewerProxy form, Func<Form> createForm) {
            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) {
                Form f = createForm();
                StartProxyClient(config, form, f);
                f.ShowDialog();
                return f;
            } else {
                object createLock = new object();
                Form f = null;
                bool started = false;
                Thread t = new Thread(() => {
                    f = createForm();
                    lock (createLock)
                        MonitorChanged.PulseAll(createLock);
                    started = true;
                    StartProxyClient(config, form, f);
                    f.ShowDialog();
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Begin();
                if (!started)
                    lock (createLock)
                        MonitorChanged.Wait(createLock);
                return f;
            }
        }

        private static void StartProxyClient(IConfig config, ViewerProxy form, Form f) {
            f.VisibleChanged += (source, args) => {
                if (!f.Visible)
                    return;
                bool autostartProxy = Get(config, "AutoStartProxy", false);
                bool autostartClient = Get(config, "AutoStartClient", false);

                if (autostartProxy || autostartClient)
                    while (!form.StartProxy())
                        form.ProxyConfig.ProxyPort++;
                if (autostartClient)
                    form.StartClient();
            };
        }
        */
        public static IConfigSource AddFile(IConfigSource config, string file)
        {
            if (File.Exists(file) && Path.GetExtension(file).ToUpper().Equals(".CONFIG")) {
                try {
                    DotNetConfigSource dotnet = new DotNetConfigSource(file);
                    //config.Merge(dotnet);
                    dotnet.Merge(config);
                    return dotnet;
                } catch (Exception e) {
                    Logger.Warn("Unable to load app configuration file " + file + "'." + e.Message + ".\n" + e.StackTrace);
                }
            } else if (File.Exists(file) && Path.GetExtension(file).ToUpper().Equals(".INI")) {
                try {
                    IniDocument doc = new IniDocument(file, IniFileType.WindowsStyle);
                    IniConfigSource ini = new IniConfigSource(doc);
                    //config.Merge(ini);
                    ini.Merge(config);
                    return ini;
                } catch (Exception e) {
                    Logger.Warn("Unable to load ini configuration file " + file + "'." + e.Message + ".\n" + e.StackTrace);
                }
            }
            return config;
        }