001: /*
002: * BrowseFile.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: BrowseFile.java,v 1.1.1.1 2002/09/24 16:08:23 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import gnu.regexp.RE;
025: import gnu.regexp.REMatch;
026: import gnu.regexp.UncheckedRE;
027: import java.io.IOException;
028:
029: public final class BrowseFile implements Constants {
030: public static void browseFileAtDot() {
031: String browser = Editor.preferences().getStringProperty(
032: Property.BROWSER);
033: if (browser == null)
034: browser = "j";
035: final Editor editor = Editor.currentEditor();
036: String filename = browseFileGetFilename(editor);
037: if (filename == null)
038: return;
039: File file = null;
040: if (!filename.startsWith("http://")
041: && !filename.startsWith("https://")) {
042: final Buffer buffer = editor.getBuffer();
043: if (buffer.getFile() != null) {
044: String prefix = buffer.getFile().netPath();
045: if (prefix.startsWith("http://")
046: || prefix.startsWith("https://"))
047: filename = File.appendNameToPath(prefix, filename,
048: '/');
049: }
050: if (!filename.startsWith("http://")
051: && !filename.startsWith("https://")) {
052: file = File.getInstance(editor.getCurrentDirectory(),
053: filename);
054: if (file != null && file.isLocal() && file.isFile())
055: filename = "file://" + file.canonicalPath();
056: else
057: return;
058: }
059: }
060: if (browser.equals("j")) {
061: if (file != null)
062: WebBuffer.browse(editor, file, null);
063: else
064: WebBuffer.browse(editor, File.getInstance(filename),
065: null);
066: return;
067: }
068: // External browser.
069: String browserOpts = Editor.preferences().getStringProperty(
070: Property.BROWSER_OPTS);
071: try {
072: if (browserOpts != null) {
073: String[] cmdarray = { browser, browserOpts, filename };
074: Runtime.getRuntime().exec(cmdarray);
075: } else {
076: String[] cmdarray = { browser, filename };
077: Runtime.getRuntime().exec(cmdarray);
078: }
079: } catch (IOException e) {
080: Log.error(e);
081: }
082: }
083:
084: private static String browseFileGetFilename(Editor editor) {
085: if (editor.getMark() != null
086: && editor.getMarkLine() == editor.getDotLine()) {
087: // Use selection.
088: return new Region(editor).toString();
089: }
090: if (editor.getModeId() == HTML_MODE) {
091: RE re = new UncheckedRE("(href|src)=\"([^\"]+)\"",
092: RE.REG_ICASE);
093: REMatch match = null;
094: final String text = editor.getDotLine().getText();
095: final int dotOffset = editor.getDotOffset();
096: REMatch m;
097: int index = 0;
098: while ((m = re.getMatch(text, index)) != null) {
099: match = m;
100: if (match.getEndIndex() > dotOffset)
101: break; // All subsequent matches will be further away.
102: index = match.getEndIndex();
103: }
104: if (match != null)
105: return match.toString(2);
106: }
107: return editor.getFilenameAtDot();
108: }
109: }
|