FieldFactory.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 » FieldFactory.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.Reflection;

namespace FileHelpers{
  internal sealed class FieldFactory
  {
    #region "  Avoid Creation  "

    private FieldFactory()
    {
    }

    #endregion

    public static FieldBase CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute, bool someOptional)
    {
      // If ignored, return null
      if (fi.IsDefined(typeof (FieldIgnoredAttribute), true))
        return null;

      FieldBase res = null;

      FieldAttribute[] attributes;
      FieldAttribute fieldAttb;

      attributes = (FieldAttribute[]) fi.GetCustomAttributes(typeof (FieldAttribute), true);

      // CHECK USAGE ERRORS !!!

      if (attributes.Length > 1)
        throw new BadUsageException("The field: " + fi.Name + " has more than one FieldAttribute (left only one or none)");

      if (attributes.Length == 0 && recordAttribute is FixedLengthRecordAttribute)
        throw new BadUsageException("The record class marked with the FixedLengthRecord attribute must include a FixedLength attribute in each field.");

      if (recordAttribute is DelimitedRecordAttribute && fi.IsDefined(typeof (FieldAlignAttribute), true))
        throw new BadUsageException("The AlignAttribute is only valid for fixed length records and are used only for write purpouse.");


      // PROCESS IN NORMAL CONDITIONS

      if (attributes.Length > 0)
      {
        fieldAttb = attributes[0];

        if (fieldAttb is FieldFixedLengthAttribute)
        {
          if (recordAttribute is DelimitedRecordAttribute)
            throw new BadUsageException("The FieldFixedLengthAttribute is only for the FixedLengthRecords not for the delimited ones.");

          FieldFixedLengthAttribute attb = ((FieldFixedLengthAttribute) fieldAttb);

          FieldAlignAttribute[] alignAttbs = (FieldAlignAttribute[]) fi.GetCustomAttributes(typeof (FieldAlignAttribute), true);
          FieldAlignAttribute align = null;

          if (alignAttbs.Length > 0)
            align = alignAttbs[0];

          res = new FixedLengthField(fi, attb.Length, align);
          ((FixedLengthField) res).mFixedMode = ((FixedLengthRecordAttribute)recordAttribute).mFixedMode;
        }
        else if (fieldAttb is FieldDelimiterAttribute)
        {
          if (recordAttribute is FixedLengthRecordAttribute)
            throw new BadUsageException("The DelimitedAttribute is only for DelimitedRecords not for the fixed ones.");

          res = new DelimitedField(fi, ((FieldDelimiterAttribute) fieldAttb).mSeparator);

        }
        else
          throw new BadUsageException("Custom TypedRecords not currently supported.");
      }
      else // attributes.Length == 0
      {
        if (recordAttribute is DelimitedRecordAttribute)
          res = new DelimitedField(fi, ((DelimitedRecordAttribute) recordAttribute).Separator);
      }

      //-----  TRIMMING

      if (res != null)
      {
        FieldTrimAttribute[] trim = (FieldTrimAttribute[]) fi.GetCustomAttributes(typeof (FieldTrimAttribute), true);
        if (trim.Length > 0)
        {
          res.mTrimMode = trim[0].TrimMode;
          res.mTrimChars = trim[0].TrimChars;
        }

        FieldQuotedAttribute[] quotedAttributes = (FieldQuotedAttribute[]) fi.GetCustomAttributes(typeof (FieldQuotedAttribute), true);
        if (quotedAttributes.Length > 0)
        {
          if (res is FixedLengthField)
            throw new BadUsageException("The QuotedAttribute can't be used in FixedLength fields.");

          ((DelimitedField) res).mQuoteChar = quotedAttributes[0].QuoteChar;
          ((DelimitedField) res).mQuoteMode = quotedAttributes[0].QuoteMode;
          ((DelimitedField) res).mQuoteMultiline = quotedAttributes[0].QuoteMultiline;
        }

        FieldOptionalAttribute[] optionalAttribs = (FieldOptionalAttribute[]) fi.GetCustomAttributes(typeof (FieldOptionalAttribute), true);

        if (optionalAttribs.Length > 0)
          res.mIsOptional  = true;
        else if (someOptional)
          throw new BadUsageException("When you define a field as FieldOptional, the next fields must be marked with the same attribute. ( Try adding [FieldOptional] to " + res.mFieldInfo.Name + " )");

        
        res.mInNewLine = fi.IsDefined(typeof(FieldInNewLineAttribute), true);
      }


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