/** * Finds the program that is associated with an extension. The extension may or may not begin with * a '.'. Note that a <code>Display</code> must already exist to guarantee that this method * returns an appropriate result. * * @param extension the program extension * @return the program or <code>null</code> * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT when extension is null * </ul> */ public static Program findProgram(String extension) { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { if (extension == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (extension.length() == 0) return null; Program program = null; char[] chars; if (extension.charAt(0) != '.') { chars = new char[extension.length()]; extension.getChars(0, chars.length, chars, 0); } else { chars = new char[extension.length() - 1]; extension.getChars(1, extension.length(), chars, 0); } NSString ext = NSString.stringWithCharacters(chars, chars.length); if (ext != null) { byte[] fsRef = new byte[80]; if (OS.LSGetApplicationForInfo( OS.kLSUnknownType, OS.kLSUnknownCreator, ext.id, OS.kLSRolesAll, fsRef, null) == OS.noErr) { long /*int*/ url = OS.CFURLCreateFromFSRef(OS.kCFAllocatorDefault(), fsRef); if (url != 0) { NSString bundlePath = new NSURL(url).path(); NSBundle bundle = NSBundle.bundleWithPath(bundlePath); if (bundle != null) program = getProgram(bundle); OS.CFRelease(url); } } } return program; } finally { pool.release(); } }
static NSURL getURL(String fileName) { NSString unescapedStr; String lowercaseName = fileName.toLowerCase(); if (lowercaseName.startsWith(PREFIX_HTTP) || lowercaseName.startsWith(PREFIX_HTTPS) || lowercaseName.startsWith(PREFIX_FILE)) { unescapedStr = NSString.stringWith("%#"); // $NON-NLS-1$ } else { unescapedStr = NSString.stringWith("%"); // $NON-NLS-1$ } NSString fullPath = NSString.stringWith(fileName); if (NSFileManager.defaultManager().fileExistsAtPath(fullPath)) { fullPath = NSURL.fileURLWithPath(fullPath).absoluteString(); } long /*int*/ ptr = OS.CFURLCreateStringByAddingPercentEscapes( 0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8); NSString escapedString = new NSString(ptr); NSURL url = NSURL.URLWithString(escapedString); OS.CFRelease(ptr); return url; }