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

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

namespace Transformalize.Libs.FileHelpers.RunTime
{
    /// <summary>Used to create fields that are part of a dilimited record class.</summary>
    public sealed class DelimitedFieldBuilder : FieldBuilder
    {
        internal DelimitedFieldBuilder(string fieldName, string fieldType) : base(fieldName, fieldType)
        {
        }

        internal DelimitedFieldBuilder(string fieldName, Type fieldType) : base(fieldName, fieldType)
        {
        }

#if NET_2_0
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
        private bool mFieldQuoted;

        /// <summary>Indicates if the field is quoted with some char. (works with QuoteMode and QuoteChar)</summary>
        public bool FieldQuoted
        {
            get { return mFieldQuoted; }
            set { mFieldQuoted = value; }
        }

#if NET_2_0
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
        private char mQuoteChar = '"';

        /// <summary>Indicates the char used to quote this field. (only used when FieldQuoted is true)</summary>
        public char QuoteChar
        {
            get { return mQuoteChar; }
            set { mQuoteChar = value; }
        }

#if NET_2_0
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
        private QuoteMode mQuoteMode = QuoteMode.OptionalForRead;

        /// <summary>Indicates the QuoteMode for this field. (only used when FieldQuoted is true)</summary>
        public QuoteMode QuoteMode
        {
            get { return mQuoteMode; }
            set { mQuoteMode = value; }
        }

#if NET_2_0
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
        private MultilineMode mQuoteMultiline = MultilineMode.AllowForRead;

        /// <summary>Indicates if this quoted field can span multiple lines. (only used when FieldQuoted is true)</summary>
        public MultilineMode QuoteMultiline
        {
            get { return mQuoteMultiline; }
            set { mQuoteMultiline = value; }
        }


        internal override void AddAttributesCode(AttributesBuilder attbs, NetLanguage leng)
        {
            if (mFieldQuoted)
            {
                if (leng == NetLanguage.CSharp)
                {
                    var quoteStr = mQuoteChar.ToString();
                    if (mQuoteChar == '\'') quoteStr = @"\'";

                    attbs.AddAttribute("FieldQuoted('" + quoteStr + "', QuoteMode." + mQuoteMode.ToString() + ", MultilineMode." + mQuoteMultiline.ToString() + ")");
                }
                else if (leng == NetLanguage.VbNet)
                {
                    var quoteStr = mQuoteChar.ToString();
                    if (mQuoteChar == '"') quoteStr = "\"\"";

                    attbs.AddAttribute("FieldQuoted(\"" + quoteStr + "\"c, QuoteMode." + mQuoteMode.ToString() + ", MultilineMode." + mQuoteMultiline.ToString() + ")");
                }
            }
        }

        internal override void WriteHeaderAttributes(XmlHelper writer)
        {
        }

        internal override void WriteExtraElements(XmlHelper writer)
        {
            writer.WriteElement("FieldQuoted", FieldQuoted);
            writer.WriteElement("QuoteChar", QuoteChar.ToString(), "\"");
            writer.WriteElement("QuoteMode", QuoteMode.ToString(), "OptionalForRead");
            writer.WriteElement("QuoteMultiline", QuoteMultiline.ToString(), "AllowForRead");
        }

        internal override void ReadFieldInternal(XmlNode node)
        {
            XmlNode ele;

            FieldQuoted = node["FieldQuoted"] != null;

            ele = node["QuoteChar"];
            if (ele != null) QuoteChar = ele.InnerText[0];

            ele = node["QuoteMode"];
            if (ele != null) QuoteMode = (QuoteMode) Enum.Parse(typeof (QuoteMode), ele.InnerText);

            ele = node["QuoteMultiline"];
            if (ele != null) QuoteMultiline = (MultilineMode) Enum.Parse(typeof (MultilineMode), ele.InnerText);
        }
    }
}

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