示例#1
0
文件: Node.cs 项目: wushian/Nxdb
        internal static Node Get(DBNode dbNode, Database database, bool check = true)
        {
            if (dbNode == null)
            {
                throw new ArgumentNullException("dbNode");
            }
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }

            // Copy the DBNode if it wasn't created from scratch
            if (check)
            {
                // Is it already created?
                Node node = database.GetNode(dbNode.pre);
                if (node != null)
                {
                    return(node);
                }

                // Otherwise, copy the DBNode so we can make a new Node
                dbNode = dbNode.copy();
            }

            // Create the appropriate database node class
            Node     newNode;
            NodeType nodeType = dbNode.nodeType();

            if (nodeType == org.basex.query.item.NodeType.ELM)
            {
                newNode = new Element(dbNode, database);
            }
            else if (nodeType == org.basex.query.item.NodeType.TXT)
            {
                newNode = new Text(dbNode, database);
            }
            else if (nodeType == org.basex.query.item.NodeType.ATT)
            {
                newNode = new Attribute(dbNode, database);
            }
            else if (nodeType == org.basex.query.item.NodeType.DOC)
            {
                newNode = new Document(dbNode, database);
            }
            else if (nodeType == org.basex.query.item.NodeType.COM)
            {
                newNode = new Comment(dbNode, database);
            }
            else if (nodeType == org.basex.query.item.NodeType.PI)
            {
                newNode = new ProcessingInstruction(dbNode, database);
            }
            else
            {
                throw new ArgumentException("Invalid node type");
            }

            // Cache and return the node
            database.SetNode(dbNode.pre, newNode);
            return(newNode);
        }