Ejemplo n.º 1
0
 public void AddFieldTemplate(FieldTemplate fieldTemplate)
 {
     if ((fieldTemplate.StartPosition + fieldTemplate.FieldSize) > this.LineSize)
     {
         throw new Exception(String.Format(Messages.FieldExceedsLineBoundary, fieldTemplate.FieldName));
     }
     this.FieldTemplates.Add(fieldTemplate.FieldName, fieldTemplate);
 }
Ejemplo n.º 2
0
 public void AddFieldTemplate(FieldTemplate fieldTemplate)
 {
     if ((fieldTemplate.StartPosition + fieldTemplate.FieldSize) > this.LineSize)
     {
         throw new Exception(String.Format("Field \"{0}\" exceeds line boundary", fieldTemplate.FieldName));
     }
     this.FieldTemplates.Add(fieldTemplate.FieldName, fieldTemplate);
 }
Ejemplo n.º 3
0
 //Constructor
 public ParsedField(byte[] lineBytes, FieldTemplate fieldTemplate)
 {
     try
     {
         this.Template = fieldTemplate;
         bool isParsedSuccessfully;
         this.Text = this.ParseField(lineBytes, fieldTemplate, out isParsedSuccessfully);
         this.ParsedSuccessfully = isParsedSuccessfully;
     }
     catch (Exception ex)
     {
         //Used for debugging
         Console.WriteLine("Exception parsing field {0} ({1})", fieldTemplate.FieldName, fieldTemplate.Type.ToString());
         throw ex;
     }
 }
Ejemplo n.º 4
0
 //Constructor
 public ParsedField(byte[] lineBytes, FieldTemplate fieldTemplate)
 {
     try
     {
         this.Template = fieldTemplate;
         bool isParsedSuccessfully;
         this.Text = this.ParseField(lineBytes, fieldTemplate, out isParsedSuccessfully);
         this.ParsedSuccessfully = isParsedSuccessfully;
     }
     catch (Exception ex)
     {
         //Used for debugging
         Console.WriteLine("Exception parsing field {0} ({1})", fieldTemplate.FieldName, fieldTemplate.Type.ToString());
         throw ex;
     }
 }
Ejemplo n.º 5
0
        private void InitializeLineTemplateFromXml(XElement lineTemplaeXml)
        {
            //Input XML:
            //<lineTemplate Name="RNA_RecType01" Length="1190">
            //  <fields>
            //    <fieldTemplate Name="RecordType" Type="AlphaNum" StartPosition="13" Size="2" DecimalPlaces="0" />
            //    <fieldTemplate Name="SourceInd" Type="AlphaNum" StartPosition="15" Size="1" DecimalPlaces="0" />
            //  </fields>
            //</lineTemplate>

            int    lineSize     = ParserUtilities.GetAttributeNumericValue(lineTemplaeXml, Fields.Length);
            string templateName = ParserUtilities.GetCompulsoryAttributeValue(lineTemplaeXml, Fields.Name);

            this.PopulateInitialObjectProperties(lineSize, templateName);

            foreach (XElement fieldXml in lineTemplaeXml.Element(Fields.XmlFields).Elements(Fields.XmlFieldTemplate))
            {
                FieldTemplate fieldTemplate = new FieldTemplate(fieldXml);
                this.AddFieldTemplate(fieldTemplate);
            }
        }
Ejemplo n.º 6
0
        // TODO: PasedField should be like a data transfer object. Move parsing logic into dedicated class.
        private string ParseField(byte[] lineBytes, FieldTemplate fieldTemplate, out bool isParsedSuccessfully)
        {
            if (lineBytes == null || lineBytes.Length == 0)
            {
                ParserUtilities.PrintError("lineBytes array is null or empty");
                isParsedSuccessfully = false;
                return(string.Empty);
            }
            if (lineBytes.Length < (fieldTemplate.StartPosition + fieldTemplate.FieldSize))
            {
                Array.Resize <byte>(ref lineBytes, fieldTemplate.StartPosition + fieldTemplate.FieldSize);
                //throw new Exception(String.Format(Messages.FieldOutsideLineBoundary, fieldTemplate.FieldName));
            }

            byte[] fieldBytes = new byte[fieldTemplate.FieldSize];
            Array.Copy(lineBytes, fieldTemplate.StartPosition, fieldBytes, 0, fieldTemplate.FieldSize);
            this.OriginalBytes = fieldBytes;

            string result = string.Empty;

            isParsedSuccessfully = true;

            switch (fieldTemplate.Type)
            {
            case FieldType.String:
                result = this.ConvertEbcdicString(fieldBytes);
                break;

            case FieldType.NumericString:
                result = this.ConvertEbcdicNumericString(fieldBytes, fieldTemplate.DecimalPlaces, out isParsedSuccessfully);
                break;

            case FieldType.Packed:
                result = this.Unpack(fieldBytes, fieldTemplate.DecimalPlaces, out isParsedSuccessfully);
                break;

            case FieldType.BinaryNum:
                result = this.ConvertBinaryNumber(fieldBytes, fieldTemplate.DecimalPlaces, out isParsedSuccessfully);
                break;

            case FieldType.PackedDate:
                result = this.ConvertMainframePackedDate(fieldBytes, out isParsedSuccessfully);
                break;

            case FieldType.DateString:
                result = ConvertEbcdicDateString(fieldBytes, out isParsedSuccessfully);
                break;

            case FieldType.DateStringMMDDYY:
                result = this.ConvertEbcdicCustomDateStr(fieldBytes, Formats.MMDDYY, Formats.YYYY_MM_DD, out isParsedSuccessfully);
                break;

            case FieldType.SourceBytesBase16:
                result = ParserUtilities.ConvertBytesToStringBase16(fieldBytes);
                break;

            case FieldType.SourceBytesBase10:
                result = ParserUtilities.ConvertBytesToStringBase10(fieldBytes);
                break;

            case FieldType.SourceBytesBase2:
                result = ParserUtilities.ConvertBytesToStringBase2(fieldBytes);
                break;

            case FieldType.StringEncIbm935:
                result = CustomIbm935Mapper.GetUnicodeString(fieldBytes).Trim();
                break;

            case FieldType.StringUnicode:
                result = System.Text.Encoding.Default.GetString(fieldBytes);
                break;

            default:
                isParsedSuccessfully = false;
                new Exception(String.Format(Messages.ParserNotImplemented, fieldTemplate.FieldName, fieldTemplate.Type.ToString()));
                break;
            }

            return(result);
        }
Ejemplo n.º 7
0
        private void InitializeLineTemplateFromXml(XElement lineTemplaeXml)
        {
            //Input XML:
            //<lineTemplate Name="RNA_RecType01" Length="1190">
            //  <fields>
            //    <fieldTemplate Name="RecordType" Type="AlphaNum" StartPosition="13" Size="2" DecimalPlaces="0" />
            //    <fieldTemplate Name="SourceInd" Type="AlphaNum" StartPosition="15" Size="1" DecimalPlaces="0" />
            //  </fields>
            //</lineTemplate>

            int lineSize = ParserUtilities.GetAttributeNumericValue(lineTemplaeXml, "Length");
            string templateName = ParserUtilities.GetCompulsoryAttributeValue(lineTemplaeXml, "Name");
            this.PopulateInitialObjectProperties(lineSize, templateName);

            foreach (XElement fieldXml in lineTemplaeXml.Element("fields").Elements("fieldTemplate"))
            {
                FieldTemplate fieldTemplate = new FieldTemplate(fieldXml);
                this.AddFieldTemplate(fieldTemplate);
            }
        }
Ejemplo n.º 8
0
        private string ParseField(byte[] lineBytes, FieldTemplate fieldTemplate, out bool isParsedSuccessfully)
        {
            if (lineBytes == null || lineBytes.Length == 0)
            {
                ParserUtilities.PrintError("lineBytes array is null or empty");
                isParsedSuccessfully = false;
                return "";
            }
            if (lineBytes.Length < (fieldTemplate.StartPosition + fieldTemplate.FieldSize))
            {
                throw new Exception(String.Format("Field \"{0}\" length falls outside the line length", fieldTemplate.FieldName));
            }

            byte[] fieldBytes = new byte[fieldTemplate.FieldSize];
            Array.Copy(lineBytes, fieldTemplate.StartPosition, fieldBytes, 0, fieldTemplate.FieldSize);
            this.OriginalBytes = fieldBytes;

            if (fieldTemplate.Type == FieldType.String)
            {
                isParsedSuccessfully = true;
                return this.ConvertEbcdicString(fieldBytes);
            }
            else if (fieldTemplate.Type == FieldType.NumericString)
            {
                return this.ConvertEbcdicNumericString(fieldBytes, fieldTemplate.DecimalPlaces, out isParsedSuccessfully);
            }
            else if (fieldTemplate.Type == FieldType.Packed)
            {
                return this.Unpack(fieldBytes, fieldTemplate.DecimalPlaces, out isParsedSuccessfully);
            }
            else if (fieldTemplate.Type == FieldType.BinaryNum)
            {
                return this.ConvertBinaryNumber(fieldBytes, fieldTemplate.DecimalPlaces, out isParsedSuccessfully);
            }
            else if (fieldTemplate.Type == FieldType.PackedDate)
            {
                return this.ConvertMainframePackedDate(fieldBytes, out isParsedSuccessfully);
            }
            else if (fieldTemplate.Type == FieldType.DateString)
            {
                return ConvertEbcdicDateString(fieldBytes, out isParsedSuccessfully);
            }
            else if (fieldTemplate.Type == FieldType.DateStringMMDDYY)
            {
                return this.ConvertEbcdicCustomDateStr(fieldBytes, "MMddyy", yyyy_MM_dd, out isParsedSuccessfully);
            }
            else if (fieldTemplate.Type == FieldType.SourceBytesBase16)
            {
                isParsedSuccessfully = true;
                return ParserUtilities.ConvertBytesToStringBase16(fieldBytes);
            }
            else if (fieldTemplate.Type == FieldType.SourceBytesBase10)
            {
                isParsedSuccessfully = true;
                return ParserUtilities.ConvertBytesToStringBase10(fieldBytes);
            }
            else if (fieldTemplate.Type == FieldType.SourceBytesBase2)
            {
                isParsedSuccessfully = true;
                return ParserUtilities.ConvertBytesToStringBase2(fieldBytes);
            }
            else if (fieldTemplate.Type == FieldType.StringEncIbm935)
            {
                isParsedSuccessfully = true;
                return CustomIbm935Mapper.GetUnicodeString(fieldBytes).Trim();
            }
            else
            {
                throw new Exception(String.Format("Unable to parse field \"{0}\". Parser not implemented for field type \"{1}\"", fieldTemplate.FieldName, fieldTemplate.Type.ToString()));
            }
        }