1 21 22 package org.armedbear.j; 23 24 import gnu.regexp.RE; 25 import gnu.regexp.REMatch; 26 import gnu.regexp.UncheckedRE; 27 import java.io.IOException ; 28 29 public final class BrowseFile implements Constants 30 { 31 public static void browseFileAtDot() 32 { 33 String browser = 34 Editor.preferences().getStringProperty(Property.BROWSER); 35 if (browser == null) 36 browser = "j"; 37 final Editor editor = Editor.currentEditor(); 38 String filename = browseFileGetFilename(editor); 39 if (filename == null) 40 return; 41 File file = null; 42 if (!filename.startsWith("http://") && !filename.startsWith("https://")) { 43 final Buffer buffer = editor.getBuffer(); 44 if (buffer.getFile() != null) { 45 String prefix = buffer.getFile().netPath(); 46 if (prefix.startsWith("http://") || prefix.startsWith("https://")) 47 filename = File.appendNameToPath(prefix, filename, '/'); 48 } 49 if (!filename.startsWith("http://") && !filename.startsWith("https://")) { 50 file = File.getInstance(editor.getCurrentDirectory(), filename); 51 if (file != null && file.isLocal() && file.isFile()) 52 filename = "file://" + file.canonicalPath(); 53 else 54 return; 55 } 56 } 57 if (browser.equals("j")) { 58 if (file != null) 59 WebBuffer.browse(editor, file, null); 60 else 61 WebBuffer.browse(editor, File.getInstance(filename), null); 62 return; 63 } 64 String browserOpts = 66 Editor.preferences().getStringProperty(Property.BROWSER_OPTS); 67 try { 68 if (browserOpts != null) { 69 String [] cmdarray = {browser, browserOpts, filename}; 70 Runtime.getRuntime().exec(cmdarray); 71 } else { 72 String [] cmdarray = {browser, filename}; 73 Runtime.getRuntime().exec(cmdarray); 74 } 75 } 76 catch (IOException e) { 77 Log.error(e); 78 } 79 } 80 81 private static String browseFileGetFilename(Editor editor) 82 { 83 if (editor.getMark() != null && editor.getMarkLine() == editor.getDotLine()) { 84 return new Region(editor).toString(); 86 } 87 if (editor.getModeId() == HTML_MODE) { 88 RE re = new UncheckedRE("(href|src)=\"([^\"]+)\"", RE.REG_ICASE); 89 REMatch match = null; 90 final String text = editor.getDotLine().getText(); 91 final int dotOffset = editor.getDotOffset(); 92 REMatch m; 93 int index = 0; 94 while ((m = re.getMatch(text, index)) != null) { 95 match = m; 96 if (match.getEndIndex() > dotOffset) 97 break; index = match.getEndIndex(); 99 } 100 if (match != null) 101 return match.toString(2); 102 } 103 return editor.getFilenameAtDot(); 104 } 105 } 106 | Popular Tags |