Пример #1
0
        public StringBuilder encode(StringBuilder finalSB, ClassicEncodingSettings settings, bool isTransferEncode, int encodeLevel)
        {
            if (finalSB.Length + this.getEstimateDataSize() > finalSB.Capacity)
            {
                finalSB.EnsureCapacity(finalSB.Capacity + getEstimateDataSize());
            }

            int?formatId = null;

            bool formatWasInserted = false;

            bool needToInsertFormat = settings != null && settings.isEncodeFormat();

            bool isKnown = false;

            KnownFormatCollector collector = settings != null?settings.getKnownFormatCollector() : null;


            if (needToInsertFormat)
            {
                if (this.getFormat().getFieldCount() > 0 && settings.getFormatCache() != null)
                {
                    formatId = settings.getFormatCache().addIfNotExists(this.getFormat());

                    if (collector != null)
                    {
                        needToInsertFormat = false;

                        if (collector.isKnown(formatId) && collector.isMarked(formatId))
                        {
                            // Format is known - inserting ID only
                            new Element(ELEMENT_FORMAT_ID, formatId.ToString()).encode(finalSB, settings, isTransferEncode, encodeLevel);

                            isKnown = true;
                        }
                        else
                        {
                            var oldEncodeFormat = settings.isEncodeFormat();
                            settings.setEncodeFormat(true);

                            try
                            {
                                // Format is not known - inserting both format and ID
                                new Element(ELEMENT_FORMAT, this.getFormat()).encode(finalSB, settings, isTransferEncode, encodeLevel);
                                new Element(ELEMENT_FORMAT_ID, formatId.ToString()).encode(finalSB, settings, isTransferEncode, encodeLevel);

                                formatWasInserted = true;
                            }
                            finally
                            {
                                settings.setEncodeFormat((bool)oldEncodeFormat);
                            }
                        }
                    }
                }

                if (needToInsertFormat)
                {
                    var oldEncodeFormat = settings.isEncodeFormat();
                    settings.setEncodeFormat(true);
                    try
                    {
                        new Element(ELEMENT_FORMAT, this.getFormat()).encode(finalSB, settings, isTransferEncode, encodeLevel);

                        formatWasInserted = true;
                    }
                    finally
                    {
                        settings.setEncodeFormat((bool)oldEncodeFormat);
                    }
                }
            }

            bool?oldEncodeFormat1 = null;

            if (settings != null)
            {
                oldEncodeFormat1 = settings.isEncodeFormat();
            }
            try
            {
                if (formatWasInserted)
                {
                    settings.setEncodeFormat(false);
                }

                this.getEncodedData(finalSB, settings, isTransferEncode, encodeLevel + 1);
            }
            finally
            {
                if (oldEncodeFormat1 != null)
                {
                    settings.setEncodeFormat((bool)oldEncodeFormat1);
                }
            }

            if (isInvalid())
            {
                new Element(ELEMENT_FORMAT, this.invalidationMessage).encode(finalSB, settings, isTransferEncode, encodeLevel);
            }


            if (!isKnown && formatId != null && collector != null)
            {
                // Marking format as known
                collector.makeKnown((int)formatId, true);
            }

            return(finalSB);
        }
Пример #2
0
        public DataTable(string data, ClassicEncodingSettings settings, bool validate)
        {
            if (data == null)
            {
                return;
            }

            var           found         = false;
            string        encodedFormat = null;
            List <string> fieldNames    = null;

            bool flag = false;

            if (settings != null)
            {
                flag = settings.isUseVisibleSeparators();
            }

            var recs = StringUtils.elements(data, flag);

            foreach (var el in recs)
            {
                if (el.getName() == null)
                {
                    continue;
                }

                if (el.getName().Equals(ELEMENT_FORMAT_ID))
                {
                    var formatId = int.Parse(el.getValue());

                    if (settings == null || settings.getFormatCache() == null)
                    {
                        throw new InvalidOperationException("Can't use format ID - format cache not found");
                    }

                    if (encodedFormat != null)
                    {
                        // If format was already found in the encoded data
                        var newFormat1 = new TableFormat(encodedFormat, settings, validate);
                        settings.getFormatCache().put(formatId, newFormat1);
                        continue;
                    }

                    var newFormat = settings.getFormatCache().get(formatId);

                    // encodedFormat = settings.getFormatCache().get(formatId);
                    if (newFormat == null)
                    {
                        throw new InvalidOperationException(
                                  "Format with specified ID not found in the cache: " + formatId);
                    }

                    this.setFormat(newFormat);
                    found = true;
                    continue;
                }

                if (el.getName().Equals(ELEMENT_FORMAT))
                {
                    encodedFormat = el.getValue();
                    setFormat(new TableFormat(encodedFormat, settings, validate));
                    found = true;
                    continue;
                }

                if (el.getName().Equals(ELEMENT_RECORD))
                {
                    // Using table's format if encodedFormat is not NULL (i.e. was found in the encoded data)
                    var fmt = found ? this.getFormat() : (settings != null ? settings.getFormat() : null);

                    if (fmt == null)
                    {
                        throw new InvalidOperationException(
                                  "Table format is neither found in encoded table nor provided by decoding environment");
                    }

                    addRecord(new DataRecord(fmt, el.getValue(), settings, validate, fieldNames));
                    continue;
                }

                if (el.getName().Equals(ELEMENT_FIELD_NAME))
                {
                    if (fieldNames == null)
                    {
                        fieldNames = new List <string>();
                    }
                    fieldNames.Add(el.getValue());
                    continue;
                }

                if (el.getName().Equals(ELEMENT_TIMESTAMP))
                {
                    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                    this.timestamp = epoch.AddSeconds(Convert.ToDouble(el.getValue()) / 1000).AddMilliseconds(Convert.ToDouble(el.getValue()) % 1000);
                    continue;
                }

                if (el.getName().Equals(ELEMENT_QUALITY))
                {
                    this.quality = Convert.ToInt32(el.getValue());
                    continue;
                }

                if (el.getName().Equals(ELEMENT_INVALIDATOR))
                {
                    this.invalidationMessage = el.getValue();
                }
            }
        }