Exemple #1
0
 void AssertTwoDataElementErrorsAreEqual(DataElementErrorContext context, DataElementErrorContext other)
 {
     Assert.AreEqual(context.Code, other.Code);
     Assert.AreEqual(context.ComponentPosition, other.ComponentPosition);
     Assert.AreEqual(context.Name, other.Name);
     Assert.AreEqual(context.Position, other.Position);
     Assert.AreEqual(context.RepetitionPosition, other.RepetitionPosition);
     Assert.AreEqual(context.Value, other.Value);
 }
Exemple #2
0
        private SegmentErrorContext ValidateDataElement(IList list, InstanceContext instanceContext,
                                                        int segmentIndex, int inSegmentIndex, int inCompositeIndex)
        {
            if (instanceContext.Parent == null)
            {
                throw new Exception(
                          string.Format("Parent of data element {0} must be either a segment or a composite.",
                                        instanceContext.Property.Name));
            }

            var errorCode = list.Count > MaxCount
                ? DataElementErrorCode.TooManyRepetitions
                : DataElementErrorCode.TooFewRepetitions;

            var repIndex = list.Count > MaxCount
                ? MaxCount + 1
                : MinCount - list.Count + 1;

            var value = list.Count > MaxCount
                ? list[MaxCount] as string
                : null;

            var segmentName = instanceContext.Parent.IsPropertyOfType <SegmentAttribute>()
                ? instanceContext.Parent.GetId()
                : instanceContext.Parent.GetDeclaringTypeId();

            var segmentType = instanceContext.Parent.IsPropertyOfType <SegmentAttribute>()
                ? instanceContext.Parent.GetStandardType()
                : instanceContext.Parent.Property.GetStandardDeclaringType();

            if (string.IsNullOrEmpty(segmentName) && instanceContext.Parent.Instance != null)
            {
                var type         = instanceContext.Parent.Instance.GetStandardType();
                var ediAttribute = type.GetCustomAttribute <EdiAttribute>();
                if (ediAttribute == null)
                {
                    throw new Exception(string.Format("Can't find segment name for {0}", GetType().Name));
                }

                segmentName = ediAttribute.Id;
                segmentType = type;
            }

            var dataElementAttr = instanceContext.Property.GetCustomAttribute <DataElementAttribute>();
            var name            = dataElementAttr == null ? "" : dataElementAttr.Code;

            var result       = new SegmentErrorContext(segmentName, segmentIndex, segmentType);
            var errorContext = new DataElementErrorContext(name, inSegmentIndex, errorCode, inCompositeIndex, repIndex, value);

            result.Add(errorContext);
            return(result);
        }
Exemple #3
0
        public void TestSerializationOfDataElementErrorContext()
        {
            var context = new DataElementErrorContext("Name", 66, ErrorCodes.ComponentDataElementsTooMany, 32, 96, "876");

            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, context);
            stream.Seek(0, SeekOrigin.Begin);
            var copy = (DataElementErrorContext)formatter.Deserialize(stream);

            AssertTwoDataElementErrorsAreEqual(context, copy);
        }
Exemple #4
0
        private SegmentErrorContext ValidateComposite(InstanceContext instanceContext,
                                                      int segmentIndex, int inSegmentIndex)
        {
            if (instanceContext.Parent == null || !instanceContext.Parent.IsPropertyOfType <SegmentAttribute>())
            {
                throw new Exception(string.Format("Parent of composite {0} must be a segment.",
                                                  instanceContext.Property.Name));
            }

            var result       = new SegmentErrorContext(instanceContext.Parent.GetId(), segmentIndex, instanceContext.Parent.GetStandardType());
            var errorContext = new DataElementErrorContext(instanceContext.GetId(), inSegmentIndex,
                                                           DataElementErrorCode.RequiredDataElementMissing, 0, 0, null);

            result.Add(errorContext);

            return(result);
        }
        private SegmentErrorContext ValidateDataElement(string value, InstanceContext instanceContext,
                                                        int segmentIndex, int inSegmentIndex, int inCompositeIndex, int repetitionIndex)
        {
            if (instanceContext.Parent == null)
            {
                throw new Exception(
                          string.Format("Parent of data element {0} must be either a segment or a composite.",
                                        instanceContext.Property.Name));
            }

            var errorCode = value.Length < MinLen
                ? DataElementErrorCode.DataElementTooShort
                : DataElementErrorCode.DataElementTooLong;

            var segmentName = instanceContext.Parent.IsPropertyOfType <SegmentAttribute>()
                ? instanceContext.Parent.GetId()
                : instanceContext.Parent.GetDeclaringTypeId();

            var segmentType = instanceContext.Parent.IsPropertyOfType <SegmentAttribute>()
                ? instanceContext.Parent.GetStandardType()
                : instanceContext.Parent.Property.GetStandardDeclaringType();

            if (string.IsNullOrEmpty(segmentName) && instanceContext.Parent.Instance != null)
            {
                var type         = instanceContext.Parent.Instance.GetStandardType();
                var ediAttribute = type.GetCustomAttribute <EdiAttribute>();
                if (ediAttribute == null)
                {
                    throw new Exception(string.Format("Can't find segment name for {0}", GetType().Name));
                }

                segmentName = ediAttribute.Id;
                segmentType = type;
            }

            var dataElementAttr = instanceContext.Property.GetCustomAttribute <DataElementAttribute>();
            var name            = dataElementAttr == null ? "" : dataElementAttr.Code;

            var result       = new SegmentErrorContext(segmentName, segmentIndex, segmentType);
            var errorContext = new DataElementErrorContext(name, inSegmentIndex, errorCode, inCompositeIndex,
                                                           repetitionIndex, value);

            result.Add(errorContext);
            return(result);
        }
Exemple #6
0
        public override void Parse(string value, bool allowPartial)
        {
            if (String.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException("value");
            }

            if (!Children.Any())
            {
                BuildChildren();
            }

            SetParsed();

            var remainder = value;
            var index     = 0;

            foreach (var child in Children.OfType <DataElement>())
            {
                index++;
                if (child.MaxSize == 0)
                {
                    throw new Exception(string.Format("DataElement {0} MaxSize is 0.", child.EdiName));
                }

                if (remainder.Length < child.MaxSize)
                {
                    if (allowPartial)
                    {
                        continue;
                    }

                    var errorContext = new DataElementErrorContext("", index, DataElementErrorCode.TooManyDataElements,
                                                                   0, 0, remainder, "Too many Data Elements");
                    throw new ParserSegmentException(errorContext);
                }

                var current = remainder.Substring(0, child.MaxSize);
                remainder = remainder.Substring(child.MaxSize);
                child.Parse(current, allowPartial);
            }
        }
Exemple #7
0
        private SegmentErrorContext ValidateComposite(IList list, InstanceContext instanceContext,
                                                      int segmentIndex, int inSegmentIndex)
        {
            if (instanceContext.Parent == null || !instanceContext.Parent.IsPropertyOfType <SegmentAttribute>())
            {
                throw new Exception(string.Format("Parent of composite {0} must be a segment.",
                                                  instanceContext.Property.Name));
            }

            var errorCode = list.Count > MaxCount
                ? DataElementErrorCode.TooManyRepetitions
                : DataElementErrorCode.TooFewRepetitions;

            var repIndex = list.Count > MaxCount
                ? MaxCount + 1
                : MinCount - list.Count + 1;

            var result       = new SegmentErrorContext(instanceContext.Parent.GetId(), segmentIndex, instanceContext.Parent.GetStandardType());
            var errorContext = new DataElementErrorContext(instanceContext.GetId(), inSegmentIndex, errorCode, 0,
                                                           repIndex, null);

            result.Add(errorContext);
            return(result);
        }
Exemple #8
0
        public override void Parse(string value, Separators separators, bool allowPartial)
        {
            if (String.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException("value");
            }
            if (separators == null)
            {
                throw new ArgumentNullException("separators");
            }

            if (!Children.Any())
            {
                BuildChildren();
            }

            SetParsed();

            var index = 0;

            foreach (var currentToParse in value.GetDataElements(separators))
            {
                if (String.IsNullOrEmpty(currentToParse))
                {
                    index++;
                    continue;
                }

                if (index >= Children.Count)
                {
                    if (allowPartial)
                    {
                        continue;
                    }

                    var errorContext = new DataElementErrorContext("", index, DataElementErrorCode.TooManyDataElements,
                                                                   0, 0, currentToParse, "Too many Data Elements");
                    throw new ParserSegmentException(errorContext);
                }

                var currentElement = Children.ElementAt(index);

                var repetitions = currentElement.IsX12RepetitionSeparator()
                    ? new List <string> {
                    currentToParse
                }
                    : currentToParse.GetRepetitions(separators);

                var repIndex = 0;
                foreach (var repetition in repetitions)
                {
                    repIndex++;
                    if (String.IsNullOrEmpty(repetition))
                    {
                        continue;
                    }
                    if (currentElement.IsParsed)
                    {
                        currentElement = currentElement.InsertRepetition();
                        index++;
                    }
                    try
                    {
                        currentElement.Parse(repetition, separators, allowPartial);
                    }
                    catch (ParserElementException ex)
                    {
                        var errorContext = new DataElementErrorContext(currentElement.EdiName, index, ex.ErrorCode,
                                                                       ex.ComponentPosition, repIndex, repetition, ex.Message);
                        throw new ParserSegmentException(errorContext);
                    }
                }
                index++;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ParserSegmentException"/> class.
 /// </summary>
 /// <param name="errorContext">The data element error context.</param>
 public ParserSegmentException(DataElementErrorContext errorContext)
     : base(errorContext.Message)
 {
     ErrorContext = errorContext;
 }