Exemplo n.º 1
0
        /// <summary>
        /// Save the dataset to a CSV file.
        /// </summary>
        /// <param name="targetFile">The target file.</param>
        /// <param name="format">The format to use.</param>
        /// <param name="set">The data set.</param>
        public static void SaveCSV(FileInfo targetFile, CSVFormat format, IMLDataSet set)
        {
            try
            {
                var file = new StreamWriter(targetFile.ToString());

                foreach (IMLDataPair data in set)
                {
                    var line = new StringBuilder();

                    for (int i = 0; i < data.Input.Count; i++)
                    {
                        double d = data.Input[i];
                        BasicFile.AppendSeparator(line, format);
                        line.Append(format.Format(d, EncogFramework.DefaultPrecision));
                    }

                    for (int i = 0; i < data.Ideal.Count; i++)
                    {
                        double d = data.Ideal[i];
                        BasicFile.AppendSeparator(line, format);
                        line.Append(format.Format(d, EncogFramework.DefaultPrecision));
                    }

                    file.WriteLine(line);
                }

                file.Close();
            }
            catch (IOException ex)
            {
                throw new EncogError(ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Replaces the content with the new js/css paths
        /// </summary>
        /// <param name="html"></param>
        /// <param name="namedGroup"></param>
        /// <param name="extensions"></param>
        /// <param name="type"></param>
        /// <param name="regex"></param>
        /// <param name="http"></param>
        /// <returns></returns>
        /// <remarks>
        /// For some reason ampersands that aren't html escaped are not compliant to HTML standards when they exist in 'link' or 'script' tags in URLs,
        /// we need to replace the ampersands with &amp; . This is only required for this one w3c compliancy, the URL itself is a valid URL.
        /// </remarks>
        private static string ReplaceContent(string html, string namedGroup, string[] extensions,
                                             ClientDependencyType type, string regex, HttpContextBase http)
        {
            html = Regex.Replace(html, regex,
                                 (m) =>
            {
                var grp = m.Groups[namedGroup];

                //if there is no namedGroup group name or it doesn't end with a js/css extension or it's already using the composite handler,
                //the return the existing string.
                if (grp == null ||
                    string.IsNullOrEmpty(grp.ToString()) ||
                    !grp.ToString().EndsWithOneOf(extensions) ||
                    grp.ToString().StartsWith(ClientDependencySettings.Instance.CompositeFileHandlerPath))
                {
                    return(m.ToString());
                }


                //make sure that it's an internal request, though we can deal with external
                //requests, we'll leave that up to the developer to register an external request
                //explicitly if they want to include in the composite scripts.
                try
                {
                    var url = new Uri(grp.ToString(), UriKind.RelativeOrAbsolute);
                    if (!url.IsLocalUri(http))
                    {
                        return(m.ToString());    //not a local uri
                    }
                    else
                    {
                        var dependency = new BasicFile(type)
                        {
                            FilePath = grp.ToString()
                        };

                        var file = new[] { new BasicFile(type)
                                           {
                                               FilePath = dependency.ResolveFilePath(http)
                                           } };

                        var resolved = ClientDependencySettings.Instance.DefaultCompositeFileProcessingProvider.ProcessCompositeList(
                            file,
                            type,
                            http).Single();

                        return(m.ToString().Replace(grp.ToString(), resolved.Replace("&", "&amp;")));
                    }
                }
                catch (UriFormatException)
                {
                    //malformed url, let's exit
                    return(m.ToString());
                }
            },
                                 RegexOptions.Compiled);

            return(html);
        }
Exemplo n.º 3
0
        //инициализация
        public FileJournalStorage(ICommonLogger logger, string fullFileName = null)
        {
            _logger = logger;

            FileInfo journalFileInfo = new FileInfo(fullFileName);
            _basicFile = new BasicFile(journalFileInfo);
            _journal = ReadAllValuesFromFile();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Prepare the output file, write headers if needed.
        /// </summary>
        ///
        /// <param name="outputFile">The name of the output file.</param>
        /// <returns>The output stream for the text file.</returns>
        private StreamWriter AnalystPrepareOutputFile(FileInfo outputFile)
        {
            try
            {
                var tw = new StreamWriter(outputFile.OpenWrite());
                // write headers, if needed
                if (ProduceOutputHeaders)
                {
                    var line = new StringBuilder();

                    // first, handle any input fields
                    if (_inputCount > 0)
                    {
                        for (int i = 0; i < _inputCount; i++)
                        {
                            BasicFile.AppendSeparator(line, Format);
                            line.Append("\"");
                            line.Append("input:" + i);
                            line.Append("\"");
                        }
                    }

                    // now, handle the ideal fields
                    if (_idealCount > 0)
                    {
                        for (int i = 0; i < _idealCount; i++)
                        {
                            BasicFile.AppendSeparator(line, Format);
                            line.Append("\"");
                            line.Append("ideal:" + i);
                            line.Append("\"");
                        }
                    }

                    // now, handle the output fields
                    if (_outputCount > 0)
                    {
                        for (int i = 0; i < _outputCount; i++)
                        {
                            BasicFile.AppendSeparator(line, Format);
                            line.Append("\"");
                            line.Append("output:" + i);
                            line.Append("\"");
                        }
                    }

                    tw.WriteLine(line.ToString());
                }

                return(tw);
            }
            catch (IOException e)
            {
                throw new QuantError(e);
            }
        }
Exemplo n.º 5
0
        public void WhiteSpacePath_Throws()
        {
            var mockHttp = Mock.Of <HttpContextBase>();
            var file     = new BasicFile(ClientDependencyType.Javascript)
            {
                FilePath = "  ",
            };

            Assert.Throws <ArgumentException>(() => file.ResolveFilePath(mockHttp));
        }
Exemplo n.º 6
0
        public void NonCanonicalAbsolutePath_IsCanonicalized()
        {
            var mockHttp = Mock.Of <HttpContextBase>();
            var file     = new BasicFile(ClientDependencyType.Javascript)
            {
                FilePath = "/website/folder/../js.js",
            };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("/website/js.js", resolvedPath);
        }
        public void Schema_Relative_Path()
        {
            var mockHttp = Mock.Of <HttpContextBase>();
            var file     = new BasicFile(ClientDependencyType.Javascript)
            {
                FilePath = "//website/js.js",
            };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("//website/js.js", resolvedPath);
        }
Exemplo n.º 8
0
        public void AbsolutePath_IsUnaltered()
        {
            var mockHttp = Mock.Of <HttpContextBase>();

            var file = new BasicFile(ClientDependencyType.Javascript)
            {
                FilePath = "/file.js",
            };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("/file.js", resolvedPath);
        }
Exemplo n.º 9
0
        public void RelativePath_HasCurrentExecutionPathPrefixed()
        {
            var mockHttp = Mock.Of <HttpContextBase>();

            Mock.Get(mockHttp).DefaultValue = DefaultValue.Mock;
            Mock.Get(mockHttp.Request).Setup(r => r.AppRelativeCurrentExecutionFilePath).Returns("/the/path/page.aspx");

            var file = new BasicFile(ClientDependencyType.Javascript)
            {
                FilePath = "file.js",
            };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("/the/path/file.js", resolvedPath);
        }
Exemplo n.º 10
0
        public void TildePath_IsResolved()
        {
            var mockHttp = Mock.Of <HttpContextBase>();

            Mock.Get(mockHttp).DefaultValue = DefaultValue.Mock;
            Mock.Get(mockHttp.Request).Setup(r => r.ApplicationPath).Returns("/");

            var file = new BasicFile(ClientDependencyType.Javascript)
            {
                FilePath = "~/file.js",
            };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("/file.js", resolvedPath);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Add headings for a raw file.
        /// </summary>
        ///
        /// <param name="line">The line to write the raw headings to.</param>
        /// <param name="prefix">The prefix to place.</param>
        /// <param name="format">The format to use.</param>
        public void AddRawHeadings(StringBuilder line,
                                   String prefix, CSVFormat format)
        {
            int subFields = ColumnsNeeded;

            for (int i = 0; i < subFields; i++)
            {
                String str = CSVHeaders.TagColumn(_name, i,
                                                  _timeSlice, subFields > 1);
                BasicFile.AppendSeparator(line, format);
                line.Append('\"');
                if (prefix != null)
                {
                    line.Append(prefix);
                }
                line.Append(str);
                line.Append('\"');
            }
        }
        /// <summary>
        /// Encode the headers used by this field.
        /// </summary>
        ///
        /// <returns>A string containing a comma separated list with the headers.</returns>
        public String EncodeHeaders()
        {
            var line = new StringBuilder();

            switch (_action)
            {
            case NormalizationAction.SingleField:
                BasicFile.AppendSeparator(line, CSVFormat.EgFormat);
                line.Append('\"');
                line.Append(_name);
                line.Append('\"');
                break;

            case NormalizationAction.Equilateral:
                for (int i = 0; i < _classes.Count - 1; i++)
                {
                    BasicFile.AppendSeparator(line, CSVFormat.EgFormat);
                    line.Append('\"');
                    line.Append(_name);
                    line.Append('-');
                    line.Append(i);
                    line.Append('\"');
                }
                break;

            case NormalizationAction.OneOf:
                for (int i = 0; i < _classes.Count; i++)
                {
                    BasicFile.AppendSeparator(line, CSVFormat.EgFormat);
                    line.Append('\"');
                    line.Append(_name);
                    line.Append('-');
                    line.Append(i);
                    line.Append('\"');
                }
                break;

            default:
                return(null);
            }
            return(line.ToString());
        }
Exemplo n.º 13
0
        internal IEnumerable <BasicFile> GetIncludes(string innerHtml, ClientDependencyType dependencyType)
        {
            string tag, sourceAttribute, mime;

            if (dependencyType == ClientDependencyType.Css)
            {
                tag             = "link";
                sourceAttribute = "href";
                mime            = "text/css";
            }
            else
            {
                tag             = "script";
                sourceAttribute = "src";
                mime            = "text/javascript";
            }

            var tagPattern = string.Format(TagPattern, tag);

            var files = new List <BasicFile>();

            foreach (Match match in Regex.Matches(innerHtml, tagPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
            {
                var allAttributes = Regex.Matches(match.Value, MatchAllAttributes,
                                                  RegexOptions.Compiled | RegexOptions.IgnoreCase |
                                                  RegexOptions.CultureInvariant)
                                    .Cast <Match>()
                                    .ToArray();

                var type = allAttributes.FirstOrDefault(x =>
                {
                    if (x.Groups.Count < 3)
                    {
                        return(false);
                    }
                    return(x.Groups[1].Value == "type");
                });

                var href = allAttributes.FirstOrDefault(x =>
                {
                    if (x.Groups.Count < 3)
                    {
                        return(false);
                    }
                    return(x.Groups[1].Value == sourceAttribute);
                });

                if (type == null || href == null || type.Groups[2].Value != mime)
                {
                    continue;
                }

                var attributes = allAttributes.Where(x =>
                {
                    if (x.Groups.Count < 3)
                    {
                        return(false);
                    }
                    return(x.Groups[1].Value != sourceAttribute && x.Groups[1].Value != "type");
                }).ToDictionary(x => x.Groups[1].Value, x => x.Groups[2].Value);

                var file = new BasicFile(dependencyType)
                {
                    FilePath      = href.Groups[2].Value,
                    Group         = Group,
                    Priority      = Priority,
                    ForceProvider = ForceProvider
                };

                foreach (var a in attributes)
                {
                    file.HtmlAttributes.Add(a.Key, a.Value);
                }

                files.Add(file);
            }
            return(files);
        }
Exemplo n.º 14
0
        internal IEnumerable<BasicFile> GetIncludes(string innerHtml, ClientDependencyType dependencyType)
        {
            string tag, sourceAttribute, mime;
            if (dependencyType == ClientDependencyType.Css)
            {
                tag = "link";
                sourceAttribute = "href";
                mime = "text/css";
            }
            else
            {
                tag = "script";
                sourceAttribute = "src";
                mime = "text/javascript";
            }

            var tagPattern = string.Format(TagPattern, tag);

            var files = new List<BasicFile>();
            foreach (Match match in Regex.Matches(innerHtml, tagPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
            {
                var allAttributes = Regex.Matches(match.Value, MatchAllAttributes,
                                                  RegexOptions.Compiled | RegexOptions.IgnoreCase |
                                                  RegexOptions.CultureInvariant)
                                         .Cast<Match>()
                                         .ToArray();

                var type = allAttributes.FirstOrDefault(x =>
                {
                    if (x.Groups.Count < 3) return false;
                    return x.Groups[1].Value == "type";
                });

                var href = allAttributes.FirstOrDefault(x =>
                {
                    if (x.Groups.Count < 3) return false;
                    return x.Groups[1].Value == sourceAttribute;
                });

                if (type == null || href == null || type.Groups[2].Value != mime) continue;

                var attributes = allAttributes.Where(x =>
                {
                    if (x.Groups.Count < 3) return false;
                    return x.Groups[1].Value != sourceAttribute && x.Groups[1].Value != "type";
                }).ToDictionary(x => x.Groups[1].Value, x => x.Groups[2].Value);

                var file = new BasicFile(dependencyType)
                    {
                        FilePath = href.Groups[2].Value,
                        Group = Group,
                        Priority = Priority,
                        ForceProvider = ForceProvider
                    };

                foreach (var a in attributes)
                {
                    file.HtmlAttributes.Add(a.Key, a.Value);
                }

                files.Add(file);
            }
            return files;
        }