Exemple #1
0
 private static void fixWrongFileEntries(BibtexEntry entry, NamedCompound ce) {
   String oldValue = entry.getField(Globals.FILE_FIELD);
   if (oldValue == null) {
     return;
   }
   FileListTableModel flModel = new FileListTableModel();
   flModel.setContent(oldValue);
   if (flModel.getRowCount() == 0) {
     return;
   }
   boolean changed = false;
   for (int i = 0; i < flModel.getRowCount(); i++) {
     FileListEntry flEntry = flModel.getEntry(i);
     String link = flEntry.getLink();
     String description = flEntry.getDescription();
     if ("".equals(link) && (!"".equals(description))) {
       // link and description seem to be switched, quickly fix that
       flEntry.setLink(flEntry.getDescription());
       flEntry.setDescription("");
       changed = true;
     }
   }
   if (changed) {
     String newValue = flModel.getStringRepresentation();
     assert (!oldValue.equals(newValue));
     entry.setField(Globals.FILE_FIELD, newValue);
     ce.addEdit(new UndoableFieldChange(entry, Globals.FILE_FIELD, oldValue, newValue));
   }
 }
  public String format(String field) {
    StringBuilder sb = new StringBuilder();

    // Build the table model containing the links:
    FileListTableModel tableModel = new FileListTableModel();
    if (field == null) return "";
    tableModel.setContent(field);

    int piv = 1; // counter for relevant iterations
    for (int i = 0; i < tableModel.getRowCount(); i++) {
      FileListEntry flEntry = tableModel.getEntry(i);
      // Use this entry if we don't discriminate on types, or if the type fits:
      if ((fileType == null) || flEntry.getType().getName().toLowerCase().equals(fileType)) {

        for (FormatEntry entry : format) {
          switch (entry.getType()) {
            case STRING:
              sb.append(entry.getString());
              break;
            case ITERATION_COUNT:
              sb.append(String.valueOf(piv));
              break;
            case FILE_PATH:
              if (flEntry.getLink() == null) break;

              String dir;
              // We need to resolve the file directory from the database's metadata,
              // but that is not available from a formatter. Therefore, as an
              // ugly hack, the export routine has set a global variable before
              // starting the export, which contains the database's file directory:
              if (Globals.prefs.fileDirForDatabase != null) dir = Globals.prefs.fileDirForDatabase;
              else dir = Globals.prefs.get(GUIGlobals.FILE_FIELD + "Directory");

              File f = Util.expandFilename(flEntry.getLink(), new String[] {dir});
              /*
               * Stumbled over this while investigating
               *
               * https://sourceforge.net/tracker/index.php?func=detail&aid=1469903&group_id=92314&atid=600306
               */
              if (f != null) {
                try {
                  sb.append(replaceStrings(f.getCanonicalPath())); // f.toURI().toString();
                } catch (IOException ex) {
                  ex.printStackTrace();
                  sb.append(replaceStrings(f.getPath()));
                }
              } else {
                sb.append(replaceStrings(flEntry.getLink()));
              }

              break;
            case RELATIVE_FILE_PATH:
              if (flEntry.getLink() == null) break;

              /*
               * Stumbled over this while investigating
               *
               * https://sourceforge.net/tracker/index.php?func=detail&aid=1469903&group_id=92314&atid=600306
               */
              sb.append(replaceStrings(flEntry.getLink())); // f.toURI().toString();

              break;
            case FILE_EXTENSION:
              if (flEntry.getLink() == null) break;
              int index = flEntry.getLink().lastIndexOf('.');
              if ((index >= 0) && (index < flEntry.getLink().length() - 1))
                sb.append(replaceStrings(flEntry.getLink().substring(index + 1)));
              break;
            case FILE_TYPE:
              sb.append(replaceStrings(flEntry.getType().getName()));
              break;
            case FILE_DESCRIPTION:
              sb.append(replaceStrings(flEntry.getDescription()));
              break;
          }
        }

        piv++; // update counter
      }
    }

    return sb.toString();
  }