source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/file/FileAssociationManager.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 12.1 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 java.util.regex.*;
31import javax.swing.table.*;
32import org.greenstone.gatherer.Configuration;
33import org.greenstone.gatherer.DebugStream;
34import org.greenstone.gatherer.Dictionary;
35import org.greenstone.gatherer.gui.FileAssociationDialog;
36import org.greenstone.gatherer.gui.PreviewCommandDialog;
37import org.greenstone.gatherer.util.ExternalProgram;
38import org.greenstone.gatherer.util.StaticStrings;
39import org.greenstone.gatherer.util.Utility;
40import org.greenstone.gatherer.util.XMLTools;
41import org.w3c.dom.*;
42
43public class FileAssociationManager
44 extends AbstractTableModel {
45 static final public String FILENAME_ARG = "%1";
46 static final private String ESCAPE = "\\\\"; // '\'
47 static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\'
48 static final private String SPACE = " ";
49 static final private String ESCAPED_SPACE = "\\\\ ";
50 private Element associations_element;
51 private File data_file;
52
53 public FileAssociationManager() {
54 // Retrieve the associations_element from the config
55 associations_element = Configuration.getFileAssociations();
56 // Initialize the associations. This involves looking through all current associations searching for those with a command of "".
57 if(associations_element != null) {
58 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
59 for(int i = 0; i < entries.getLength(); i++) {
60 Element entry = (Element) entries.item(i);
61 String command = XMLTools.getValue(entry);
62 // If we encounter a command of ""...
63 if(command.length() == 0) {
64 // if we are on windows, we default to the start command
65 if(Utility.isWindows()) {
66 if (Utility.isWindows9x()) {
67 XMLTools.setValue(entry, StaticStrings.WIN_9X_OPEN_COMMAND);
68 } else {
69 XMLTools.setValue(entry, StaticStrings.WIN_OPEN_COMMAND);
70 }
71 }
72 // and if we are on mac, we default to the open program
73 else if(Utility.isMac()) {
74 XMLTools.setValue(entry, StaticStrings.MAC_OPEN_COMMAND);
75 }
76 //else { XMLTools.setValue(entry, StaticStrings.LINUX_OPEN_COMMAND); } // assume linux?
77 }
78 command = null;
79 entry = null;
80 }
81 entries = null;
82 }
83 else {
84 DebugStream.println("Didn't parse anything. About to crash.");
85 }
86 }
87
88 public void edit() {
89 FileAssociationDialog dialog = new FileAssociationDialog(this);
90 dialog.display(null);
91 dialog = null;
92 }
93
94 public String getBrowserCommand(String url) {
95 DebugStream.println("Get browser command: " + url);
96 // First off we try to retrieve one from the configuration
97 String command = Configuration.getPreviewCommand();
98 // If that worked, substitute in the url and return
99 if(command != null && command.length() > 0) {
100 command = command.replaceAll("%1", url);
101 DebugStream.println("Result = " + command);
102 return command;
103 }
104 command = null;
105 // Failing that we have a guess at a sensible default
106 if(Utility.isWindows()) {
107 // we use cmd and start
108 if (Utility.isWindows9x()) {
109 command = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
110 } else {
111 command = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
112 }
113 } else if (Utility.isMac()) {
114 command = StaticStrings.MAC_OPEN_COMMAND; // "open %1"
115 } else {
116 // we try to look for a browser
117 String [] browsers = new String [] {"mozilla", "netscape"};
118 for (int i=0; i<browsers.length; i++) {
119 if (isAvailable(browsers[i])) {
120 command = browsers[i]+ " %1";
121 break;
122 }
123 }
124 //if (command == null) { command = StaticStrings.LINUX_OPEN_COMMAND; } // "xdg-open %1"
125 }
126
127 // if we still haven't found something, prompt the user
128 if (command == null) {
129 PreviewCommandDialog dialog = new PreviewCommandDialog();
130 command = dialog.display();
131 dialog.dispose();
132 dialog = null;
133 }
134
135 // Store the result if any
136 if(command != null && !command.equals("")) {
137 Configuration.setPreviewCommand(command);
138 command = command.replaceAll(FILENAME_ARG, url);
139 DebugStream.println("Result = " + command);
140 return command;
141 }
142 // if we haven't got a command by now, we'll never get one
143 DebugStream.println("Result = null");
144 return null;
145
146 }
147
148 public int getColumnCount() {
149 return 2;
150 }
151
152 public String getColumnName(int column) {
153 String name;
154 switch(column) {
155 case 0:
156 name = Dictionary.get("FileAssociationDialog.Table.Extension");
157 break;
158 default:
159 name = Dictionary.get("FileAssociationDialog.Table.Command");
160 }
161 return name;
162 }
163
164 public String [] getCommand(File file) {
165 String command = null;
166 String [] commands = null;
167 if(file.isFile()) {
168 // Determine extension
169 String filename = file.getAbsolutePath();
170 String extension = filename.substring(filename.lastIndexOf(".") + 1);
171 // Try to retrieve a value from cache
172 Element entry = getCommand(extension);
173 if(entry != null) {
174 ///ystem.err.println("Retrieved Value From Cache");
175 command = XMLTools.getValue(entry);
176 }
177 if(command == null || command.length() == 0) {
178 ///ystem.err.println("No Existing Command");
179 // If command is null, and we are on windows try searching the registry.
180 if(Utility.isWindows()) {
181 //try the start command
182 if (Utility.isWindows9x()) {
183 command = StaticStrings.WIN_9X_OPEN_COMMAND;
184 } else {
185 command = StaticStrings.WIN_OPEN_COMMAND;
186 }
187
188 }
189
190 // If we are on a mac, default to using the open program
191 else if(Utility.isMac()) {
192 command = StaticStrings.MAC_OPEN_COMMAND;
193 }
194
195 //else { command = StaticStrings.LINUX_OPEN_COMMAND; } // If linux, default to using xdg-open
196
197 // Otherwise display the dialog and ask the user to enter launching command.
198 if(command == null || command.length() == 0) {
199 ///ystem.err.println("Show Dialog");
200 // Show the dialog which forces a user to select the launch command for a certain file.
201 FileAssociationDialog dialog = new FileAssociationDialog(this);
202 command = dialog.display(extension);
203 dialog = null;
204 }
205
206 // Hopefully by now we have a command, or else we're never going to get one. Add the association.
207 if (command != null && !command.equals("")) {
208 // If no previous entry existed create one.
209 if(entry == null) {
210 entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
211 entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
212 associations_element.appendChild(entry);
213 }
214 // 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
215 //XMLTools.setValue(entry, command.replaceAll(filename, FILENAME_ARG));
216 XMLTools.setValue(entry, command);
217 }
218 }
219
220 if (command != null && !command.equals("")) {
221
222 // Make the command into a string []
223 commands = command.split(" ");
224
225 // Now substitute any occurrences of %1 with its filename
226 // Note this is done after the split on spaces to avoid
227 // any conflict with filenames with spaces in them.
228
229 // We have to fix filename under windows to escape the backslashes
230 filename = filename.replaceAll(ESCAPE, ESCAPED_ESCAPE);
231
232 // dealing with spaces in filepath when using start command
233 if(Utility.isWindows() && filename.indexOf(" ") != -1 && command.indexOf("start") != -1) {
234 // On Windows, start command used spaces in filepath. In this case:
235 // start and its arguments all together need to go into one element of the commands array
236 // otherwise <cmd /c start "window title" "%1"> does not work if there are spaces in the
237 // file path %1, when running the Process with a command array.
238
239 // Need <"start \"window\" \"%1\""> to be an element in the command array:
240 String[] tmp = commands;
241 int index = 0;
242
243 for(int i = 0; i < commands.length; i++) {
244 if(commands[i].indexOf("start") != -1) {
245 index = i;
246 }
247 }
248
249 commands = new String[index+1];
250 for(int i = 0; i < index; i++) {
251 commands[i] = tmp[i];
252 }
253
254 commands[index] = tmp[index];
255 for(int i = index+1; i < tmp.length; i++) {
256 commands[index] = commands[index] + " " + tmp[i];
257 }
258
259 }
260
261 for (int i=0; i<commands.length; i++) {
262 // Replace %1 with the appropriate filename
263 commands[i] = commands[i].replaceAll(FILENAME_ARG, filename);
264 }
265 }
266
267 entry = null;
268 extension = null;
269 filename = null;
270 }
271 return commands;
272 }
273
274 public Element getCommand(String target_extension) {
275 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
276 for(int i = 0; i < entries.getLength(); i++) {
277 Element entry = (Element) entries.item(i);
278 String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
279 if(extension.equalsIgnoreCase(target_extension)) {
280 entries = null;
281 extension = null;
282 return entry;
283 }
284 }
285 entries = null;
286 return null;
287 }
288
289 public String getCommandString(String target_extension) {
290 Element entry = getCommand(target_extension);
291 if(entry != null) {
292 return XMLTools.getValue(entry);
293 }
294 else {
295 return "";
296 }
297 }
298
299 public String getExtension(int index) {
300 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
301 if(0 <= index && index < entries.getLength()) {
302 Element entry = (Element) entries.item(index);
303 return entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
304 }
305 return "";
306 }
307
308 public int getRowCount() {
309 return size();
310 }
311
312 public Object getValueAt(int row, int column) {
313 String extension = getExtension(row);
314 switch(column) {
315 case 0:
316 return extension;
317 default:
318 return getCommandString(extension);
319 }
320 }
321
322 public void save() {
323 }
324
325 public void setCommand(String extension, String command) {
326 DebugStream.println("Set Launch: " + extension + " with " + command);
327 // Retrieve any existing entry for this extension
328 Element entry = getCommand(extension);
329 // If no previous entry existed create one.
330 if(entry == null && command != null) {
331 entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
332 entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
333 associations_element.appendChild(entry);
334 }
335
336 if(command != null) {
337 // Replace the text in this node. If the user has used filename instead of %1 then too bad.
338 XMLTools.setValue(entry, command);
339 }
340 else {
341 // Remove the entry
342 associations_element.removeChild(entry);
343 }
344 entry = null;
345 fireTableDataChanged(); // Can't be anymore efficient as DOM does not gareuntee ordering of new child nodes is consistant
346 }
347
348 public int size() {
349 NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
350 return entries.getLength();
351 }
352
353
354 protected boolean isAvailable(String program) {
355 try {
356 ExternalProgram e = new ExternalProgram("which", program);
357 e.exitProgram();
358 String out = e.getLineOfProgramOutput();
359 if (out == null) {
360 return false;
361 }
362 return true;
363 } catch (Exception exc) {
364 return false;
365 }
366 }
367}
Note: See TracBrowser for help on using the repository browser.