public virtual void FromXml(XmlElement e) { foreach (XmlNode node in e.ChildNodes) { if (!(node is XmlElement)) { continue; } XmlElement element = node as XmlElement; if (element.Name != "node" && element.Name != "leaf") { Console.WriteLine(element.Name); Debug.Assert(element.Name == "node" || element.Name == "leaf"); } string name = element.GetAttribute("name"); if (ChildrenNames().Contains(name)) { IDocLeaf child = ChildByName(name) ?? CreateChildByName(Type.GetType(element.GetAttribute("type")), name); (child as IXml).FromXml(element); } } }
static void TestPathBasics() { IDocLeaf doc = DocNode.Create <TestDocument>(); string pathToMyInt = "TestList.2"; DocObj <int> myInt = PathUtils.ChildByPath <DocObj <int> >( doc as IDocNode, pathToMyInt); UnitTest.Test(myInt.Value == 2); UnitTest.Test(PathUtils.PathByChild(myInt) == pathToMyInt); string pathToMyBool = "TestBool"; DocObj <bool> myBool = PathUtils.ChildByPath <DocObj <bool> >( doc as IDocNode, pathToMyBool); UnitTest.Test(myBool.Value == true); UnitTest.Test(PathUtils.PathByChild(myBool) == pathToMyBool); string pathToMyInt2 = "SubDoc.TestInt0"; DocObj <int> myInt2 = PathUtils.ChildByPath <DocObj <int> >( doc as IDocNode, pathToMyInt2); UnitTest.Test(myInt2.Value == 2); UnitTest.Test(PathUtils.PathByChild(myInt2) == pathToMyInt2); }
public IEnumerable <IAtomicOperation> CreateActions(IDocLeaf sender) { DocList <T> docList = sender as DocList <T>; List <IAtomicOperation> actions = new List <IAtomicOperation>(); if (NewItems != null) { for (int i = 0; i < NewItems.Count; i++) { T obj = (T)NewItems[i]; actions.Add(new DocListActionAdd( sender, NewStartingIndex + i, obj)); } } if (OldItems != null) { for (int i = OldItems.Count - 1; i >= 0; i--) { T obj = (T)OldItems[i]; actions.Add(new DocListActionRemove( sender, docList.IndexOf(obj), obj)); } } return(actions); }
public static XmlDocument ToXmlDump(this IDocLeaf leaf) { HashSet <object> findReferences = new HashSet <object>(); XmlDocument doc = new XmlDocument(); doc.AppendChild(leaf.DumpWholeTree(leaf.Name, findReferences, doc)); return(doc); }
public static IDocLeaf RootDocument(IDocLeaf doc) { IDocLeaf root = doc; while (root.Parent != null) { root = root.Parent; } return(root); }
public DocListActionRemove(IDocLeaf sender, int index, T value) : base(sender) { if (!(sender as IList <T>).Contains(value)) { throw new Exception("Can not remove item not in list"); } Value = value; Index = index; }
public PreferencesItem(Text displayName, Text helpText, IDocLeaf initialItem) { DisplayName = displayName; Help = helpText; Item = initialItem; ResolveChildrenLinks(); if (Item is DocBase) { (Item as DocBase).Help = Help; } }
public static T ChildByPath <T>(IDocNode root, string path) where T : IDocLeaf { string[] names = SplitPath(path); IDocLeaf cur = root; foreach (string name in names) { cur = (cur as IDocNode).ChildByName(name); } return((T)cur); }
public void ResolveChildrenLinks() { foreach (var childName in ChildrenNames()) { IDocLeaf child = ChildByName(childName); child.ResolveParentLink(this, childName); if (child is IDocNode) { (child as IDocNode).ResolveChildrenLinks(); } } }
public static void CollectChildrenRecursive(IDocLeaf root, List <IDocLeaf> collection) { collection.Add(root); if (root is IDocNode) { IDocNode node = (IDocNode)root; IEnumerable <IDocLeaf> children = node.Children(); foreach (IDocLeaf child in children) { CollectChildrenRecursive(child, collection); } } }
protected IDocLeaf CreateChildByName(Type type, string name) { IDocLeaf child = Activator.CreateInstance(type) as IDocLeaf; if (child is IDocNode) { (child as IDocNode).ResolveChildrenLinks(); } PropertyUtils.SetByName(this, name, child); child.ResolveParentLink(this, name); return(child); }
public static void RemoveHookRecursive(IDocLeaf doc, EventHandler <EventArgs> listener) { if (doc is IDoc) { (doc as IDoc).Hook -= listener; } if (doc is DocNode) { foreach (IDocLeaf leaf in (doc as DocNode).Children()) { RemoveHookRecursive(leaf, listener); } } }
public static void CheckParentChildrenLink(IDocLeaf doc, IDocLeaf parent) { if (!Object.ReferenceEquals(doc.Parent, parent)) { throw new Exception("CheckParentChildrenLink failed"); } if (doc is IDocNode) { foreach (IDocLeaf children in (doc as IDocNode).Children()) { CheckParentChildrenLink(children, doc); } } }
public static string PathByChild(IDocLeaf child) { List <string> path = new List <string>(); IDocLeaf obj = child; while (obj.Parent != null) { path.Add(obj.Name); obj = obj.Parent; } path.Reverse(); return(BuildPath(path.ToArray())); }
private static XmlElement DumpWholeTree(this IDocLeaf leaf, string name, HashSet <object> alreadyDumped, XmlDocument doc) { bool seemsToBeAReference = alreadyDumped.Contains(leaf); XmlElement element = doc.CreateElement("Leaf"); element.SetAttribute("Class", leaf.GetType().Name); element.SetAttribute("Name", name); element.SetAttribute("Owner", (!seemsToBeAReference).ToString()); if (!seemsToBeAReference) { alreadyDumped.Add(leaf); if (leaf is DocBase) { string[] names = PropertyUtils.NamesByType(leaf.GetType(), PropertyUtils.IsSupported); foreach (var n in names) { IDocLeaf child = PropertyUtils.ByName(leaf, n) as IDocLeaf; if (child != null) { element.AppendChild(child.DumpWholeTree(n, alreadyDumped, doc)); } } } else if (leaf is IDocNode) { foreach (var child in (leaf as IDocNode).Children()) { if (child != null) { element.AppendChild(child.DumpWholeTree(child.Name, alreadyDumped, doc)); } } } } return(element); }
public DocListActionAdd(IDocLeaf sender, int index, T value) : base(sender) { Value = value; Index = index; }
public static bool IsInHistoryTree(this IDocLeaf doc) { return(History.Instance.Root != null && History.Instance.Root == RootDocument(doc)); }
public DocObjAction(IDocLeaf document, T newValue) : base(document) { OldValue = (document as DocObj <T>).ForceValue; NewValue = newValue; }
public IEnumerable <IAtomicOperation> CreateActions(IDocLeaf sender) { yield return(new DocObjAction(sender, NewValue)); }
public DocAtomicOperation(IDocLeaf document) : base(document.GetHashCode()) { Sender = document; }