Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / BrowserControl.java @ 40561

History | View | Annotate | Download (8.16 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * Created on 18-oct-2004
26
 *
27
 * TODO To change the template for this generated file go to
28
 * Window - Preferences - Java - Code Generation - Code and Comments
29
 */
30
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
31
 *
32
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
33
 *
34
 * This program is free software; you can redistribute it and/or
35
 * modify it under the terms of the GNU General Public License
36
 * as published by the Free Software Foundation; either version 2
37
 * of the License, or (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
47
 *
48
 * For more information, contact:
49
 *
50
 *  Generalitat Valenciana
51
 *   Conselleria d'Infraestructures i Transport
52
 *   Av. Blasco Ib??ez, 50
53
 *   46010 VALENCIA
54
 *   SPAIN
55
 *
56
 *      +34 963862235
57
 *   gvsig@gva.es
58
 *      www.gvsig.gva.es
59
 *
60
 *    or
61
 *
62
 *   IVER T.I. S.A
63
 *   Salamanca 50
64
 *   46005 Valencia
65
 *   Spain
66
 *
67
 *   +34 963163400
68
 *   dac@iver.es
69
 */
70
package org.gvsig.utils;
71
import java.io.IOException;
72
import java.util.ArrayList;
73

    
74
import org.slf4j.Logger;
75
import org.slf4j.LoggerFactory;
76

    
77
 
78
/**
79
* <p>A simple, static class to display a URL in the system browser.<br></p>
80
*
81
* <p>Under <b>Unix</b> systems, it will open one of the browser set through the
82
* <b>setBrowserCommand(String)</b>. The argument of this method
83
* must be a fully qualified command or one of the commands returned
84
* by <b>getSupportedBrowsers()</b>.<br>By default, the browser is Firefox,
85
* which is included in the list of supported browsers.<br></p>
86
*
87
* <p>Under <b>Windows</b>, this will bring up the default browser under windows,
88
* usually either Netscape or Microsoft IE.  The default browser is
89
* determined by the OS so no config is necessary.<br>This has been tested under Windows 95/98/NT/XP.</p>
90
* <p>Notice that <b>you must include</b> the url type -- either "http://" or  "file://".</p>
91
*
92
* <p>Under <b>Mac</b>, the usability of this class is pending of test. So probably it does not work</p>
93
* Examples:
94
* BrowserControl.displayURL("http://www.javaworld.com")
95
* BrowserControl.displayURL("file://c:\\docs\\index.html")
96
* BrowserContorl.displayURL("file:///user/joe/index.html");
97
*
98
* @author jaume dominguez faus - jaume.dominguez@iver.es
99
*/
100
public class BrowserControl
101
{
102
        private static Logger logger = LoggerFactory.getLogger(BrowserControl.class.getName());
103
        public static final String OPERA = "Opera";
104
        public static final String NETSCAPE = "Netscape";
105
        public static final String MOZILLA = "Mozilla";
106
        public static final String GALEON = "Galeon";
107
        public static final String EPIPHANY = "Epiphany";
108
        public static final String FIREFOX = "Firefox";
109
        public static final String KONQUEROR = "Konqueror";
110
        private static String browserCommand = FIREFOX;
111
        private static ArrayList supportedBrowsers;
112

    
113
    /**
114
     * Display a file in the system browser.  If you want to display a
115
     * file, you must include the absolute path name.
116
     *
117
     * @param url the file's url (the url must start with either "http://" or  "file://").
118
     */
119
    public static void displayURL(String url) {
120
        boolean windows = isWindowsPlatform();
121
        String cmd = null;
122
        try {
123
                if (windows) {
124
                        // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
125
                        cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
126
                        Process p = Runtime.getRuntime().exec(cmd);
127
                } else {
128
                        url = "'" + url.replace("'", "\'") + "'";
129
                        if (browserCommand.equals(NETSCAPE) ||
130
                                browserCommand.equals(FIREFOX) ||
131
                                browserCommand.equals(OPERA) ||
132
                                browserCommand.equals(MOZILLA)) {
133
                                // Under Unix, Netscape has to be running for the "-remote"
134
                                // command to work.  So, we try sending the command and
135
                                // check for an exit value.  If the exit command is 0,
136
                                // it worked, otherwise we need to start the browser.
137
                                // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
138
                                cmd = browserCommand.toLowerCase() + " -remote openURL\\(" + url + "\\)";
139
                                Process p = exec(cmd);
140
                                try
141
                                {
142
                                        // wait for exit code -- if it's 0, command worked,
143
                                        // otherwise we need to start the browser up.
144
                                        int exitCode = p.waitFor();
145
                                        if (exitCode != 0)
146
                                        {
147
                                                // Command failed, start up the browser
148
                                                // cmd = 'netscape http://www.javaworld.com'
149
                                                cmd = browserCommand.toLowerCase() + " "  + url;
150
                                                p = exec(cmd);
151
                                        }
152
                                } catch(InterruptedException x) {
153
                                        logger.info("Error bringing up browser, cmd='" + cmd + "'");
154
                                        logger.info("Caught: " + x);
155
                                }
156
                        } else if (browserCommand.equals(KONQUEROR)) {
157
                                cmd = "konqueror "+ url;
158
                                exec(cmd);
159
                        } else if (browserCommand.equals(EPIPHANY)) {
160
                                cmd = "epiphany "+ url;
161
                                exec(cmd);
162
                        } else {
163
                                cmd = browserCommand.replaceAll("%url|%URL", url);
164
                                exec(cmd);
165
                        }
166

    
167
                }
168
        } catch(IOException x) {
169
            // couldn't exec browser
170
                logger.warn("Could not invoke browser, command=" + cmd);
171
                logger.info("Caught: " + x);
172
        }
173

    
174
    }
175
    
176
    private static Process exec(String cmd) throws IOException {
177
            logger.info(cmd);
178
                return Runtime.getRuntime().exec(cmd);
179
    }
180

    
181
    /**
182
     * Try to determine whether this application is running under Windows
183
     * or some other platform by examing the "os.name" property.
184
     *
185
     * @return true if this application is running under a Windows OS
186
     */
187
    public static boolean isWindowsPlatform()
188
    {
189
        String os = System.getProperty("os.name");
190
        if ( os != null && os.startsWith(WIN_ID))
191
            return true;
192
        else
193
            return false;
194
    }
195

    
196

    
197
    public static void setBrowserCommand(String browserCommand) {
198
            BrowserControl.browserCommand = browserCommand;
199
    }
200
    /**
201
     * Returns a list of supported browsers.
202
     * @return
203
     */
204
    public static ArrayList getSupportedBrowsers() {
205
            if (supportedBrowsers == null) {
206
                    supportedBrowsers = new ArrayList();
207
                    supportedBrowsers.add(KONQUEROR);
208
                    supportedBrowsers.add(FIREFOX);
209
                    supportedBrowsers.add(EPIPHANY);
210
                    supportedBrowsers.add(MOZILLA);
211
                    supportedBrowsers.add(NETSCAPE);
212
                    supportedBrowsers.add(OPERA);
213
            }
214
            return supportedBrowsers;
215
    }
216
    // Used to identify the windows platform.
217
    private static final String WIN_ID = "Windows";
218
    // The default system browser under windows.
219
    private static final String WIN_PATH = "rundll32";
220
    // The flag to display a url.
221
    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
222
    // The default browser under unix.
223
//    private static final String UNIX_PATH = "netscape";
224
    // The flag to display a url.
225
//    private static final String UNIX_FLAG = "-remote openURL";
226
}