public void TestGetAttribute() { DomNodeType type = new DomNodeType("child"); AttributeInfo info = GetIntAttribute("int"); type.Define(info); DomNode test = new DomNode(type); Assert.True(test.IsAttributeDefault(info)); Assert.Null(test.GetLocalAttribute(info)); Assert.False(test.IsAttributeSet(info)); test.SetAttribute(info, 2); Assert.AreEqual(test.GetAttribute(info), 2); Assert.AreEqual(test.GetLocalAttribute(info), 2); Assert.False(test.IsAttributeDefault(info)); Assert.True(test.IsAttributeSet(info)); test.SetAttribute(info, null); Assert.True(test.IsAttributeDefault(info)); Assert.Null(test.GetLocalAttribute(info)); Assert.False(test.IsAttributeSet(info)); test.SetAttribute(info, 0); Assert.AreEqual(test.GetAttribute(info), 0); Assert.True(test.IsAttributeDefault(info)); Assert.AreEqual(test.GetLocalAttribute(info), 0); Assert.True(test.IsAttributeSet(info)); }
private bool Equals(DomNode n1, DomNode n2) { if (n1 == null || n2 == null) { return(n1 == n2); } if (n1.Type != n2.Type) { return(false); } if (n1.ChildInfo != n2.ChildInfo) { return(false); } foreach (AttributeInfo info in n1.Type.Attributes) { object val1 = n1.GetLocalAttribute(info); object val2 = n2.GetLocalAttribute(info); if (val1 == null || val2 == null) { if (val1 != val2) { return(false); } } } foreach (ChildInfo info in n1.Type.Children) { if (info.IsList) { IList <DomNode> children1 = n1.GetChildList(info); IList <DomNode> children2 = n1.GetChildList(info); if (children1.Count != children2.Count) { return(false); } for (int i = 0; i < children1.Count; i++) { if (!Equals(children1[i], children2[i])) { return(false); } } } else { DomNode child1 = n1.GetChild(info); DomNode child2 = n2.GetChild(info); if (!Equals(child1, child2)) { return(false); } } } return(true); }
/// <summary> /// Clones this control point</summary> /// <returns>Cloned control point</returns> public IControlPoint Clone() { DomNode node = new DomNode(UISchema.controlPointType.Type); // clone local attributes foreach (AttributeInfo attributeInfo in DomNode.Type.Attributes) { object value = DomNode.GetLocalAttribute(attributeInfo); if (value != null) { node.SetAttribute(attributeInfo, attributeInfo.Type.Clone(value)); } } node.InitializeExtensions(); return(node.As <IControlPoint>()); }
private bool Equals(DomNode n1, DomNode n2) { if (n1 == null || n2 == null) return n1 == n2; if (n1.Type != n2.Type) return false; if (n1.ChildInfo != n2.ChildInfo) return false; foreach (AttributeInfo info in n1.Type.Attributes) { object val1 = n1.GetLocalAttribute(info); object val2 = n2.GetLocalAttribute(info); if (val1 == null || val2 == null) if (val1 != val2) return false; } foreach (ChildInfo info in n1.Type.Children) { if (info.IsList) { IList<DomNode> children1 = n1.GetChildList(info); IList<DomNode> children2 = n1.GetChildList(info); if (children1.Count != children2.Count) return false; for (int i = 0; i < children1.Count; i++) if (!Equals(children1[i], children2[i])) return false; } else { DomNode child1 = n1.GetChild(info); DomNode child2 = n2.GetChild(info); if (!Equals(child1, child2)) return false; } } return true; }
public void TestSetAttribute() { DomNodeType type = new DomNodeType("child"); AttributeInfo info = GetIntAttribute("int"); type.Define(info); DomNode test = new DomNode(type); Assert.False(test.IsAttributeSet(info)); test.SetAttribute(info, 2); Assert.AreEqual(test.GetAttribute(info), 2); Assert.AreEqual(test.GetLocalAttribute(info), 2); Assert.True(test.IsAttributeSet(info)); test.SetAttribute(info, null); Assert.True(test.IsAttributeDefault(info)); Assert.Null(test.GetLocalAttribute(info)); Assert.False(test.IsAttributeSet(info)); }
private static void Serialize(DomNode node, Dictionary<DomNode, int> nodeIds, BinaryWriter writer) { writer.Write(node.Type.Name); foreach (AttributeInfo info in node.Type.Attributes) { object value = node.GetLocalAttribute(info); // references are serialized as an integer id if (info.Type.Type == AttributeTypes.Reference) { DomNode reference = value as DomNode; int refId; if (reference == null || !nodeIds.TryGetValue(reference, out refId)) { // null, or reference was external to top level nodes and their subtrees writer.Write(false); } else { writer.Write(true); writer.Write(refId); } } else { if (value == null) { writer.Write(false); } else { writer.Write(true); writer.Write(info.Type.Convert(value)); } } } foreach (ChildInfo info in node.Type.Children) { if (info.IsList) { IList<DomNode> children = node.GetChildList(info); writer.Write(children.Count); foreach (DomNode child in children) Serialize(child, nodeIds, writer); } else { DomNode child = node.GetChild(info); if (child == null) { writer.Write(false); } else { writer.Write(true); Serialize(child, nodeIds, writer); } } } }
private static void Serialize(DomNode node, Dictionary <DomNode, int> nodeIds, BinaryWriter writer) { writer.Write(node.Type.Name); foreach (AttributeInfo info in node.Type.Attributes) { object value = node.GetLocalAttribute(info); // references are serialized as an integer id if (info.Type.Type == AttributeTypes.Reference) { DomNode reference = value as DomNode; int refId; if (reference == null || !nodeIds.TryGetValue(reference, out refId)) { // null, or reference was external to top level nodes and their subtrees writer.Write(false); } else { writer.Write(true); writer.Write(refId); } } else { if (value == null) { writer.Write(false); } else { writer.Write(true); writer.Write(info.Type.Convert(value)); } } } foreach (ChildInfo info in node.Type.Children) { if (info.IsList) { IList <DomNode> children = node.GetChildList(info); writer.Write(children.Count); foreach (DomNode child in children) { Serialize(child, nodeIds, writer); } } else { DomNode child = node.GetChild(info); if (child == null) { writer.Write(false); } else { writer.Write(true); Serialize(child, nodeIds, writer); } } } }