84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
using System.Globalization;
|
|
using System.Text;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.VisualTree;
|
|
using XSDVisualiser.Models;
|
|
|
|
namespace XSDVisualiser.Desktop.Views
|
|
{
|
|
public partial class LeftTreeView : UserControl
|
|
{
|
|
public LeftTreeView()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private async void OnCopyAsTsvClick(object? sender, RoutedEventArgs e)
|
|
{
|
|
// Determine the node from the menu item's DataContext
|
|
var node = (sender as MenuItem)?.DataContext as SchemaNode;
|
|
if (node == null)
|
|
node = (sender as Control)?.GetVisualAncestors().OfType<Control>().FirstOrDefault()?.DataContext as SchemaNode;
|
|
if (node == null)
|
|
return;
|
|
|
|
var tsv = BuildTsv(node);
|
|
|
|
var topLevel = TopLevel.GetTopLevel(this);
|
|
if (topLevel?.Clipboard != null)
|
|
{
|
|
await topLevel.Clipboard.SetTextAsync(tsv);
|
|
}
|
|
}
|
|
|
|
private static string BuildTsv(SchemaNode root)
|
|
{
|
|
var sb = new StringBuilder();
|
|
// Header
|
|
sb.AppendLine("Depth\tName\tNamespace\tTypeName\tBuiltInType\tMinOccurs\tMaxOccurs\tContentModel\tIsNillable");
|
|
AppendNode(sb, root, 0);
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static void AppendNode(StringBuilder sb, SchemaNode node, int depth)
|
|
{
|
|
string San(string? s)
|
|
{
|
|
if (string.IsNullOrEmpty(s)) return string.Empty;
|
|
return s.Replace("\t", " ").Replace("\r", " ").Replace("\n", " ");
|
|
}
|
|
|
|
var min = node.Cardinality?.Min.ToString(CultureInfo.InvariantCulture) ?? string.Empty;
|
|
string max;
|
|
if (node.Cardinality?.MaxIsUnbounded == true)
|
|
max = "unbounded";
|
|
else
|
|
max = node.Cardinality != null ? node.Cardinality.Max.ToString(CultureInfo.InvariantCulture) : string.Empty;
|
|
|
|
var line = string.Join("\t", new[]
|
|
{
|
|
depth.ToString(CultureInfo.InvariantCulture),
|
|
San(node.Name),
|
|
San(node.Namespace),
|
|
San(node.TypeName),
|
|
San(node.BuiltInType),
|
|
min,
|
|
max,
|
|
San(node.ContentModel),
|
|
node.IsNillable ? "true" : "false"
|
|
});
|
|
sb.AppendLine(line);
|
|
|
|
if (node.Children != null)
|
|
{
|
|
foreach (var child in node.Children)
|
|
{
|
|
AppendNode(sb, child, depth + 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|