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

static boolean net::sf::jabref::Util::openExternalFileAnyFormat ( final MetaData  metaData,
String  link,
final ExternalFileType  fileType 
) throws IOException [inline, static]

Open an external file, attempting to use the correct viewer for it.

Parameters:
metaData The MetaData for the database this file belongs to.
link The file name.
Returns:
false if the link couldn't be resolved, true otherwise.

Definition at line 746 of file Util.java.

References expandFilename(), openFileOnWindows(), and openFileWithApplicationOnWindows().

                                                                                                     {

        boolean httpLink = false;

        if (remoteLinkPattern.matcher(link.toLowerCase()).matches()) {
            httpLink = true;
        }
        /*if (link.toLowerCase().startsWith("file://")) {
            link = link.substring(7);
        }
        final String ln = link;
        if (remoteLinkPattern.matcher(link.toLowerCase()).matches()) {
            (new Thread(new Runnable() {
                public void run() {
                    openRemoteExternalFile(metaData, ln, fileType);
                }
            })).start();

            return true;
        }*/

        //boolean httpLink = link.toLowerCase().startsWith("http:")
        //        || link.toLowerCase().startsWith("ftp:");

        
        // For other platforms we'll try to find the file type:
            File file = new File(link);

            // We try to check the extension for the file:
            String name = file.getName();
            int pos = name.lastIndexOf('.');
            String extension = ((pos >= 0) && (pos < name.length() - 1)) ? name.substring(pos + 1)
                  .trim().toLowerCase() : null;
            // Find the default directory for this field type, if any:
            String dir = metaData.getFileDirectory(extension);
            // Include the standard "file" directory:
        String fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD);
        // Include the directory of the bib file:
        String[] dirs;
        if (metaData.getFile() != null) {
            String databaseDir = metaData.getFile().getParent();
            dirs = new String[] { dir, fileDir, databaseDir };
        }
        else
            dirs = new String[] { dir, fileDir };

        if (!httpLink) {
            File tmp = expandFilename(link, dirs);
            if (tmp != null)
                file = tmp;
        }

        // Check if we have arrived at a file type, and either an http link or an existing file:
            if ((httpLink || file.exists()) && (fileType != null)) {
            // Open the file:
                  try {
                String filePath = httpLink ? link : file.getPath();
                if (Globals.ON_MAC) {
                    // Use "-a <application>" if the app is specified, and just "open <filename>" otherwise:
                    String[] cmd = ((fileType.getOpenWith() != null) && (fileType.getOpenWith().length() > 0)) ?
                            new String[] { "/usr/bin/open", "-a", fileType.getOpenWith(), filePath } :
                            new String[] { "/usr/bin/open", filePath };
                              Runtime.getRuntime().exec(cmd);
                        } else if (Globals.ON_WIN) {
                    if ((fileType.getOpenWith() != null) && (fileType.getOpenWith().length() > 0)) {
                        // Application is specified. Use it:
                        openFileWithApplicationOnWindows(filePath, fileType.getOpenWith());
                    } else
                        openFileOnWindows(filePath, 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[] cmdArray = new String[openWith.length+1];
                    System.arraycopy(openWith, 0, cmdArray, 0, openWith.length);
                    cmdArray[cmdArray.length-1] = filePath;
                    Runtime.getRuntime().exec(cmdArray);
                        }
                return true;
            } catch (IOException e) {
                throw e;
                /*e.printStackTrace();
                        System.err.println("An error occured on the command: " + fileType.getOpenWith()
                              + " #" + link);
                        System.err.println(e.getMessage());*/
                  }

            } else {

            return false;
            // No file matched the name, or we didn't know the file type.

            }


    }


Generated by  Doxygen 1.6.0   Back to index