Esempio n. 1
0
        private bool ParseSegmentName()
        {
            char firstChar = _chars[_charPos];

            ShiftBufferIfNeeded();
            ReadStringIntoBuffer();

            string segmentName;

            if (NameTable != null)
            {
                segmentName = NameTable.Get(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length);
                // no match in name table
                if (segmentName == null)
                {
                    segmentName = _stringReference.ToString();
                }
            }
            else
            {
                segmentName = _stringReference.ToString();
            }
            EatWhitespace(false);

            if (Grammar.SegmentNameDelimiter != _chars[_charPos])
            {
                throw EdiReaderException.Create(this, "Invalid character after parsing segment name. Expected '{1}' but got: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos], string.Join("', '", Grammar.DataElementSeparator)));
            }

            SetToken(EdiToken.SegmentName, segmentName);

            ClearRecentString();
            return(true);
        }
Esempio n. 2
0
        internal decimal?ReadAsDecimalInternal(Picture?picture)
        {
            EdiToken t;

            if (!ReadInternal())
            {
                SetToken(EdiToken.None);
                return(null);
            }

            t = TokenType;
            if (t == EdiToken.Null)
            {
                return(null);
            }
            if (t == EdiToken.String)
            {
                string s = (string)Value;
                if (string.IsNullOrEmpty(s))
                {
                    SetToken(EdiToken.Null);
                    return(null);
                }
                decimal d;
                if (s.TryParse(picture, Culture, out d))
                {
                    SetToken(EdiToken.Float, d, false);
                    return(d);
                }
                throw EdiReaderException.Create(this, "Could not convert string to decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
            }
            throw EdiReaderException.Create(this, "Error reading decimal. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Esempio n. 3
0
        internal override bool ReadInternal()
        {
            while (true)
            {
                switch (_currentState)
                {
                case State.Start:
                    return(ParseServiceStringAdvice());

                case State.Segment:
                case State.SegmentStart:
                    return(ParseSegment());

                case State.Element:
                case State.ElementStart:
                case State.Component:
                case State.ComponentStart:
                    return(ParseValue());

                case State.SegmentName:
                case State.PostValue:
                    // returns true if it hits
                    // end of object or array
                    if (ParsePostValue())
                    {
                        return(true);
                    }
                    break;

                case State.Finished:
                    if (EnsureChars(0, false))
                    {
                        EatWhitespace(false);
                        if (_isEndOfFile)
                        {
                            return(false);
                        }
                        throw EdiReaderException.Create(this, "Additional text encountered after finished reading EDI content: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos]));
                    }
                    return(false);

                default:
                    throw EdiReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState));
                }
            }
        }
Esempio n. 4
0
        private char ParseUnicode()
        {
            char writeChar;

            if (EnsureChars(4, true))
            {
                string hexValues = new string(_chars, _charPos, 4);
                char   hexChar   = Convert.ToChar(int.Parse(hexValues, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo));
                writeChar = hexChar;

                _charPos += 4;
            }
            else
            {
                throw EdiReaderException.Create(this, "Unexpected end while parsing unicode character.");
            }
            return(writeChar);
        }
Esempio n. 5
0
        internal string ReadAsStringInternal()
        {
            EdiToken t;

            if (!ReadInternal())
            {
                SetToken(EdiToken.None);
                return(null);
            }
            else
            {
                t = TokenType;
            }
            if (t == EdiToken.String)
            {
                return((string)Value);
            }

            if (t == EdiToken.Null)
            {
                return(null);
            }

            if (t.IsPrimitiveToken())
            {
                if (Value != null)
                {
                    string s;
                    if (Value is IFormattable)
                    {
                        s = ((IFormattable)Value).ToString(null, Culture);
                    }
                    else
                    {
                        s = Value.ToString();
                    }

                    SetToken(EdiToken.String, s, false);
                    return(s);
                }
            }
            throw EdiReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Esempio n. 6
0
        private void Push(EdiContainerType value)
        {
            if (_currentPosition.Type == EdiContainerType.None)
            {
                _currentPosition = new EdiPosition(value);
            }
            else
            {
                _stack.Add(_currentPosition);
                _currentPosition = new EdiPosition(value);

                // this is a little hacky because Depth increases when first property/value is written but only testing here is faster/simpler
                if (_maxDepth != null && Depth + 1 > _maxDepth && !_hasExceededMaxDepth)
                {
                    _hasExceededMaxDepth = true;
                    throw EdiReaderException.Create(this, "The reader's MaxDepth of {0} has been exceeded.".FormatWith(CultureInfo.InvariantCulture, _maxDepth));
                }
            }
        }
Esempio n. 7
0
        internal long?ReadAsInt64Internal()
        {
            EdiToken t;

            if (!ReadInternal())
            {
                SetToken(EdiToken.None);
                return(null);
            }
            else
            {
                t = TokenType;
            }
            if (t == EdiToken.Null)
            {
                return(null);
            }
            long i;

            if (t == EdiToken.String)
            {
                string s = (string)Value;
                if (s != null)
                {
                    s = s.TrimStart('Z'); // Z suppresses leading zeros
                }
                if (string.IsNullOrEmpty(s))
                {
                    SetToken(EdiToken.Null);
                    return(null);
                }
                if (long.TryParse(s, NumberStyles.Integer, Culture, out i))
                {
                    SetToken(EdiToken.Integer, i, false);
                    return(i);
                }
                throw EdiReaderException.Create(this, "Could not convert string to Int64: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
            }
            throw EdiReaderException.Create(this, "Error reading Int64. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
        }
Esempio n. 8
0
        internal DateTime?ReadAsDateTimeInternal()
        {
            if (!ReadInternal())
            {
                SetToken(EdiToken.None);
                return(null);
            }
            if (TokenType == EdiToken.Date)
            {
                return((DateTime)Value);
            }

            if (TokenType == EdiToken.Null)
            {
                return(null);
            }

            if (TokenType == EdiToken.String)
            {
                string s = (string)Value;
                if (string.IsNullOrEmpty(s))
                {
                    SetToken(EdiToken.Null);
                    return(null);
                }
                DateTime dt;

                if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
                {
                    SetToken(EdiToken.Date, dt, false);
                    return(dt);
                }
                throw EdiReaderException.Create(this, "Could not convert string to DateTime: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
            }

            throw EdiReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
        }
Esempio n. 9
0
        internal int?ReadAsInt32Internal()
        {
            EdiToken t;

            if (!ReadInternal())
            {
                SetToken(EdiToken.None);
                return(null);
            }
            else
            {
                t = TokenType;
            }
            if (t == EdiToken.Null)
            {
                return(null);
            }
            int i;

            if (t == EdiToken.String)
            {
                string s = (string)Value;
                if (string.IsNullOrEmpty(s))
                {
                    SetToken(EdiToken.Null);
                    return(null);
                }
                if (int.TryParse(s, NumberStyles.Integer, Culture, out i))
                {
                    SetToken(EdiToken.Integer, i, false);
                    return(i);
                }
                throw EdiReaderException.Create(this, "Could not convert string to integer: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
            }
            throw EdiReaderException.Create(this, "Error reading integer. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
        }
Esempio n. 10
0
        private void ReadStringIntoBuffer()
        {
            int          charPos           = _charPos;
            int          initialPosition   = _charPos;
            int          lastWritePosition = _charPos;
            StringBuffer buffer            = null;

            while (true)
            {
                var charAt = _chars[charPos++];
                if ('\0' == charAt)
                {
                    if (_charsUsed == charPos - 1)
                    {
                        charPos--;

                        if (ReadData(true) == 0)
                        {
                            _charPos = charPos;
                            throw EdiReaderException.Create(this, "Unterminated string. Expected delimiter.");
                        }
                    }
                }
                // Make use of the release character. Identify escape sequence
                else if (Grammar.ReleaseCharacter == charAt)
                {
                    _charPos = charPos;
                    if (!EnsureChars(0, true))
                    {
                        _charPos = charPos;
                        throw EdiReaderException.Create(this, "Unterminated string. Expected delimiter.");
                    }

                    // start of escape sequence
                    int escapeStartPos = charPos - 1;

                    char currentChar = _chars[charPos];

                    char writeChar;

                    if (Grammar.IsSpecial(currentChar) || Grammar.ReleaseCharacter == currentChar)
                    {
                        charPos++;
                        writeChar = currentChar;
                    }
                    else
                    {
                        charPos++;
                        _charPos = charPos;
                        throw EdiReaderException.Create(this, "Bad EDI escape sequence: {0}{1}.".FormatWith(CultureInfo.InvariantCulture, Grammar.ReleaseCharacter, currentChar));
                    }

                    if (buffer == null)
                    {
                        buffer = GetBuffer();
                    }

                    WriteCharToBuffer(buffer, writeChar, lastWritePosition, escapeStartPos);

                    lastWritePosition = charPos;
                }
                else if (StringUtils.CarriageReturn == charAt && !Grammar.IsSpecial(charAt))
                {
                    _charPos = charPos - 1;
                    ProcessCarriageReturn(true);
                    charPos = _charPos;
                }
                else if (StringUtils.LineFeed == charAt && !Grammar.IsSpecial(charAt))
                {
                    _charPos = charPos - 1;
                    ProcessLineFeed();
                    charPos = _charPos;
                }
                else if (Grammar.IsSpecial(charAt))
                {
                    if (StringUtils.LineFeed == charAt)
                    {
                        ProcessLineFeed(forwardPosition: false);
                    }
                    charPos--;
                    if (initialPosition == lastWritePosition)
                    {
                        _stringReference = new StringReference(_chars, initialPosition, charPos - initialPosition);
                    }
                    else
                    {
                        if (buffer == null)
                        {
                            buffer = GetBuffer();
                        }

                        if (charPos > lastWritePosition)
                        {
                            buffer.Append(_chars, lastWritePosition, charPos - lastWritePosition);
                        }

                        _stringReference = new StringReference(buffer.GetInternalBuffer(), 0, buffer.Position);
                    }
                    _charPos = charPos;
                    return;
                }
            }
        }
Esempio n. 11
0
        private bool ParsePostValue()
        {
            while (true)
            {
                char currentChar = _chars[_charPos];

                switch (currentChar)
                {
                case ' ':
                case StringUtils.Tab:
                    // eat
                    _charPos++;
                    break;

                case StringUtils.CarriageReturn:
                    if (Grammar.SegmentTerminator == currentChar)
                    {
                        SetToken(EdiToken.SegmentStart);
                        ProcessCarriageReturn(false);
                        return(true);
                    }
                    ProcessCarriageReturn(false);
                    break;

                case StringUtils.LineFeed:
                    if (Grammar.SegmentTerminator == currentChar)
                    {
                        SetToken(EdiToken.SegmentStart);
                        ProcessLineFeed();
                        return(true);
                    }
                    ProcessLineFeed();
                    break;

                default:
                    if (char.IsWhiteSpace(currentChar))
                    {
                        // eat
                        _charPos++;
                        break;
                    }
                    if (Grammar.IsSpecial(currentChar))
                    {
                        if (Grammar.ComponentDataElementSeparator == currentChar)
                        {
                            _charPos++;
                            // finished parsing
                            SetToken(EdiToken.ComponentStart);
                            return(true);
                        }
                        else if (Grammar.DataElementSeparator == currentChar || Grammar.SegmentNameDelimiter == currentChar)
                        {
                            _charPos++;
                            SetToken(EdiToken.ElementStart);
                            return(true);
                        }
                        else if (Grammar.SegmentTerminator == currentChar)
                        {
                            _charPos++;
                            SetToken(EdiToken.SegmentStart);
                            return(true);
                        }
                        else
                        {
                            throw EdiReaderException.Create(this, "After parsing a value an unexpected character was encountered: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
                        }
                    }
                    else
                    {
                        throw EdiReaderException.Create(this, "After parsing a value an unexpected character was encountered: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
                    }
                }
            }
        }