source: trunk/gli/src/org/greenstone/gatherer/file/FileAssociationManager.java@ 8505

Last change on this file since 8505 was 8240, checked in by mdewsnip, 20 years ago

Removed unnecessary imports of org.greenstone.gatherer.Gatherer.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.file;
28
29import java.io.*;
30import javax.swing.table.*;
31import org.greenstone.gatherer.Configuration;
32import org.greenstone.gatherer.DebugStream;
33import org.greenstone.gatherer.Dictionary;
34import org.greenstone.gatherer.gui.FileAssociationDialog;
35import org.greenstone.gatherer.gui.PreviewCommandDialog;
36import org.greenstone.gatherer.util.ExternalProgram;
37import org.greenstone.gatherer.util.StaticStrings;
38import org.greenstone.gatherer.util.Utility;
39import org.greenstone.gatherer.util.XMLTools;
40import org.w3c.dom.*;
41
42public class FileAssociationManager
43 extends AbstractTableModel {
44 static final public String FILENAME_ARG = "%1";
45 static final private String ESCAPE = "\\\\"; // '\'
46 static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\'
47 static final private String SPACE = " ";
48 static final private String ESCAPED_SPACE = "\\\\ ";
49 private Element associations_element;
50 private File data_file;
51
52 public FileAssociationManager() {
53 // Retrieve the associations_element from the config
54 associations_element = Configuration.getFileAssociations();
55 // Initialize the associations. This involves looking through all current associations searching for those with a command of "".
56 if(associations_element != null) {
57 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
58 for(int i = 0; i < entries.getLength(); i++) {
59 Element entry = (Element) entries.item(i);
60 String command = XMLTools.getValue(entry);
61 // If we encounter a command of ""...
62 if(command.length() == 0) {
63 // if we are on windows, we default to the start command
64 if(Utility.isWindows()) {
65 if (Utility.isWindows9x()) {
66 XMLTools.setValue(entry, StaticStrings.WIN_9X_OPEN_COMMAND);
67 } else {
68 XMLTools.setValue(entry, StaticStrings.WIN_OPEN_COMMAND);
69 }
70 }
71 // and if we are on mac, we default to the open program
72 else if(Utility.isMac()) {
73 XMLTools.setValue(entry, StaticStrings.MAC_OPEN_COMMAND);
74 }
75 }
76 command = null;
77 entry = null;
78 }
79 entries = null;
80 }
81 else {
82 DebugStream.println("Didn't parse anything. About to crash.");
83 }
84 }
85
86 public void edit() {
87 FileAssociationDialog dialog = new FileAssociationDialog(this);
88 dialog.display(null);
89 dialog = null;
90 }
91
92 public String getBrowserCommand(String url) {
93 DebugStream.println("Get browser command: " + url);
94 // First off we try to retrieve one from the configuration
95 String command = Configuration.getPreviewCommand();
96 // If that worked, substitute in the url and return
97 if(command != null && command.length() > 0) {
98 command = command.replaceAll("%1", url);
99 DebugStream.println("Result = " + command);
100 return command;
101 }
102 command = null;
103 // Failing that we have a guess at a sensible default
104 if(Utility.isWindows()) {
105 // we use cmd and start
106 if (Utility.isWindows9x()) {
107 command = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
108 } else {
109 command = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
110 }
111 } else if (Utility.isMac()) {
112 command = StaticStrings.MAC_OPEN_COMMAND; // "open %1"
113 } else {
114 // we try to look for a browser
115 String [] browsers = new String [] {"mozilla", "netscape"};
116 for (int i=0; i<browsers.length; i++) {
117 if (isAvailable(browsers[i])) {
118 command = browsers[i]+ " %1";
119 break;
120 }
121 }
122 }
123
124 // if we still haven't found something, prompt the user
125 if (command == null) {
126 PreviewCommandDialog dialog = new PreviewCommandDialog();
127 command = dialog.display();
128 dialog.dispose();
129 dialog = null;
130 }
131
132 // Store the result if any
133 if(command != null && !command.equals("")) {
134 Configuration.setPreviewCommand(command);
135 command = command.replaceAll(FILENAME_ARG, url);
136 DebugStream.println("Result = " + command);
137 return command;
138 }
139 // if we haven't got a command by now, we'll never get one
140 DebugStream.println("Result = null");
141 return null;
142
143 }
144
145 public int getColumnCount() {
146 return 2;
147 }
148
149 public String getColumnName(int column) {
150 String name;
151 switch(column) {
152 case 0:
153 name = Dictionary.get("FileAssociationDialog.Table.Extension");
154 break;
155 default:
156 name = Dictionary.get("FileAssociationDialog.Table.Command");
157 }
158 return name;
159 }
160
161 public String [] getCommand(File file) {
162 String command = null;
163 String [] commands = null;
164 if(file.isFile()) {
165 // Determine extension
166 String filename = file.getAbsolutePath();
167 String extension = filename.substring(filename.lastIndexOf(".") + 1);
168 // Try to retrieve a value from cache
169 Element entry = getCommand(extension);
170 if(entry != null) {
171 ///ystem.err.println("Retrieved Value From Cache");
172 command = XMLTools.getValue(entry);
173 }
174 if(command == null || command.length() == 0) {
175 ///ystem.err.println("No Existing Command");
176 // If command is null, and we are on windows try searching the registry.
177 if(Utility.isWindows()) {
178 //try the start command
179 if (Utility.isWindows9x()) {
180 command = StaticStrings.WIN_9X_OPEN_COMMAND;
181 } else {
182 command = StaticStrings.WIN_OPEN_COMMAND;
183 }
184
185 }
186
187 // If we are on a mac, default to using the open program
188 else if(Utility.isMac()) {
189 command = StaticStrings.MAC_OPEN_COMMAND;
190 }
191
192 // Otherwise display the dialog and ask the user to enter launching command.
193 if(command == null || command.length() == 0) {
194 ///ystem.err.println("Show Dialog");
195 // Show the dialog which forces a user to select the launch command for a certain file.
196 FileAssociationDialog dialog = new FileAssociationDialog(this);
197 command = dialog.display(extension);
198 dialog = null;
199 }
200 // Hopefully by now we have a command, or else we're never going to get one. Add the association.
201 if(command != null) {
202 // If no previous entry existed create one.
203 if(entry == null) {
204 entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
205 entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
206 associations_element.appendChild(entry);
207 }
208 // Replace the text in this node. Remember to replace the dummy filename with %1 - I dont think the filename will ever be in the comand now
209 //XMLTools.setValue(entry, command.replaceAll(filename, FILENAME_ARG));
210 XMLTools.setValue(entry, command);
211 }
212 }
213 if(command != null) {
214 // We have to fix filename under windows to escape the backslashes
215 filename = filename.replaceAll(ESCAPE, ESCAPED_ESCAPE);
216 // Replace %1 with the appropriate filename
217 command = command.replaceAll(FILENAME_ARG, filename);
218
219 // Make the command into a string [] in case the filename has spaces in it
220 commands = command.split(" ");
221 }
222
223 entry = null;
224 extension = null;
225 filename = null;
226 }
227 return commands;
228 }
229
230 public Element getCommand(String target_extension) {
231 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
232 for(int i = 0; i < entries.getLength(); i++) {
233 Element entry = (Element) entries.item(i);
234 String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
235 if(extension.equalsIgnoreCase(target_extension)) {
236 entries = null;
237 extension = null;
238 return entry;
239 }
240 }
241 entries = null;
242 return null;
243 }
244
245 public String getCommandString(String target_extension) {
246 Element entry = getCommand(target_extension);
247 if(entry != null) {
248 return XMLTools.getValue(entry);
249 }
250 else {
251 return "";
252 }
253 }
254
255 public String getExtension(int index) {
256 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
257 if(0 <= index && index < entries.getLength()) {
258 Element entry = (Element) entries.item(index);
259 return entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
260 }
261 return "";
262 }
263
264 public int getRowCount() {
265 return size();
266 }
267
268 public Object getValueAt(int row, int column) {
269 String extension = getExtension(row);
270 switch(column) {
271 case 0:
272 return extension;
273 default:
274 return getCommandString(extension);
275 }
276 }
277
278 public void save() {
279 }
280
281 public void setCommand(String extension, String command) {
282 DebugStream.println("Set Launch: " + extension + " with " + command);
283 // Retrieve any existing entry for this extension
284 Element entry = getCommand(extension);
285 // If no previous entry existed create one.
286 if(entry == null && command != null) {
287 entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
288 entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
289 associations_element.appendChild(entry);
290 }
291
292 if(command != null) {
293 // Replace the text in this node. If the user has used filename instead of %1 then too bad.
294 XMLTools.setValue(entry, command);
295 }
296 else {
297 // Remove the entry
298 associations_element.removeChild(entry);
299 }
300 entry = null;
301 fireTableDataChanged(); // Can't be anymore efficient as DOM does not gareuntee ordering of new child nodes is consistant
302 }
303
304 public int size() {
305 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
306 return entries.getLength();
307 }
308
309
310 protected boolean isAvailable(String program) {
311 try {
312 ExternalProgram e = new ExternalProgram("which", program);
313 e.exitProgram();
314 String out = e.getLineOfProgramOutput();
315 if (out == null) {
316 return false;
317 }
318 return true;
319 } catch (Exception exc) {
320 return false;
321 }
322 }
323}
Note: See TracBrowser for help on using the repository browser.