Exemplo n.º 1
0
        //-----------------------------------------------------------------------
        // Create the following document:
        //
        // <myNamespace:person xmlns:myNamespace="http://MyNamespace.com" myNamespace:uniqueid="178442">
        //    <myNamespace:Name>
        //	      <myNamespace:Given>Peter</myNamespace:Given>
        //	      <myNamespace:Surname>Jones</myNamespace:Surname>
        //	   </myNamespace:Name>
        //    <myNamespace:Address>
        //	      <myNamespace:City>San Francisco</myNamespace:City>
        //	      <myNamespace:State>California</myNamespace:State>
        //	   </myNamespace:Address>
        //    <myNamespace:Age>37</myNamespace:Age>
        //    <myNamespace:HomePhone>307-888-1445</myNamespace:HomePhone>
        //    <myNamespace:CellPhone>307-612-1445</myNamespace:CellPhone>
        //	</myNamespace:person>
        //
        // It is assumed that the db object has already started an update transaction.
        //-----------------------------------------------------------------------
        static void createADocument(Db db)
        {
            DOMNode rootNode   = null;
            DOMNode parentNode = null;
            DOMNode childNode  = null;

            // <myNamespace:person myNamespace:uniqueid="178442">
            rootNode = db.createRootElement((uint)PredefinedXFlaimCollections.XFLM_DATA_COLLECTION, uiPersonElementId);
            rootNode.setAttributeValueULong(uiUniqueIdAttributeId, 178442);

            //    <myNamespace:Name>
            parentNode = rootNode.createNode(eDomNodeType.ELEMENT_NODE, uiNameElementId, eNodeInsertLoc.XFLM_FIRST_CHILD, parentNode);

            //	      <myNamespace:Given>Peter</myNamespace:Given>
            childNode = parentNode.createNode(eDomNodeType.ELEMENT_NODE, uiGivenElementId, eNodeInsertLoc.XFLM_FIRST_CHILD, childNode);
            childNode.setString("Peter");

            //	      <myNamespace:Surname>Jones</myNamespace:Surname>
            childNode = parentNode.createNode(eDomNodeType.ELEMENT_NODE, uiSurnameElementId, eNodeInsertLoc.XFLM_LAST_CHILD, childNode);
            childNode.setString("Jones");

            //    <myNamespace:Address>
            parentNode = rootNode.createNode(eDomNodeType.ELEMENT_NODE, uiAddressElementId, eNodeInsertLoc.XFLM_LAST_CHILD, parentNode);

            //	      <myNamespace:City>San Francisco</myNamespace:City>
            childNode = parentNode.createNode(eDomNodeType.ELEMENT_NODE, uiCityElementId, eNodeInsertLoc.XFLM_FIRST_CHILD, childNode);
            childNode.setString("San Francisco");

            //	      <myNamespace:State>California</myNamespace:State>
            childNode = parentNode.createNode(eDomNodeType.ELEMENT_NODE, uiStateElementId, eNodeInsertLoc.XFLM_LAST_CHILD, childNode);
            childNode.setString("California");

            //    <myNamespace:Age>37</myNamespace:Age>
            childNode = rootNode.createNode(eDomNodeType.ELEMENT_NODE, uiAgeElementId, eNodeInsertLoc.XFLM_LAST_CHILD, childNode);
            childNode.setUInt(37);

            //    <myNamespace:HomePhone>307-888-1445</myNamespace:HomePhone>
            childNode = rootNode.createNode(eDomNodeType.ELEMENT_NODE, uiHomePhoneElementId, eNodeInsertLoc.XFLM_LAST_CHILD, childNode);
            childNode.setString("307-888-1445");

            //    <myNamespace:CellPhone>307-612-1445</myNamespace:CellPhone>
            childNode = rootNode.createNode(eDomNodeType.ELEMENT_NODE, uiCellPhoneElementId, eNodeInsertLoc.XFLM_LAST_CHILD, childNode);
            childNode.setString("307-612-1445");
        }
Exemplo n.º 2
0
        public bool createDocumentTest(
            Db db)
        {
            DOMNode     doc     = null;
            DOMNode     docRoot = null;
            DOMNode     node    = null;
            DOMNode     node2   = null;
            DOMNode     attr    = null;
            ulong       ulDocId;
            ulong       ulNodeId;
            ulong       ulDocRootId;
            uint        uiTag;
            FlmDataType eDataType;
            uint        uiSetValue = 12345;
            uint        uiGetValue;
            ulong       ulSetValue = 123456;
            ulong       ulGetValue;
            int         iSetValue = -12345;
            int         iGetValue;
            long        lSetValue = -123456;
            long        lGetValue;
            string      sSetValue = "String value";
            string      sGetValue = null;

            byte [] ucSetValue = new byte [] { 0x01, 0x02, 0x03, 0x04, 0x05 };
            byte [] ucGetValue = null;
            RCODE   rc;

            // Create a document

            beginTest("Create document test");
            try
            {
                doc = db.createDocument((uint)PredefinedXFlaimCollections.XFLM_DATA_COLLECTION);
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "creating document");
                return(false);
            }

            // Create a node - can only be one element node subordinate to a document node.

            try
            {
                docRoot = doc.createNode(eDomNodeType.ELEMENT_NODE,
                                         (uint)ReservedElmTag.ELM_ELEMENT_TAG,
                                         eNodeInsertLoc.XFLM_FIRST_CHILD,
                                         docRoot);
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "creating node");
                return(false);
            }

            // Create a node subordinate to the root element node.

            try
            {
                node = docRoot.createNode(eDomNodeType.ELEMENT_NODE,
                                          (uint)ReservedElmTag.ELM_ELEMENT_TAG,
                                          eNodeInsertLoc.XFLM_FIRST_CHILD,
                                          node);
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "creating node");
                return(false);
            }

            // Get the document ID

            try
            {
                ulDocId = node.getDocumentId();
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "calling getDocumentId");
                return(false);
            }

            // Get the node ID

            try
            {
                ulNodeId    = doc.getNodeId();
                ulDocRootId = docRoot.getNodeId();
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "calling getNodeId");
                return(false);
            }
            if (ulNodeId != ulDocId)
            {
                endTest(false, false);
                System.Console.WriteLine("Incorrect document ID: NodeID: {0}, DocID: {1}",
                                         ulNodeId, ulDocId);
                return(false);
            }

            // Create a bunch of siblings and add attributes to them

            for (uint uiLoop = 0; uiLoop < NUM_CHILD_NODES - 1; uiLoop++)
            {
                try
                {
                    node2 = node.createNode(eDomNodeType.ELEMENT_NODE,
                                            (uint)ReservedElmTag.ELM_ELEMENT_TAG,
                                            uiLoop % 2 == 0 ? eNodeInsertLoc.XFLM_NEXT_SIB : eNodeInsertLoc.XFLM_PREV_SIB,
                                            node2);
                }
                catch (XFlaimException ex)
                {
                    endTest(false, ex, "calling createNode");
                    return(false);
                }

                // Node better be an element node, and better not have child nodes.

                try
                {
                    if (node2.getNodeType() != eDomNodeType.ELEMENT_NODE)
                    {
                        endTest(false, false);
                        System.Console.WriteLine("Illegal node type returned from getNodeType: {0}",
                                                 node2.getNodeType());
                        return(false);
                    }
                }
                catch (XFlaimException ex)
                {
                    endTest(false, ex, "calling getNodeType");
                    return(false);
                }


                try
                {
                    if (node2.hasChildren())
                    {
                        endTest(false, false);
                        System.Console.WriteLine("Node erroneously claims to have children");
                        return(false);
                    }
                }
                catch (XFlaimException ex)
                {
                    endTest(false, ex, "calling hasChildren");
                    return(false);
                }

                // Create an attribute and make sure it is present.

                uiTag = (uint)ReservedAttrTag.XFLM_FIRST_RESERVED_ATTRIBUTE_TAG + (uiLoop % BUILTIN_ATTRIBUTES);

                try
                {
                    attr = node2.createAttribute(uiTag, attr);
                }
                catch (XFlaimException ex)
                {
                    endTest(false, ex, "calling createAttribute");
                    return(false);
                }

                try
                {
                    if (!node2.hasAttribute(uiTag))
                    {
                        endTest(false, false);
                        System.Console.WriteLine("Node is missing an attribute");
                        return(false);
                    }
                }
                catch (XFlaimException ex)
                {
                    endTest(false, ex, "calling hasAttribute");
                    return(false);
                }

                // Look up the tag's data type and set an appropriate value
                // Then retrieve it again for verification

                try
                {
                    eDataType = attr.getDataType();
                }
                catch (XFlaimException ex)
                {
                    endTest(false, ex, "calling getDataType");
                    return(false);
                }

                switch (eDataType)
                {
                case FlmDataType.XFLM_NUMBER_TYPE:
                    if (uiLoop % 4 == 1)
                    {
                        ulGetValue = ulSetValue + 1;
                        try
                        {
                            attr.setULong(ulSetValue);
                        }
                        catch (XFlaimException ex)
                        {
                            endTest(false, ex, "calling setULong");
                            return(false);
                        }
                        try
                        {
                            ulGetValue = attr.getULong();
                        }
                        catch (XFlaimException ex)
                        {
                            endTest(false, ex, "calling getULong");
                            return(false);
                        }
                        if (ulSetValue != ulGetValue)
                        {
                            endTest(false, false);
                            System.Console.WriteLine("ulSetValue {0} != ulGetValue {1}",
                                                     ulSetValue, ulGetValue);
                            return(false);
                        }
                    }
                    else if (uiLoop % 4 == 2)
                    {
                        lGetValue = lSetValue + 1;
                        try
                        {
                            attr.setLong(lSetValue);
                        }
                        catch (XFlaimException ex)
                        {
                            endTest(false, ex, "calling setLong");
                            return(false);
                        }
                        try
                        {
                            lGetValue = attr.getLong();
                        }
                        catch (XFlaimException ex)
                        {
                            endTest(false, ex, "calling getLong");
                            return(false);
                        }
                        if (lSetValue != lGetValue)
                        {
                            endTest(false, false);
                            System.Console.WriteLine("lSetValue {0} != lGetValue {1}",
                                                     lSetValue, lGetValue);
                            return(false);
                        }
                    }
                    else if (uiLoop % 4 == 3)
                    {
                        uiGetValue = uiSetValue + 1;
                        try
                        {
                            attr.setUInt(uiSetValue);
                        }
                        catch (XFlaimException ex)
                        {
                            endTest(false, ex, "calling setUInt");
                            return(false);
                        }
                        try
                        {
                            uiGetValue = attr.getUInt();
                        }
                        catch (XFlaimException ex)
                        {
                            endTest(false, ex, "calling getUInt");
                            return(false);
                        }
                        if (uiSetValue != uiGetValue)
                        {
                            endTest(false, false);
                            System.Console.WriteLine("uiSetValue {0} != uiGetValue {1}",
                                                     uiSetValue, uiGetValue);
                            return(false);
                        }
                    }
                    else
                    {
                        iGetValue = iSetValue + 1;
                        try
                        {
                            attr.setInt(iSetValue);
                        }
                        catch (XFlaimException ex)
                        {
                            endTest(false, ex, "calling setInt");
                            return(false);
                        }
                        try
                        {
                            iGetValue = attr.getInt();
                        }
                        catch (XFlaimException ex)
                        {
                            endTest(false, ex, "calling getInt");
                            return(false);
                        }
                        if (iSetValue != iGetValue)
                        {
                            endTest(false, false);
                            System.Console.WriteLine("iSetValue {0} != iGetValue {1}",
                                                     iSetValue, iGetValue);
                            return(false);
                        }
                    }
                    break;

                case FlmDataType.XFLM_TEXT_TYPE:
                    sGetValue = "";
                    try
                    {
                        attr.setString(sSetValue);
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling setString");
                        return(false);
                    }
                    try
                    {
                        sGetValue = attr.getString();
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling getString");
                        return(false);
                    }
                    if (sSetValue != sGetValue)
                    {
                        endTest(false, false);
                        System.Console.WriteLine("sSetValue [{0}] != sGetValue [{1}]",
                                                 sSetValue, sGetValue);
                        return(false);
                    }
                    break;

                case FlmDataType.XFLM_BINARY_TYPE:
                {
                    bool bDataSame;

                    ucGetValue = null;
                    try
                    {
                        attr.setBinary(ucSetValue);
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling setBinary");
                        return(false);
                    }
                    try
                    {
                        ucGetValue = attr.getBinary();
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling getBinary");
                        return(false);
                    }
                    bDataSame = true;
                    if (ucSetValue.Length != ucGetValue.Length)
                    {
                        bDataSame = false;
                    }
                    else
                    {
                        for (uint uiLoop2 = 0; uiLoop2 < ucSetValue.Length; uiLoop2++)
                        {
                            if (ucSetValue [uiLoop2] != ucGetValue [uiLoop2])
                            {
                                bDataSame = false;
                                break;
                            }
                        }
                    }
                    if (!bDataSame)
                    {
                        endTest(false, false);
                        System.Console.WriteLine("Set binary data does not match get binary data");
                        System.Console.Write("Set Binary Data Length: {0}\n[", ucSetValue.Length);
                        for (uint uiLoop2 = 0; uiLoop2 < ucSetValue.Length; uiLoop2++)
                        {
                            System.Console.Write("{0} ", ucSetValue [uiLoop2]);
                        }
                        System.Console.WriteLine("]");
                        System.Console.Write("Get Binary Data Length: {0}\n[", ucGetValue.Length);
                        for (uint uiLoop2 = 0; uiLoop2 < ucGetValue.Length; uiLoop2++)
                        {
                            System.Console.Write("{0} ", ucGetValue [uiLoop2]);
                        }
                        System.Console.WriteLine("]");
                        return(false);
                    }
                    break;
                }

                default:
                    endTest(false, false);
                    System.Console.WriteLine("Invalid data type for attr {0}", eDataType);
                    return(false);
                }

                // Since there's only one attribute, either one of these functions will do
                if ((uiLoop % 2) == 0)
                {
                    try
                    {
                        attr = node2.getFirstAttribute(attr);
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling getFirstAttribute");
                        return(false);
                    }
                }
                else
                {
                    try
                    {
                        attr = node2.getAttribute(uiTag, attr);
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling getAttribute");
                        return(false);
                    }
                }

                // We gave these nodes one and only one attribute
                // The attributes should have no siblings

                rc = RCODE.NE_XFLM_OK;
                try
                {
                    node2 = attr.getPreviousSibling(node2);
                }
                catch (XFlaimException ex)
                {
                    rc = ex.getRCode();
                    if (rc != RCODE.NE_XFLM_DOM_NODE_NOT_FOUND)
                    {
                        endTest(false, ex, "calling getPreviousSibling");
                        return(false);
                    }
                }
                if (rc == RCODE.NE_XFLM_OK)
                {
                    endTest(false, false);
                    System.Console.WriteLine("Attribute should not have a previous sibling");
                    return(false);
                }

                rc = RCODE.NE_XFLM_OK;
                try
                {
                    node2 = attr.getNextSibling(node2);
                }
                catch (XFlaimException ex)
                {
                    rc = ex.getRCode();
                    if (rc != RCODE.NE_XFLM_DOM_NODE_NOT_FOUND)
                    {
                        endTest(false, ex, "calling getNextSibling");
                        return(false);
                    }
                }
                if (rc == RCODE.NE_XFLM_OK)
                {
                    endTest(false, false);
                    System.Console.WriteLine("Attribute should not have a next sibling");
                    return(false);
                }
            }

            // Document node should now have children.

            try
            {
                if (!docRoot.hasChildren())
                {
                    endTest(false, false);
                    System.Console.WriteLine("Document root erroneously reports that it has no child nodes");
                    return(false);
                }
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "calling hasChildren");
                return(false);
            }

            // Reposition to the first child under the document root,
            // iterate through its children (first->last) and perform
            // various DOMNode ops

            for (uint uiLoop = 0; uiLoop < NUM_CHILD_NODES; uiLoop++)
            {
                if (uiLoop == 0)
                {
                    try
                    {
                        node = docRoot.getFirstChild(node);
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling getFirstChild");
                        return(false);
                    }
                }
                else
                {
                    try
                    {
                        node = node.getNextSibling(node);
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling getNextSibling");
                        return(false);
                    }
                }

                try
                {
                    if (node.getParentId() != ulDocRootId)
                    {
                        endTest(false, false);
                        System.Console.WriteLine("Node's parent ID {0} does not match document id {1}",
                                                 node.getParentId(), ulDocId);
                        return(false);
                    }
                }
                catch (XFlaimException ex)
                {
                    endTest(false, ex, "calling getParentId");
                    return(false);
                }
            }

            // There should be no more siblings

            rc = RCODE.NE_XFLM_OK;
            try
            {
                node = node.getNextSibling(node);
            }
            catch (XFlaimException ex)
            {
                rc = ex.getRCode();
                if (rc != RCODE.NE_XFLM_DOM_NODE_NOT_FOUND)
                {
                    endTest(false, ex, "calling getNextSibling");
                    return(false);
                }
            }
            if (rc == RCODE.NE_XFLM_OK)
            {
                endTest(false, false);
                System.Console.WriteLine("Should have been no more next siblings");
                return(false);
            }
            endTest(false, true);


            beginTest("Delete DOM nodes test");

            // Move backwards through the siblings deleting them (except the last one)

            for (uint uiLoop = 0; uiLoop < NUM_CHILD_NODES; uiLoop++)
            {
                if (uiLoop == 0)
                {
                    try
                    {
                        node = docRoot.getLastChild(node);
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling getLastChild");
                        return(false);
                    }
                }
                else
                {
                    try
                    {
                        node2 = node.getPreviousSibling(node2);
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling getPreviousSibling");
                        return(false);
                    }
                    try
                    {
                        node.deleteNode();
                    }
                    catch (XFlaimException ex)
                    {
                        endTest(false, ex, "calling deleteNode");
                        return(false);
                    }
                    node  = node2;
                    node2 = null;
                }
            }

            // There should be no more siblings

            rc = RCODE.NE_XFLM_OK;
            try
            {
                node2 = node.getPreviousSibling(node2);
            }
            catch (XFlaimException ex)
            {
                rc = ex.getRCode();
                if (rc != RCODE.NE_XFLM_DOM_NODE_NOT_FOUND)
                {
                    endTest(false, ex, "calling getPreviousSibling");
                    return(false);
                }
            }
            if (rc == RCODE.NE_XFLM_OK)
            {
                endTest(false, false);
                System.Console.WriteLine("Should have been no more previous siblings");
                return(false);
            }
            try
            {
                node.deleteNode();
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "calling deleteNode");
                return(false);
            }
            node = null;

            endTest(false, true);

            // Test next/previous document.

            beginTest("Next/Previous Document");

            // Create a 2nd document.

            try
            {
                node = db.createDocument((uint)PredefinedXFlaimCollections.XFLM_DATA_COLLECTION);
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "creating document");
                return(false);
            }

            // Make sure 1st document has a next document.

            try
            {
                node2 = doc.getNextDocument(node2);
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "calling getNextDocument");
                return(false);
            }

            // Make sure 2nd document does not have a next document.

            rc = RCODE.NE_XFLM_OK;
            try
            {
                node2 = node.getNextDocument(node2);
            }
            catch (XFlaimException ex)
            {
                rc = ex.getRCode();
                if (rc != RCODE.NE_XFLM_DOM_NODE_NOT_FOUND)
                {
                    endTest(false, ex, "calling getNextDocument");
                    return(false);
                }
            }
            if (rc == RCODE.NE_XFLM_OK)
            {
                endTest(false, false);
                System.Console.WriteLine("Document should NOT have a next document");
                return(false);
            }

            // Make sure 2nd document has a previous document

            try
            {
                node2 = node.getPreviousDocument(node2);
            }
            catch (XFlaimException ex)
            {
                endTest(false, ex, "calling getPreviousDocument");
                return(false);
            }

            // Make sure 1st document does not have a previous document.

            rc = RCODE.NE_XFLM_OK;
            try
            {
                node2 = doc.getPreviousDocument(node2);
            }
            catch (XFlaimException ex)
            {
                rc = ex.getRCode();
                if (rc != RCODE.NE_XFLM_DOM_NODE_NOT_FOUND)
                {
                    endTest(false, ex, "calling getPreviousDocument");
                    return(false);
                }
            }
            if (rc == RCODE.NE_XFLM_OK)
            {
                endTest(false, false);
                System.Console.WriteLine("Document should NOT have a previous document");
                return(false);
            }
            endTest(false, true);

            return(true);
        }