From 24010bcb7d9b1b5eef1a9b0fe31e5c749cd424aa Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Fri, 28 May 2021 07:35:25 -0400 Subject: [PATCH] implement quick and dirty (very hacky) but configurable hierarchical group support --- .../WindowsConfigurationDataFormat.cs | 32 +++++++++++++++++-- .../PropertyList/PropertyListItem.cs | 32 +++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/Libraries/UniversalEditor.Essential/DataFormats/PropertyList/WindowsConfigurationDataFormat.cs b/Libraries/UniversalEditor.Essential/DataFormats/PropertyList/WindowsConfigurationDataFormat.cs index b348d6fb..dedb723d 100644 --- a/Libraries/UniversalEditor.Essential/DataFormats/PropertyList/WindowsConfigurationDataFormat.cs +++ b/Libraries/UniversalEditor.Essential/DataFormats/PropertyList/WindowsConfigurationDataFormat.cs @@ -25,6 +25,7 @@ using UniversalEditor.ObjectModels.PropertyList; using UniversalEditor.IO; using System.Linq; using System.Collections.Generic; +using MBS.Framework.Settings; namespace UniversalEditor.DataFormats.PropertyList { @@ -37,6 +38,8 @@ namespace UniversalEditor.DataFormats.PropertyList { DataFormatReference dfr = base.MakeReferenceInternal(); dfr.Capabilities.Add(typeof(PropertyListObjectModel), DataFormatCapabilities.All); + dfr.ImportOptions.SettingsGroups[0].Settings.Add(new TextSetting("GroupHierarchySeparator", "Group _hierarchy separator", ".")); + dfr.ExportOptions.SettingsGroups[0].Settings.Add(new TextSetting("GroupHierarchySeparator", "Group _hierarchy separator", ".")); return dfr; } @@ -60,6 +63,13 @@ namespace UniversalEditor.DataFormats.PropertyList /// /// The with which to separate a property name from a property value. public string PropertyNameValueSeparator { get; set; } = "="; + /// + /// Gets or sets the with which to separate hierarchical group names. + /// + /// The group hierarchy separator. + public string GroupHierarchySeparator { get; set; } = null; + + public static string DefaultGroupHierarchySeparator { get; set; } = "."; protected override void LoadInternal(ref ObjectModel objectModel) { @@ -108,7 +118,7 @@ namespace UniversalEditor.DataFormats.PropertyList if (line.StartsWith("[") && line.EndsWith("]")) { string groupName = line.Substring(1, line.Length - 2); - CurrentGroup = plom.Items.AddGroup(groupName); + CurrentGroup = plom.Items.AddGroup(groupName, GroupHierarchySeparator); } else { @@ -170,15 +180,31 @@ namespace UniversalEditor.DataFormats.PropertyList tw.Flush(); } - private void WriteGroup(Writer tw, Group g, bool endline) + private void WriteGroup(Writer tw, Group g, bool endline, string prefix = null) { - tw.WriteLine("[" + g.Name + "]"); + string groupHierarchySeparator = GroupHierarchySeparator; + if (groupHierarchySeparator == null) + groupHierarchySeparator = DefaultGroupHierarchySeparator; + + string fullName = String.Format("{0}{1}", prefix == null ? String.Empty : String.Format("{0}{1}", prefix, groupHierarchySeparator), g.Name); + tw.WriteLine(String.Format("[{0}]", fullName)); IEnumerable properties = g.Items.OfType(); foreach (Property p in properties) { WriteProperty(tw, p, g.Items.IndexOf(p) < g.Items.Count - 1); } + + IEnumerable groups = g.Items.OfType(); + if (groups.Any()) + { + tw.WriteLine(); + foreach (Group g2 in groups) + { + WriteGroup(tw, g2, endline, fullName); + } + } + if (endline) { tw.WriteLine(); diff --git a/Libraries/UniversalEditor.Essential/ObjectModels/PropertyList/PropertyListItem.cs b/Libraries/UniversalEditor.Essential/ObjectModels/PropertyList/PropertyListItem.cs index 28c7b555..1d969c9f 100644 --- a/Libraries/UniversalEditor.Essential/ObjectModels/PropertyList/PropertyListItem.cs +++ b/Libraries/UniversalEditor.Essential/ObjectModels/PropertyList/PropertyListItem.cs @@ -74,6 +74,38 @@ namespace UniversalEditor.ObjectModels.PropertyList Add(group); return group; } + public Group AddGroup(string name, string groupHierarchySeparator, PropertyListItem[] items = null) + { + if (groupHierarchySeparator == null) + { + Group group = new Group(name, items); + Add(group); + return group; + } + + string[] path = name.Split(groupHierarchySeparator); + Group parent = this[path[0]] as Group; + if (parent == null) + { + parent = new Group(path[0]); + Add(parent); + } + + for (int i = 1; i < path.Length; i++) + { + Group pg = parent.Items[path[i]] as Group; + if (pg == null) + { + pg = parent.Items.AddGroup(path[i]); + } + parent = pg; + } + + if (items != null) + parent.Items.AddRange(items); + + return parent; + } protected override void ClearItems() {