示例#1
0
  /**
   * Return the names of the contained items, if path leads to a Directory. Or, return the path
   * provided, if it leads to a File.
   *
   * @param paths a list of Strings containing the desired paths.
   * @return a String containing the relevant contents.
   */
  public String ls(List<String> paths) throws Exception {
    // If ls is called without parameters.
    if (paths.isEmpty()) {
      if (!currentOptions_.equals("R")) {
        if (currentDirectory_.getSize() == 0) {
          return "";
        } else {
          return currentDirectory_.ls().substring(2);
        }
      }
      paths.add(currentDirectory_.getPath());
      return ls(paths);

      // If ls is called with paramters.
    } else {
      String lsOutput = "";
      for (int i = 0; i < paths.size(); i++) {
        JShellItem target = getItemAtPath(paths.get(i), 0);
        if (!lsOutput.isEmpty()) {
          lsOutput += "\n";
        }
        lsOutput += paths.get(i);
        if (target == null) {
          lsOutput += ": No such file or directory";
        } else {

          if (target instanceof Directory
              && currentOptions_.equals("R")
              && ((Directory) target).getNumDirectories() > 0
              && !(target instanceof DirectoryAlias)) {
            List<String> recursivePath = recurseOnPath(paths.get(i), false);
            Collections.reverse(recursivePath);
            lsOutput += target.ls();
            lsOutput += "\n";
            lsOutput += ls(recursivePath.subList(1, recursivePath.size()));
          } else {
            // list paths separately
            lsOutput += target.ls();

            // if there are more paths, print a blank line
            if (i < paths.size() - 1) {
              lsOutput += "\n";
            }
            // notify user if there is an error in the path
            // specified
          }
        }
      }
      return lsOutput;
    }
  }