Package org.xmldb.api.modules

Examples of org.xmldb.api.modules.XPathQueryService


        String query;
        boolean exceptionThrown;
        @SuppressWarnings("unused")
    String message;
        try {
            XPathQueryService service =
                    storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);

            System.out.println("testTypedVariables 1: ========");
            query = "let $v as element()* := ( <assign/> , <assign/> )\n" + "let $w := <r>{ $v }</r>\n" + "let $x as element()* := $w/assign\n" + "return $x";
            result = service.query(query);
            assertEquals("XQuery: " + query, 2, result.getSize());
            assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
            assertEquals("XQuery: " + query, "assign", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());

            System.out.println("testTypedVariables 2: ========");
            query = "let $v as node()* := ()\n" + "return $v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 0, result.getSize());

            System.out.println("testTypedVariables 3: ========");
            query = "let $v as item()* := ()\n" + "return $v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 0, result.getSize());

            System.out.println("testTypedVariables 4: ========");
            query = "let $v as empty() := ()\n" + "return $v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 0, result.getSize());

            System.out.println("testTypedVariables 5: ========");
            query = "let $v as item() := ()\n" + "return $v";
            try {
                exceptionThrown = false;
                result = service.query(query);
            } catch (XMLDBException e) {
                exceptionThrown = true;
                message = e.getMessage();
            }
            assertTrue("XQuery: " + query, exceptionThrown);

            System.out.println("testTypedVariables 6: ========");
            query = "let $v as item()* := ( <a/> , 1 )\n" + "return $v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 2, result.getSize());
            assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
            assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
            assertEquals("XQuery: " + query, "1", ((XMLResource) result.getResource(1)).getContent());

            System.out.println("testTypedVariables 7: ========");
            query = "let $v as node()* := ( <a/> , 1 )\n" + "return $v";
            try {
                exceptionThrown = false;
                result = service.query(query);
            } catch (XMLDBException e) {
                exceptionThrown = true;
                message = e.getMessage();
            }
            assertTrue(exceptionThrown);

            System.out.println("testTypedVariables 8: ========");
            query = "let $v as item()* := ( <a/> , 1 )\n" + "let $w as element()* := $v\n" + "return $w";
            try {
                exceptionThrown = false;
                result = service.query(query);
                result = service.query(query);
            } catch (XMLDBException e) {
                exceptionThrown = true;
                message = e.getMessage();
            }
            assertTrue(exceptionThrown);

            System.out.println("testTypedVariables 9: ========");
            query = "declare variable $v as element()* {( <assign/> , <assign/> ) };\n" + "declare variable $w { <r>{ $v }</r> };\n" + "declare variable $x as element()* { $w/assign };\n" + "$x";
            result = service.query(query);
            assertEquals("XQuery: " + query, 2, result.getSize());
            assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
            assertEquals("XQuery: " + query, "assign", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());

            System.out.println("testTypedVariables 10: ========");
            query = "declare variable $v as node()* { () };\n" + "$v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 0, result.getSize());

            System.out.println("testTypedVariables 11: ========");
            query = "declare variable $v as item()* { () };\n" + "$v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 0, result.getSize());

            System.out.println("testTypedVariables 12: ========");
            query = "declare variable $v as empty() { () };\n" + "$v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 0, result.getSize());

            System.out.println("testTypedVariables 13: ========");
            query = "declare variable $v as item() { () };\n" + "$v";
            try {
                exceptionThrown = false;
                result = service.query(query);
            } catch (XMLDBException e) {
                exceptionThrown = true;
                message = e.getMessage();
            }
            assertTrue("XQuery: " + query, exceptionThrown);

            System.out.println("testTypedVariables 14: ========");
            query = "declare variable $v as item()* { ( <a/> , 1 ) }; \n" + "$v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 2, result.getSize());
            assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
            assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
            assertEquals("XQuery: " + query, "1", ((XMLResource) result.getResource(1)).getContent());

            System.out.println("testTypedVariables 15: ========");
            query = "declare variable $v as node()* { ( <a/> , 1 ) };\n" + "$v";
            try {
                exceptionThrown = false;
                result = service.query(query);
            } catch (XMLDBException e) {
                exceptionThrown = true;
                message = e.getMessage();
            }
            assertTrue(exceptionThrown);

            System.out.println("testTypedVariables 16: ========");
            query = "declare variable $v as item()* { ( <a/> , 1 ) };\n" + "declare variable $w as element()* { $v };\n" + "$w";
            try {
                exceptionThrown = false;
                result = service.query(query);
            } catch (XMLDBException e) {
                exceptionThrown = true;
                message = e.getMessage();
            }
            assertTrue(exceptionThrown);

            System.out.println("testTypedVariables 15: ========");
            query = "let $v as document-node() :=  doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "') \n" + "return $v";
            result = service.query(query);
            assertEquals("XQuery: " + query, 1, result.getSize());
            //TODO : no way to test the node type ?
            //assertEquals( "XQuery: " + query, Node.DOCUMENT_NODE, ((XMLResource)result.getResource(0)));
            assertEquals("XQuery: " + query, "test", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());

View Full Code Here


          XMLResource.RESOURCE_TYPE);
      r.setContent(
        "<properties><property key=\"type\">Table</property></properties>");
      coll.storeResource(r);

      XPathQueryService xpqs =
        (XPathQueryService) coll.getService("XPathQueryService", "1.0");
      ResourceSet rs =
        xpqs.query(
          "//properties[property[@key='type' and text()='Table']]");
      for (ResourceIterator i = rs.getIterator();
        i.hasMoreResources();
        ) {
        r = (XMLResource) i.nextResource();
View Full Code Here

     */
    public boolean execute() throws Exception {
        Collection col = DatabaseManager.getCollection(collectionPath, "admin", null);
    XUpdateQueryService service = (XUpdateQueryService)
      col.getService("XUpdateQueryService", "1.0");
    XPathQueryService query = (XPathQueryService)
        col.getService("XPathQueryService", "1.0");
    append(service);
    query(query);
        remove(service);
        return false;
View Full Code Here

   
    Assert.assertEquals(1, mods);
    System.out.println(mods + " nodes updated.");
   
    // query for section
    XPathQueryService query = (XPathQueryService)
      col.getService("XPathQueryService", "1.0");
    ResourceSet result = query.query("/article/section/para/text()");
    Assert.assertEquals(1, result.getSize());
    updateText = result.getResource(0).getContent().toString();
    result = query.query("/article/section/para[. = '" + updateText + "']");
    Assert.assertEquals(1, result.getSize());
    System.out.println(result.getResource(0).getContent().toString());
   
    mods = service.update(REMOVE);
    Assert.assertEquals(1, mods);
View Full Code Here

        String content = "<?xml version='1.0'?><person id=\"" + id + "\"><name>Jason</name></person>";
        resource = testCollection.createResource(resourceName, "XMLResource");
        resource.setContent(content);
        testCollection.storeResource(resource);

        XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
        ResourceSet rs = service.query("/person[@id='" + id + "']");

        for (ResourceIterator iterator = rs.getIterator(); iterator.hasMoreResources();) {
            Resource r = iterator.nextResource();
            System.err.println("Resource id=" + r.getId() + " xml=" + r.getContent());
            testCollection.removeResource(r);
View Full Code Here

       XMLResource doc = (XMLResource)
       testCollection.createResource(TestConstants.TEST_XML_URI.toString(), "XMLResource" );
       doc.setContent(DOCUMENT_CONTENT2);
       testCollection.storeResource(doc);

       XPathQueryService service = (XPathQueryService)
       testCollection.getService("XPathQueryService", "1.0");

       try {
               exceptionThrown = false;
               result = service.query("/test/c[(# exist:force-index-use #) { . = xs:dateTime(\"2002-12-07T12:20:46.275+01:00\") }]");
       } catch (Exception e) {
           e.printStackTrace();
           if (e.getMessage().indexOf("XQDYxxxx") != Constants.STRING_NOT_FOUND)
                       exceptionThrown = true;
               else throw e;
       }
       assertTrue("Exception expected : missing index", exceptionThrown);

       try {
               exceptionThrown = false;
               result = service.query("/test/d[(# exist:force-index-use #) { . = xs:double(1) }]");
           } catch (Exception e) {
               if (e.getMessage().indexOf("XQDYxxxx") != Constants.STRING_NOT_FOUND)
                       exceptionThrown = true;
               else throw e;
           }
           assertTrue("Exception expected : missing index", exceptionThrown);

           try {
               exceptionThrown = false;
               result = service.query("/test/e[(# exist:force-index-use #) { . = xs:float(1) }]");
               } catch (Exception e) {
               if (e.getMessage().indexOf("XQDYxxxx") != Constants.STRING_NOT_FOUND)
                       exceptionThrown = true;
               else throw e;
               }
               assertTrue("Exception expected : missing index", exceptionThrown);

           try {
               exceptionThrown = false;
               result = service.query("/test/f[(# exist:force-index-use #) { . = true() }]");
                    } catch (Exception e) {
               if (e.getMessage().indexOf("XQDYxxxx") != Constants.STRING_NOT_FOUND)
                       exceptionThrown = true;
               else throw e;
                    }
                    assertTrue("Exception expected : missing index", exceptionThrown);

            try {
               exceptionThrown = false;
               result = service.query("/test/g[(# exist:force-index-use #) { . = 1 }]");
               } catch (Exception e) {
               if (e.getMessage().indexOf("XQDYxxxx") != Constants.STRING_NOT_FOUND)
                       exceptionThrown = true;
               else throw e;
               }
               assertTrue("Exception expected : missing index", exceptionThrown);

           try {
               exceptionThrown = false;
               result = service.query("/test/h[(# exist:force-index-use #) { . = '1' }]");
           } catch (Exception e) {
               if (e.getMessage().indexOf("XQDYxxxx") != Constants.STRING_NOT_FOUND)
                       exceptionThrown = true;
               else throw e;
           }
View Full Code Here

        //... then index document
        XMLResource doc = (XMLResource)
        testCollection.createResource(TestConstants.TEST_XML_URI.toString(), "XMLResource" );
        doc.setContent(DOCUMENT_CONTENT); testCollection.storeResource(doc);

        XPathQueryService service = (XPathQueryService)
        testCollection.getService("XPathQueryService", "1.0");

        //3 numeric values
        ResourceSet result = service.query("util:index-key-occurrences(/test/a, 1)");
        assertEquals(1, result.getSize());
        assertEquals("3", result.getResource(0).getContent());
        //... but 1 string value
        result = service.query("util:index-key-occurrences(/test/b, \"1\")");
        assertEquals(1, result.getSize());
        assertEquals("1", result.getResource(0).getContent());

        //3 numeric values
        result = service.query("util:qname-index-lookup(xs:QName(\"a\"), 1 ) ");
        assertEquals(3, result.getSize());
        //... but 1 string value
        result = service.query("util:qname-index-lookup(xs:QName(\"b\"), \"1\" ) ");
        assertEquals(1, result.getSize());
   }
View Full Code Here

       testCollection.storeResource(doc);

       // ... then configure collection *manually*
       storeConfiguration(CONF_COLL_URI, CollectionConfiguration.DEFAULT_COLLECTION_CONFIG_FILE_URI, CONFIG1);

       XPathQueryService service = (XPathQueryService) testCollection
               .getService("XPathQueryService", "1.0");

       // No numeric values because we have no index
       ResourceSet result = service.query("util:index-key-occurrences( /test/a, 1 ) ");
       assertEquals(0, result.getSize());
       // No string value because we have no index
       result = service.query("util:index-key-occurrences( /test/b, \"1\" ) ");
       assertEquals(0, result.getSize());

       // No numeric values because we have no index
       result = service.query("util:qname-index-lookup( xs:QName(\"a\"), 1 ) ");
       assertEquals(0, result.getSize());
       // No string value because we have no index
       result = service.query("util:qname-index-lookup( xs:QName(\"b\"), \"1\" ) ");
       assertEquals(0, result.getSize());

       // ...let's activate the index
       IndexQueryService idxConf = (IndexQueryService)
           testCollection.getService("IndexQueryService", "1.0");
       idxConf.reindexCollection();

       //3 numeric values
       result = service.query("util:index-key-occurrences(/test/a, 1)");
       assertEquals("3", result.getResource(0).getContent());
       //... but 1 string value
       result = service.query("util:index-key-occurrences(/test/b, \"1\")");
       assertEquals("1", result.getResource(0).getContent());

       // 3 numeric values
       result = service.query("util:qname-index-lookup( xs:QName(\"a\"), 1 ) ");
       assertEquals(3, result.getSize());
       // ... but 1 string value
       result = service.query("util:qname-index-lookup( xs:QName(\"b\"), \"1\" ) ");
       assertEquals(1, result.getSize());
   }
View Full Code Here

       XMLResource doc = (XMLResource) testCollection.createResource(
               TestConstants.TEST_XML_URI.toString(), "XMLResource");
       doc.setContent(DOCUMENT_CONTENT);
       testCollection.storeResource(doc);

       XPathQueryService service = (XPathQueryService) testCollection
               .getService("XPathQueryService", "1.0");

       //our config file
       ResourceSet result = service.query("xmldb:get-child-resources('" +
               CONF_COLL_URI +
               "')");
       assertEquals(configurationFileName.toString(), result.getResource(0).getContent());

       //3 numeric values
       result = service.query("util:index-key-occurrences(/test/a, 1)");
       assertEquals("3", result.getResource(0).getContent());
       //... but 1 string value
       result = service.query("util:index-key-occurrences(/test/b, \"1\")");
       assertEquals("1", result.getResource(0).getContent());

       // 3 numeric values
       result = service.query("util:qname-index-lookup( xs:QName(\"a\"), 1 ) ");
       assertEquals(3, result.getSize());
       // ... but 1 string value
       result = service.query("util:qname-index-lookup( xs:QName(\"b\"), \"1\" ) ");
       assertEquals(1, result.getSize());
   }
View Full Code Here

       //... then configure collection automatically
       IndexQueryService idxConf = (IndexQueryService)
       testCollection.getService("IndexQueryService", "1.0");
       idxConf.configureCollection(CONFIG1);

       XPathQueryService service = (XPathQueryService) testCollection
       .getService("XPathQueryService", "1.0");

       //our config file
       ResourceSet result = service.query("xmldb:get-child-resources('" +
               CONF_COLL_URI +
               "')");
       assertEquals(configurationFileName.toString(), result.getResource(0).getContent());

       // No numeric values because we have no index
       result = service.query("util:index-key-occurrences( /test/a, 1 ) ");
       assertEquals(0, result.getSize());
       // No string value because we have no index
       result = service.query("util:index-key-occurrences( /test/b, \"1\" ) ");
       assertEquals(0, result.getSize());

       // No numeric values because we have no index
       result = service.query("util:qname-index-lookup( xs:QName(\"a\"), 1 ) ");
       assertEquals(0, result.getSize());
       // No string value because we have no index
       result = service.query("util:qname-index-lookup( xs:QName(\"b\"), \"1\" ) ");
       assertEquals(0, result.getSize());

       // ...let's activate the index
       idxConf.reindexCollection();

       //WARNING : the code hereafter used to *not* work whereas
       //testCollectionConfigurationService4 did.
       //Adding confMgr.invalidateAll(getName()); in Collection.storeInternal solved the problem
       //Strange case that needs investigations... -pb

       //3 numeric values
       result = service.query("util:index-key-occurrences(/test/a, 1)");
       assertEquals("3", result.getResource(0).getContent());
       //... but 1 string value
       result = service.query("util:index-key-occurrences(/test/b, \"1\")");
       assertEquals("1", result.getResource(0).getContent());

       // 3 numeric values
       result = service.query("util:qname-index-lookup( xs:QName(\"a\"), 1 ) ");
       assertEquals(3, result.getSize());
       // ... but 1 string value
       result = service.query("util:qname-index-lookup( xs:QName(\"b\"), \"1\" ) ");
       assertEquals(1, result.getSize());
   }
View Full Code Here

TOP

Related Classes of org.xmldb.api.modules.XPathQueryService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.