Example #1
0
  /**
   * Executes a query.
   *
   * @param query query to be executed
   * @return list of serialized result items
   * @throws IOException error during query execution
   */
  private StringList execute(final WebDAVQuery query) throws IOException {
    final ClassLoader cl = getClass().getClassLoader();
    final InputStream s = cl.getResourceAsStream(FILE);
    if (s == null) throw new IOException("WebDAV module not found");
    final byte[] module = new IOStream(s).read();

    final QueryProcessor qp = new QueryProcessor(query.toString(), http.context());
    try {
      for (final Entry<String, Object> entry : query.entries()) {
        qp.bind(entry.getKey(), entry.getValue());
      }
      qp.ctx.parseLibrary(string(module), FILE, qp.sc);

      final Result r = qp.execute();
      final int n = (int) r.size();
      final StringList items = new StringList(n);
      for (int i = 0; i < n; i++) {
        final ArrayOutput ao = new ArrayOutput();
        r.serialize(Serializer.get(ao), 0);
        items.add(ao.toString());
      }
      return items;
    } catch (final QueryException ex) {
      throw new BaseXException(ex);
    } catch (final Exception ex) {
      Util.debug(ex);
      throw new BaseXException(ex);
    } finally {
      qp.close();
    }
  }
Example #2
0
 /**
  * Checks if the constructed base-uri matches the base-uri of added documents.
  *
  * @throws Exception exception
  */
 @Test
 public void baseUri() throws Exception {
   final String find = "base-uri(collection('" + NAME + '/' + DIR + "xmark.xml'))";
   try (final QueryProcessor qp = new QueryProcessor(find, context)) {
     assertEquals(NAME + '/' + FILES[1], qp.execute().toString());
   }
 }
Example #3
0
 /**
  * Finds documents in path.
  *
  * @throws Exception exception
  */
 @Test
 public void findDocs() throws Exception {
   final String find = "collection('" + NAME + "/test/zipped') ";
   try (final QueryProcessor qp = new QueryProcessor(find, context)) {
     assertEquals(4, qp.execute().size());
   }
 }
Example #4
0
 /**
  * Checks if a query yields the specified error code.
  *
  * @param query query string
  * @param error expected error
  */
 protected static void error(final String query, final Err... error) {
   final QueryProcessor qp = new QueryProcessor(query, CONTEXT);
   try {
     final String res = qp.execute().toString().replaceAll("(\\r|\\n) *", "");
     fail("Query did not fail:\n" + query + "\n[E] " + error[0] + "...\n[F] " + res);
   } catch (final QueryException ex) {
     check(ex, error);
   } finally {
     try {
       qp.close();
     } catch (final IOException e) {
     }
   }
 }
Example #5
0
 /**
  * Runs the specified query.
  *
  * @param query query string
  * @return result
  */
 protected static String query(final String query) {
   final QueryProcessor qp = new QueryProcessor(query, CONTEXT);
   try {
     return qp.execute().toString().replaceAll("(\\r|\\n) *", "");
   } catch (final QueryException ex) {
     fail("Query failed:\n" + query + "\nMessage: " + ex.getMessage());
     return null;
   } finally {
     try {
       qp.close();
     } catch (final IOException e) {
     }
   }
 }
Example #6
0
 /**
  * Finds single doc.
  *
  * @throws Exception exception
  */
 @Test
 public void findDoc() throws Exception {
   final String find =
       "for $x in collection('"
           + NAME
           + '/'
           + DIR
           + "xmark.xml') "
           + "where $x//location contains text 'uzbekistan' "
           + "return $x";
   try (final QueryProcessor qp = new QueryProcessor(find, context)) {
     assertEquals(1, qp.execute().size());
   }
 }
Example #7
0
  /**
   * Runs the example code.
   *
   * @param args (ignored) command-line arguments
   * @throws QueryException if an error occurs while evaluating the query
   */
  public static void main(final String[] args) throws QueryException {
    // Database context.
    Context context = new Context();

    System.out.println("=== BindVariable ===");

    // Specify query to be executed
    String query =
        "declare variable $var1 as xs:string external;\n"
            + "declare variable $var2 external;\n"
            + "($var1, $var2)";

    // Show query
    System.out.println("\n* Query:");
    System.out.println(query);

    // Create a query processor
    try (QueryProcessor proc = new QueryProcessor(query, context)) {

      // Define the items to be bound
      String string = "Hello World!\n";
      String number = "123";

      // Bind the variables
      proc.bind("var1", string);
      proc.bind("var2", number, "xs:integer");

      // Execute the query
      Result result = proc.execute();

      System.out.println("\n* Result:");

      // ------------------------------------------------------------------------
      // Print result as string
      System.out.println(result);
    }

    // ------------------------------------------------------------------------
    // Close the database context
    context.close();
  }