Пример #1
0
 /**
  * @param xquery
  * @param mess
  * @return TODO
  */
 private ResourceSet querySingleLine(String xquery, String mess) throws XMLDBException {
   // query a single line:
   XPathQueryService service = (XPathQueryService) root.getService("XPathQueryService", "1.0");
   ResourceSet result = null;
   if (xquery != "") {
     // xquery = "/*/*[2]";
     System.out.println("Querying \"" + xquery + "\" ...");
     long t0 = System.currentTimeMillis();
     result = service.queryResource("big.xml", xquery);
     // assertEquals(1, result.getSize());
     long t1 = System.currentTimeMillis();
     System.out.println(
         "Time for query \"" + xquery + "\" on " + mess + ": " + (t1 - t0) + " ms.");
   }
   return result;
 }
Пример #2
0
  private void fetchDb() throws Exception {
    XPathQueryService xquery = (XPathQueryService) testCol.getService("XPathQueryService", "1.0");
    ResourceSet result =
        xquery.query(
            "for $n in collection('"
                + XmldbURI.ROOT_COLLECTION
                + "/test')//* return local-name($n)");

    for (int i = 0; i < result.getSize(); i++) {
      Resource r = result.getResource(i);
      String tag = r.getContent().toString();
      System.out.println("Retrieving " + tag);

      ResourceSet result2 = xquery.query("//" + tag);
      assertEquals(result2.getSize(), 1);

      System.out.println(result2.getResource(0).getContent());
    }
  }
Пример #3
0
  /**
   * Main method of the example class.
   *
   * @param args (ignored) command-line arguments
   * @throws Exception exception
   */
  public static void main(final String[] args) throws Exception {

    System.out.println("=== XMLDBQuery ===\n");

    System.out.println("* Run query via XML:DB:");

    // Collection instance
    Collection coll = null;

    try {
      // Register the database
      Class<?> c = Class.forName(DRIVER);
      Database db = (Database) c.newInstance();
      DatabaseManager.registerDatabase(db);

      // Receive the database
      coll = DatabaseManager.getCollection(DBNAME);

      // Receive the XPath query service
      XPathQueryService service = (XPathQueryService) coll.getService("XPathQueryService", "1.0");

      // Execute the query and receives all results
      ResourceSet set = service.query(QUERY);

      // Create a result iterator
      ResourceIterator iter = set.getIterator();

      // Loop through all result items
      while (iter.hasMoreResources()) {
        // Receive the next results
        Resource res = iter.nextResource();

        // Write the result to the console
        System.out.println(res.getContent());
      }
    } catch (final XMLDBException ex) {
      // Handle exceptions
      System.err.println("XML:DB Exception occured " + ex.errorCode);
    } finally {
      // Close the collection
      if (coll != null) coll.close();
    }
  }
Пример #4
0
  private void insertTags() throws Exception {
    XUpdateQueryService service =
        (XUpdateQueryService) testCol.getService("XUpdateQueryService", "1.0");
    XPathQueryService xquery = (XPathQueryService) testCol.getService("XPathQueryService", "1.0");

    String[] tagsWritten = new String[RUNS];
    for (int i = 0; i < RUNS; i++) {
      String tag = tags[i];
      String parent;
      if (i > 0 && rand.nextInt(100) < 70) {
        parent = "//" + tagsWritten[rand.nextInt(i) / 2];
      } else parent = "/root";
      String xupdate =
          "<xupdate:modifications version=\"1.0\" xmlns:xupdate=\"http://www.xmldb.org/xupdate\">"
              + "<xupdate:append select=\""
              + parent
              + "\">"
              + "<xupdate:element name=\""
              + tag
              + "\"/>"
              + "</xupdate:append>"
              + "</xupdate:modifications>";

      long mods = service.updateResource("test.xml", xupdate);
      System.out.println("Inserted " + tag + ": " + mods + " ; parent = " + parent);
      assertEquals(mods, 1);

      tagsWritten[i] = tag;
      String query = "//" + tagsWritten[rand.nextInt(i + 1)];
      ResourceSet result = xquery.query(query);
      assertEquals(result.getSize(), 1);

      System.out.println(result.getResource(0).getContent());
    }

    XMLResource res = (XMLResource) testCol.getResource("test.xml");
    assertNotNull(res);
    System.out.println(res.getContent());
  }
Пример #5
0
  /**
   * This method performs an XPath query to the database and returns the results as a Vector of
   * Strings.
   *
   * @param collection The name of the collection to look for the document.
   * @param query A string containing an XPath expression which shall act as a query against the
   *     database.
   * @param username The identifier of the user calling the method used for authentication.
   * @param password The password of the user calling the method used for authentication.
   * @return A Vector containing the answers to the query as Strings.
   */
  public Vector query(String collection, String query, String username, String password) {
    Vector response = new Vector();
    try {
      Class cl = Class.forName(_driver);
      Database database = (Database) cl.newInstance();
      DatabaseManager.registerDatabase(database);

      Collection col = DatabaseManager.getCollection(_URI + collection, username, password);
      XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
      service.setProperty("indent", "yes");

      ResourceSet result = service.query(query);
      ResourceIterator i = result.getIterator();
      while (i.hasMoreResources()) {
        Resource r = i.nextResource();
        response.add((String) r.getContent());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return response;
  }
  public static void main(String args[]) throws Exception {
    String driver = "org.exist.xmldb.DatabaseImpl";
    Class cl = Class.forName(driver);
    Database database = (Database) cl.newInstance();
    DatabaseManager.registerDatabase(database);

    Collection col = DatabaseManager.getCollection("xmldb:exist:///db", "admin", "");
    XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
    service.setProperty("indent", "yes");

    ResourceSet result =
        service.query(
            "for $s in //intervention/(speaker|writer)/affiliation[@EPparty ='PSE'] return data($s/../../(speech|writing)/@ref)");
    ResourceIterator i = result.getIterator();
    while (i.hasMoreResources()) {
      Resource r = i.nextResource();
      System.out.println((String) r.getContent());
    }
    // shut down the database
    DatabaseInstanceManager manager =
        (DatabaseInstanceManager) col.getService("DatabaseInstanceManager", "1.0");
    manager.shutdown();
  }
  public final void testXPathQueryServiceSuccess() throws Exception {

    XPathQueryService s = (XPathQueryService) this.getService("XPathQueryService");

    ResourceSet rs1 = s.query("//author/@id");
    assertTrue("Expected a non empty ResourceSet.", rs1.getSize() > 0);

    ResourceSet rs2 = s.query("//foo/@bar");
    assertTrue("Expected a non empty ResourceSet.", rs2.getSize() > 0);

    int id1[] = new int[(int) rs1.getSize()];
    for (int i = 0; i < id1.length; i++) {
      Resource res = rs1.getResource(i);
      assertTrue(
          "Expected instance of VirtualResource and not " + res.getClass(),
          res instanceof VirtualResource);

      Document doc = (Document) ((VirtualResource) res).getContentAsDOM();
      Element elem = doc.getDocumentElement();

      assertEquals(
          "Expected " + NEXDEngineI.QNAME_QUERY_RESULT + " but it is " + elem.getTagName(),
          NEXDEngineI.QNAME_QUERY_RESULT,
          elem.getTagName());

      NamedNodeMap nnm = elem.getAttributes();

      String value = elem.getAttribute("id");
      id1[i] = Integer.parseInt(value);
    }

    int id2[] = new int[(int) rs2.getSize()];
    for (int i = 0; i < id2.length; i++) {
      Resource res = rs2.getResource(i);
      assertTrue(
          "Expected instance of VirtualResource and not " + res.getClass(),
          res instanceof VirtualResource);

      Document doc = (Document) ((VirtualResource) res).getContentAsDOM();
      Element elem = doc.getDocumentElement();

      assertEquals(
          "Expected " + NEXDEngineI.QNAME_QUERY_RESULT + " but it is " + elem.getTagName(),
          NEXDEngineI.QNAME_QUERY_RESULT,
          elem.getTagName());

      String value = elem.getAttribute("bar");
      id2[i] = Integer.parseInt(value);
    }

    for (int i = 0; i < id1.length; i++) {
      int id = id1[i];

      boolean in1 = false;

      for (int j = 0; j < id2.length; j++) {

        if (id == id2[j]) {
          in1 = true;
        }

        boolean in2 = false;
        for (int k = 0; k < id1.length; k++) {
          if (id2[j] == id1[k]) {
            in2 = true;
          }
        }
        assertTrue("Expected id=" + id2[j] + " is in the first array.", in2);
      }
      assertTrue("Expected id=" + id + " is in the second array.", in1);
    }
  }