KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > BrowseFile


1 /*
2  * BrowseFile.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: BrowseFile.java,v 1.1.1.1 2002/09/24 16:08:23 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

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 JavaDoc;
28
29 public final class BrowseFile implements Constants
30 {
31     public static void browseFileAtDot()
32     {
33         String JavaDoc browser =
34             Editor.preferences().getStringProperty(Property.BROWSER);
35         if (browser == null)
36             browser = "j";
37         final Editor editor = Editor.currentEditor();
38         String JavaDoc 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 JavaDoc 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         // External browser.
65
String JavaDoc browserOpts =
66             Editor.preferences().getStringProperty(Property.BROWSER_OPTS);
67         try {
68             if (browserOpts != null) {
69                 String JavaDoc[] cmdarray = {browser, browserOpts, filename};
70                 Runtime.getRuntime().exec(cmdarray);
71             } else {
72                 String JavaDoc[] cmdarray = {browser, filename};
73                 Runtime.getRuntime().exec(cmdarray);
74             }
75         }
76         catch (IOException JavaDoc e) {
77             Log.error(e);
78         }
79     }
80
81     private static String JavaDoc browseFileGetFilename(Editor editor)
82     {
83         if (editor.getMark() != null && editor.getMarkLine() == editor.getDotLine()) {
84             // Use selection.
85
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 JavaDoc 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; // All subsequent matches will be further away.
98
index = match.getEndIndex();
99             }
100             if (match != null)
101                 return match.toString(2);
102         }
103         return editor.getFilenameAtDot();
104     }
105 }
106
Popular Tags