Package org.exist.xquery

Examples of org.exist.xquery.XQuery


        this.config = config;
        final Boolean temp = (Boolean) config.getProperty(NativeValueIndex.PROPERTY_INDEX_CASE_SENSITIVE);
        if (temp != null)
            {caseSensitive = temp.booleanValue();}
        this.pool = pool;
        xqueryService = new XQuery(this);
        initIndexModules();
    }
View Full Code Here


        throw new XPathException(this, "Only DBA can call clear-xquery-cache function.");
      }
     
      final DBBroker broker = context.getBroker();
     
      final XQuery xquery = broker.getXQueryService();
    final XQueryPool pool = xquery.getXQueryPool();
   
    pool.clear();
   
        return Sequence.EMPTY_SEQUENCE;
    }
View Full Code Here

  }

  public void getEntryById(DBBroker broker, String path, String id,
      OutgoingMessage response) throws EXistException,
      BadRequestException, PermissionDeniedException {
    final XQuery xquery = broker.getXQueryService();
    CompiledXQuery feedQuery = xquery.getXQueryPool().borrowCompiledXQuery(
        broker, entryByIdSource);

    XQueryContext context;
    if (feedQuery == null) {
      context = xquery.newContext(AccessContext.REST);
      try {
        feedQuery = xquery.compile(context, entryByIdSource);
      } catch (final XPathException ex) {
        throw new EXistException("Cannot compile xquery "
            + entryByIdSource.getURL(), ex);
      } catch (final IOException ex) {
        throw new EXistException(
            "I/O exception while compiling xquery "
                + entryByIdSource.getURL(), ex);
      }
    } else {
      context = feedQuery.getContext();
    }
    context.setStaticallyKnownDocuments(new XmldbURI[] { XmldbURI.create(
        path).append(AtomProtocol.FEED_DOCUMENT_NAME) });

    try {
      context.declareVariable("id", id);
      final Sequence resultSequence = xquery.execute(feedQuery, null);
      if (resultSequence.isEmpty()) {
        throw new BadRequestException("No topic was found.");
      }

      final String charset = getContext().getDefaultCharset();
      response.setContentType("application/atom+xml; charset=" + charset);
      final Serializer serializer = broker.getSerializer();
      serializer.reset();
      try {
        final Writer w = new OutputStreamWriter(response.getOutputStream(), charset);
        final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final Properties outputProperties = new Properties();
        sax.setOutput(w, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);

        serializer.toSAX(resultSequence, 1, 1, false, false);

        SerializerPool.getInstance().returnObject(sax);
        w.flush();
        w.close();
      } catch (final IOException ex) {
        LOG.fatal("Cannot read resource " + path, ex);
        throw new EXistException("I/O error on read of resource "
            + path, ex);
      } catch (final SAXException saxe) {
        LOG.warn(saxe);
        throw new BadRequestException("Error while serializing XML: "
            + saxe.getMessage());
      }
      resultSequence.itemAt(0);

    } catch (final XPathException ex) {
      throw new EXistException("Cannot execute xquery "
          + entryByIdSource.getURL(), ex);

    } finally {
      xquery.getXQueryPool().returnCompiledXQuery(entryByIdSource, feedQuery);
    }

  }
View Full Code Here

  public void getFeed(DBBroker broker, String path, OutgoingMessage response)
      throws EXistException, BadRequestException,
      PermissionDeniedException {
   
    final XQuery xquery = broker.getXQueryService();
    CompiledXQuery feedQuery = xquery.getXQueryPool().borrowCompiledXQuery(
        broker, getFeedSource);

    XQueryContext context;
    if (feedQuery == null) {
      context = xquery.newContext(AccessContext.REST);
      try {
        feedQuery = xquery.compile(context, getFeedSource);
      } catch (final XPathException ex) {
        throw new EXistException("Cannot compile xquery "
            + getFeedSource.getURL(), ex);
      } catch (final IOException ex) {
        throw new EXistException(
            "I/O exception while compiling xquery "
                + getFeedSource.getURL(), ex);
      }
    } else {
      context = feedQuery.getContext();
    }
    context.setStaticallyKnownDocuments(
      new XmldbURI[] {
          XmldbURI.create(path).append(AtomProtocol.FEED_DOCUMENT_NAME)
      }
    );

    try {
      final Sequence resultSequence = xquery.execute(feedQuery, null);
      if (resultSequence.isEmpty()) {
        throw new BadRequestException("No feed was found.");
      }

      final String charset = getContext().getDefaultCharset();
      response.setContentType("application/atom+xml; charset=" + charset);
      final Serializer serializer = broker.getSerializer();
      serializer.reset();

      try {
        final Writer w = new OutputStreamWriter(response.getOutputStream(), charset);
        final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final Properties outputProperties = new Properties();
        sax.setOutput(w, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);

        serializer.toSAX(resultSequence, 1, 1, false, false);

        SerializerPool.getInstance().returnObject(sax);
        w.flush();
        w.close();

      } catch (final IOException ex) {
        LOG.fatal("Cannot read resource " + path, ex);
        throw new EXistException("I/O error on read of resource " + path, ex);
      } catch (final SAXException saxe) {
        LOG.warn(saxe);
        throw new BadRequestException("Error while serializing XML: " + saxe.getMessage());
      }
      resultSequence.itemAt(0);

    } catch (final XPathException ex) {
      throw new EXistException("Cannot execute xquery " + getFeedSource.getURL(), ex);

    } finally {
      xquery.getXQueryPool().returnCompiledXQuery(getFeedSource, feedQuery);
    }
  }
View Full Code Here

            if (source == null) {
                LOG.info(String.format("No Xquery found at '%s'", path));

            } else {
                // Setup xquery service
                XQuery service = broker.getXQueryService();
                context = service.newContext(AccessContext.TRIGGER);

                // Allow use of modules with relative paths
                String moduleLoadPath = StringUtils.substringBeforeLast(path, "/");
                context.setModuleLoadPath(moduleLoadPath);

                // Compile query
                CompiledXQuery compiledQuery = service.compile(context, source);

                LOG.info(String.format("Starting Xquery at '%s'", path));

                // Finish preparation
                context.prepareForExecution();

                // Execute
                Sequence result = service.execute(compiledQuery, null);

                // Log results
                LOG.info(String.format("Result xquery: '%s'", result.getStringValue()));

            }
View Full Code Here

                    LOG.error("Resource [" + xqueryResourcePath + "] does not exist.");
                    return;
                }


                final XQuery xquery = broker.getXQueryService();

                if (xquery == null) {
                    LOG.error("broker unable to retrieve XQueryService");
                    return;
                }

                final XQueryPool xqpool = xquery.getXQueryPool();
                CompiledXQuery compiled = xqpool.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()));

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

                final Properties outputProperties = new Properties();
                Sequence result = null;

                try {
                    final long startTime = System.currentTimeMillis();
                    result = xquery.execute(compiled, null, outputProperties);
                    final long queryTime = System.currentTimeMillis() - startTime;
                    LOG.info("XQuery execution results: " + result.toString()  + " in " + queryTime + "ms.");
                } finally {
                    xqpool.returnCompiledXQuery(source, compiled);
                }
View Full Code Here

      XQueryContext context = compiledXQuery.getContext().copyContext();
      context.setDebuggeeJoint(null);
      context.undeclareGlobalVariable(Debuggee.SESSION);
      context.undeclareGlobalVariable(Debuggee.IDEKEY);
     
      XQuery service = broker.getXQueryService();
      CompiledXQuery compiled = service.compile(context, script);
     
      Sequence resultSequence = service.execute(compiled, null);
 
          SAXSerializer sax = null;
          Serializer serializer = broker.getSerializer();
      serializer.reset();
          try {
View Full Code Here

     
            BrokerPool.registerStatusObserver(this);
     
      broker = db.get(null);

          XQuery xquery = broker.getXQueryService();

          xquery.execute(expression, null);

//          XQueryContext context = expression.getContext();
//         
//          expression.reset();
//
View Full Code Here

          // Try to find the XQuery
          Source source = SourceFactory.getSource(broker, "", uri, true);
 
          if (source == null) return null;
 
          XQuery xquery = broker.getXQueryService();
     
          XQueryContext queryContext = xquery.newContext(AccessContext.REST);
 
      // Find correct script load path
      queryContext.setModuleLoadPath(XmldbURI.create(uri).removeLastSegment().toString());
 
      CompiledXQuery compiled;
      try {
        compiled = xquery.compile(queryContext, source);
      } catch (IOException e) {
        e.printStackTrace();
        return null;
      }
         
View Full Code Here

                    {source = new DBSource(serializer.broker, (BinaryDocument) doc, true);}
                else {
                    xpointer = checkNamespaces(xpointer);
                    source = new StringSource(xpointer);
                }
                final XQuery xquery = serializer.broker.getXQueryService();
                final XQueryPool pool = xquery.getXQueryPool();
                XQueryContext context;
                CompiledXQuery compiled = pool.borrowCompiledXQuery(serializer.broker, source);
                if (compiled != null)
                    {context = compiled.getContext();}
                else
                    {context = xquery.newContext(AccessContext.XINCLUDE);}
                context.declareNamespaces(namespaces);
                context.declareNamespace("xinclude", XINCLUDE_NS);
               
                //setup the http context if known
                if(serializer.httpContext != null)
                {
                  if(serializer.httpContext.getRequest() != null)
                    {context.declareVariable(RequestModule.PREFIX + ":request", serializer.httpContext.getRequest());}
                 
                  if(serializer.httpContext.getResponse() != null)
                    {context.declareVariable(ResponseModule.PREFIX + ":response", serializer.httpContext.getResponse());}
                 
                  if(serializer.httpContext.getSession() != null)
                    {context.declareVariable(SessionModule.PREFIX + ":session", serializer.httpContext.getSession());}
                }
               
                //TODO: change these to putting the XmldbURI in, but we need to warn users!
                if(document!=null){
                    context.declareVariable("xinclude:current-doc", document.getFileURI().toString());
                    context.declareVariable("xinclude:current-collection", document.getCollection().getURI().toString());
                }
               
                if (xpointer != null) {
                    if(doc != null)
                        {context.setStaticallyKnownDocuments(new XmldbURI[] { doc.getURI() } );}
                    else if (docUri != null)
                        {context.setStaticallyKnownDocuments(new XmldbURI[] { docUri });}
                }

                // pass parameters as variables
                if (params != null) {
                    for (final Map.Entry<String, String> entry : params.entrySet()) {
                        context.declareVariable(entry.getKey(), entry.getValue());
                    }
                }

                if(compiled == null) {
                    try {
                        compiled = xquery.compile(context, source, xpointer != null);
                    } catch (final IOException e) {
                        throw new SAXException("I/O error while reading query for xinclude: " + e.getMessage(), e);
                    }
                }
                LOG.info("xpointer query: " + ExpressionDumper.dump((Expression) compiled));
                Sequence contextSeq = null;
                if (memtreeDoc != null)
                    {contextSeq = memtreeDoc;}
                final Sequence seq = xquery.execute(compiled, contextSeq);

                if(Type.subTypeOf(seq.getItemType(), Type.NODE)) {
                    if (LOG.isDebugEnabled())
                        {LOG.debug("xpointer found: " + seq.getItemCount());}
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.