Package org.exist.xquery

Examples of org.exist.xquery.XQuery


            root.store(transaction, broker, info, new InputSource(f.toURI().toASCIIString()), false);

            transact.commit(transaction);

            transaction = transact.beginTransaction();
            XQuery xquery = broker.getXQueryService();
            xquery.execute(query, null, AccessContext.TEST);
            transact.commit(transaction);
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
View Full Code Here


    public void virtualNodeSet_11() throws XPathException, SAXException, PermissionDeniedException {
        executeQuery(broker, "//SPEECH[fn:contains(LINE, 'perturbed spirit')]/following-sibling::*", 1, null);
    }
   
    private static Sequence executeQuery(DBBroker broker, String query, int expected, String expectedResult) throws XPathException, SAXException, PermissionDeniedException {
        XQuery xquery = broker.getXQueryService();
        Sequence seq = xquery.execute(query, null, AccessContext.TEST);
        assertEquals(expected, seq.getItemCount());
       
        if (expectedResult != null) {
            Item item = seq.itemAt(0);
            String value = serialize(broker, item);
View Full Code Here

    public void testNodeStorage() throws Exception {
        BrokerPool pool = BrokerPool.getInstance();
        DBBroker broker = null;
        try {
            broker = pool.get(pool.getSecurityManager().getSystemSubject());
            XQuery xquery = broker.getXQueryService();
            assertNotNull(xquery);
            // test element ids
            Sequence seq = xquery.execute("doc('/db/test/test_string.xml')/test/para",
                    null, AccessContext.TEST);
            assertEquals(3, seq.getItemCount());
            NodeProxy comment = (NodeProxy) seq.itemAt(0);
            assertEquals(comment.getNodeId().toString(), "1.1");
            comment = (NodeProxy) seq.itemAt(1);
            assertEquals(comment.getNodeId().toString(), "1.3");
            comment = (NodeProxy) seq.itemAt(2);
            assertEquals(comment.getNodeId().toString(), "1.5");

            seq = xquery.execute("doc('/db/test/test_string.xml')/test//a",
                    null, AccessContext.TEST);
            assertEquals(1, seq.getItemCount());
            NodeProxy a = (NodeProxy) seq.itemAt(0);
            assertEquals("1.3.2", a.getNodeId().toString());

            // test attribute id
            seq = xquery.execute("doc('/db/test/test_string.xml')/test//a/@href",
                    null, AccessContext.TEST);
            assertEquals(1, seq.getItemCount());
            NodeProxy href = (NodeProxy) seq.itemAt(0);
            System.out.println(StorageAddress.toString(href.getInternalAddress()));
            assertEquals("1.3.2.1", href.getNodeId().toString());
            // test Attr deserialization
            Attr attr = (Attr) href.getNode();
            System.out.println(StorageAddress.toString(((StoredNode)attr).getInternalAddress()));
            // test Attr fields
            assertEquals(attr.getNodeName(), "href");
            assertEquals(attr.getName(), "href");
            assertEquals(attr.getValue(), "#");
             // test DOMFile.getNodeValue()
            assertEquals(href.getStringValue(), "#");

            // test text node
            seq = xquery.execute("doc('/db/test/test_string.xml')/test//b/text()",
                    null, AccessContext.TEST);
            assertEquals(1, seq.getItemCount());
            NodeProxy text = (NodeProxy) seq.itemAt(0);
            assertEquals("1.5.2.1", text.getNodeId().toString());
            // test DOMFile.getNodeValue()
View Full Code Here

     * @throws PermissionDeniedException
     */
    private CompiledXQuery compileXQuery(DBBroker broker, Source xqSource, XmldbURI[] staticallyKnownDocuments, XmldbURI xqwsCollectionUri, HttpServletRequest request, HttpServletResponse response) throws XPathException, PermissionDeniedException
    {
      //Get the xquery service
        final XQuery xquery = broker.getXQueryService();
    final XQueryPool pool = xquery.getXQueryPool();
        XQueryContext context;
       
        //try and get pre-compiled XQuery from the cache
        CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, xqSource);
       
        //Create the context and set a header to indicate cache status
        if(compiled == null)
        {
          context = xquery.newContext(AccessContext.REST);
          //response.setHeader("X-XQuery-Cached", "false");
      }
        else
      {
          context = compiled.getContext();
          //response.setHeader("X-XQuery-Cached", "true");
        }
       
        //Setup the context
        declareVariables(context, request, response);
        context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(xqwsCollectionUri).toString());
        context.setStaticallyKnownDocuments(staticallyKnownDocuments);
       
        //no pre-compiled XQuery, so compile, it
        if(compiled == null)
        {
            try
            {
                compiled = xquery.compile(context, xqSource);
            }
            catch (final IOException e)
            {
                LOG.debug(e.getMessage());
                throw new XPathException("Failed to compile query: " + xqSource.toString() , e);
View Full Code Here

     
      //Create an XQuery to call the XQWS function
      final CompiledXQuery xqCallXQWS = XQueryExecuteXQWSFunction(broker, nSOAPFunction, description, request, response);
     
      //xqCallXQWS
      final XQuery xqueryService = broker.getXQueryService();
      final Sequence xqwsResult = xqueryService.execute(xqCallXQWS, null);
     
      // 6) Create a SOAP Response describing the Result
      String funcName = nSOAPFunction.getLocalName();
      if(funcName == null)
      {
View Full Code Here

      throws XMLDBException {
      final Subject preserveSubject = pool.getSubject();
    DBBroker broker = null;
    try {
      broker = pool.get(user);
      final XQuery xquery = broker.getXQueryService();
      final Sequence nodes = xquery.execute(xpath, null, parent.getAccessContext());
      return broker.getTextEngine().scanIndexTerms(nodes.getDocumentSet(),
          nodes.toNodeSet(),  start, end);
    } catch (final EXistException e) {
      throw new XMLDBException(
          ErrorCodes.VENDOR_ERROR,
View Full Code Here

        // fixme! - hook for debugger here /ljo

        final Sequence sequence = null;

        final XQuery xqueryService = evalContext.getBroker().getXQueryService();
        final XQueryContext innerContext;
        if (contextInit != null) {
            // eval-with-context: initialize a new context
            innerContext = xqueryService.newContext(evalContext.getAccessContext());
            initContextSequence = initContext(contextInit.getNode(), innerContext);
        } else {
            // use the existing outer context
            // TODO: check if copying the static context would be sufficient???
            innerContext = evalContext.copyContext();
View Full Code Here

        }

        final XmldbURI pathUri = XmldbURI.create(path);
        try {
            final Source source = new StringSource(query);
            final XQuery xquery = broker.getXQueryService();
            final XQueryPool pool = xquery.getXQueryPool();
            CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);

            XQueryContext context;
            if (compiled == null) {
                context = xquery.newContext(AccessContext.REST);
            } else {
                context = compiled.getContext();
            }

            context.setStaticallyKnownDocuments(new XmldbURI[]{pathUri});
            context.setBaseURI(new AnyURIValue(pathUri.toString()));

            declareNamespaces(context, namespaces);
            declareVariables(context, variables, request, response);

            if (compiled == null) {
                compiled = xquery.compile(context, source);
            } else {
                compiled.getContext().updateContext(context);
                context.getWatchDog().reset();
            }

            try {
                final long startTime = System.currentTimeMillis();
                final Sequence resultSequence = xquery.execute(compiled, null, outputProperties);
                final long queryTime = System.currentTimeMillis() - startTime;

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found " + resultSequence.getItemCount() + " in " + queryTime + "ms.");
                }
View Full Code Here

            final HttpServletRequest request, final HttpServletResponse response,
            final Properties outputProperties, final String servletPath, final String pathInfo)
            throws XPathException, BadRequestException, PermissionDeniedException {

        final Source source = new DBSource(broker, (BinaryDocument) resource, true);
        final XQuery xquery = broker.getXQueryService();
        final XQueryPool pool = xquery.getXQueryPool();
        XQueryContext context;

        CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            // special header to indicate that the query is not returned from
            // cache
            response.setHeader("X-XQuery-Cached", "false");
            context = xquery.newContext(AccessContext.REST);

        } else {
            response.setHeader("X-XQuery-Cached", "true");
            context = compiled.getContext();
        }

        // TODO: don't hardcode this?
        context.setModuleLoadPath(
                XmldbURI.EMBEDDED_SERVER_URI.append(
                resource.getCollection().getURI()).toString());

        context.setStaticallyKnownDocuments(
                new XmldbURI[]{resource.getCollection().getURI()});

        final HttpRequestWrapper reqw = declareVariables(context, null, request, response);
        reqw.setServletPath(servletPath);
        reqw.setPathInfo(pathInfo);

        if (compiled == null) {
            try {
                compiled = xquery.compile(context, source);
            } catch (final IOException e) {
                throw new BadRequestException("Failed to read query from " + resource.getURI(), e);
            }
        }

        DebuggeeFactory.checkForDebugRequest(request, context);

        boolean wrap = outputProperties.getProperty("_wrap") != null
                && "yes".equals(outputProperties.getProperty("_wrap"));

        try {
            final Sequence result = xquery.execute(compiled, null, outputProperties);
            writeResults(response, broker, result, -1, 1, false, outputProperties, wrap);

        } finally {
            context.runCleanupTasks();
            pool.returnCompiledXQuery(source, compiled);
View Full Code Here

            final HttpServletRequest request, final HttpServletResponse response,
            final Properties outputProperties, final String servletPath, final String pathInfo)
            throws XPathException, BadRequestException, PermissionDeniedException {

        final URLSource source = new URLSource(this.getClass().getResource("run-xproc.xq"));
        final XQuery xquery = broker.getXQueryService();
        final XQueryPool pool = xquery.getXQueryPool();
        XQueryContext context;
        CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            context = xquery.newContext(AccessContext.REST);
        } else {
            context = compiled.getContext();
        }

        context.declareVariable("pipeline", resource.getURI().toString());
       
        final String stdin = request.getParameter("stdin");
        context.declareVariable("stdin", stdin == null ? "" : stdin);

        final String debug = request.getParameter("debug");
        context.declareVariable("debug", debug == null ? "0" : "1");

        final String bindings = request.getParameter("bindings");
        context.declareVariable("bindings", bindings == null ? "<bindings/>" : bindings);

        final String autobind = request.getParameter("autobind");
        context.declareVariable("autobind", autobind == null ? "0" : "1");

        final String options = request.getParameter("options");
        context.declareVariable("options", options == null ? "<options/>" : options);

        // TODO: don't hardcode this?
        context.setModuleLoadPath(
                XmldbURI.EMBEDDED_SERVER_URI.append(
                resource.getCollection().getURI()).toString());

        context.setStaticallyKnownDocuments(
                new XmldbURI[]{resource.getCollection().getURI()});

        final HttpRequestWrapper reqw = declareVariables(context, null, request, response);
        reqw.setServletPath(servletPath);
        reqw.setPathInfo(pathInfo);
        if (compiled == null) {
            try {
                compiled = xquery.compile(context, source);
            } catch (final IOException e) {
                throw new BadRequestException("Failed to read query from "
                        + source.getURL(), e);
            }
        }

        try {
            final Sequence result = xquery.execute(compiled, null, outputProperties);
            writeResults(response, broker, result, -1, 1, false, outputProperties, false);
        } finally {
            pool.returnCompiledXQuery(source, compiled);
        }
    }
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.