Ejemplo n.º 1
0
        public void TestGetId()
        {
            DomNodeType   testId = new DomNodeType("test");
            AttributeInfo info   = GetStringAttribute("string");

            testId.Define(info);
            testId.SetIdAttribute(info.Name);
            DomNode test = new DomNode(testId);

            Assert.Null(test.GetId());

            test.SetAttribute(info, "foo");
            Assert.AreEqual(test.GetId(), "foo");
        }
Ejemplo n.º 2
0
        public void TestGetIdDefault()
        {
            DomNodeType testNoId = new DomNodeType("test");
            DomNode     test     = new DomNode(testNoId);

            Assert.Null(test.GetId());
        }
        public static GameObjectReference Create(DomNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (!node.Is <IGameObject>())
            {
                throw new ArgumentException(node.Type.Name + " is not derived from " +
                                            Schema.gameObjectType.Type.Name);
            }
            DomNode      rootNode = node.GetRoot();
            GameDocument gameDoc  = rootNode.As <GameDocument>();

            if (gameDoc == null)
            {
                throw new ArgumentException("nod must belong to a document.");
            }


            // create game object reference.
            DomNode             refNode = new DomNode(Schema.gameObjectReferenceType.Type);
            GameObjectReference gobRef  = refNode.As <GameObjectReference>();

            // Create Uri
            Uri ur = new Uri(gameDoc.Uri + "#" + node.GetId());

            refNode.SetAttribute(Schema.gameObjectReferenceType.refAttribute, ur);
            gobRef.m_target = node;
            return(gobRef);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructs descriptive name of circuit item. Debugging Helper method.</summary>
        /// <param name="domNode">DomNode of circuit item</param>
        /// <returns>Descriptive name of circuit item</returns>
        static public string GetDomNodeName(DomNode domNode)
        {
            string result = string.Empty;

            if (domNode.Is <Element>())
            {
                result = domNode.Cast <Element>().Name;
            }
            else if (domNode.Is <GroupPin>())
            {
                result = "Group Pin : " + domNode.Cast <GroupPin>().Name;
            }
            else if (domNode.Is <Wire>())
            {
                var connection = domNode.Cast <Wire>();
                int inputPinIndex, outputPinIndex;
                // during undo/redo, the pin index may temporarily out of range, need to check index before call OutputPin|InputPin
                if (connection.IsValid(out inputPinIndex, out outputPinIndex))
                {
                    result = "Edge from " + connection.OutputElement.Name + "[" + connection.OutputPin.Name + "]" +
                             " to " + connection.InputElement.Name + "[" + connection.InputPin.Name + "]";
                }
                else
                {
                    result = "Edge from " + connection.OutputElement.Name + "[" + outputPinIndex + "]" +
                             " to " + connection.InputElement.Name + "[" + inputPinIndex + "]";
                }
            }
            else if (domNode.Is <Circuit>())
            {
                var doc = domNode.As <IDocument>();
                if (doc != null && doc.Uri != null)
                {
                    result = "Circuit " + Path.GetFileNameWithoutExtension(doc.Uri.LocalPath);
                }
                else
                {
                    result = "Circuit " + domNode.GetId();
                }
            }
            if (result == string.Empty)
            {
                result = domNode.GetId() ?? domNode.ToString();
            }
            return(result);
        }
Ejemplo n.º 5
0
        private void NameNode(DomNode node, UniqueNamer namer)
        {
            // if the name isn't unique, make it so
            string id     = node.GetId();
            string unique = namer.Name(id);

            if (id != unique)
            {
                node.SetAttribute(node.Type.IdAttribute, unique);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets item's display information</summary>
        /// <param name="item">Item being displayed</param>
        /// <param name="info">Item info, to fill out</param>
        public void GetInfo(object item, ItemInfo info)
        {
            DomNode node = item as DomNode;

            if (node != null)
            {
                if (node.Type == UISchema.UIRefType.Type)
                {
                    info.ImageIndex = info.GetImageList().Images.IndexOfKey(Resources.RefImage);
                    string   label    = string.Empty;
                    UIRef    uiRef    = node.As <UIRef>();
                    UIObject uiTarget = uiRef.UIObject;
                    if (uiTarget != null)
                    {
                        label = uiTarget.Name;
                    }

                    info.Label = "[" + label + "]";
                }
                else if (node.Is <Curve>())
                {
                    Curve cv = node.Cast <Curve>();
                    info.Label      = string.IsNullOrWhiteSpace(cv.DisplayName) ? cv.Name : cv.DisplayName;
                    info.ImageIndex = info.GetImageList().Images.IndexOfKey(Resources.CurveImage);
                }
                else
                {
                    NodeTypePaletteItem paletteItem = node.Type.GetTag <NodeTypePaletteItem>();
                    if (paletteItem != null)
                    {
                        info.ImageIndex = info.GetImageList().Images.IndexOfKey(paletteItem.ImageName);
                    }

                    info.Label = node.GetId();
                }

                info.IsLeaf = !GetChildren(item).Any();
            }
            else
            {
                EmptyRef emptyRef = item as EmptyRef;
                if (emptyRef != null)
                {
                    info.Label      = "Ref";
                    info.ImageIndex = info.GetImageList().Images.IndexOfKey(Resources.RefEmptyImage);
                    info.IsLeaf     = true;
                }
            }

            if (string.IsNullOrEmpty(info.Label))
            {
                throw new ArgumentException("info.lable");
            }
        }
Ejemplo n.º 7
0
        private void NameNode(DomNode node)
        {
            // if the name isn't unique, make it so
            string id = node.GetId();

            if (node.Type.IdAttribute != null)
            {
                var    uniqueNamer = GetUniqueNamer(GetIdCategory(node));
                string unique      = uniqueNamer.Name(id);
                if (id != unique)
                {
                    node.SetAttribute(node.Type.IdAttribute, unique);
                }
            }
        }
Ejemplo n.º 8
0
        private void NameNode(DomNode node)
        {
            // if the name isn't unique, make it so
            string id = node.GetId();

            // DAN: Edit here to allow nodes with no ID attribute to exist!
            if (node.Type.IdAttribute != null)
            {
                string unique = m_uniqueNamer.Name(id);
                if (id != unique)
                {
                    node.SetAttribute(node.Type.IdAttribute, unique);
                }
            }
        }
Ejemplo n.º 9
0
        private void updateNode(DomNode node, ObjectOverride objectOverride)
        {
            if (node == null || objectOverride == null)
            {
                return;
            }
            string nodeId = node.GetId();

            System.Diagnostics.Debug.Assert(nodeId == objectOverride.ObjectName);

            foreach (AttributeOverride attrOverride in objectOverride.AttributeOverrides)
            {
                AttributeInfo attrInfo = node.Type.GetAttributeInfo(attrOverride.Name);
                node.SetAttribute(attrInfo, attrInfo.Type.Convert(attrOverride.AttribValue));
            }
        }
Ejemplo n.º 10
0
        private static string GetNodeReferenceString(DomNode refNode, DomNode root)
        {
            var id = refNode.GetId();

            // if referenced node is in another resource, prepend URI
            if (!refNode.IsDescendantOf(root))
            {
                var nodeRoot = refNode.GetRoot();
                var resource = nodeRoot.As <IResource>();
                if (resource != null)
                {
                    id = resource.Uri.LocalPath + "#" + id;
                }
            }

            return(id);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Converts a node to a string reference when writing DomNode</summary>
        /// <param name="refNode">Node that is referenced</param>
        /// <param name="root">Root node of data that is being written</param>
        /// <param name="uri">URI of data that is being written</param>
        /// <returns>String encoding the reference to the node</returns>
        protected virtual string GetNodeReferenceString(DomNode refNode, DomNode root, Uri uri)
        {
            string id = refNode.GetId();

            // if referenced node is in another resource, prepend URI
            DomNode nodeRoot = refNode.GetRoot();

            if (nodeRoot != root)
            {
                IResource resource = nodeRoot.As <IResource>();
                if (resource != null)
                {
                    id = resource.Uri.LocalPath + "#" + id;
                }
            }

            return(id);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Converts a node to a string reference when writing</summary>
        /// <param name="refNode">Node that is referenced</param>
        /// <param name="root">Root node of data that is being written</param>
        /// <param name="uri">URI of data that is being written</param>
        /// <returns>String encoding the reference to the node</returns>
        protected virtual string GetNodeReferenceString(DomNode refNode, DomNode root, Uri uri)
        {
            string id = refNode.GetId();

            // if referenced node is in another resource, prepend URI
            if (!refNode.IsDescendantOf(root))
            {
                DomNode   nodeRoot = refNode.GetRoot();
                IResource resource = nodeRoot.As <IResource>();
                if (resource != null)
                {
                    Uri relativeUri = uri.MakeRelativeUri(resource.Uri);
                    id = relativeUri + "#" + id;
                }
            }

            return(id);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Gets item's display information</summary>
        /// <param name="item">Item being displayed</param>
        /// <param name="info">Item info, to fill out</param>
        public void GetInfo(object item, ItemInfo info)
        {
            DomNode node = item as DomNode;

            if (node != null)
            {
                if (node.Type == UISchema.UIRefType.Type)
                {
                    info.ImageIndex = info.GetImageList().Images.IndexOfKey(Resources.RefImage);
                    string   label    = string.Empty;
                    UIRef    uiRef    = node.As <UIRef>();
                    UIObject uiTarget = uiRef.UIObject;
                    if (uiTarget != null)
                    {
                        label = uiTarget.Name;
                    }

                    info.Label = "[" + label + "]";
                }
                else
                {
                    NodeTypePaletteItem paletteItem = node.Type.GetTag <NodeTypePaletteItem>();
                    if (paletteItem != null)
                    {
                        info.ImageIndex = info.GetImageList().Images.IndexOfKey(paletteItem.ImageName);
                        info.Label      = node.GetId();
                    }
                }

                info.IsLeaf = !Enumerable.Any(GetChildren(item));
                return;
            }
            else
            {
                EmptyRef emptyRef = item as EmptyRef;
                if (emptyRef != null)
                {
                    info.Label      = "Ref";
                    info.ImageIndex = info.GetImageList().Images.IndexOfKey(Resources.RefEmptyImage);
                    info.IsLeaf     = true;
                }
            }
        }
Ejemplo n.º 14
0
 private string CreateKey(DomNode node, AttributeInfo attrib)
 {
     return(string.Format("{0}.{1}", node.GetId(), attrib.Name));
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Reads the node specified by the child metadata</summary>
        /// <param name="nodeInfo">Child metadata for node</param>
        /// <param name="reader">XML reader</param>
        /// <returns>DomNode specified by the child metadata</returns>
        protected virtual DomNode ReadElement(ChildInfo nodeInfo, XmlReader reader)
        {
            // handle polymorphism, if necessary
            DomNodeType type = null;
            var         substitutionGroupRule = nodeInfo.Rules.OfType <SubstitutionGroupChildRule>().FirstOrDefault();

            if (substitutionGroupRule != null)
            {
                foreach (var sub in substitutionGroupRule.Substitutions)
                {
                    if (sub.Name == reader.LocalName)
                    {
                        type = sub.Type;
                        break;
                    }
                }

                if (type == null)
                {
                    throw new InvalidOperationException("Could not match substitution group for child " + nodeInfo.Name);
                }
            }
            else
            {
                type = GetChildType(nodeInfo.Type, reader);
            }

            int    index  = type.Name.LastIndexOf(':');
            string typeNS = type.Name.Substring(0, index);

            DomNode node = new DomNode(type, nodeInfo);

            // read attributes
            while (reader.MoveToNextAttribute())
            {
                if (reader.Prefix == string.Empty ||
                    reader.LookupNamespace(reader.Prefix) == typeNS)
                {
                    AttributeInfo attributeInfo = type.GetAttributeInfo(reader.LocalName);
                    if (attributeInfo != null)
                    {
                        ReadAttribute(node, attributeInfo, reader.Value);
                    }
                }
            }

            // add node to map if it has an id
            if (node.Type.IdAttribute != null)
            {
                string id = node.GetId();
                if (!string.IsNullOrEmpty(id))
                {
                    m_nodeDictionary[id] = node; // don't Add, in case there are multiple DomNodes with the same id
                }
            }

            reader.MoveToElement();

            if (!reader.IsEmptyElement)
            {
                // read child elements
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        // look up metadata for this element
                        ChildInfo childInfo = type.GetChildInfo(reader.LocalName);
                        if (childInfo == null)
                        {
                            // Try and get substitution group
                            childInfo = GetSubsitutionGroup(type, reader.LocalName);
                        }

                        if (childInfo != null)
                        {
                            DomNode childNode = ReadElement(childInfo, reader);
                            if (childNode != null)
                            {
                                // childNode is fully populated sub-tree
                                if (childInfo.IsList)
                                {
                                    node.GetChildList(childInfo).Add(childNode);
                                }
                                else
                                {
                                    node.SetChild(childInfo, childNode);
                                }
                            }
                        }
                        else
                        {
                            // try reading as an attribute
                            AttributeInfo attributeInfo = type.GetAttributeInfo(reader.LocalName);
                            if (attributeInfo != null)
                            {
                                reader.MoveToElement();

                                if (!reader.IsEmptyElement)
                                {
                                    // read element text
                                    while (reader.Read())
                                    {
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            ReadAttribute(node, attributeInfo, reader.Value);
                                            // skip child elements, as this is an attribute value
                                            reader.Skip();
                                            break;
                                        }
                                        if (reader.NodeType == XmlNodeType.EndElement)
                                        {
                                            break;
                                        }
                                    }

                                    reader.MoveToContent();
                                }
                            }
                            else
                            {
                                // skip unrecognized element
                                reader.Skip();
                                // if that takes us to the end of the enclosing element, break
                                if (reader.NodeType == XmlNodeType.EndElement)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        AttributeInfo attributeInfo = type.GetAttributeInfo(string.Empty);
                        if (attributeInfo != null)
                        {
                            ReadAttribute(node, attributeInfo, reader.Value);
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }
                }
            }

            reader.MoveToContent();

            return(node);
        }
Ejemplo n.º 16
0
 private void NameNode(DomNode node, UniqueNamer namer)
 {
     // if the name isn't unique, make it so
     string id = node.GetId();
     string unique = namer.Name(id);
     if (id != unique)
     {
         node.SetAttribute(node.Type.IdAttribute, unique);
     }
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Converts a node to a string reference when writing</summary>
        /// <param name="refNode">Node that is referenced</param>
        /// <param name="root">Root node of data that is being written</param>
        /// <param name="uri">URI of data that is being written</param>
        /// <returns>String encoding the reference to the node</returns>
        protected virtual string GetNodeReferenceString(DomNode refNode, DomNode root, Uri uri)
        {
            string id = refNode.GetId();

            // if referenced node is in another resource, prepend URI
            if (!refNode.IsDescendantOf(root))
            {
                DomNode nodeRoot = refNode.GetRoot();
                IResource resource = nodeRoot.As<IResource>();
                if (resource != null)
                {
                    Uri relativeUri = uri.MakeRelativeUri(resource.Uri);
                    id = relativeUri + "#" + id;
                }
            }

            return id;
        }
 private void NameNode(DomNode node)
 {
     // if the name isn't unique, make it so
     string id = node.GetId();
     
     if (node.Type.IdAttribute != null)
     {
         var uniqueNamer = GetUniqueNamer(GetIdCategory(node));
         string unique = uniqueNamer.Name(id);
         if (id != unique)
         {
             node.SetAttribute(node.Type.IdAttribute, unique);
         }
     }
 }
Ejemplo n.º 19
0
        public void TestGetId()
        {
            DomNodeType testId = new DomNodeType("test");
            AttributeInfo info = GetStringAttribute("string");
            testId.Define(info);
            testId.SetIdAttribute(info.Name);
            DomNode test = new DomNode(testId);
            Assert.Null(test.GetId());

            test.SetAttribute(info, "foo");
            Assert.AreEqual(test.GetId(), "foo");
        }
Ejemplo n.º 20
0
 public void TestGetIdDefault()
 {
     DomNodeType testNoId = new DomNodeType("test");
     DomNode test = new DomNode(testNoId);
     Assert.Null(test.GetId());
 }
Ejemplo n.º 21
0
 private void NameNode(DomNode node)
 {
     // if the name isn't unique, make it so
     string id = node.GetId();
     
     // DAN: Edit here to allow nodes with no ID attribute to exist!
     if (node.Type.IdAttribute != null)
     {
         string unique = m_uniqueNamer.Name(id);
         if (id != unique)
         {
             node.SetAttribute(node.Type.IdAttribute, unique);
         }
     }
 }
Ejemplo n.º 22
0
        private void updateNode(DomNode node, ObjectOverride objectOverride)
        {
            if (node == null || objectOverride == null)
                return;
            string nodeId = node.GetId();
            System.Diagnostics.Debug.Assert(nodeId == objectOverride.ObjectName);

            foreach (AttributeOverride attrOverride in objectOverride.AttributeOverrides)
            {
                AttributeInfo attrInfo = node.Type.GetAttributeInfo(attrOverride.Name);
                node.SetAttribute(attrInfo, attrInfo.Type.Convert(attrOverride.AttribValue));
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Reads the node specified by the child metadata</summary>
        /// <param name="nodeInfo">Child metadata for node</param>
        /// <param name="reader">XML reader</param>
        /// <returns>DomNode specified by the child metadata</returns>
        protected virtual DomNode ReadElement(ChildInfo nodeInfo, XmlReader reader)
        {
            // handle polymorphism, if necessary
            DomNodeType type = GetChildType(nodeInfo.Type, reader);
            int index = type.Name.LastIndexOf(':');
            string typeNS = type.Name.Substring(0, index);

            DomNode node = new DomNode(type, nodeInfo);

            // read attributes
            while (reader.MoveToNextAttribute())
            {
                if (reader.Prefix == string.Empty ||
                    reader.LookupNamespace(reader.Prefix) == typeNS)
                {
                    AttributeInfo attributeInfo = type.GetAttributeInfo(reader.LocalName);
                    if (attributeInfo != null)
                    {
                        string valueString = reader.Value;
                        if (attributeInfo.Type.Type == AttributeTypes.Reference)
                        {
                            // save reference so it can be resolved after all nodes have been read
                            m_nodeReferences.Add(new XmlNodeReference(node, attributeInfo, valueString));
                        }
                        else
                        {
                            object value = attributeInfo.Type.Convert(valueString);
                            node.SetAttribute(attributeInfo, value);
                        }
                    }
                }
            }

            // add node to map if it has an id
            if (node.Type.IdAttribute != null)
            {
                string id = node.GetId();
                if (!string.IsNullOrEmpty(id))
                    m_nodeDictionary[id] = node; // don't Add, in case there are multiple DomNodes with the same id
            }

            reader.MoveToElement();

            if (!reader.IsEmptyElement)
            {
                // read child elements
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        // look up metadata for this element
                        ChildInfo childInfo = type.GetChildInfo(reader.LocalName);
                        if (childInfo != null)
                        {
                            DomNode childObject = ReadElement(childInfo, reader);
                            // at this point, child is a fully populated sub-tree

                            if (childInfo.IsList)
                            {
                                node.GetChildList(childInfo).Add(childObject);
                            }
                            else
                            {
                                node.SetChild(childInfo, childObject);
                            }
                        }
                        else
                        {
                            // try reading as an attribute
                            AttributeInfo attributeInfo = type.GetAttributeInfo(reader.LocalName);
                            if (attributeInfo != null)
                            {
                                reader.MoveToElement();

                                if (!reader.IsEmptyElement)
                                {
                                    // read element text
                                    while (reader.Read())
                                    {
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            object value = attributeInfo.Type.Convert(reader.Value);
                                            node.SetAttribute(attributeInfo, value);
                                            // skip child elements, as this is an attribute value
                                            reader.Skip();
                                            break;
                                        }
                                        if (reader.NodeType == XmlNodeType.EndElement)
                                        {
                                            break;
                                        }
                                    }

                                    reader.MoveToContent();
                                }
                            }
                            else
                            {
                                // skip unrecognized element
                                reader.Skip();
                                // if that takes us to the end of the enclosing element, break
                                if (reader.NodeType == XmlNodeType.EndElement)
                                    break;
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        AttributeInfo attributeInfo = type.GetAttributeInfo(string.Empty);
                        if (attributeInfo != null)
                        {
                            object value = attributeInfo.Type.Convert(reader.Value);
                            node.SetAttribute(attributeInfo, value);
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }
                }
            }

            reader.MoveToContent();

            return node;
        }
Ejemplo n.º 24
0
        private DomNode ReadElement(ChildInfo nodeInfo, XmlReader reader)
        {
            // handle polymorphism, if necessary
            var type   = GetChildType(nodeInfo.Type, reader);
            var index  = type.Name.LastIndexOf(':');
            var typeNs = type.Name.Substring(0, index);

            var node = new DomNode(type, nodeInfo);

            // read attributes
            while (reader.MoveToNextAttribute())
            {
                if (reader.Prefix == string.Empty ||
                    reader.LookupNamespace(reader.Prefix) == typeNs)
                {
                    var attributeInfo = type.GetAttributeInfo(reader.LocalName);
                    if (attributeInfo != null)
                    {
                        var valueString = reader.Value;
                        if (attributeInfo.Type.Type == AttributeTypes.Reference)
                        {
                            // save reference so it can be resolved after all nodes have been read
                            m_nodeReferences.Add(new XmlNodeReference(node, attributeInfo, valueString));
                        }
                        else
                        {
                            var value = attributeInfo.Type.Convert(valueString);
                            node.SetAttribute(attributeInfo, value);
                        }
                    }
                }
            }

            // add node to map if it has an id
            if (node.Type.IdAttribute != null)
            {
                var id = node.GetId();
                if (!string.IsNullOrEmpty(id))
                {
                    m_nodeDictionary[id] = node; // don't Add, in case there are multiple DomNodes with the same id
                }
            }

            reader.MoveToElement();

            if (!reader.IsEmptyElement)
            {
                // read child elements
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        // look up metadata for this element
                        var childInfo = type.GetChildInfo(reader.LocalName);
                        if (childInfo != null)
                        {
                            var childNode = ReadElement(childInfo, reader);
                            if (childNode != null)
                            {
                                // childNode is fully populated sub-tree
                                if (childInfo.IsList)
                                {
                                    node.GetChildList(childInfo).Add(childNode);
                                }
                                else
                                {
                                    node.SetChild(childInfo, childNode);
                                }
                            }
                        }
                        else
                        {
                            // try reading as an attribute
                            var attributeInfo = type.GetAttributeInfo(reader.LocalName);
                            if (attributeInfo != null)
                            {
                                reader.MoveToElement();

                                if (!reader.IsEmptyElement)
                                {
                                    // read element text
                                    while (reader.Read())
                                    {
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            var value = attributeInfo.Type.Convert(reader.Value);
                                            node.SetAttribute(attributeInfo, value);
                                            // skip child elements, as this is an attribute value
                                            reader.Skip();
                                            break;
                                        }
                                        if (reader.NodeType == XmlNodeType.EndElement)
                                        {
                                            break;
                                        }
                                    }

                                    reader.MoveToContent();
                                }
                            }
                            else
                            {
                                // skip unrecognized element
                                reader.Skip();
                                // if that takes us to the end of the enclosing element, break
                                if (reader.NodeType == XmlNodeType.EndElement)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        var attributeInfo = type.GetAttributeInfo(string.Empty);
                        if (attributeInfo != null)
                        {
                            var value = attributeInfo.Type.Convert(reader.Value);
                            node.SetAttribute(attributeInfo, value);
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }
                }
            }

            reader.MoveToContent();

            return(node);
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Reads the node specified by the child metadata</summary>
        /// <param name="nodeInfo">Child metadata for node</param>
        /// <param name="reader">XML reader</param>
        /// <returns>DomNode specified by the child metadata</returns>
        protected virtual DomNode ReadElement(ChildInfo nodeInfo, XmlReader reader)
        {
            // handle polymorphism, if necessary
            DomNodeType type = null;
            var substitutionGroupRule = nodeInfo.Rules.OfType<SubstitutionGroupChildRule>().FirstOrDefault();
            if (substitutionGroupRule != null)
            {
                foreach (var sub in substitutionGroupRule.Substitutions)
                {
                    if (sub.Name == reader.LocalName)
                    {
                        type = sub.Type;
                        break;
                    }
                }

                // Fallback to non-substituted version (for example loading an old schema).
                if (type == null)
                    type = GetChildType(nodeInfo.Type, reader);

                if (type == null)
                    throw new InvalidOperationException("Could not match substitution group for child " + nodeInfo.Name);
            }
            else
            {
                type = GetChildType(nodeInfo.Type, reader);
            }

            int index = type.Name.LastIndexOf(':');
            string typeNS = type.Name.Substring(0, index);

            DomNode node = new DomNode(type, nodeInfo);

            // read attributes
            while (reader.MoveToNextAttribute())
            {
                if (reader.Prefix == string.Empty ||
                    reader.LookupNamespace(reader.Prefix) == typeNS)
                {
                    AttributeInfo attributeInfo = type.GetAttributeInfo(reader.LocalName);
                    if (attributeInfo != null)
                    {
                        ReadAttribute(node, attributeInfo, reader.Value);
                    }
                }
            }

            // add node to map if it has an id
            if (node.Type.IdAttribute != null)
            {
                string id = node.GetId();
                if (!string.IsNullOrEmpty(id))
                    m_nodeDictionary[id] = node; // don't Add, in case there are multiple DomNodes with the same id
            }

            reader.MoveToElement();

            if (!reader.IsEmptyElement)
            {
                // read child elements
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        // look up metadata for this element
                        ChildInfo childInfo = type.GetChildInfo(reader.LocalName);
                        if (childInfo == null)
                        {
                            // Try and get substitution group
                            childInfo = GetSubsitutionGroup(type, reader.LocalName);
                        }

                        if (childInfo != null)
                        {
                            DomNode childNode = ReadElement(childInfo, reader);
                            if (childNode != null)
                            {
                                // childNode is fully populated sub-tree
                                if (childInfo.IsList)
                                {
                                    node.GetChildList(childInfo).Add(childNode);
                                }
                                else
                                {
                                    node.SetChild(childInfo, childNode);
                                }
                            }
                        }
                        else
                        {
                            // try reading as an attribute
                            AttributeInfo attributeInfo = type.GetAttributeInfo(reader.LocalName);
                            if (attributeInfo != null)
                            {
                                reader.MoveToElement();

                                if (!reader.IsEmptyElement)
                                {
                                    // read element text
                                    while (reader.Read())
                                    {
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            ReadAttribute(node, attributeInfo, reader.Value);                                            
                                            // skip child elements, as this is an attribute value
                                            reader.Skip();
                                            break;
                                        }
                                        if (reader.NodeType == XmlNodeType.EndElement)
                                        {
                                            break;
                                        }
                                    }

                                    reader.MoveToContent();
                                }
                            }
                            else
                            {
                                    // skip unrecognized element
                                    reader.Skip();
                                    // if that takes us to the end of the enclosing element, break
                                    if (reader.NodeType == XmlNodeType.EndElement)
                                        break;
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        AttributeInfo attributeInfo = type.GetAttributeInfo(string.Empty);
                        if (attributeInfo != null)
                        {
                            ReadAttribute(node, attributeInfo, reader.Value);                                                      
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }
                }
            }

            reader.MoveToContent();

            return node;
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Converts a node to a string reference when writing DomNode</summary>
        /// <param name="refNode">Node that is referenced</param>
        /// <param name="root">Root node of data that is being written</param>
        /// <param name="uri">URI of data that is being written</param>
        /// <returns>String encoding the reference to the node</returns>
        protected virtual string GetNodeReferenceString(DomNode refNode, DomNode root, Uri uri)
        {
            string id = refNode.GetId();

            // if referenced node is in another resource, prepend URI
            DomNode nodeRoot = refNode.GetRoot();
            if (nodeRoot != root)
            {
                IResource resource = nodeRoot.As<IResource>();
                if (resource != null)
                    id = resource.Uri.LocalPath + "#" + id;
            }

            return id;
        }
Ejemplo n.º 27
0
        private static string GetNodeReferenceString(DomNode refNode, DomNode root)
        {
            var id = refNode.GetId();

            // if referenced node is in another resource, prepend URI
            if (!refNode.IsDescendantOf(root))
            {
                var nodeRoot = refNode.GetRoot();
                var resource = nodeRoot.As<IResource>();
                if (resource != null)
                    id = resource.Uri.LocalPath + "#" + id;
            }

            return id;
        }
Ejemplo n.º 28
0
 private string CreateKey(DomNode node, AttributeInfo attrib)
 {
     return string.Format("{0}.{1}", node.GetId(), attrib.Name);
 }