Logo Search packages:      
Sourcecode: jabref version File versions  Download package

static String net::sf::jabref::Util::findFile ( BibtexEntry  entry,
BibtexDatabase  database,
File  directory,
String  file 
) [inline, static, protected]

The actual work-horse. Will find absolute filepaths starting from the given directory using the given regular expression string for search.

Definition at line 1424 of file Util.java.

References expandBrackets(), findFile(), and join().

                         {

            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 = 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 = join(fileParts, "/", i + 1, fileParts.length);

                              for (int sub = 0; sub < subDirs.length; sub++) {
                                    if (subDirs[sub].isDirectory()) {
                                          String result = findFile(entry, database, subDirs[sub],
                                                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 = join(fileParts, "/", i + 1, fileParts.length);

                              // Before checking the subdirs, we first check the current
                              // dir
                              String result = 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) // No permission?
                                          continue;

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

                                    for (int sub = 0; sub < subDirs.length; sub++) {
                                          if (!subDirs[sub].isDirectory())
                                                continue;
                                          result = findFile(entry, database, subDirs[sub], 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() {
                              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 = expandBrackets(fileParts[fileParts.length - 1], entry, database);

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

            File[] matches = directory.listFiles(new FilenameFilter() {
                  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;
            }
      }


Generated by  Doxygen 1.6.0   Back to index