예제 #1
0
        private void CreateDEs(DataString dataString)
        {
            DataElements = new DataElementCollection();

            DataString dataElementsString = dataString.SubString(readerIndex);

            List <int> dataElementsBitMap = BitMaps.GetPresentDataElements();

            foreach (int de in dataElementsBitMap)
            {
                if (DataDefinitionDictionary.ContainsElementDefinition(de))
                {
                    DataDefinition dataDef = DataDefinitionDictionary.GetDataDefinition(de);

                    int dataElementsReaderIndex = 0;

                    try
                    {
                        DataString data = dataDef.GetAllData(dataElementsString, ref dataElementsReaderIndex);

                        dataElementsString = dataElementsString.SubString(dataElementsReaderIndex);

                        DataElements.AddOrReplaceDataElement(new DataElement(de, dataDef, data));
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Missing or incorrectly defined DE: " + de + ".", ex);
                    }
                }
                else
                {
                    throw new Exception("Missing definition, DE: " + de + ".");
                }
            }
        }
        private DataElement CreateDataElement(Node node, DataDefinition definition)
        {
            if (!node.HasChilds())
            {
                string allData = definition.FillWithLength(node.Data);
                return(new DataElement(node.Number, definition, new DataString(allData)));
            }
            else
            {
                if (node.Childs.Count != definition.SubDefinitions?.Count)
                {
                    throw new Exception("Node childs Count must be equal to SubDefinitions Count.");
                }

                HashSet <DataElement> subDataElements = new HashSet <DataElement>();
                foreach (KeyValuePair <int, Node> kvp in node.Childs)
                {
                    DataElement innerDataElement = CreateDataElement(kvp.Value,
                                                                     definition.SubDefinitions[kvp.Key]);

                    subDataElements.Add(innerDataElement);
                }

                return(new DataElement(node.Number, definition, subDataElements));
            }
        }
        protected override bool IsEqualTo(DataDefinition other)
        {
            if (!(other is FixedLengthDataDefinition))
            {
                return(false);
            }

            return(Length == (other as FixedLengthDataDefinition).Length);
        }
        protected override bool IsEqualTo(DataDefinition other)
        {
            if (!(other is VariableLengthDataDefinition))
            {
                return(false);
            }

            VariableLengthDataDefinition theOther = other as VariableLengthDataDefinition;

            return(LenthType == theOther.LenthType && _maxLength == theOther._maxLength);
        }
        private bool AreValidSubElements(DataDefinition definition, HashSet <DataElement> subEmelemnts)
        {
            if (definition.SubDefinitions.Count != subEmelemnts.Count)
            {
                return(false);
            }

            foreach (DataElement de in subEmelemnts)
            {
                if (!definition.SubDefinitions[de.Number].Equals(de.Definition))
                {
                    return(false);
                }
            }

            return(true);
        }
        public DataElement(int number, DataDefinition dataDefinition, HashSet <DataElement> subElements)
        {
            SubElements = new DataElementCollection();
            Number      = number;
            Definition  = dataDefinition;

            if (AreValidSubElements(dataDefinition, subElements))
            {
                foreach (DataElement dataElement in subElements)
                {
                    SubElements.AddOrReplaceDataElement(dataElement);
                }
            }
            else
            {
                throw new ArgumentException("DataDefinition does not match subElements definition.");
            }
        }
        public DataElement(int number, DataDefinition dataDefinition, DataString data)
        {
            SubElements = new DataElementCollection();
            Number      = number;
            Definition  = dataDefinition;

            if (dataDefinition.HasSubfields())
            {
                foreach (KeyValuePair <int, DataDefinition> kvp in dataDefinition.SubDefinitions)
                {
                    int            fieldNumber     = kvp.Key;
                    DataDefinition fieldDefinition = kvp.Value;
                    DataString     fieldData       = dataDefinition.GetSubFieldData(data, fieldNumber);
                    SubElements.AddOrReplaceDataElement(new DataElement(fieldNumber, fieldDefinition, fieldData));
                }
            }
            else
            {
                _data = data;
            }
        }