Esempio n. 1
0
 public String getPageIds(ClientRequest cr) {
   HttpSession session = cr.getServletRequest().getSession();
   PageState.AllPageInfo info = (PageState.AllPageInfo) session.getAttribute("fiz.PageState");
   if (info == null) {
     return "";
   }
   Object[] keys = info.keySet().toArray();
   Arrays.sort(keys);
   return StringUtil.join(keys, ", ");
 }
Esempio n. 2
0
 public static String buildPath(String[] parts) {
   return StringUtil.join(Arrays.asList(parts), System.getProperty("file.separator"));
 }
Esempio n. 3
0
  /**
   * The actual work-horse. Will find absolute filepaths starting from the given directory using the
   * given regular expression string for search.
   */
  private static String findFile(
      BibtexEntry entry, BibtexDatabase database, File directory, String file) {

    if (file.startsWith("/")) {
      directory = new File(".");
      file = file.substring(1);
    }

    // Escape handling...
    Matcher m = Pattern.compile("([^\\\\])\\\\([^\\\\])").matcher(file);
    StringBuffer s = new StringBuffer();
    while (m.find()) {
      m.appendReplacement(s, m.group(1) + '/' + m.group(2));
    }
    m.appendTail(s);
    file = s.toString();
    String[] fileParts = file.split("/");

    if (fileParts.length == 0) {
      return null;
    }

    if (fileParts.length > 1) {

      for (int i = 0; i < (fileParts.length - 1); i++) {

        String dirToProcess = fileParts[i];

        dirToProcess = Util.expandBrackets(dirToProcess, entry, database);

        if (dirToProcess.matches("^.:$")) { // Windows Drive Letter
          directory = new File(dirToProcess + '/');
          continue;
        }
        if (dirToProcess.equals(".")) { // Stay in current directory
          continue;
        }
        if (dirToProcess.equals("..")) {
          directory = new File(directory.getParent());
          continue;
        }
        if (dirToProcess.equals("*")) { // Do for all direct subdirs

          File[] subDirs = directory.listFiles();
          if (subDirs == null) {
            return null; // No permission?
          }

          String restOfFileString = StringUtil.join(fileParts, "/", i + 1, fileParts.length);

          for (File subDir : subDirs) {
            if (subDir.isDirectory()) {
              String result = UtilFindFiles.findFile(entry, database, subDir, restOfFileString);
              if (result != null) {
                return result;
              }
            }
          }
          return null;
        }
        // Do for all direct and indirect subdirs
        if (dirToProcess.equals("**")) {
          List<File> toDo = new LinkedList<File>();
          toDo.add(directory);

          String restOfFileString = StringUtil.join(fileParts, "/", i + 1, fileParts.length);

          // Before checking the subdirs, we first check the current
          // dir
          String result = UtilFindFiles.findFile(entry, database, directory, restOfFileString);
          if (result != null) {
            return result;
          }

          while (!toDo.isEmpty()) {

            // Get all subdirs of each of the elements found in toDo
            File[] subDirs = toDo.remove(0).listFiles();
            if (subDirs == null) {
              continue;
            }

            toDo.addAll(Arrays.asList(subDirs));

            for (File subDir : subDirs) {
              if (!subDir.isDirectory()) {
                continue;
              }
              result = UtilFindFiles.findFile(entry, database, subDir, restOfFileString);
              if (result != null) {
                return result;
              }
            }
          }
          // We already did the currentDirectory
          return null;
        }

        final Pattern toMatch = Pattern.compile(dirToProcess.replaceAll("\\\\\\\\", "\\\\"));

        File[] matches =
            directory.listFiles(
                new FilenameFilter() {

                  @Override
                  public boolean accept(File arg0, String arg1) {
                    return toMatch.matcher(arg1).matches();
                  }
                });
        if ((matches == null) || (matches.length == 0)) {
          return null;
        }

        directory = matches[0];

        if (!directory.exists()) {
          return null;
        }
      } // End process directory information
    }
    // Last step check if the given file can be found in this directory
    String filenameToLookFor =
        Util.expandBrackets(fileParts[fileParts.length - 1], entry, database);

    final Pattern toMatch =
        Pattern.compile('^' + filenameToLookFor.replaceAll("\\\\\\\\", "\\\\") + '$');

    File[] matches =
        directory.listFiles(
            new FilenameFilter() {

              @Override
              public boolean accept(File arg0, String arg1) {
                return toMatch.matcher(arg1).matches();
              }
            });
    if ((matches == null) || (matches.length == 0)) {
      return null;
    }

    try {
      return matches[0].getCanonicalPath();
    } catch (IOException e) {
      return null;
    }
  }
Esempio n. 4
0
 public String getPropertyNames(PageState state) {
   Object[] keys = state.properties.keySet().toArray();
   Arrays.sort(keys);
   return StringUtil.join(keys, ", ");
 }