Beispiel #1
0
        /// <summary>
        /// Create the comma-delimited representation of a set of data and escapes.
        /// </summary>
        /// <remarks>
        /// The escapes are inserted into the comma-delimited list in the appropriate places.
        /// Any escape that occurs after the last element is ignored.
        /// </remarks>
        /// <param name="values">The collection of values to store.</param>
        /// <exception cref="ArgumentNullException">
        /// If the parameter <paramref name="values"/> is null.
        /// </exception>
        protected void SetOtherData(IList <OtherItemDataCsvItem> values)
        {
            Validator.ThrowIfArgumentNull(values, nameof(values), Resources.OtherItemDataValuesNull);

            StringBuilder builder = new StringBuilder();

            int currentItemIndex = 0;

            foreach (OtherItemDataCsvItem item in values)
            {
                if (currentItemIndex != 0)
                {
                    builder.Append(",");
                }

                OtherItemDataCsvEscape itemEscape = item as OtherItemDataCsvEscape;
                if (itemEscape != null)
                {
                    string name  = itemEscape.Name.Replace("=", @"\=");
                    string value = itemEscape.Value.Replace("=", @"\=");
                    builder.Append(name);
                    builder.Append("=");
                    builder.Append(value);
                }

                OtherItemDataCsvDouble itemDouble = item as OtherItemDataCsvDouble;
                if (itemDouble != null)
                {
                    builder.Append(itemDouble.Value.ToString());
                }

                OtherItemDataCsvString itemString = item as OtherItemDataCsvString;
                if (itemString != null)
                {
                    string value = itemString.Value.Replace("=", @"\=");
                    value = itemString.Value.Replace(",", @"\,");
                    builder.Append(value);
                }

                currentItemIndex++;
            }

            Data            = builder.ToString();
            ContentType     = "text/csv";
            ContentEncoding = string.Empty;
        }
Beispiel #2
0
        /// <summary>
        /// Parse the comma-separated representation into an array of strings.
        /// </summary>
        /// <remarks>
        /// When this method returns, the Escapes collection will contain any
        /// escapes encountered during the parsing.
        /// </remarks>
        /// <returns>A collection of the strings.</returns>
        /// <exception cref="ArgumentException">
        /// If the content type is not "text/csv".
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// If the Data section is null.
        /// </exception>
        protected Collection <OtherItemDataCsvItem> GetAsString()
        {
            if (ContentType != "text/csv")
            {
                throw new ArgumentException(Resources.OtherItemDataFormat, nameof(ContentType));
            }

            Validator.ThrowIfArgumentNull(Data, nameof(Data), Resources.OtherItemDataNull);

            Collection <OtherItemDataCsvItem> values = new Collection <OtherItemDataCsvItem>();

            List <string> stringValues = BreakStringAtCharacter(Data, ',');

            for (int i = 0; i < stringValues.Count; i++)
            {
                // Get current value, remove any comma escapes (no longer needed)...
                string current = stringValues[i].Replace(@"\,", ",");

                // See if this is a name=value escape...
                List <string> escapeParts = BreakStringAtCharacter(current, '=');

                for (int parts = 0; parts < escapeParts.Count; parts++)
                {
                    escapeParts[0] = escapeParts[0].Replace(@"\=", "=");
                    escapeParts[0] = escapeParts[0].Replace(@"\\", @"\");
                }

                if (escapeParts.Count >= 2)
                {
                    OtherItemDataCsvEscape escapeItem = new OtherItemDataCsvEscape(escapeParts[0], escapeParts[1]);

                    values.Add(escapeItem);
                }
                else
                {
                    values.Add(new OtherItemDataCsvString(escapeParts[0]));
                }
            }

            return(values);
        }