Ejemplo n.º 1
0
  /**
   * Gets the online data to write it.
   *
   * @param doc
   * @param type
   */
  public void getDataToWrite(Document doc, String type, String name) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    String loc = null;
    try {
      // 2 Documents: doc to get the data online, and docu where we will
      // write the data.
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document docu = builder.newDocument();
      Element root = docu.createElement(name.toLowerCase());
      docu.appendChild(root);

      NodeList movieList = doc.getElementsByTagName("movie");
      Node p = movieList.item(0);
      if (p == null) {
        nodecheck = false;
      }
      // Sets the attributes of the new data from the ones gathered from
      // the document doc.
      if (p.getNodeType() == Node.ELEMENT_NODE) {
        Element movie = (Element) p;
        root.setAttribute("title", movie.getAttribute("title"));
        root.setAttribute("type", movie.getAttribute("type"));
        root.setAttribute("year", movie.getAttribute("year"));
        root.setAttribute("runtime", movie.getAttribute("runtime"));
        root.setAttribute("genre", movie.getAttribute("genre"));
        root.setAttribute("plot", movie.getAttribute("plot"));
        root.setAttribute("language", movie.getAttribute("language"));
        root.setAttribute("actors", movie.getAttribute("actors"));
        root.setAttribute("director", movie.getAttribute("director"));
      }

      DOMSource source = new DOMSource(docu);

      // The program understands where yo want to do the action.
      if (type.equals("series")) {
        loc = "resources/series.xml";
      } else if (type.equals("movies")) {
        loc = "resources/movies.xml";
      }

      Result result = new StreamResult(loc);

      TransformerFactory transf = TransformerFactory.newInstance();
      Transformer transformer = transf.newTransformer();
      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.transform(source, result);

      System.out.println("Archivo escrito.");

    } catch (Exception ex) {
      System.out.println("Please, write a correct database name.");
      // If the database name is incorrect, the method will launch again.
      getDataToWrite(api.getDocu(), api.enterType(), api.enterName());
    }
  }
Ejemplo n.º 2
0
  /** Here is the main menu, where the user will interact. */
  public void menu() {

    System.out.println("Insert the option number to execute it:");
    System.out.println("-------------------------");
    System.out.println("1 -Get data online and read it:");
    System.out.println("2 -Get online data and write it into the local database:");
    System.out.println("3 -Look for a specific record in the local database:");
    while (check) {
      getOption();
    }

    switch (str) {
      case "1":
        {
          Visual visual;

          visual = getDataToSee(api.transformXML());
          if (checkNode()) {
            System.out.println(visual.toString());
            System.out.println("-------------------------");
            System.out.println("Want to save it? Y/N :");
            str = sc.next();
            if (str.equals("Y") || str.equals("y")) {
              System.out.println("Info saved.");
              System.out.println("");
              submenu();
            } else if (str.equals("N") || str.equals("n")) {
              submenu();
            }
          } else {
            submenu();
          }
          submenu();
        }

      case "2":
        {
          getDataToSee(api.transformXML());
          if (checkNode()) {
            System.out.println(
                "Received data: Now choose in what database you want it, and the name of the register:");
            getDataToWrite(api.getDocu(), api.enterType(), api.enterName());

            submenu();
          } else {
            submenu();
          }
        }

      case "3":
        {
          Visual visual = null;
          try {

            visual = readWrittenData(api.lookForType(), api.lookForName());
            if (checkNode()) {
              System.out.println(visual.toString());
            } else {
              System.out.println("Sorry, couldn´t find the data.");
            }
          } catch (IOException e) {
            System.out.println("Error. The file that you were trying to read doesn´t exist.");
            submenu();
          }
          submenu();
        }
    }
  }