DelimitedField.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » FileHelpers Library 
FileHelpers Library » FileHelpers » DelimitedField.cs
#region "   Copyright 2005-07 to Marcos Meli - http://www.marcosmeli.com.ar" 

// Errors, suggestions, contributions, send a mail to: marcos@filehelpers.com.

#endregion

using System;
using System.Globalization;
using System.Reflection;
using System.Text;

namespace FileHelpers{
  internal sealed class DelimitedField : FieldBase
  {
    #region "  Constructor  "

    internal DelimitedField(FieldInfo fi, string sep) : base(fi)
    {
      Separator = sep;
    }

    #endregion

    private static CompareInfo mCompare = CultureInfo.InvariantCulture.CompareInfo;
    
    #region "  Properties  "

    private string mSeparator;

    internal string Separator
    {
      get { return mSeparator; }
      set
      {
        mSeparator = value;

        if (mIsLast)
          mCharsToDiscard = 0;
        else
          mCharsToDiscard = mSeparator.Length;
      }
    }

    internal char mQuoteChar = '\0';
    internal QuoteMode mQuoteMode;
    internal MultilineMode  mQuoteMultiline = MultilineMode.AllowForBoth;

    #endregion

    #region "  Overrides String Handling  "


    protected override ExtractedInfo ExtractFieldString(LineInfo line)
    {
      if (mIsOptional && line.IsEOL() )
        return ExtractedInfo.Empty;


      if (mQuoteChar == '\0')
        return BasicExtractString(line);
      else
      {
        //TODO: UnComment and Fix

        if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left)
        {

          //int pos = line.mCurrentPos;
          line.TrimStart(mTrimChars);
//          from2 = from.TrimStart(mTrimChars);
          //res.CharsRemoved = line.mCurrentPos - pos;
        }

        string quotedStr = mQuoteChar.ToString();
        if (line.StartsWith(quotedStr))
        {

//          ExtractedInfo res = null;
//          res = new ExtractedInfo(line, line.mCurrentPos);

          return StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead);
//          if (mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead)
//          {
//
//            //res.ExtractedString = ei.ExtractedString;
//            //res.CharsRemoved += ei.CharsRemoved;
//            //res.ExtraLines = ei.ExtraLines;
//            //res.NewRestOfLine = ei.NewRestOfLine;
//          }
//          else
//          {
//            return StringHelper.ExtractQuotedString(from2, mQuoteChar, out index);
//            //res.CharsRemoved += index;
//          }

        //  return res;

        }
        else
        {
          if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead)
            return BasicExtractString(line);
          else if (line.StartsWithTrim(quotedStr))
            throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' has spaces before the QuotedChar at line "+ line.mReader.LineNumber.ToString() + ". Use the TrimAttribute to by pass this error. Field String: " + line.CurrentString);
          else
            throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' not begin with the QuotedChar at line "+ line.mReader.LineNumber.ToString() + ". You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: " + line.CurrentString);
        }

      }


    }

    private ExtractedInfo BasicExtractString(LineInfo line)
    {
      ExtractedInfo res;
      
      if (mIsLast)
        res = new ExtractedInfo(line);
      else
      {
        int sepPos;

        sepPos = line.IndexOf(mSeparator);

        if (sepPos == -1)
        {
          if (this.mNextIsOptional == false)
          {
            string msg = null;

            if (mIsFirst && line.EmptyFromPos())
              msg = "The line " + line.mReader.LineNumber.ToString() + " is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.";
            else
              msg = "The delimiter '" + this.mSeparator + "' cant be found after the field '" + this.mFieldInfo.Name + "' at line " + line.mReader.LineNumber.ToString() + " (the record has less fields, the delimiter is wrong or the next field must be marked as optional).";

            throw new FileHelpersException(msg);

          }
          else
            sepPos = line.mLine.Length - 1;
        }

        res = new ExtractedInfo(line, sepPos);
      }
      return res;
    }

    protected override void CreateFieldString(StringBuilder sb, object fieldValue)
    {
      string field = base.BaseFieldString(fieldValue);

      bool hasNewLine = mCompare.IndexOf(field, StringHelper.NewLine) >= 0;

      // If have a new line and this is not allowed throw an exception
      if (hasNewLine &&
        (mQuoteMultiline == MultilineMode.AllowForRead || 
         mQuoteMultiline == MultilineMode.NotAllow))
        throw new BadUsageException("One value for the field " + this.mFieldInfo.Name + " has a new line inside. To allow write this value you must add a FieldQuoted attribute with the multiline option in true.");

      // Add Quotes If:
      //     -  optional == false
      //     -  is optional and contains the separator 
      //     -  is optional and contains a new line

      if ((mQuoteChar != '\0') && 
        (mQuoteMode == QuoteMode.AlwaysQuoted || 
          mQuoteMode == QuoteMode.OptionalForRead || 
          ( (mQuoteMode == QuoteMode.OptionalForWrite || mQuoteMode == QuoteMode.OptionalForBoth)  
          && mCompare.IndexOf(field, mSeparator) >= 0) || hasNewLine))
        StringHelper.CreateQuotedString(sb, field, mQuoteChar);
      else
        sb.Append(field);

      if (mIsLast == false)
        sb.Append(mSeparator);
    }

    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.