//Constructor 2
        public FieldTemplate(XElement fieldTemplateXml)
        {
            //Input XML: <fieldTemplate Name="RecordType" Type="AlphaNum" StartPosition="13" Size="2" DecimalPlaces="0" />
            string    fieldName        = ParserUtilities.GetCompulsoryAttributeValue(fieldTemplateXml, Fields.Name);
            FieldType fieldType        = ParserUtilities.GetFieldType(fieldTemplateXml, Fields.Type);
            int       startPosition    = ParserUtilities.GetAttributeNumericValue(fieldTemplateXml, Fields.StartPosition);
            int       fieldSize        = ParserUtilities.GetAttributeNumericValue(fieldTemplateXml, Fields.Size);
            string    decimalPlacesStr = ParserUtilities.GetNullableAttributeValue(fieldTemplateXml, Fields.DecimalPlaces);

            int decimalPlaces = 0;

            if (!String.IsNullOrEmpty(decimalPlacesStr))
            {
                decimalPlaces = Int32.Parse(decimalPlacesStr);
            }

            this.PopulateAllProperties(fieldName, fieldType, startPosition, fieldSize, decimalPlaces);
        }
Beispiel #2
0
        public XElement ToXml(bool includeSrcBytesInHex = false)
        {
            XElement element = new XElement(Fields.XmlFields);

            element.Add(new XAttribute(Fields.Name, this.Template.FieldName));

            if (includeSrcBytesInHex)
            {
                element.Add(new XAttribute(Fields.SrcHex, this.OriginalBytesBase16)); //For testing
            }

            if (this.Template.Type != FieldType.StringEncIbm935)
            {
                element.Value = ParserUtilities.RemoveNonAsciiChars(this.Text);
            }
            else
            {
                element.Add(this.Text);
            }
            return(element);
        }
Beispiel #3
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);
            }
        }
Beispiel #4
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);
        }
Beispiel #5
0
        private string ConvertEbcdicNumericString(byte[] ebcdicBytes, int decimalPlaces, out bool isParsedSuccessfully)
        {
            string tempNumStr = this.ConvertEbcdicString(ebcdicBytes).Trim();

            if (tempNumStr == string.Empty)
            {
                isParsedSuccessfully = true;
                return(string.Empty);
            }
            if (String.IsNullOrWhiteSpace(tempNumStr))
            {
                isParsedSuccessfully = false;
                return(string.Empty);
            }

            if (Regex.IsMatch(tempNumStr, @"^\d+$")) //Unsigned integer
            {
                isParsedSuccessfully = true;
                string result = this.AdjustDecimals(Int64.Parse(tempNumStr), decimalPlaces);
                return(result);
            }
            else if (Regex.IsMatch(tempNumStr, @"^\d*[A-R}{]$")) //Signed integer. Last characrer is A-R, or curly braces
            {
                string lastChar = ParserUtilities.Right(tempNumStr, 1);
                long   parsedNumber;

                switch (lastChar)
                {
                case "{":
                    tempNumStr   = tempNumStr.Replace("{", "0");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "A":
                    tempNumStr   = tempNumStr.Replace("A", "1");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "B":
                    tempNumStr   = tempNumStr.Replace("B", "2");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "C":
                    tempNumStr   = tempNumStr.Replace("C", "3");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "D":
                    tempNumStr   = tempNumStr.Replace("D", "4");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "E":
                    tempNumStr   = tempNumStr.Replace("E", "5");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "F":
                    tempNumStr   = tempNumStr.Replace("F", "6");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "G":
                    tempNumStr   = tempNumStr.Replace("G", "7");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "H":
                    tempNumStr   = tempNumStr.Replace("H", "8");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "I":
                    tempNumStr   = tempNumStr.Replace("I", "9");
                    parsedNumber = Int64.Parse(tempNumStr);
                    break;

                case "}":     //Negative numbers
                    tempNumStr   = tempNumStr.Replace("}", "0");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "J":
                    tempNumStr   = tempNumStr.Replace("J", "1");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "K":
                    tempNumStr   = tempNumStr.Replace("K", "2");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "L":
                    tempNumStr   = tempNumStr.Replace("L", "3");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "M":
                    tempNumStr   = tempNumStr.Replace("M", "4");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "N":
                    tempNumStr   = tempNumStr.Replace("N", "5");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "O":
                    tempNumStr   = tempNumStr.Replace("O", "6");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "P":
                    tempNumStr   = tempNumStr.Replace("P", "7");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "Q":
                    tempNumStr   = tempNumStr.Replace("Q", "8");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                case "R":
                    tempNumStr   = tempNumStr.Replace("R", "9");
                    parsedNumber = Int64.Parse(tempNumStr) * (-1);
                    break;

                default:
                    throw new Exception(String.Format(Messages.UnableToConvertStringToNumber, tempNumStr));
                }

                isParsedSuccessfully = true;
                if (decimalPlaces > 0)
                {
                    return(this.AdjustDecimals(parsedNumber, decimalPlaces));
                }
                return(parsedNumber.ToString());
            }
            else
            {
                isParsedSuccessfully = false;
                return(tempNumStr);
            }
        }
Beispiel #6
0
 public bool SaveParsedLinesAsXmlFile(string outputFilePath)
 {
     return(ParserUtilities.WriteParsedLineArrayToXml(this.ParsedLines, outputFilePath));
 }
Beispiel #7
0
 public bool SaveParsedLinesAsTxtFile(string outputFilePath, string delimiter = "\t", bool includeColumnNames = true, bool addQuotes = true, string quoteCharacter = "\"", bool append = false)
 {
     return(ParserUtilities.WriteParsedLineArrayToTxt(this.ParsedLines, outputFilePath, delimiter, includeColumnNames, addQuotes, quoteCharacter, append));
 }
Beispiel #8
0
 public bool SaveParsedLinesAsCsvFile(string outputFilePath, bool includeColumnNames = true, bool addQuotes = true, bool append = false)
 {
     return(ParserUtilities.WriteParsedLineArrayToCsv(this.ParsedLines, outputFilePath, includeColumnNames, addQuotes, false));
 }