Exemplo n.º 1
0
  private static void writeHtmlOutput() throws Exception {
    boolean odd = false;

    // Opens the output file for writing.
    try (PrintWriter out = new PrintWriter(new File(OUTPUT_FILE_PATH))) {
      for (Alumnus alumnus : filteredAlumni) {
        // Writes a line to the output file.
        alumnus.printTableLine(out, odd, homepageMap);
        odd = !odd;
      }
    }
  }
Exemplo n.º 2
0
  private static void parseDetails() throws Exception {
    // Processes all alumni.
    for (Alumnus alumnus : alumni) {
      // Opens the detail page of the alumnus.
      System.out.printf("Checking details for %s... ", alumnus);
      Document doc = Jsoup.connect(alumnus.getUrl()).get();

      // Obtains the tables in the document and looks for the products table.
      Elements tables = doc.select(SELECTOR_TABLE);
      Element table = null;
      for (Element aTable : tables) {
        Elements headers = aTable.select(SELECTOR_HEADER);
        if (HEADER_DETAIL_DEFENSEDATE_TEXT.equals(headers.get(HEADER_DETAIL_DEFENSEDATE).text())) {
          table = aTable;
          break;
        }
      }

      // Checks if the table was found.
      if (table == null) {
        System.out.println("product table not found!");
      } else {
        // Extracts the rows from the table. Goes through all of them looking for the alumnus'
        // defended work.
        String workUri = null;
        Elements rows = table.select(SELECTOR_ROW);
        for (Element row : rows) {
          // Extracts the columns from the row.
          Elements columns = row.select(SELECTOR_COLUMN);

          // Read the columns with useful information.
          if (!columns.isEmpty()) {
            Element titleCell = columns.get(COLUMN_DETAIL_TITLE);
            String type = columns.get(COLUMN_DETAIL_TYPE).text();

            if (COLUMN_DETAIL_TYPE_MASTERS.equals(type)) {
              workUri = baseUrl + titleCell.select(SELECTOR_DETAIL_LINK).attr(ATTRIBUTE_LINK);
              alumnus.setWorkTitle(titleCell.text());
            }
          }
        }

        // Checks if the alumnus' defended work was found.
        if (workUri == null) {
          System.out.println("defended work not found!");
        } else {
          // Opens the detail page of the alumnus' defended work.
          doc = Jsoup.connect(workUri).get();

          // Looks for the first table in the document, where the names of the supervisors are.
          table = doc.select(SELECTOR_TABLE).first();

          // Extracts the rows from the table. Goes through all of them looking for the main
          // supervisor.
          String supervisor = null;
          if (table != null) {
            rows = table.select(SELECTOR_ROW);
            for (Element aRow : rows) {
              // Extracts the columns from the row.
              Elements columns = aRow.select(SELECTOR_COLUMN);

              // Checks if it's the main supervisor.
              if (!columns.isEmpty()) {
                if (COLUMN_WORK_DETAIL_SUPERVISOR_ROLE_MAIN.equals(
                    columns.get(COLUMN_WORK_DETAIL_SUPERVISOR_ROLE).text())) {
                  supervisor = columns.get(COLUMN_WORK_DETAIL_SUPERVISOR_NAME).text();
                }
              }
            }
          }

          // Checks if the supervisor was found.
          if (supervisor == null) {
            System.out.println("supervisor name not found!");
          }

          // Checks if the alumnus passes the supervisor filter.
          else if (supervisorFilter.contains(supervisor)) {
            alumnus.setSupervisor(supervisor);
            filteredAlumni.add(alumnus);
            System.out.printf("OK! (%s)%n", supervisor);
          }

          // Otherwise...
          else {
            System.out.println("filtered out by supervisor.");
          }
        }
      }
    }
  }