Exemple #1
0
 /// <summary>
 /// Composes the specified <see cref="ExtendedGeoName"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="ExtendedGeoName"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="ExtendedGeoName"/>.</returns>
 public override string Compose(ExtendedGeoName value)
 {
     return(string.Join(FieldSeparator.ToString(), value.Id, value.Name, value.NameASCII, ArrayToValue(value.AlternateNames),
                        value.Latitude.ToString(CultureInfo.InvariantCulture), value.Longitude.ToString(CultureInfo.InvariantCulture), value.FeatureClass, value.FeatureCode, value.CountryCode,
                        ArrayToValue(value.AlternateCountryCodes), value.Admincodes[0], value.Admincodes[1], value.Admincodes[2], value.Admincodes[3],
                        value.Population, value.Elevation, value.Dem, value.Timezone.Replace(" ", "_"), value.ModificationDate.ToString("yyyy-MM-dd")));
 }
Exemple #2
0
            public object CreateInstance(Type propertyType)
            {
                var type = propertyType;

                using (var dialog = new OpenFileDialog()
                {
                    Filter = "CSV files|*.csv|All files|*.*"
                })
                {
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        var dataTable = new DataTable();
                        var csvLines  = File.ReadAllLines(dialog.FileName);
                        dataTable.Columns.AddRange(csvLines.First().Split(FieldSeparator.ToCharArray()).Select(x => new DataColumn(x)).ToArray());

                        var objectInstances = GetObjectInstances(csvLines, dataTable, type).ToArray();
                        var array           = Array.CreateInstance(type, objectInstances.Count());
                        for (int i = 0; i < objectInstances.Count(); i++)
                        {
                            array.SetValue(objectInstances[i], i);
                        }
                        return(array);
                    }
                }
                return(null);
            }
Exemple #3
0
        /// <summary>
        /// Gets the code for a field, starting at the given field start node.
        /// If the field is not well formed, then an exception is thrown.
        /// </summary>
        public static string ParseInlineField(this FieldStart start, out FieldSeparator separator, out string result, out FieldEnd end)
        {
            if (start.NextSibling == null)
                throw new Exception("Found field start with no next sibling.");

            Node lastNode;

            var code = ParseInlineCode(start, out lastNode);

            if (lastNode == null || lastNode == start)
                throw new Exception(string.Format("Didn't find field code in \"{0}\".", GetText(start, code, separator: start.NextSibling as FieldSeparator)));

            separator = lastNode.NextSibling as FieldSeparator;

            if (separator == null)
                throw new Exception(string.Format("Did not find node of type 'FieldSeparator' after \"{0}\".", GetText(start, code)));

            result = ParseInlineResult(start, code, separator, out lastNode);

            end = separator.GetFollowingSiblings().OfType<FieldEnd>().FirstOrDefault();

            if (end == null)
                throw new Exception(string.Format("Did not find node of type 'FieldEnd' after \"{0}\".", GetText(start, code, separator, result)));

            return code;
        }
 /// <summary>
 /// Composes the specified <see cref="ExtendedGeoName"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="ExtendedGeoName"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="ExtendedGeoName"/>.</returns>
 public override string Compose(ExtendedGeoName value)
 {
     return(string.Join(FieldSeparator.ToString(), value.Id, value.Name, value.NameASCII, ArrayToValue(value.AlternateNames),
                        value.Latitude.ToString(CultureInfo.InvariantCulture), value.Longitude.ToString(CultureInfo.InvariantCulture), value.FeatureClass, value.FeatureCode, value.CountryCode,
                        ArrayToValue(value.AlternateCountryCodes), GetArrayValue(value.Admincodes, 0), GetArrayValue(value.Admincodes, 1), GetArrayValue(value.Admincodes, 2), GetArrayValue(value.Admincodes, 3),
                        value.Population, value.Elevation, value.Dem, TimeZoneToString(value.Timezone), DateTimeToString(value.ModificationDate)));
 }
Exemple #5
0
            public static string GetCsvLine(dynamic o, FieldSeparator separator, bool normalize = true)
            {
                List <string> ss = new List <string>();

                foreach (System.Reflection.PropertyInfo pi in o.GetType().GetProperties())
                {
                    if (pi.GetCustomAttribute <FieldPreparation.IgnoredField>() != null)
                    {
                        continue;
                    }
                    string s;
                    object p = pi.GetValue(o);
                    if (pi.PropertyType == typeof(string))
                    {
                        s = (string)p;
                    }
                    else if (p != null)
                    {
                        s = p.ToString();
                    }
                    else
                    {
                        s = null;
                    }
                    ss.Add(GetCsvField(s, separator, normalize));
                }
                return(string.Join(separator.Value, ss));
            }
 /// <summary>
 /// Composes the specified <see cref="Postalcode"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="Postalcode"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="Postalcode"/>.</returns>
 public override string Compose(Postalcode value)
 {
     return(string.Join(FieldSeparator.ToString(), value.CountryCode, value.PostalCode, value.PlaceName,
                        value.AdminName[0], value.AdminCode[0], value.AdminName[1], value.AdminCode[1], value.AdminName[2], value.AdminCode[2],
                        DoubleToString(value.Latitude), DoubleToString(value.Longitude), value.Accuracy
                        ));
 }
Exemple #7
0
 private IEnumerable <object> GetObjectInstances(string[] csvLines, DataTable dataTable, Type type)
 {
     for (int i = 1; i < csvLines.Count(); i++)
     {
         var dataRow = dataTable.Rows.Add(csvLines.ElementAt(i).Split(FieldSeparator.ToCharArray()));
         yield return(GetObjectInstance(type, dataRow));
     }
 }
Exemple #8
0
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
            {
                // Once reached a field separator node, we enable the output because we are
                // now entering the field result nodes.
                mIsSkipText = false;

                return(VisitorAction.Continue);
            }
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
            {
                if (this.isHidden(fieldSeparator))
                {
                    fieldSeparator.Remove();
                }

                return(VisitorAction.Continue);
            }
Exemple #10
0
 private static string GetText(FieldStart start, string code, FieldSeparator separator = null, string result = null, FieldEnd end = null, bool useRawFieldChars = false)
 {
     return string.Format("{0}{1}{2}{3}{4}",
         start != null ? useRawFieldChars ? FieldStart : "{" : "",
         code ?? "",
         separator != null ? useRawFieldChars ? FieldSeparator : "|" : "",
         result ?? "",
         end != null ? useRawFieldChars ? FieldEnd : "}" : "");
 }
        /// <summary>
        /// Create a hierarchical group of nodes from the start of the field.
        /// </summary>
        /// <param name="fieldStart">The node which represents the start of a field.</param>
        public WordField(FieldStart fieldStart)
        {
            // Create the object.
            this.endDictionary   = new Dictionary <Node, WordField>();
            this.startDictionary = new Dictionary <Node, WordField>();
            this.fieldStart      = fieldStart;

            // This will find the end of the field and acts as a kind of delimiter.  Since the DOM structure makes heavy use of linked lists, it can also act
            // to join the end of a field to the start and back again when navigating through the document.
            Boolean parsing = true;
            Node    node    = this.fieldStart.NextSibling;

            while (parsing)
            {
                // This is the field that is going to be parsed out of the stream.
                WordField wordField = default(WordField);

                // Each node is parsed to determine what kind of an object has been discovered here.
                switch (node.NodeType)
                {
                case NodeType.FieldStart:

                    // This will recurse into any of the embedded fields creating a hierarchical order of fields that can be evaluated from the inside out.
                    wordField = CreateField(node as FieldStart);

                    // Any field found in the field field result area is ignored.  All other fields are added to a collection that can be indexed sequentially
                    // using an enumerator or directly using the dictionary.
                    this.startDictionary.Add(wordField.FieldStart, wordField);
                    this.endDictionary.Add(wordField.FieldEnd, wordField);

                    // This moves the parser to the position after the field no matter how many levels of embedded fields were included.
                    node = wordField.FieldEnd;

                    break;

                case NodeType.FieldSeparator:

                    // This indicates where the literal portion of a field starts.
                    this.fieldSeperator = node as FieldSeparator;

                    break;

                case NodeType.FieldEnd:

                    // The end of the field is part of this obect and used when navigating forward or backward through the nodes of a paragraph.
                    this.fieldEnd = node as FieldEnd;

                    // When the final field is found the parsing of this field is completed.
                    parsing = false;

                    break;
                }

                // The parsing continues until the last field node is discovered.
                node = node.NextSibling;
            }
        }
Exemple #12
0
        public static string GetCsvHeaderLine(Type t, FieldSeparator separator, bool normalize = true)
        {
            List <string> ss = new List <string>();

            foreach (System.Reflection.PropertyInfo pi in t.GetProperties())
            {
                ss.Add(GetCsvField(pi.Name, separator, normalize));
            }
            return(string.Join(separator.Value, ss));
        }
Exemple #13
0
        public static string GetCsvHeaderLine(IEnumerable <string> headers, FieldSeparator separator, bool normalize = true)
        {
            List <string> ss = new List <string>();

            foreach (string h in headers)
            {
                ss.Add(GetCsvField(h, separator, normalize));
            }
            return(string.Join(separator.Value, ss));
        }
Exemple #14
0
 /// <summary>
 /// Composes the specified <see cref="GeoName"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="GeoName"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="GeoName"/>.</returns>
 public override string Compose(GeoName value)
 {
     if (UseExtendedFileFormat)
     {
         return(string.Join(FieldSeparator.ToString(), value.Id, value.Name, null, null, DoubleToString(value.Latitude), DoubleToString(value.Longitude),
                            null, null, null, null, null, null, null, null, null, null, null, null, null));
     }
     else
     {
         return(string.Join(FieldSeparator.ToString(), value.Id, value.Name, DoubleToString(value.Latitude), DoubleToString(value.Longitude)));
     }
 }
        public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
        {
            if (this.skipFieldSeparator)
            {
                this.skipFieldSeparator = false;
            }
            else
            {
                this.structureBuilder.AppendLine("<FieldSeparator />");
            }

            return(VisitorAction.Continue);
        }
        public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
        {
            // When visiting a field separator we should decrease the depth level.
            if (fieldSeparator.FieldType.Equals(mTargetFieldType))
            {
                mFieldDepth--;
                fieldSeparator.Remove();
            }
            else
            {
                // This removes the field separator if it's inside a field that is being converted.
                CheckDepthAndRemoveNode(fieldSeparator);
            }

            return VisitorAction.Continue;
        }
        public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
        {
            // When visiting a field separator we should decrease the depth level.
            if (fieldSeparator.FieldType.Equals(mTargetFieldType))
            {
                mFieldDepth--;
                fieldSeparator.Remove();
            }
            else
            {
                // This removes the field separator if it's inside a field that is being converted.
                CheckDepthAndRemoveNode(fieldSeparator);
            }

            return(VisitorAction.Continue);
        }
Exemple #18
0
 public static string GetCsvField(string value, FieldSeparator separator, bool normalize = true)
 {
     if (value == null)
     {
         return("");
     }
     if (normalize)
     {
         value = Normalize(value);
     }
     value = Regex.Replace(value, "\"", "\"\"", RegexOptions.Compiled | RegexOptions.Singleline);
     if (Regex.IsMatch(value, separator.Value, RegexOptions.Compiled | RegexOptions.Singleline))
     {
         value = "\"" + value + "\"";
     }
     return(value);
 }
Exemple #19
0
        public string ToString(RecordSeparator rs, FieldSeparator fs)
        {
            if (this.Count == 0)
            {
                return("");
            }

            string rsStr  = GetRecordSeparator(rs);
            string fsStr  = GetFieldSeparator(fs);
            int    length = this.Count;

            if (this.MEAny)
            {
                length++;
            }


            string[] rep      = new string[length];
            int      position = 0;

            // L'erreur de mesure
            if (this.MEAny)
            {
                rep[position] = string.Format("{0}({1}, {2})",
                                              ME.ThroughCV ? "CV" : "SD",
                                              this.ME.Minimum,
                                              this.ME.Maximum);
                position++;
            }

            StringBuilder sb = new StringBuilder(measuresList[0].ToString());

            for (int i = 0; i < measuresList.Count; i++)
            {
                rep[position] = measuresList[i].ToString();
                position++;
            }

            return(string.Join(rsStr, rep));
        }
        /// <summary>
        /// Processes each record.
        /// </summary>
        protected override void ProcessRecord()
        {
            var testSuite = new TestSuite();

            var actions = new List <ICsvProfileAction>();


            if (MyInvocation.BoundParameters.ContainsKey("FieldSeparator"))
            {
                actions.Add(new FieldSeparatorAction(FieldSeparator.ToCharArray()[0]));
            }
            if (MyInvocation.BoundParameters.ContainsKey("TextQualifier"))
            {
                actions.Add(new TextQualifierAction(TextQualifier.ToCharArray()[0]));
            }
            if (MyInvocation.BoundParameters.ContainsKey("RecordSeparator"))
            {
                actions.Add(new RecordSeparatorAction(RecordSeparator));
            }
            if (MyInvocation.BoundParameters.ContainsKey("FirstRowHeader"))
            {
                actions.Add(new FirstRowHeaderAction(FirstRowHeader.ToBool()));
            }
            if (MyInvocation.BoundParameters.ContainsKey("EmptyCell"))
            {
                actions.Add(new EmptyCellAction(EmptyCell));
            }
            if (MyInvocation.BoundParameters.ContainsKey("MissingCell"))
            {
                actions.Add(new MissingCellAction(MissingCell));
            }

            foreach (var action in actions)
            {
                action.Execute(testSuite);
                WriteVerbose(action.Display);
            }
            WriteObject(testSuite.Settings.CsvProfile);
        }
Exemple #21
0
        public static string GetCsvLine(IEnumerable <object> values, FieldSeparator separator, bool normalize = true)
        {
            List <string> ss = new List <string>();

            foreach (object v in values)
            {
                string s;
                if (v is string)
                {
                    s = (string)v;
                }
                else if (v != null)
                {
                    s = v.ToString();
                }
                else
                {
                    s = null;
                }
                ss.Add(GetCsvField(s, separator, normalize));
            }
            return(string.Join(separator.Value, ss));
        }
Exemple #22
0
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
            {
                if (isHidden(fieldSeparator))
                    fieldSeparator.Remove();

                return VisitorAction.Continue;
            }
Exemple #23
0
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
            {
                // Once reached a field separator node, we enable the output because we are
                // now entering the field result nodes.
                mIsSkipText = false;

                return VisitorAction.Continue;
            }
Exemple #24
0
 /// <summary>
 /// Composes the specified <see cref="Continent"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="Continent"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="Continent"/>.</returns>
 public override string Compose(Continent value)
 {
     return(string.Join(FieldSeparator.ToString(), value.Code, value.Name, value.GeoNameId));
 }
 /// <summary>
 /// Composes the specified <see cref="FeatureCode"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="FeatureCode"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="FeatureCode"/>.</returns>
 public override string Compose(FeatureCode value)
 {
     return(string.Join(FieldSeparator.ToString(), GetFeatureCodeString(value), value.Name, value.Description));
 }
Exemple #26
0
 /// <summary>
 /// Composes the specified <see cref="UserTag"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="UserTag"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="UserTag"/>.</returns>
 public override string Compose(UserTag value)
 {
     return(string.Join(FieldSeparator.ToString(), value.GeoNameId, value.Tag));
 }
 /// <summary>
 /// Composes the specified <see cref="CountryInfo"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="CountryInfo"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="CountryInfo"/>.</returns>
 public override string Compose(CountryInfo value)
 {
     return(string.Join(FieldSeparator.ToString(), value.ISO_Alpha2, value.ISO_Alpha3, value.ISO_Numeric, value.FIPS, value.Country,
                        value.Capital, value.Area, value.Population, value.Continent, value.Tld, value.CurrencyCode, value.CurrencyName, value.Phone,
                        value.PostalCodeFormat, value.PostalCodeRegex, ArrayToValue(value.Languages), value.GeoNameId, ArrayToValue(value.Neighbours), value.EquivalentFipsCode));
 }
Exemple #28
0
        public void AddFieldSeparator(FieldSeparator fieldSeparator)
        {
            var sepString = ((int)fieldSeparator).ToString();

            this.codec.Add("sep", sepString);
        }
        public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
        {
            if (this.skipFieldSeparator)
            {
                this.skipFieldSeparator = false;
            }
            else
            {
                this.structureBuilder.AppendLine("<FieldSeparator />");
            }

            return VisitorAction.Continue;
        }
Exemple #30
0
 /// <summary>
 /// Composes the specified <see cref="Admin1Code"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="Admin1Code"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="Admin1Code"/>.</returns>
 public override string Compose(Admin1Code value)
 {
     return(string.Join(FieldSeparator.ToString(), value.Code, value.Name, value.NameASCII, value.GeoNameId));
 }
Exemple #31
0
        private static string ParseInlineResult(FieldStart start, string code, FieldSeparator separator, out Node lastNode)
        {
            var resultBuilder = new StringBuilder();

            lastNode = separator;

            foreach (var sibling in separator.GetFollowingSiblings().TakeWhile(n => !(n is FieldEnd)))
            {
                var run = sibling as Run;
                if (run != null)
                    resultBuilder.Append(run.GetText());
                else if (!(sibling is BookmarkStart) && !(sibling is BookmarkEnd))
                    throw new Exception(string.Format("Found unexpected node of type '{0}' after \"{1}\".", sibling.GetType().Name, GetText(start, code, separator, resultBuilder.ToString())));

                lastNode = sibling;
            }

            return resultBuilder.ToString();
        }
Exemple #32
0
 /// <summary>
 /// Composes the specified <see cref="FeatureClass"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="FeatureClass"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="FeatureClass"/>.</returns>
 public override string Compose(FeatureClass value)
 {
     return(string.Join(FieldSeparator.ToString(), value.Class, value.Description));
 }
Exemple #33
0
 /// <summary>
 /// Composes the specified <see cref="HierarchyNode"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="HierarchyNode"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="HierarchyNode"/>.</returns>
 public override string Compose(HierarchyNode value)
 {
     return(string.Join(FieldSeparator.ToString(), value.ParentId, value.ChildId, value.Type));
 }
Exemple #34
0
 /// <summary>
 /// Composes the specified <see cref="TimeZone"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="TimeZone"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="TimeZone"/>.</returns>
 public override string Compose(TimeZone value)
 {
     return(string.Join(FieldSeparator.ToString(), value.CountryCode, value.TimeZoneId.Replace(" ", "_"), FloatToString(value.GMTOffset),
                        FloatToString(value.DSTOffset), FloatToString(value.RawOffset)));
 }
Exemple #35
0
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
            {
                IndentAndAppendLine("[FieldSeparator]");

                return(VisitorAction.Continue);
            }
 /// <summary>
 /// Composes the specified <see cref="AlternateName"/> into a string.
 /// </summary>
 /// <param name="value">The <see cref="AlternateName"/> to be composed into a string.</param>
 /// <returns>A string representing the specified <see cref="AlternateName"/>.</returns>
 public override string Compose(AlternateName value)
 {
     return(string.Join(FieldSeparator.ToString(), value.Id, value.GeoNameId, string.IsNullOrEmpty(value.Type) ? value.ISOLanguage : value.Type,
                        value.Name, Bool2String(value.IsPreferredName), Bool2String(value.IsShortName), Bool2String(value.IsColloquial), Bool2String(value.IsHistoric)));
 }
Exemple #37
0
 public override string Compose(CustomEntity value)
 {
     return(string.Join(FieldSeparator.ToString(), value.Data));
 }