Package org.exist.xquery

Examples of org.exist.xquery.CompiledXQuery


        DBBroker broker = null;
        DocumentImpl resource = null;
        Source source = null;
        XQueryPool xqPool  = null;
        CompiledXQuery compiled = null;
        XQueryContext context = null;

        try {

            //get the xquery
            broker = pool.get(user);

            if(xqueryresource.indexOf(':') > 0) {
                source = SourceFactory.getSource(broker, "", xqueryresource, true);
            } else {
                final XmldbURI pathUri = XmldbURI.create(xqueryresource);
                resource = broker.getXMLResource(pathUri, Lock.READ_LOCK);

                if(resource != null) {
                    source = new DBSource(broker, (BinaryDocument)resource, true);
                }
            }

            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());
View Full Code Here


            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();

            //execute the XQuery
            try {
            final UserDefinedFunction function = context.resolveFunction(functionName, 0);
            if (function != null) {
                  context.getProfiler().traceQueryStart();
                  pm.queryStarted(context.getWatchDog());
                 
                  final FunctionCall call = new FunctionCall(context, function);
                  if (args != null)
                    {call.setArguments(args);}
                  call.analyze(new AnalyzeContextInfo());
              call.eval(NodeSet.EMPTY_SET);
            }
            } catch(final XPathException e) {
              //XXX: log
              e.printStackTrace();
            } finally {
              if (pm != null) {
                context.getProfiler().traceQueryEnd(context);
                pm.queryCompleted(context.getWatchDog());
              }
              compiled.reset();
            context.reset();
            }
           
        } catch (final Exception e) {
          //XXX: log
View Full Code Here

               
              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
View Full Code Here

        final String requestAttr = (String) request.getAttribute(ATTR_XQUERY_ATTRIBUTE);
        DBBroker broker = null;
        try {
          broker = getPool().get(user);
            final XQuery xquery = broker.getXQueryService();
            CompiledXQuery query = xquery.getXQueryPool().borrowCompiledXQuery(broker, source);

            XQueryContext context;
            if (query==null) {
               context = xquery.newContext(AccessContext.REST);
               context.setModuleLoadPath(moduleLoadPath);
               try {
                 query = xquery.compile(context, source);
                  
               } catch (final XPathException ex) {
                  throw new EXistException("Cannot compile xquery: "+ ex.getMessage(), ex);
                 
               } catch (final IOException ex) {
                  throw new EXistException("I/O exception while compiling xquery: " + ex.getMessage() ,ex);
               }
              
            } else {
               context = query.getContext();
               context.setModuleLoadPath(moduleLoadPath);
            }

            final Properties outputProperties = new Properties();
            outputProperties.put("base-uri", collectionURI.toString());
View Full Code Here

                for (String path : staticDocs) {
                    contextDocs[i++] = XmldbURI.createInternal(path);
                }
                context.setStaticallyKnownDocuments(contextDocs);
            }
            final CompiledXQuery compiled = xquery.compile(context, xquery3declaration + caseScript);
            result = xquery.execute(compiled, contextSequence);

            for (int i = 0; i < expected.getLength(); i++) {
                Node node = expected.item(i);
                checkResults(node.getLocalName(), node.getChildNodes(), result);
View Full Code Here

    }
   
    public List<RestXqService> findServices(final DBBroker broker, final DocumentImpl document) throws ExQueryException {
       
        try {
            final CompiledXQuery compiled = XQueryCompiler.compile(broker, document);

            /*
             * examine the compiled query, record all modules and modules of modules.
             * Keep a dependencies list so that we can act on it if a module is deleted.
             */
View Full Code Here

    }

    @Override
    public CompiledXQuery getCompiledQuery(final DBBroker broker, final URI xqueryLocation) throws RestXqServiceException {
       
        CompiledXQuery xquery = null;
        cacheLock.writeLock().lock();
        try {
            final Deque<CompiledXQuery> queries = cache.get(xqueryLocation);
           
            if(queries != null && !queries.isEmpty()) {
                xquery = queries.pop();
            }
        } finally {
            cacheLock.writeLock().unlock();
        }
       
        if(xquery == null) {
            xquery = XQueryCompiler.compile(broker, xqueryLocation);
        }
       
        //reset the state of the query
        xquery.reset();
        xquery.getContext().getWatchDog().reset();
        xquery.getContext().prepareForExecution();
       
        return xquery;
    }
View Full Code Here

            context.setStaticallyKnownDocuments(docs);

            String query = IMPORT + xqueryContent;
            System.out.println("query: " + query);

            CompiledXQuery compiled = service.compile(context, query);

            for (int i = 0; i < count; i++) {
                generatedFiles[i] = File.createTempFile(prefix, ".xml");

                context.declareVariable("filename", generatedFiles[i].getName());
View Full Code Here

    public Sequence execute(final ResourceFunction resourceFunction, final Iterable<TypedArgumentValue> arguments, final HttpRequest request) throws RestXqServiceException {
       
        final RestXqServiceCompiledXQueryCache cache = RestXqServiceCompiledXQueryCacheImpl.getInstance();
       
        DBBroker broker = null;
        CompiledXQuery xquery = null;
        ProcessMonitor processMonitor = null;
       
        try {
           
            broker = getBrokerPool().get(getBrokerPool().getSubject());
           
            //ensure we can execute the function before going any further
            checkSecurity(broker, resourceFunction.getXQueryLocation());
           
            //get a compiled query service from the cache
            xquery = cache.getCompiledQuery(broker, resourceFunction.getXQueryLocation());
           
            //find the function that we will execute
            final UserDefinedFunction fn = findFunction(xquery, resourceFunction.getFunctionSignature());
           
            final XQueryContext xqueryContext = xquery.getContext();
           
            //set the request object - can later be used by the EXQuery Request Module
            xqueryContext.setAttribute(EXQ_REQUEST_ATTR, request);
           
            //TODO this is a workaround?
            declareVariables(xqueryContext);
           
            //START workaround: evaluate global variables in modules, as they are reset by XQueryContext.reset()
            final Expression rootExpr = xqueryContext.getRootExpression();
            for(int i = 0; i < rootExpr.getSubExpressionCount(); i++) {
                final Expression subExpr = rootExpr.getSubExpression(i);
                if(subExpr instanceof VariableDeclaration) {
                    subExpr.eval(null);
                }
            }
            //END workaround
           
            //setup monitoring
            processMonitor = broker.getBrokerPool().getProcessMonitor();
            xqueryContext.getProfiler().traceQueryStart();
            processMonitor.queryStarted(xqueryContext.getWatchDog());
           
            //create a function call
            final FunctionReference fnRef = new FunctionReference(new FunctionCall(xqueryContext, fn));
           
            //convert the arguments
            final org.exist.xquery.value.Sequence[] fnArgs = convertToExistFunctionArguments(xqueryContext, fn, arguments);
           
            //execute the function call
            fnRef.analyze(new AnalyzeContextInfo());
            final org.exist.xquery.value.Sequence result = fnRef.evalFunction(null, null, fnArgs);
           
            return new SequenceAdapter(result);
        } catch(final URISyntaxException use) {
            throw new RestXqServiceException(use.getMessage(), use);
        } catch(final PermissionDeniedException pde) {
            throw new RestXqServiceException(pde.getMessage(), pde);
        } catch(final XPathException xpe) {
            throw new RestXqServiceException(xpe.getMessage(), xpe);
        } catch(final EXistException ee) {
            throw new RestXqServiceException(ee.getMessage(), ee);
        } finally {
           
            //clear down monitoring
            if(processMonitor != null) {
                xquery.getContext().getProfiler().traceQueryEnd(xquery.getContext());
                processMonitor.queryCompleted(xquery.getContext().getWatchDog());
            }
           
            //return the broker
            if(broker != null) {
                getBrokerPool().release(broker);
View Full Code Here

          XQueryContext context = xquery.newContext(AccessContext.TEST);
          //context.setModuleLoadPath();
         
            Source query = new ClassLoaderSource(source);
           
            CompiledXQuery compiledQuery = xquery.compile(context, query);
           
      for(Iterator<UserDefinedFunction> i = context.localFunctions(); i.hasNext(); ) {
        UserDefinedFunction func = i.next();
        FunctionSignature sig = func.getSignature();
       
View Full Code Here

TOP

Related Classes of org.exist.xquery.CompiledXQuery

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.