protected override Task <ExpandoObject> ReadLineAsync(string currentLine, IRecordDescriptor descriptor)
        {
            return(Task.Run(() =>
            {
                var item = new ExpandoObject();
                string[] fieldsValue = currentLine.Split(((DelimitedRecordDescriptor)Descriptor).Delimiter);
                int index = 0;
                foreach (KeyValuePair <string, IFieldInfoTypeDescriptor> fieldInfoTypeDescriptor in descriptor.Fields)
                {
                    string fieldValue = index >= fieldsValue.Length ? null : fieldsValue[index];
                    item.TryAdd(
                        fieldInfoTypeDescriptor.Key,
                        fieldInfoTypeDescriptor.Value.IsArray
                                ? ((IDelimitedArrayFieldInfoDescriptor)fieldInfoTypeDescriptor.Value)
                        .StringToArray(fieldValue, Descriptor.NullChar)
                                : ((IFieldInfoDescriptor)fieldInfoTypeDescriptor.Value)
                        .StringToRecord(fieldValue, Descriptor.NullChar)
                        );

                    index++;
                }

                return item;
            }));
        }
        private static string GenerateCode(this IRecordDescriptor descriptor, string language, string className, string @namespace)
        {
            var codeNamespace = new CodeNamespace(@namespace);

            codeNamespace.Imports.Add(new CodeNamespaceImport("System"));

            codeNamespace.GenerateClass(descriptor, className, out bool hasArrayType);

            if (hasArrayType)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            }

            var targetUnit = new CodeCompileUnit();

            targetUnit.Namespaces.Add(codeNamespace);

            var provider = CodeDomProvider.CreateProvider(language);
            var options  = new CodeGeneratorOptions
            {
                BracingStyle             = "C",
                BlankLinesBetweenMembers = true
            };

            var sb = new StringBuilder();

            using (var writer = new StringWriterWithEncoding(sb, Encoding.UTF8))
            {
                provider.GenerateCodeFromCompileUnit(targetUnit, writer, options);
            }
            return(sb.ToString());
        }
예제 #3
0
        private ExpandoObject ReadElement(XElement element, IRecordDescriptor descriptor)
        {
            var item = new ExpandoObject();

            foreach (KeyValuePair <string, IFieldInfoTypeDescriptor> fieldInfoTypeDescriptor in descriptor.Fields)
            {
                string propertyName = string.IsNullOrWhiteSpace(((IXmlFieldPropertyNameInfoDescriptor)fieldInfoTypeDescriptor.Value).PropertyName)
                            ? fieldInfoTypeDescriptor.Key
                            : ((IXmlFieldPropertyNameInfoDescriptor)fieldInfoTypeDescriptor.Value).PropertyName;

                if (fieldInfoTypeDescriptor.Value.IsArray)
                {
                    item.TryAdd(propertyName,
                                ((IXmlArrayFieldInfoDescriptor)fieldInfoTypeDescriptor.Value).ToRecordArray(
                                    fieldInfoTypeDescriptor.Key,
                                    element
                                    ));
                    continue;
                }

                item.TryAdd(propertyName,
                            ((IXmlFieldInfoDescriptor)fieldInfoTypeDescriptor.Value).ToRecord(fieldInfoTypeDescriptor.Key, element)
                            );
            }

            return(item);
        }
예제 #4
0
        public static FixedArrayFieldInfoBuilder AddSubArray(this IRecordDescriptor recordInfo, string fieldName)
        {
            if (string.IsNullOrWhiteSpace(fieldName))
            {
                throw new BadFluentConfigurationException($"The {nameof(fieldName)} cannot be null or empty");
            }

            var fieldInfo = new FixedArrayFieldInfoBuilder();

            recordInfo.Add(fieldName, fieldInfo);
            return(fieldInfo);
        }
        public static DelimitedArrayFieldInfoBuilder AddArray(this IRecordDescriptor descriptor, string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new BadFluentConfigurationException($"The {nameof(name)} cannot be null or empty");
            }

            var fieldInfo = new DelimitedArrayFieldInfoBuilder();

            descriptor.Add(name, fieldInfo);
            return(fieldInfo);
        }
        private static void GenerateClass(this CodeNamespace codeNamespace, IRecordDescriptor descriptor, string className, out bool hasArrayType)
        {
            var targetClass = new CodeTypeDeclaration(className)
            {
                IsClass        = true,
                TypeAttributes = System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Sealed
            };

            targetClass.AddProperties(codeNamespace, descriptor, out hasArrayType);

            codeNamespace.Types.Add(targetClass);
        }
 protected FluentEngine(IRecordDescriptor descriptor, Encoding encoding = null) : this(encoding)
 {
     CheckDescriptor(descriptor);
     RecordItems = new List <RecordItem>
     {
         new RecordItem
         {
             Name         = DefaultRecordItemName,
             Descriptor   = descriptor,
             RegexPattern = string.Empty
         }
     };
 }
예제 #8
0
        private static DataTable CreateDataTableCore(IRecordDescriptor descriptor)
        {
            DataTable table = new DataTable();
            table.Locale = CultureInfo.CurrentCulture;
            table.ExtendedProperties.Add(typeof(IRecordDescriptor), descriptor.Copy());

            int fieldCount = descriptor.FieldCount;

            for (int i = 0; i < fieldCount; i++)
                table.Columns.Add(
                    descriptor.GetFieldName(i),
                    descriptor.GetFieldType(i));

            return table;
        }
        protected void CheckDescriptor(IRecordDescriptor descriptor, bool isArray = false)
        {
            if (!descriptor.Fields.Any())
            {
                throw new BadFluentConfigurationException(isArray ? "The array property has no fields" : "The builder has no fields");
            }

            foreach (KeyValuePair <string, IFieldInfoTypeDescriptor> fieldInfoDescriptor in descriptor.Fields)
            {
                if (string.IsNullOrWhiteSpace(fieldInfoDescriptor.Key))
                {
                    throw new BadFluentConfigurationException();
                }

                CheckFieldDescriptor(fieldInfoDescriptor.Key, fieldInfoDescriptor.Value);
            }
        }
예제 #10
0
        private static void CreateArray(IRecordDescriptor descriptor, dynamic fieldInfo)
        {
            var arrayBuilder = descriptor.AddArray((string)fieldInfo.Name);

            dynamic fieldInfoValue = fieldInfo.Value;

            if (fieldInfoValue.ArrayLength != null)
            {
                arrayBuilder.SetArrayLength((int)fieldInfoValue.ArrayLength.Value);
            }
            if (fieldInfoValue.ArrayItemLength != null)
            {
                arrayBuilder.SetArrayItemLength((int)fieldInfoValue.ArrayItemLength.Value);
            }
            if (fieldInfoValue.Align != null)
            {
                arrayBuilder.SetAlign((bool)fieldInfoValue.Align.Value);
            }
            if (fieldInfoValue.AlignChar != null && !string.IsNullOrWhiteSpace((string)fieldInfoValue.AlignChar.Value))
            {
                arrayBuilder.SetAlignChar(((string)fieldInfoValue.AlignChar.Value)[0]);
            }

            if (fieldInfoValue.Fields == null)
            {
                return;
            }

            foreach (dynamic field in fieldInfoValue.Fields)
            {
                if (field.Name == null || field.Value == null)
                {
                    continue;
                }
                if (field.Value.IsArray != null && (bool)field.Value.IsArray.Value)
                {
                    CreateArray(arrayBuilder, field);
                    continue;
                }
                CreateField(arrayBuilder, field);
            }
        }
예제 #11
0
        private static void CreateField(IRecordDescriptor descriptor, dynamic fieldInfo)
        {
            var fieldBuilder = descriptor.AddField((string)fieldInfo.Name);

            dynamic fieldInfoValue = fieldInfo.Value;

            if (fieldInfoValue.Length != null)
            {
                fieldBuilder.SetLength((int)fieldInfoValue.Length.Value);
            }
            if (fieldInfoValue.Converter != null)
            {
                fieldBuilder.SetConverter(Type.GetType(fieldInfoValue.Converter.Value));
            }
            if (fieldInfoValue.NullValue != null)
            {
                fieldBuilder.SetNullValue(fieldInfoValue.NullValue.Value);
            }
            if (fieldInfoValue.AlignMode != null)
            {
                fieldBuilder.SetAlignMode((AlignMode)fieldInfoValue.AlignMode.Value);
            }
            if (fieldInfoValue.AlignChar != null && !string.IsNullOrWhiteSpace((string)fieldInfoValue.AlignChar.Value))
            {
                fieldBuilder.SetAlignChar(((string)fieldInfoValue.AlignChar.Value)[0]);
            }
            if (fieldInfoValue.ConverterFormat != null)
            {
                fieldBuilder.SetConverterFormat((string)fieldInfoValue.ConverterFormat.Value);
            }
            if (fieldInfoValue.TrimMode != null)
            {
                fieldBuilder.SetTrimMode((TrimMode)fieldInfoValue.TrimMode.Value);
            }
            if (fieldInfoValue.Type != null)
            {
                fieldBuilder.SetType(Type.GetType(fieldInfoValue.Type.Value));
            }
        }
 public static string GenerateVisualBasicClass(this IRecordDescriptor descriptor, string className, string @namespace) =>
 descriptor.GenerateCode("visualbasic", className, @namespace);
 public static string GenerateCSharpClass(this IRecordDescriptor descriptor, string className, string @namespace) =>
 descriptor.GenerateCode("CSharp", className, @namespace);
 internal static IRecordDescriptor Copy(IRecordDescriptor metadata)
 {
     return new Descriptor(metadata);
 }
 internal Descriptor(IRecordDescriptor metadata)
     : this(metadata.FieldCount)
 {
     for (int i = 0; i < _names.Length; i++)
     {
         string fieldName = metadata.GetFieldName(i);
         _names[i] = fieldName;
         _types[i] = metadata.GetFieldType(i);
         _ordinals.Add(fieldName, i);
     }
 }
예제 #16
0
 public DelimitedFluentEngine(
     IRecordDescriptor descriptor,
     Encoding encoding = null) : base(descriptor, encoding)
 {
 }
 private static void AddProperties(this CodeTypeDeclaration targetClass, CodeNamespace codeNamespace, IRecordDescriptor descriptor, out bool hasArrayType)
 {
     hasArrayType = false;
     foreach (var field in descriptor.Fields)
     {
         if (field.Value.IsArray)
         {
             codeNamespace.GenerateClass((IRecordDescriptor)field.Value, $"{field.Key}Item", out hasArrayType);
             targetClass.AddArrayProperty(field.Key, $"{field.Key}Item");
             hasArrayType = true;
             continue;
         }
         targetClass.AddProperty(field.Key, field.Value);
     }
 }
예제 #18
0
 public FluentFixedEngine(IRecordDescriptor descriptor, Encoding encoding) : base(descriptor, encoding)
 {
 }
예제 #19
0
 protected override Task <ExpandoObject> ReadLineAsync(string currentLine, IRecordDescriptor descriptor)
 {
     throw new NotImplementedException();
 }
 protected abstract Task <ExpandoObject> ReadLineAsync(string currentLine, IRecordDescriptor descriptor);
예제 #21
0
 protected virtual Task <ExpandoObject> ReadLineAsync(string currentLine, IRecordDescriptor descriptor)
 {
     return(null);
 }
 protected FluentEventEngineBase(IRecordDescriptor descriptor, Encoding encoding = null) : base(descriptor, encoding)
 {
 }
 public DelimitedFluentEngine(IRecordDescriptor descriptor) : base(descriptor)
 {
 }
예제 #24
0
 protected FluentEngineBase(IRecordDescriptor descriptor)
     : this(descriptor, Encoding.UTF8)
 {
 }
예제 #25
0
 public XmlFluentEngine(IRecordDescriptor descriptor, Encoding encoding) : base(descriptor, encoding)
 {
 }
예제 #26
0
 protected FluentEngineBase(IRecordDescriptor descriptor, Encoding encoding)
 {
     CheckDescriptor(descriptor);
     Encoding   = encoding;
     Descriptor = descriptor;
 }
예제 #27
0
 public FluentFixedEngine(IRecordDescriptor descriptor) : base(descriptor)
 {
 }
예제 #28
0
        protected override async Task <ExpandoObject> ReadLineAsync(string currentLine, IRecordDescriptor descriptor) =>
        await Task.Run(() =>
        {
            var item             = new ExpandoObject();
            string[] fieldsValue = currentLine.Split(new string[] { ((DelimitedRecordDescriptor)descriptor).Delimiter }, StringSplitOptions.RemoveEmptyEntries);
            int index            = 0;
            foreach (KeyValuePair <string, IFieldInfoTypeDescriptor> fieldInfoTypeDescriptor in descriptor.Fields)
            {
                if (fieldInfoTypeDescriptor.Value.IsArray)
                {
                    continue;
                }

                string fieldValue = index >= fieldsValue.Length ? null : fieldsValue[index];
                item.InternalTryAdd(fieldInfoTypeDescriptor.Key,
                                    ((IFieldInfoDescriptor)fieldInfoTypeDescriptor.Value).StringToRecord(fieldValue, descriptor.NullChar));

                index++;
            }

            return(item);
        });
        protected override async Task <ExpandoObject> ReadLineAsync(string currentLine, IRecordDescriptor descriptor)
        {
            return(await Task.Run(() =>
            {
                var item = new ExpandoObject();

                var offset = 0;
                foreach (KeyValuePair <string, IFieldInfoTypeDescriptor> fieldInfoTypeDescriptor in descriptor.Fields)
                {
                    if (fieldInfoTypeDescriptor.Value.IsArray)
                    {
                        item.TryAdd(fieldInfoTypeDescriptor.Key,
                                    ((IArrayFieldInfoDescriptor)fieldInfoTypeDescriptor.Value).StringToArray(currentLine,
                                                                                                             ref offset));
                        continue;
                    }

                    item.TryAdd(fieldInfoTypeDescriptor.Key,
                                ((IFixedFieldInfoDescriptor)fieldInfoTypeDescriptor.Value).StringToRecord(currentLine, ref offset));
                }

                return item;
            }));
        }
예제 #30
0
 public XmlFluentEngine(IRecordDescriptor descriptor) : base(descriptor)
 {
 }