/// <summary> /// Loads a file as <see cref="XElement"/> into a specified property. /// </summary> /// <param name="setterPath">The property setter for the file path.</param> /// <param name="setterData">The property setter for the <see cref="XElement"/> data.</param> /// <param name="filter">The filter string.</param> private void Load(Action <string> setterPath, Action <XmlNodeViewModel[]> setterData, string filter) { var dialog = new OpenFileDialog { Filter = $"Comparable files (*.{filter})|*.{filter}", FilterIndex = 0, InitialDirectory = Path.Combine(Environment.CurrentDirectory, "Datasets") }; var showDialog = dialog.ShowDialog(); if (showDialog != null && (bool)showDialog) { try { var data = File.ReadAllText(dialog.FileName); var xElement = XElement.Parse(data); setterPath(dialog.FileName); setterData(new[] { (filter == FilterXsd ? XmlNodeViewModel.FromXsd(xElement, this.AddToXml) : XmlNodeViewModel.FromXml(xElement, this.AddToXml)) }); } catch (Exception exception) { this._messageService.Show(exception.Message, exception.GetType().Name); } } }
/// <summary> /// Checks if this and descending nodes exist /// in another tree and marks missing nodes. /// </summary> /// <param name="root">The root of the tree that is checked.</param> public void CompareTo(XmlNodeViewModel root) { this.IsMissing = !root.Contains(this); if (this.IsMissing) { this.ExpandPath(); } foreach (var child in this.Children) { child.CompareTo(root); } }
/// <summary> /// Creates a xml representation of the <see cref="XElement"/>. /// </summary> /// <param name="root">The root of the tree.</param> /// <param name="addAction">The action to add nodes to another tree.</param> /// <returns>The root of the representation.</returns> public static XmlNodeViewModel FromXml(XElement root, Action <XmlNodeViewModel> addAction) { var node = new XmlNodeViewModel { Name = root.Name.LocalName, Value = root.HasElements ? string.Empty : root.Value, AddAction = addAction }; node.Children = new ObservableCollection <XmlNodeViewModel>( root.Elements().Select(x => FromXml(x, addAction).WithParent(node))); return(node); }
/// <summary> /// Checks if this sub tree contains the specified node. /// </summary> /// <param name="node">The node to check for.</param> /// <returns><c>true</c> if the sub tree contains the <param name="node">node</param>, <c>false</c> otherwise.</returns> private bool Contains(XmlNodeViewModel node) { var nodes = new[] { this }; foreach (var pathNode in node.GetPath()) { var found = nodes.FirstOrDefault(x => x.Equals(pathNode)); if (found == null) { return(false); } nodes = found.Children.ToArray(); } return(true); }
/// <summary> /// Adds one node of a tree to another. /// </summary> /// <param name="node">The node to add.</param> public void AddToXml(XmlNodeViewModel node) { var nodes = this.Xml; XmlNodeViewModel parent = null; foreach (var pathNode in node.GetPath()) { var found = nodes.FirstOrDefault(x => x.Equals(pathNode)); if (found == null) { found = pathNode.Clone(); parent?.AddChild(found); } parent = found; nodes = parent.Children.ToArray(); } this.Compare(null); }
/// <summary> /// Creates a xsd representation of the <see cref="XElement"/>. /// </summary> /// <param name="root">The root of the tree.</param> /// <param name="parent">The parent for the created nodes.</param> /// <param name="addAction">The action to add nodes to another tree.</param> /// <returns>Created nodes of this depth.</returns> private static IEnumerable <XmlNodeViewModel> CreateXsd(XElement root, XmlNodeViewModel parent, Action <XmlNodeViewModel> addAction) { if (root.Attribute("name") == null) { foreach (var child in root.Elements().SelectMany(x => CreateXsd(x, parent, addAction))) { yield return(child); } } else { var node = new XmlNodeViewModel { Name = root.Attribute("name")?.Value ?? string.Empty, Value = root.Attribute("default")?.Value ?? string.Empty, AddAction = addAction, Parent = parent }; node.Children = new ObservableCollection <XmlNodeViewModel>(root.Elements().SelectMany(x => CreateXsd(x, node, addAction)).Where(x => x != null)); yield return(node); } }
/// <summary> /// Adds a child to this node. /// </summary> /// <param name="child">The child to add.</param> public void AddChild(XmlNodeViewModel child) { this.Children.Add(child.WithParent(this)); child.ExpandPath(); }
/// <summary> /// Sets the parent of this node. /// </summary> /// <param name="parent">The new parent.</param> /// <returns>This node.</returns> public XmlNodeViewModel WithParent(XmlNodeViewModel parent) { this.Parent = parent; return(this); }