Click here to Skip to main content
15,881,593 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.5K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
#region License
// /*
// See license included in this library folder.
// */
#endregion

using System;
using System.Data;
using System.Xml;
using Transformalize.Libs.FileHelpers.Enums;
using Transformalize.Libs.FileHelpers.ErrorHandling;

namespace Transformalize.Libs.FileHelpers.RunTime
{
    /// <summary>Used to create classes that maps to Delimited records.</summary>
    public class DelimitedClassBuilder : ClassBuilder
    {
#if NET_2_0
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
        private string mDelimiter = string.Empty;

        /// <summary>The Delimiter that marks the end of each field.</summary>
        public string Delimiter
        {
            get { return mDelimiter; }
            set { mDelimiter = value; }
        }


        /// <summary>Return the field at the specified index.</summary>
        /// <param name="index">The index of the field.</param>
        /// <returns>The field at the specified index.</returns>
        public new DelimitedFieldBuilder FieldByIndex(int index)
        {
            return (DelimitedFieldBuilder) base.FieldByIndex(index);
        }

        /// <summary>Returns the current fields of the class.</summary>
        public new DelimitedFieldBuilder[] Fields
        {
            get { return (DelimitedFieldBuilder[]) mFields.ToArray(typeof (DelimitedFieldBuilder)); }
        }


        /// <summary>Creates a new DelimitedClassBuilder.</summary>
        /// <param name="className">The valid class name.</param>
        /// <param name="delimiter">The delimiter for that class.</param>
        public DelimitedClassBuilder(string className, string delimiter) : base(className)
        {
            mDelimiter = delimiter;
        }

        /// <summary>Creates a new DelimitedClassBuilder.</summary>
        /// <param name="className">The valid class name.</param>
        public DelimitedClassBuilder(string className) : this(className, string.Empty)
        {
        }

        /// <summary>Creates a new DelimitedClassBuilder with the same structure than a DataTable.</summary>
        /// <param name="className">The valid class name.</param>
        /// <param name="delimiter">The delimiter for that class.</param>
        /// <param name="dt">The DataTable from where to get the field names and types</param>
        public DelimitedClassBuilder(string className, string delimiter, DataTable dt) : this(className, delimiter)
        {
            foreach (DataColumn dc in dt.Columns)
            {
                AddField(StringToIdentifier(dc.ColumnName), dc.DataType);
            }
        }


        /// <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 virtual DelimitedFieldBuilder AddField(string fieldName, string fieldType)
        {
            var fb = new DelimitedFieldBuilder(fieldName, fieldType);
            AddFieldInternal(fb);
            return fb;
        }

        /// <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 DelimitedFieldBuilder AddField(string fieldName, Type fieldType)
        {
            return AddField(fieldName, fieldType.FullName);
        }

        /// <summary>Add a new Delimited string field to the current class.</summary>
        /// <param name="fieldName">The Name of the string field.</param>
        /// <returns>The just created field.</returns>
        public virtual DelimitedFieldBuilder AddField(string fieldName)
        {
            return AddField(fieldName, "System.String");
        }

        /// <summary>Add a new Delimited field to the current class.</summary>
        /// <param name="field">The field definition.</param>
        /// <returns>The just added field.</returns>
        public DelimitedFieldBuilder AddField(DelimitedFieldBuilder field)
        {
            AddFieldInternal(field);
            return field;
        }

        /// <summary>Return the last added field. (use it reduce casts and code)</summary>
        public DelimitedFieldBuilder LastField
        {
            get
            {
                if (mFields.Count == 0)
                    return null;
                else
                    return (DelimitedFieldBuilder) mFields[mFields.Count - 1];
            }
        }

        internal override void AddAttributesCode(AttributesBuilder attbs, NetLanguage leng)
        {
            if (mDelimiter == string.Empty)
                throw new BadUsageException("The Delimiter of the DelimiterClassBuilder can't be null or empty.");
            else
                attbs.AddAttribute("DelimitedRecord(\"" + mDelimiter + "\")");
        }

        internal override void WriteHeaderElement(XmlHelper writer)
        {
            writer.mWriter.WriteStartElement("DelimitedClass");
            writer.mWriter.WriteStartAttribute("Delimiter", "");
            writer.mWriter.WriteString(Delimiter);
            writer.mWriter.WriteEndAttribute();
        }

        internal override void WriteExtraElements(XmlHelper writer)
        {
        }

        internal static DelimitedClassBuilder LoadXmlInternal(XmlDocument document)
        {
            DelimitedClassBuilder res;
            var del = document.ChildNodes[0].Attributes[0].Value;

            var className = document.ChildNodes.Item(0).SelectNodes("/DelimitedClass/ClassName").Item(0).InnerText;

            res = new DelimitedClassBuilder(className, del);
//			
//			while(reader.mReader.EOF == false)
//			{
//				reader.ReadToNextElement();
////				if (reader.mReader.LocalName == "IgnoreEmptyLines")
//			}


            return res;
        }

        internal override void ReadClassElements(XmlDocument document)
        {
        }

        internal override void ReadField(XmlNode node)
        {
            AddField(node.Attributes.Item(0).InnerText, node.Attributes.Item(1).InnerText).ReadField(node);
        }
    }
}

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