Example #1
0
        private static IEnumerable <DynamicPropertyData> ParseDynamicProperties(IEnumerable <string> lines)
        {
            var result = new List <DynamicPropertyData>();

            var parsingBinaryProperties   = false;
            var parsingLongTextProperties = false;
            var parsingDynamicProperties  = false;
            DynamicPropertyData data      = null;
            var lineNumber = 0;

            foreach (var line in lines)
            {
                lineNumber++;
                if (line.Trim().Length == 0)
                {
                    continue;
                }
                if (line.StartsWith("VersionId: "))
                {
                    parsingBinaryProperties   = false;
                    parsingLongTextProperties = false;
                    parsingDynamicProperties  = false;
                    var versionId = int.Parse(line.Substring(11));
                    data = new DynamicPropertyData {
                        VersionId = versionId
                    };
                    result.Add(data);
                }
                else if (line == "    BinaryProperties")
                {
                    parsingBinaryProperties   = true;
                    parsingLongTextProperties = false;
                    parsingDynamicProperties  = false;
                    data.BinaryProperties     = new Dictionary <PropertyType, BinaryDataValue>();
                }
                else if (line == "    LongTextProperties")
                {
                    parsingBinaryProperties   = false;
                    parsingLongTextProperties = true;
                    parsingDynamicProperties  = false;
                    data.LongTextProperties   = new Dictionary <PropertyType, string>();
                }
                else if (line == "    DynamicProperties")
                {
                    parsingBinaryProperties   = false;
                    parsingLongTextProperties = false;
                    parsingDynamicProperties  = true;
                    data.DynamicProperties    = new Dictionary <PropertyType, object>();
                }
                else if (line.StartsWith("        "))
                {
                    if (parsingBinaryProperties)
                    {
                        // name    id, file, size, filename,    mime,                     stream reference
                        // Binary: #82, F82, 0L, OAuth.settings, application/octet-stream, /Root/System/Settings/OAuth.settings
                        var src          = line.Split(':', ',');
                        var propertyType = data.EnsurePropertyType(src[0].Trim(), DataType.Binary);
                        data.BinaryProperties.Add(propertyType, new BinaryDataValue
                        {
                            Id               = int.Parse(src[1].Trim().Substring(1)),
                            FileId           = int.Parse(src[2].Trim().Substring(1)),
                            Size             = long.Parse(src[3].Trim().TrimEnd('L')),
                            FileName         = src[4].Trim(),
                            ContentType      = src[5].Trim(),
                            BlobProviderData = src[6].Trim()
                        });
                    }
                    else if (parsingLongTextProperties)
                    {
                        // OldPasswords: <?xml version="1.0" encoding="utf-16"?>  <ArrayOfOldPasswordData ....
                        var p    = line.IndexOf(':');
                        var name = line.Substring(0, p).Trim();

                        var value        = line.Substring(p + 1).Trim();
                        var propertyType = data.EnsurePropertyType(name, DataType.Text);

                        data.LongTextProperties.Add(propertyType, value);
                    }
                    else if (parsingDynamicProperties)
                    {
                        // LastLoggedOut:DateTime: 2018-11-14T02:54:03.0000000Z
                        var p    = line.IndexOf(':');
                        var name = line.Substring(0, p).Trim();

                        var p1       = line.IndexOf(':', p + 1);
                        var dataType = (DataType)Enum.Parse(typeof(DataType), line.Substring(p + 1, p1 - p - 1));

                        var value        = ParseDynamicValue(line.Substring(p1 + 1).Trim(), dataType);
                        var propertyType = data.EnsurePropertyType(name, dataType);

                        data.DynamicProperties.Add(propertyType, value);
                    }
                    else
                    {
                        throw GetCannotParseDynamicPropertiesException(lineNumber, line);
                    }
                }
                else
                {
                    throw GetCannotParseDynamicPropertiesException(lineNumber, line);
                }
            }

            return(result);
        }
Example #2
0
        private static void Write(TextWriter writer, string path, DynamicPropertyData d)
        {
            var relevantBinaries =
                d.BinaryProperties.Where(x => x.Value != null).ToDictionary(x => x.Key, x => x.Value);

            var transformedLongText = new Dictionary <string, string>();

            foreach (var item in d.LongTextProperties)
            {
                if (item.Value == null)
                {
                    continue;
                }
                var value = ValueToString(item);
                if (value != null)
                {
                    transformedLongText.Add($"{item.Key.Name}", value);
                }
            }
            var transformedDynamic = new Dictionary <string, string>();

            foreach (var item in d.DynamicProperties)
            {
                if (item.Value == null)
                {
                    continue;
                }
                var value = ValueToString(item);
                if (value != null)
                {
                    transformedDynamic.Add($"{item.Key.Name}:{item.Key.DataType}", value);
                }
            }
            foreach (var item in d.ReferenceProperties)
            {
                if (item.Value == null || item.Value.Count == 0)
                {
                    continue;
                }
                var value = ValueToString(item);
                if (value != null)
                {
                    transformedDynamic.Add($"{item.Key.Name}:{item.Key.DataType}", value);
                }
            }

            if (relevantBinaries.Count + transformedLongText.Count + transformedDynamic.Count > 0)
            {
                writer.WriteLine($"VersionId: {d.VersionId}");

                if (relevantBinaries.Count > 0)
                {
                    writer.WriteLine("    BinaryProperties");
                    foreach (var item in relevantBinaries)
                    {
                        writer.WriteLine($"        {item.Key.Name}: {ValueToString(path, item.Value)}");
                    }
                }
                if (transformedLongText.Count > 0)
                {
                    writer.WriteLine("    LongTextProperties");
                    foreach (var item in transformedLongText)
                    {
                        writer.WriteLine($"        {item.Key}: {item.Value}");
                    }
                }
                if (transformedDynamic.Count > 0)
                {
                    writer.WriteLine("    DynamicProperties");
                    foreach (var item in transformedDynamic)
                    {
                        writer.WriteLine($"        {item.Key}: {item.Value}");
                    }
                }
            }
        }