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

static void net::sf::jabref::Util::openExternalViewer ( MetaData  metaData,
String  link,
String  fieldName 
) throws IOException [inline, static]

Open a http/pdf/ps viewer for the given link string.

Definition at line 541 of file Util.java.

References expandFilename(), net::sf::jabref::external::ExternalFileType::getOpenWith(), openFileOnWindows(), openFileWithApplicationOnWindows(), and sanitizeUrl().

                               {

        if (fieldName.equals("ps") || fieldName.equals("pdf")) {

            // Find the default directory for this field type:
                  String dir = metaData.getFileDirectory(fieldName);

                  File file = expandFilename(link, new String[] { dir, "." });

                  // Check that the file exists:
                  if ((file == null) || !file.exists()) {
                        throw new IOException(Globals.lang("File not found") + " (" + fieldName + "): '"
                              + link + "'.");
                  }
                  link = file.getCanonicalPath();

                  // Use the correct viewer even if pdf and ps are mixed up:
                  String[] split = file.getName().split("\\.");
                  if (split.length >= 2) {
                        if (split[split.length - 1].equalsIgnoreCase("pdf"))
                              fieldName = "pdf";
                        else if (split[split.length - 1].equalsIgnoreCase("ps")
                              || (split.length >= 3 && split[split.length - 2].equalsIgnoreCase("ps")))
                              fieldName = "ps";
                  }

        } else if (fieldName.equals("doi")) {
                  fieldName = "url";
                  
                  link = sanitizeUrl(link);
                  
                  // Check to see if link field already contains a well formated URL
                  if (!link.startsWith("http://")) {
                      // Remove possible 'doi:'
                      if (link.matches("^doi:/*.*")){
                      link = link.replaceFirst("^doi:/*", "");
                  }
                      link = Globals.DOI_LOOKUP_PREFIX + link;
                  }
            } else if (fieldName.equals("citeseerurl")) {
                  fieldName = "url";

                  String canonicalLink = CiteSeerFetcher.generateCanonicalURL(link);
                  if (canonicalLink != null)
                        link = canonicalLink;
            }

            if (fieldName.equals("url")) { // html
                  try {
                        link = sanitizeUrl(link);
                ExternalFileType fileType = Globals.prefs.getExternalFileTypeByExt("html");
                        if (Globals.ON_MAC) {

                    String[] cmd = ((fileType.getOpenWith() != null) && (fileType.getOpenWith().length() > 0)) ?
                            new String[] { "/usr/bin/open", "-a", fileType.getOpenWith(), link } :
                            new String[] { "/usr/bin/open", link };
                              Runtime.getRuntime().exec(cmd);
                        } else if (Globals.ON_WIN) {

                    if ((fileType.getOpenWith() != null) && (fileType.getOpenWith().length() > 0)) {
                        // Application is specified. Use it:
                        openFileWithApplicationOnWindows(link, fileType.getOpenWith());
                    } else
                        openFileOnWindows(link, true);
                        } else {
                    // Use the given app if specified, and the universal "xdg-open" otherwise:
                    String[] openWith;
                    if ((fileType.getOpenWith() != null) && (fileType.getOpenWith().length() > 0))
                        openWith = fileType.getOpenWith().split(" ");
                    else
                        openWith = new String[] {"xdg-open"};

                    String[] cmd = new String[openWith.length+1];
                    System.arraycopy(openWith, 0, cmd, 0, openWith.length);
                    cmd[cmd.length-1] = link;
                    Runtime.getRuntime().exec(cmd);
                        }

                  } catch (IOException e) {
                System.err.println(Globals.lang("Error_opening_file_'%0'.", link));
                e.printStackTrace();
                  }
            } else if (fieldName.equals("ps")) {
                  try {
                        if (Globals.ON_MAC) {
                    ExternalFileType type = Globals.prefs.getExternalFileTypeByExt("ps");
                    String viewer = type != null ? type.getOpenWith() : Globals.prefs.get("psviewer");
                    String[] cmd = { "/usr/bin/open", "-a", viewer, link };
                              Runtime.getRuntime().exec(cmd);
                        } else if (Globals.ON_WIN) {
                              openFileOnWindows(link, true);
                              /*
                               * cmdArray[0] = Globals.prefs.get("psviewer"); cmdArray[1] =
                               * link; Process child = Runtime.getRuntime().exec(
                               * cmdArray[0] + " " + cmdArray[1]);
                               */
                        } else {
                    ExternalFileType type = Globals.prefs.getExternalFileTypeByExt("ps");
                    String viewer = type != null ? type.getOpenWith() : "xdg-open";
                    String[] cmdArray = new String[2];
                    cmdArray[0] = viewer;
                              cmdArray[1] = link;
                              Runtime.getRuntime().exec(cmdArray);
                        }
                  } catch (IOException e) {
                        System.err.println("An error occured on the command: "
                              + Globals.prefs.get("psviewer") + " " + link);
                  }
            } else if (fieldName.equals("pdf")) {
                  try {
                        if (Globals.ON_MAC) {
                    ExternalFileType type = Globals.prefs.getExternalFileTypeByExt("pdf");
                    String viewer = type != null ? type.getOpenWith() : Globals.prefs.get("psviewer");
                    String[] cmd = { "/usr/bin/open", "-a", viewer, link };
                              Runtime.getRuntime().exec(cmd);
                        } else if (Globals.ON_WIN) {
                              openFileOnWindows(link, true);
                              /*
                               * String[] spl = link.split("\\\\"); StringBuffer sb = new
                               * StringBuffer(); for (int i = 0; i < spl.length; i++) { if
                               * (i > 0) sb.append("\\"); if (spl[i].indexOf(" ") >= 0)
                               * spl[i] = "\"" + spl[i] + "\""; sb.append(spl[i]); }
                               * //pr(sb.toString()); link = sb.toString();
                               * 
                               * String cmd = "cmd.exe /c start " + link;
                               * 
                               * Process child = Runtime.getRuntime().exec(cmd);
                               */
                        } else {
                    ExternalFileType type = Globals.prefs.getExternalFileTypeByExt("pdf");
                    String viewer = type != null ? type.getOpenWith() : Globals.prefs.get("psviewer");
                    String[] cmdArray = new String[2];
                    cmdArray[0] = viewer;
                              cmdArray[1] = link;
                              // Process child = Runtime.getRuntime().exec(cmdArray[0]+"
                              // "+cmdArray[1]);
                              Runtime.getRuntime().exec(cmdArray);
                        }
                  } catch (IOException e) {
                        e.printStackTrace();
                        System.err.println("An error occured on the command: "
                              + Globals.prefs.get("pdfviewer") + " #" + link);
                        System.err.println(e.getMessage());
                  }
            } else {
                  System.err
                        .println("Message: currently only PDF, PS and HTML files can be opened by double clicking");
            }
      }


Generated by  Doxygen 1.6.0   Back to index