Click here to Skip to main content
15,896,402 members
Articles / Web Development / HTML

Transformalizing NorthWind

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
24 Jul 2014GPL37 min read 57.8K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
#region License
// /*
// See license included in this library folder.
// */
#endregion

using Transformalize.Libs.FileHelpers.Engines;
using Transformalize.Libs.FileHelpers.Enums;
using Transformalize.Libs.FileHelpers.ErrorHandling;
using Transformalize.Libs.FileHelpers.Helpers;

namespace Transformalize.Libs.FileHelpers.RunTime
{
    /// <summary>Used to create classes that maps to CSV records (can be quoted, multiplelined quoted, etc).</summary>
    public sealed class CsvClassBuilder : DelimitedClassBuilder
    {
        /// <summary>Creates a new DelimitedClassBuilder.</summary>
        /// <param name="className">The valid class name.</param>
        /// <param name="delimiter">The delimiter for that class.</param>
        /// <param name="sampleFile">A sample file from where to read the field names and number</param>
        public CsvClassBuilder(string className, char delimiter, string sampleFile) : this(new CsvOptions(className, delimiter, sampleFile))
        {
        }

        /// <summary>Creates a new DelimitedClassBuilder.</summary>
        /// <param name="className">The valid class name.</param>
        /// <param name="delimiter">The delimiter for that class.</param>
        /// <param name="numberOfFields">The number of fields in each record.</param>
        public CsvClassBuilder(string className, char delimiter, int numberOfFields) : this(new CsvOptions(className, delimiter, numberOfFields))
        {
        }

        /// <summary>Creates a new DelimitedClassBuilder.</summary>
        /// <param name="options">The specifications for the Csv file.</param>
        public CsvClassBuilder(CsvOptions options) : base(options.RecordClassName, options.Delimiter.ToString())
        {
            IgnoreFirstLines = 1;

            if (options.SampleFileName != string.Empty)
            {
                var firstLine = CommonEngine.RawReadFirstLines(options.SampleFileName, 1);

                if (options.HeaderLines > 0)
                {
                    foreach (var header in firstLine.Split(options.HeaderDelimiter == char.MinValue ? options.Delimiter : options.HeaderDelimiter))
                    {
                        AddField(StringToIdentifier(header));
                    }
                }
                else
                {
                    var fieldsNbr = firstLine.Split(options.Delimiter).Length;
                    for (var i = 0; i < fieldsNbr; i++)
                        AddField(options.FieldsPrefix + i.ToString());
                }
            }
            else if (options.NumberOfFields > 0)
            {
                AddFields(options.NumberOfFields, options.FieldsPrefix);
            }
            else
                throw new BadUsageException("You must provide a SampleFileName or a NumberOfFields to parse a genric CSV file.");
        }

        //private static Regex mRemoveBlanks = new Regex(@"\W", System.Text.RegularExpressions.RegexOptions.Compiled);


        /// <summary>Add a new Delimited field to the current class.</summary>
        /// <param name="fieldName">The Name of the field.</param>
        /// <param name="fieldType">The Type of the field.</param>
        /// <returns>The just created field.</returns>
        public override DelimitedFieldBuilder AddField(string fieldName, string fieldType)
        {
            base.AddField(fieldName, fieldType);

            if (mFields.Count > 1)
            {
                LastField.FieldOptional = true;
                LastField.FieldQuoted = true;
                LastField.QuoteMode = QuoteMode.OptionalForBoth;
                LastField.QuoteMultiline = MultilineMode.AllowForBoth;
            }

            return LastField;
        }


        /// <summary>
        ///     Adds to the class the specified number of fileds.
        /// </summary>
        /// <param name="number">The number of fileds to add.</param>
        public void AddFields(int number)
        {
            AddFields(number, "Field");
        }

        /// <summary>
        ///     Adds to the class the specified number of fileds.
        /// </summary>
        /// <param name="prefix">The prefix used for the fields.</param>
        /// <param name="number">The number of fileds to add.</param>
        public void AddFields(int number, string prefix)
        {
            var initFields = mFields.Count;

            for (var i = 0; i < number; i++)
            {
                var current = i + initFields + 1;
                AddField(prefix + (current).ToString());
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions