Beispiel #1
0
        public static string ToXml
        (
            [NotNull] this MenuFile menu
        )
        {
            Sure.NotNull(menu, nameof(menu));

            XmlWriterSettings settings = new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent             = false,
                NewLineHandling    = NewLineHandling.None
            };
            StringBuilder           output     = new StringBuilder();
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            namespaces.Add(string.Empty, string.Empty);
            using (XmlWriter writer = XmlWriter.Create(output, settings))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MenuFile));
                serializer.Serialize(writer, menu, namespaces);
            }

            return(output.ToString());
        }
Beispiel #2
0
        public static MenuFile ParseStream
        (
            [NotNull] TextReader reader
        )
        {
            Sure.NotNull(reader, nameof(reader));

            MenuFile result = new MenuFile();

            while (true)
            {
                string code = reader.ReadLine();
                if (string.IsNullOrEmpty(code))
                {
                    break;
                }
                if (code.StartsWith(StopMarker))
                {
                    break;
                }

                string    comment = reader.RequireLine();
                MenuEntry entry   = new MenuEntry
                {
                    Code    = code,
                    Comment = comment
                };
                result.Entries.Add(entry);
            }

            return(result);
        }
Beispiel #3
0
        private static IrbisTreeFile.Item _BuildItem
        (
            [NotNull] MenuEntry parent,
            [NotNull] MenuFile menu
        )
        {
            string value = string.Format
                           (
                "{0}{1}{2}",
                parent.Code,
                IrbisTreeFile.Item.Delimiter,
                parent.Comment
                           );

            IrbisTreeFile.Item result   = new IrbisTreeFile.Item(value);
            MenuEntry[]        children = menu.Entries
                                          .Where(v => ReferenceEquals(v.OtherEntry, parent))
                                          .OrderBy(v => v.Code)
                                          .ToArray();

            foreach (MenuEntry child in children)
            {
                IrbisTreeFile.Item subItem = _BuildItem(child, menu);
                result.Children.Add(subItem);
            }

            return(result);
        }
Beispiel #4
0
        public static IrbisTreeFile ToTree
        (
            [NotNull] this MenuFile menu
        )
        {
            Sure.NotNull(menu, nameof(menu));

            foreach (MenuEntry entry in menu.Entries)
            {
                entry.Code = entry.Code.ThrowIfNull("entryCode").Trim();
            }

            foreach (MenuEntry first in menu.Entries)
            {
                foreach (MenuEntry second in menu.Entries)
                {
                    if (ReferenceEquals(first, second))
                    {
                        continue;
                    }
                    if (first.Code.SameString(second.Code))
                    {
                        continue;
                    }

                    if (first.Code.StartsWith(second.Code))
                    {
                        if (ReferenceEquals(first.OtherEntry, null))
                        {
                            first.OtherEntry = second;
                        }
                        else
                        {
                            if (second.Code.Length
                                > first.OtherEntry.Code.Length)
                            {
                                first.OtherEntry = second;
                            }
                        }
                    }
                }
            }

            MenuEntry[] roots = menu.Entries
                                .Where(entry => ReferenceEquals(entry.OtherEntry, null))
                                .OrderBy(entry => entry.Code)
                                .ToArray();

            IrbisTreeFile result = new IrbisTreeFile();

            foreach (MenuEntry root in roots)
            {
                IrbisTreeFile.Item item = _BuildItem(root, menu);
                result.Roots.Add(item);
            }

            return(result);
        }
Beispiel #5
0
        /// <inheritdoc cref="JsonConverter.WriteJson" />
        public override void WriteJson
        (
            JsonWriter writer,
            object value,
            JsonSerializer serializer
        )
        {
            MenuFile menu = (MenuFile)value;

            serializer.Serialize(writer, menu.Entries);
        }
Beispiel #6
0
        public static string ToJson
        (
            [NotNull] this MenuFile menu
        )
        {
            Sure.NotNull(menu, nameof(menu));

            string result = JArray.FromObject(menu.Entries)
                            .ToString(Formatting.None);

            return(result);
        }
Beispiel #7
0
        public static MenuFile ParseServerResponse
        (
            [NotNull] string response
        )
        {
            Sure.NotNullNorEmpty(response, nameof(response));

            TextReader reader = new StringReader(response);
            MenuFile   result = ParseStream(reader);

            return(result);
        }
Beispiel #8
0
        public static MenuFile ParseServerResponse
        (
            [NotNull] ServerResponse response
        )
        {
            Sure.NotNull(response, nameof(response));

            TextReader reader = response.GetReader(IrbisEncoding.Ansi);
            MenuFile   result = ParseStream(reader);

            return(result);
        }
Beispiel #9
0
        public static MenuFile ParseLocalJsonFile
        (
            [NotNull] string fileName
        )
        {
            Sure.NotNullNorEmpty(fileName, nameof(fileName));

            string text = File.ReadAllText
                          (
                fileName,
                IrbisEncoding.Utf8
                          );
            MenuFile result = FromJson(text);

            return(result);
        }
Beispiel #10
0
        public static T GetValueSensitive <T>
        (
            [NotNull] this MenuFile menu,
            [NotNull] string code,
            T defaultValue
        )
        {
            Sure.NotNull(menu, nameof(menu));
            Sure.NotNull(code, nameof(code));

            MenuEntry found = menu.FindEntrySensitive(code);

            return(found == null
                ? defaultValue
                : ConversionUtility.ConvertTo <T>(found.Comment));
        }
Beispiel #11
0
 public static string[] CollectStrings
 (
     [NotNull] this MenuFile menu,
     [NotNull] string code
 )
 {
     return(menu.Entries
            .Where
            (
                entry => entry.Code.SameString(code) ||
                MenuFile.TrimCode(entry.Code.ThrowIfNull("entry.Code"))
                .SameString(code)
            )
            .Select(entry => entry.Comment)
            .ToArray());
 }
Beispiel #12
0
        public static MenuFile FromJson
        (
            [NotNull] string text
        )
        {
            Sure.NotNullNorEmpty(text, nameof(text));

            NonNullCollection <MenuEntry> entries = JsonConvert
                                                    .DeserializeObject <NonNullCollection <MenuEntry> >
                                                    (
                text
                                                    );
            MenuFile result = new MenuFile(entries);

            return(result);
        }
Beispiel #13
0
        /// <inheritdoc cref="JsonConverter.ReadJson" />
        public override object ReadJson
        (
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer
        )
        {
            MenuFile menu = (MenuFile)existingValue;
            NonNullCollection <MenuEntry> entries = serializer
                                                    .Deserialize <NonNullCollection <MenuEntry> >
                                                    (
                reader
                                                    );

            menu.Entries.AddRange(entries);

            return(menu);
        }
Beispiel #14
0
        /// <summary>
        /// Saves the menu to local JSON file.
        /// </summary>
        public static void SaveLocalJsonFile
        (
            [NotNull] this MenuFile menu,
            [NotNull] string fileName
        )
        {
            Sure.NotNull(menu, nameof(menu));
            Sure.NotNullNorEmpty(fileName, nameof(fileName));

            string contents = JArray.FromObject(menu.Entries)
                              .ToString(Formatting.Indented);

            File.WriteAllText
            (
                fileName,
                contents,
                IrbisEncoding.Utf8
            );
        }
Beispiel #15
0
        public static MenuFile ReadFromServer
        (
            [NotNull] IIrbisConnection connection,
            [NotNull] FileSpecification fileSpecification
        )
        {
            Sure.NotNull(connection, nameof(connection));
            Sure.NotNull(fileSpecification, nameof(fileSpecification));

            string response = connection.ReadTextFile(fileSpecification);

            if (string.IsNullOrEmpty(response))
            {
                return(null);
            }

            MenuFile result = ParseServerResponse(response);

            return(result);
        }
Beispiel #16
0
        public static MenuFile ParseLocalFile
        (
            [NotNull] string fileName,
            [NotNull] Encoding encoding
        )
        {
            Sure.NotNullNorEmpty(fileName, nameof(fileName));
            Sure.NotNull(encoding, nameof(encoding));

            using (StreamReader reader = TextReaderUtility.OpenRead
                                         (
                       fileName,
                       encoding
                                         ))
            {
                MenuFile result = ParseStream(reader);
                result.FileName = Path.GetFileName(fileName);

                return(result);
            }
        }
Beispiel #17
0
        /// <summary>
        /// Adds the typed value with specified code.
        /// </summary>
        public static MenuFile Add <T>
        (
            [NotNull] this MenuFile menu,
            [NotNull] string code,
            [CanBeNull] T value
        )
        {
            Sure.NotNull(menu, nameof(menu));
            Sure.NotNull(code, nameof(code));

            if (ReferenceEquals(value, null))
            {
                menu.Add(code, string.Empty);
            }
            else
            {
                string textValue = ConversionUtility.ConvertTo <string>(value);
                menu.Add(code, textValue);
            }

            return(menu);
        }