Esempio n. 1
0
  /**
   * This method retrieves an xml document from the database based on its resource identifier. The
   * document is returned as an org.w3c.dom.Document.
   *
   * @param collection The name of the collection to look for the document.
   * @param resourceId The resource identifier of the document to be retrieved.
   * @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 The xml document retrieved as an org.w3c.dom.Document.
   */
  public Document retrieveDocument(
      String collection, String resourceId, String username, String password) {
    Document doc = null;

    try {
      // initialize database driver
      Class cl = Class.forName(_driver);
      Database database = (Database) cl.newInstance();
      DatabaseManager.registerDatabase(database);

      // get the collection
      Collection col = DatabaseManager.getCollection(_URI + collection, username, password);
      col.setProperty(OutputKeys.INDENT, "no");
      XMLResource res = (XMLResource) col.getResource(resourceId);
      if (res == null) System.out.println("document not found!");
      else {
        doc = (Document) (res.getContentAsDOM()).getOwnerDocument();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return doc;
  }
Esempio n. 2
0
  /* (non-Javadoc)
   * @see org.apache.tools.ant.Task#execute()
   */
  public void execute() throws BuildException {
    if (uri == null) throw new BuildException("You have to specify an XMLDB collection URI");
    if (resource == null && collection == null)
      throw new BuildException(
          "Missing parameter: either resource or collection should be specified");

    registerDatabase();
    try {
      log("Get base collection: " + uri, Project.MSG_DEBUG);
      Collection base = DatabaseManager.getCollection(uri, user, password);

      if (base == null) {
        throw new BuildException("Collection " + uri + " could not be found.");
      }

      log(
          "Create collection management service for collection " + base.getName(),
          Project.MSG_DEBUG);
      CollectionManagementServiceImpl service =
          (CollectionManagementServiceImpl) base.getService("CollectionManagementService", "1.0");
      if (resource != null) {
        log("Copying resource: " + resource, Project.MSG_INFO);
        Resource res = base.getResource(resource);
        if (res == null) throw new BuildException("Resource " + resource + " not found.");
        service.copyResource(resource, destination, name);
      } else {
        log("Copying collection: " + collection, Project.MSG_INFO);
        service.copy(collection, destination, name);
      }
    } catch (XMLDBException e) {
      throw new BuildException("XMLDB exception during remove: " + e.getMessage(), e);
    }
  }
  public final void testDropResourceRoot() throws Exception {

    Collection author = this.dbColl.getChildCollection("vcl-data/author");
    Collection vcoll = this.dbColl.getChildCollection("vcl-data/myvc");

    int size = vcoll.getResourceCount();

    Resource res = author.getResource("author.4.manuel.xml");

    author.removeResource(res);

    assertEquals(size - 1, vcoll.getResourceCount());
  }
  public final void testDropResourceSubset() throws Exception {

    Collection article = this.dbColl.getChildCollection("vcl-data/article");

    int size = article.getResourceCount();

    Resource r = article.getResource("article.from.manuel.xml");

    // Just do it if the resource exists.
    if (r != null) {
      article.removeResource(r);
      assertEquals(size - 1, article.getResourceCount());
    }
  }
  public final void testDropVirtualResourceFail() throws Exception {

    Collection vcoll = this.dbColl.getChildCollection("vcl-data/myvc");

    String[] names = vcoll.listResources();
    for (int i = 0; i < names.length; i++) {
      Resource res = vcoll.getResource(names[i]);

      assertTrue(
          "Expected instance of VirtualResource and not " + res.getClass(),
          res instanceof VirtualResource);

      try {
        vcoll.removeResource(res);
        assertTrue("Cannot delete a VirtualResource.", false);
      } catch (XMLDBException e) {
        assertTrue(
            "Expected errorCode.INVALID_COLLECTION", e.errorCode == ErrorCodes.INVALID_COLLECTION);
      }
    }
  }
Esempio n. 6
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());
  }
Esempio n. 7
0
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {

      // Identify Sources
      String path = getServletContext().getRealPath("/WEB-INF/");
      String XMLFileName = path + "/Home.xml";
      String XSLFileName = path + "/Home.xsl";

      StreamSource XMLSource = new StreamSource(XMLFileName);
      StreamSource XSLSource = new StreamSource(XSLFileName);

      String URI = "xmldb:exist://localhost:8444/exist/xmlrpc";
      String driver = "org.exist.xmldb.DatabaseImpl";

      XMLResource res = null;
      Node resNode = null;
      Document doc = null;

      try {
        Class cl = Class.forName(driver);
        Database database = (Database) cl.newInstance();
        DatabaseManager.registerDatabase(database);

        // get the collection
        Collection col =
            DatabaseManager.getCollection(URI + "/db/AnswerQuest", "admin", "password");

        col.setProperty(OutputKeys.INDENT, "no");
        res = (XMLResource) col.getResource("Home.xml");

        resNode = res.getContentAsDOM();

        doc = (Document) resNode;

      } catch (Exception e) {
        System.err.println("Error Document: " + e.getMessage());
      }

      DOMSource origDocSource = new DOMSource(doc);

      // Identify Result
      StreamResult homeOutput = new StreamResult(out);

      // Create TransformerFactory
      TransformerFactory xFactory = TransformerFactory.newInstance();

      // Create Transformer
      Transformer optimusPrime = xFactory.newTransformer(XSLSource);

      // Apply transform
      optimusPrime.transform(XMLSource, homeOutput);

    } catch (TransformerConfigurationException ex) {

      System.out.println("Encountered TransformerConfiguration Error: " + ex.getMessage());

    } catch (TransformerException ex) {

      System.out.println("Encountered Transformer Error: " + ex.getMessage());

    } finally {
      out.close();
    }
  }
Esempio n. 8
0
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String URI = "xmldb:exist://localhost:8444/exist/xmlrpc";
    String driver = "org.exist.xmldb.DatabaseImpl";

    XMLResource res = null;
    Node resNode = null;
    Document doc = null;

    String path = getServletContext().getRealPath("/");
    String XSLFileName = path + "/Slideshow.xsl";
    File XslFile = new File(XSLFileName);

    String name;
    String rating;

    try {
      name = request.getParameter("name");
      rating = request.getParameter("rating");

      if (name == null) {
        name = "";
      }
      if (rating == null) {
        rating = "";
      }

    } catch (Exception e) {
      name = "";
      rating = "";
    }

    try {
      Class cl = Class.forName(driver);
      Database database = (Database) cl.newInstance();
      DatabaseManager.registerDatabase(database);

      // get the collection
      Collection col = DatabaseManager.getCollection(URI + "/db/Project", "admin", "password");

      XQueryService service = (XQueryService) col.getService("XQueryService", "1.0");
      XQueryService another = (XQueryService) col.getService("XQueryService", "1.0");
      service.setProperty("indent", "yes");
      another.setProperty("indent", "yes");
      String queryString = "";
      if (!(rating.equals(""))) {
        service.declareVariable("rating", "");
        queryString =
            "for $rating in //app//name[text()='"
                + name
                + "']/../rating "
                + "return update replace $rating with <rating>"
                + rating
                + "</rating>";
        service.query(queryString);
      }

      col.setProperty(OutputKeys.INDENT, "no");

      res = (XMLResource) col.getResource("Review.xml");

      resNode = res.getContentAsDOM();

      doc = (Document) resNode;

    } catch (Exception e) {
      System.err.println("Error Document: " + e.getMessage());
    }
    DOMSource origDocSource = new DOMSource(doc);

    try {
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      StreamSource stylesheet = new StreamSource(XslFile);

      Transformer transformer = transformerFactory.newTransformer(stylesheet);

      NodeList appNodes = doc.getElementsByTagName("name");
      int numEvent = appNodes.getLength();
      String prev;
      String next;

      for (int i = 0; i < numEvent; i++) {

        Node eventNode = appNodes.item(i);

        NodeList eventNodeListChildren = eventNode.getChildNodes();
        Node eventTextNode = eventNodeListChildren.item(0);
        String appname = eventTextNode.getNodeValue();
        if (name.equals(appname)) {

          if (i != 0) {
            prev = appNodes.item(i - 1).getChildNodes().item(0).getNodeValue();
          } else {
            prev = appNodes.item(numEvent - 1).getChildNodes().item(0).getNodeValue();
          }

          if (i != (numEvent - 1)) {
            next = appNodes.item(i + 1).getChildNodes().item(0).getNodeValue();
          } else {
            next = appNodes.item(0).getChildNodes().item(0).getNodeValue();
          }

          transformer.setParameter("app_name", appname);
          transformer.setParameter("prev_name", prev);
          transformer.setParameter("next_name", next);

          transformer.transform(origDocSource, new StreamResult(out));
        }
      }
    } catch (Exception e) {
      out.println("Exception transformation :" + e.getMessage());
      e.printStackTrace(out);
    } finally {
      out.close();
    }
  }