Package org.exist.xquery

Examples of org.exist.xquery.XQuery


            }

            if(source != null) {

                //execute the xquery
                final XQuery xquery = broker.getXQueryService();
                xqPool = xquery.getXQueryPool();

                //try and get a pre-compiled query from the pool
                compiled = xqPool.borrowCompiledXQuery(broker, source);

                if(compiled == null) {
                    context = xquery.newContext(AccessContext.REST); //TODO should probably have its own AccessContext.SCHEDULER
                } else {
                    context = compiled.getContext();
                }

                //TODO: don't hardcode this?
                if(resource != null) {
                    context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(resource.getCollection().getURI()).toString());
                    context.setStaticallyKnownDocuments(new XmldbURI[] {
                        resource.getCollection().getURI()
                    });
                }

                if(compiled == null) {

                    try {
                        compiled = xquery.compile(context, source);
                    }
                    catch(final IOException e) {
                        abort("Failed to read query from " + xqueryresource);
                    }
                }

                //declare any parameters as external variables
                if(params != null) {
                    String bindingPrefix = params.getProperty("bindingPrefix");

                    if(bindingPrefix == null) {
                        bindingPrefix = "local";
                    }
                   

                    for(final Entry param : params.entrySet()) {
                        final String key = (String)param.getKey();
                        final String value = (String)param.getValue();
                        context.declareVariable( bindingPrefix + ":" + key, new StringValue(value));
                    }
                }

                xquery.execute(compiled, null);

            } else {
                LOG.warn("XQuery User Job not found: " + xqueryresource + ", job not scheduled");
            }
        } catch(final EXistException ee) {
View Full Code Here


            broker = db.get(subject);

            final Source source = getQuerySource(broker, scriptURI, script);
            if(source == null) {return;}

            final XQuery xquery = broker.getXQueryService();
            final XQueryContext context = xquery.newContext(AccessContext.XMLDB);

            final CompiledXQuery compiled = xquery.compile(context, source);

//            Sequence result = xquery.execute(compiled, subject.getName());

        final ProcessMonitor pm = db.getProcessMonitor();
View Full Code Here

        String query = "declare namespace catalog=\"http://www.w3.org/2005/02/query-test-XQTSCatalog\";"+
            "let $XQTSCatalog := xmldb:document('/db/XQTS/XQTSCatalog.xml') "+
            "return xs:string($XQTSCatalog/catalog:test-suite/@version)";

        XQuery xqs = broker.getXQueryService();
       
        Sequence results = xqs.execute(query, null, AccessContext.TEST);

        if (! results.isEmpty()) {
            String catalog = (String) results.itemAt(0).getStringValue();
            catalog = "XQTS_"+adoptString(catalog);
            File subfolder = new File(folder.getAbsolutePath()+sep+catalog);
View Full Code Here

        else
            query += "for $testGroup in $XQTSCatalog//catalog:test-group[@name = '"+parentName+"']/catalog:test-group";

        query += "\treturn xs:string($testGroup/@name)";

        XQuery xqs = broker.getXQueryService();
        Sequence results = xqs.execute(query, null, AccessContext.TEST);

        if (!results.isEmpty()) {
            File subfolder;
            String subPackage;
View Full Code Here

        String query = "declare namespace catalog=\"http://www.w3.org/2005/02/query-test-XQTSCatalog\";"+
            "let $XQTSCatalog := xmldb:document('/db/XQTS/XQTSCatalog.xml')"+
            "for $testGroup in $XQTSCatalog//catalog:test-group[@name = '"+testGroup+"']/catalog:test-case"+
            "\treturn xs:string($testGroup/@name)";

        XQuery xqs = broker.getXQueryService();
        Sequence results = xqs.execute(query, null, AccessContext.TEST);

        if (!results.isEmpty()) {
            folder.mkdirs();
            File jTest = new File(folder.getAbsolutePath()+sep+"C_"+adoptString(testGroup)+".java");
            FileWriter fstream = new FileWriter(jTest.getAbsoluteFile());
View Full Code Here

        }
    }

    private Sequence enviroment(String file) throws Exception {
        DBBroker broker = null;
        XQuery xquery = null;

        try {
            broker = db.get(db.getSecurityManager().getSystemSubject());
            xquery = broker.getXQueryService();

            broker.getConfiguration().setProperty(XQueryContext.PROPERTY_XQUERY_RAISE_ERROR_ON_FAILED_RETRIEVAL, true);

            String query = "xmldb:document('" + file + "')";

            return xquery.execute(query, null, AccessContext.TEST);

        } finally {
            db.release(broker);
        }
    }
View Full Code Here

        HashMap<String, Sequence> enviroments = new HashMap<String, Sequence>();

        DBBroker broker = null;
        try {
            broker = db.get(db.getSecurityManager().getSystemSubject());
            XQuery xquery = broker.getXQueryService();

            broker.getConfiguration().setProperty(XQueryContext.PROPERTY_XQUERY_RAISE_ERROR_ON_FAILED_RETRIEVAL, true);

            String query = "declare namespace qt='" + QT_NS + "';\n" + "let $testCases := xmldb:document('/db/QT3/" + file + "')\n"
                    + "let $tc := $testCases//qt:environment\n" + "return $tc";

            Sequence result = xquery.execute(query, null, AccessContext.TEST);

            String col = XmldbURI.create("/db/QT3/" + file).removeLastSegment().toString();

            for (NodeProxy node : result.toNodeSet()) {
                ElementImpl el = (ElementImpl) node.getNode();
View Full Code Here

        String enviroment = null;

        DBBroker broker = null;
        try {
            broker = db.get(db.getSecurityManager().getSystemSubject());
            XQuery xquery = broker.getXQueryService();

            broker.getConfiguration().setProperty(XQueryContext.PROPERTY_XQUERY_RAISE_ERROR_ON_FAILED_RETRIEVAL, true);

            String query = "declare namespace qt='" + QT_NS + "';\n" + "let $testCases := xmldb:document('/db/QT3/" + file + "')\n"
                    + "let $tc := $testCases//qt:environment[@name eq '" + name + "']\n" + "let $catalog := xmldb:document('/db/QT3/catalog.xml')\n"
                    + "let $cat := $catalog//qt:environment[@name eq '" + name + "']\n" + "return ($tc, $cat)";

            Sequence result = xquery.execute(query, null, AccessContext.TEST);

            String col = XmldbURI.create("/db/QT3/" + file).removeLastSegment().toString();

            for (NodeProxy node : result.toNodeSet()) {
                ElementImpl el = (ElementImpl) node.getNode();
View Full Code Here

    protected void testCase(String file, String tcName) {
        System.out.println("test " + tcName);

        DBBroker broker = null;
        Sequence result = null;
        XQuery xquery = null;

        // try {
        Set<String> extectedError = new HashSet<String>();
        try {
            broker = db.get(db.getSecurityManager().getSystemSubject());
            xquery = broker.getXQueryService();

            final XQueryContext context = new XQueryContext(db, AccessContext.TEST);

            broker.getConfiguration().setProperty(XQueryContext.PROPERTY_XQUERY_RAISE_ERROR_ON_FAILED_RETRIEVAL, true);

            String query = "declare namespace qt='" + QT_NS + "';\n" + "let $testCases := xmldb:document('/db/QT3/" + file + "')\n"
                    + "let $tc := $testCases//qt:test-case[@name eq \"" + tcName + "\"]\n" + "return $tc";

            XQuery xqs = broker.getXQueryService();

            Sequence results = xqs.execute(query, null, AccessContext.TEST);

            Assert.assertFalse("", results.isEmpty());

            ElementImpl TC = (ElementImpl) results.toNodeSet().get(0).getNode();
View Full Code Here

//            return; //Added by p.b. as a quick attempt to work around current blocking code
//        if (testCase.equals("Constr-cont-document-3"))
//            return; //Added by p.b. as a quick attempt to work around current blocking code

        DBBroker broker = null;
        XQuery xquery = null;

        try {
            broker = db.get(db.getSecurityManager().getSystemSubject());
            broker.getConfiguration().setProperty( XQueryContext.PROPERTY_XQUERY_RAISE_ERROR_ON_FAILED_RETRIEVAL, true);

            xquery = broker.getXQueryService();

            prepare(broker, xquery);

            String query = "declare namespace catalog=\"http://www.w3.org/2005/02/query-test-XQTSCatalog\";\n"+
            "let $XQTSCatalog := xmldb:document('/db/XQTS/XQTSCatalog.xml')\n"+
            "let $tc := $XQTSCatalog/catalog:test-suite//catalog:test-group[@name eq \""+testGroup+"\"]/catalog:test-case[@name eq \""+testCase+"\"]\n"+
            "return $tc";

            Sequence results = xquery.execute(query, null, AccessContext.TEST);
           
            Assert.assertFalse("", !results.hasOne());

            ElementImpl TC = (ElementImpl) results.toNodeSet().get(0).getNode();

            //collect test case information
            String folder = "";
            String scenario = "";
            String script = "";
            //DateTimeValue scriptDateTime = null;
            NodeListImpl inputFiles = new NodeListImpl();
            NodeListImpl outputFiles = new NodeListImpl();
            ElementImpl contextItem = null;
            NodeListImpl modules = new NodeListImpl();
            String expectedError = "";

            String name = null;

            NodeList childNodes = TC.getChildNodes();
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node child = childNodes.item(i);
                switch (child.getNodeType()) {
                    case Node.ATTRIBUTE_NODE:
                        name = ((Attr)child).getName();
                        if (name.equals("FilePath"))
                            folder = ((Attr)child).getValue();
                        else if (name.equals("scenario"))
                            scenario = ((Attr)child).getValue();
                        break;
                    case Node.ELEMENT_NODE:
                        name = ((ElementImpl) child).getLocalName();
                        if (name.equals("query")) {
                            ElementImpl el = ((ElementImpl) child);
                            script = el.getAttribute("name");
                            //scriptDateTime = new DateTimeValue(el.getAttribute("date"));
                        }
                        else if (name.equals("input-file"))
                            inputFiles.add(child);
                        else if (name.equals("output-file"))
                            outputFiles.add(child);
                        else if (name.equals("contextItem"))
                            contextItem = (ElementImpl)child;
                        else if (name.equals("module"))
                            modules.add(child);
                        else if (name.equals("expected-error"))
                            expectedError = ((ElementImpl) child).getNodeValue();
                        break;
                    default :
                        ;
                }
            }

            Sequence result = null;

            //compile & evaluate
            File caseScript = new File(XQTS_folder+"Queries/XQuery/"+folder, script+".xq");
            try {
                XQueryContext context;

                context = xquery.newContext(AccessContext.TEST);

                //map modules' namespaces to location
                Map<String, String> moduleMap = (Map<String, String>)broker.getConfiguration().getProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP);
                for (int i = 0; i < modules.getLength(); i++) {
                    ElementImpl module = (ElementImpl)modules.item(i);
                    String id = module.getNodeValue();
                    moduleMap.put(module.getAttribute("namespace"), moduleSources.get(id));
                }
                broker.getConfiguration().setProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP, moduleMap);

                //declare variable
                for (int i = 0; i < inputFiles.getLength(); i++) {
                    ElementImpl inputFile = (ElementImpl)inputFiles.item(i);
                    String id = inputFile.getNodeValue();

                    //use DocUtils
                    //context.declareVariable(
                        //inputFile.getAttribute("variable"),
                        //DocUtils.getDocument(context, sources.get(id))
                    //);

                    //in-memory nodes
                    context.declareVariable(inputFile.getAttribute("variable"), loadVarFromURI(context, sources.get(id)));
                }

                Sequence contextSequence = null;
                //set context item
                if (contextItem != null) {
                    String id = contextItem.getNodeValue();
                    contextSequence = loadVarFromURI(context, sources.get(id));
                }

                fixBrokenTests(context, testGroup, testCase);

                //compare result with one provided by test case
                boolean ok = false;
               
              Exception error = null;
                if ("runtime-error".equals(scenario)) {
                  try {
                        //compile
                        CompiledXQuery compiled = xquery.compile(context, new FileSource(caseScript, "UTF8", true));

                        //execute
                        result = xquery.execute(compiled, contextSequence);
                       
                        if (outputFiles.getLength() != 0) {
                            //can be answered
                          for (int i = 0; i < outputFiles.getLength(); i++) {
                              ElementImpl outputFile = (ElementImpl)outputFiles.item(i);
         
                              String compare = outputFile.getAttribute("compare");
                              if (compare != null && compare.equalsIgnoreCase("IGNORE")) {
                                ok = true;
                                break;
                              }
         
                              if (compareResult(script, "XQTS_1_0_3/ExpectedTestResults/"+folder, outputFile, result)) {
                                  ok = true;
                                  break;
                              }
                          }
                        } else {
                          error = catchError(result);
                        }

                  } catch (Exception e) {
                    error = e;
          }
                 
                    if (!ok && error != null && expectedError != null) {// error.getMessage().contains(expectedError)) {
                      ok = true;
                    }
                } else {
                    //compile
                    CompiledXQuery compiled = xquery.compile(context, new FileSource(caseScript, "UTF8", true));

                    //execute
                    result = xquery.execute(compiled, contextSequence);

                    //check answer
                  for (int i = 0; i < outputFiles.getLength(); i++) {
                      ElementImpl outputFile = (ElementImpl)outputFiles.item(i);
 
View Full Code Here

TOP

Related Classes of org.exist.xquery.XQuery

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.