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

Last change on this file since 6622 was 6622, checked in by jmt12, 20 years ago

More modifications to mirroring including testing for a valid version of Wget (and complaining if its missing or it is old) and rearranging buttons on the GProgressBar

  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 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.Dictionary;
32import org.greenstone.gatherer.Gatherer;
33import org.greenstone.gatherer.gui.FileAssociationDialog;
34import org.greenstone.gatherer.msm.MSMUtils;
35import org.greenstone.gatherer.util.StaticStrings;
36import org.greenstone.gatherer.util.Utility;
37import org.greenstone.gatherer.util.WinRegistry;
38import org.w3c.dom.*;
39
40public class FileAssociationManager
41 extends AbstractTableModel {
42 static final public String FILENAME_ARG = "%1";
43 static final private String DATA_FILENAME = "associations.xml";
44 static final private String ENTRY_ELEMENT = "Entry";
45 static final private String ESCAPE = "\\\\"; // '\'
46 static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\'
47 static final private String EXTENSION_ATTRIBUTE = "extension";
48 private Document document;
49 private File data_file;
50
51 public FileAssociationManager() {
52 // If a associations.xml is available in the GLI install directory load it.
53 data_file = new File(Utility.BASE_DIR + DATA_FILENAME);
54 if(data_file.exists()) {
55 document = Utility.parse(data_file, true);
56 }
57 // Load the default associations xml data file. This can be done using the classloader.
58 else {
59 document = Utility.parse(Utility.XML_DIRECTORY + DATA_FILENAME, true);
60 }
61 // Initialize the associations. This involves looking through all current associations searching for those with a command of "".
62 if(document != null) {
63 NodeList entries = (document.getDocumentElement()).getElementsByTagName(ENTRY_ELEMENT);
64 for(int i = 0; i < entries.getLength(); i++) {
65 Element entry = (Element) entries.item(i);
66 String extension = entry.getAttribute(EXTENSION_ATTRIBUTE);
67 String command = MSMUtils.getValue(entry);
68 // If we encounter a command of ""...
69 if(command.length() == 0) {
70 // and if we are on windows, we try to automatically set this command.
71 if(Utility.isWindows()) {
72 // Create a dummy filename with the appropriate extension
73 String dummy_file = "dummy." + extension;
74 command = WinRegistry.openCommand(dummy_file);
75 // If this succeeded add the association.
76 if(command != null) {
77 // Remember to replace the dummy filename with %1
78 command = command.replaceAll(dummy_file, FILENAME_ARG);
79 // Replace the text in this node.
80 MSMUtils.setValue(entry, command);
81 }
82 dummy_file = null;
83 }
84 // and if we are on windows, we default to the open program
85 if(Utility.isMac()) {
86 MSMUtils.setValue(entry, StaticStrings.MAC_OPEN_COMMAND);
87 }
88 }
89 command = null;
90 extension = null;
91 entry = null;
92 }
93 entries = null;
94 }
95 else {
96 Gatherer.println("Didn't parse anything. About to crash.");
97 }
98 }
99
100 public void edit() {
101 FileAssociationDialog dialog = new FileAssociationDialog(this);
102 dialog.display(null);
103 dialog = null;
104 }
105
106 public String getBrowserCommand(String url) {
107 Gatherer.println("Get browser command: " + url);
108 // First off we try to retrieve one from the configuration
109 String command = Gatherer.config.getPreviewCommand();
110 // If that worked, substitute in the url and return
111 if(command != null && command.length() > 0) {
112 command = command.replaceAll("%1", url);
113 Gatherer.println("Result = " + command);
114 return command;
115 }
116 // Failing that we have to prompt the user for what they want to do, but we do our best to provide a sensible default (if available).
117 // For windows we can have a pretty good guess
118 if(Utility.isWindows()) {
119 // we use cmd and start
120 if (Utility.isWindows9x()) {
121 command = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\"";
122 } else {
123 command = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\"";
124 }
125 }
126
127 // Now prompt the user
128 FileAssociationDialog dialog = new FileAssociationDialog(this);
129 command = dialog.display(true, command);
130 dialog.dispose();
131 dialog = null;
132
133 // Store the result if any
134 if(command != null) {
135 Gatherer.config.setPreviewCommand(command);
136 }
137
138 // for now uder linux get the fileassoc thing. but eventually want a separate thing.
139 // pretend we are trying to open an html file
140 // Mmmoooo. - jmt12
141 /*
142 String extension = "html";
143 Element entry = getCommand(extension);
144 if (entry != null) {
145 command = MSMUtils.getValue(entry);
146 }
147
148 if(command == null || command.length() == 0) {
149 FileAssociationDialog dialog = new FileAssociationDialog(this);
150 command = dialog.display(extension);
151 dialog = null;
152 }
153 if(command != null) {
154 // If no previous entry existed create one.
155 if(entry == null) {
156 entry = document.createElement(ENTRY_ELEMENT);
157 entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
158 document.getDocumentElement().appendChild(entry);
159 }
160 // Replace the text in this node. Remember to replace the dummy filename with %1
161 MSMUtils.setValue(entry, command.replaceAll("temp.html", FILENAME_ARG));
162 }
163 */
164 // if we haven't got a command by now, we'll never get one
165 if (command == null) {
166 Gatherer.println("Result = " + command);
167 return null;
168 }
169 // Replace %1 with the url in quotes
170 command = command.replaceAll(FILENAME_ARG, url);
171 Gatherer.println("Result = " + command);
172 return command;
173 }
174
175 public int getColumnCount() {
176 return 2;
177 }
178
179 public String getColumnName(int column) {
180 String name;
181 switch(column) {
182 case 0:
183 name = Dictionary.get("FileAssociationDialog.Table.Extension");
184 break;
185 default:
186 name = Dictionary.get("FileAssociationDialog.Table.Command");
187 }
188 return name;
189 }
190
191 public String getCommand(File file) {
192 String command = null;
193 if(file.isFile()) {
194 // Determine extension
195 String filename = file.getAbsolutePath();
196 String extension = filename.substring(filename.lastIndexOf(".") + 1);
197 // Try to retrieve a value from cache
198 Element entry = getCommand(extension);
199 if(entry != null) {
200 ///ystem.err.println("Retrieved Value From Cache");
201 command = MSMUtils.getValue(entry);
202 }
203 if(command == null || command.length() == 0) {
204 ///ystem.err.println("No Existing Command");
205 // If command is null, and we are on windows try searching the registry.
206 if(Utility.isWindows()) {
207 ///ystem.err.println("Is Windows");
208 //command = WinRegistry.openCommand(filename);
209 //try the start command
210 if (Utility.isWindows9x()) {
211 command = StaticStrings.WIN_9X_OPEN_COMMAND;
212 } else {
213 command = StaticStrings.WIN_OPEN_COMMAND;
214 }
215
216 }
217
218 // If we are on a mac, default to using the open program
219 else if(Utility.isMac()) {
220 ///ystem.err.println("Is Mac");
221 command = StaticStrings.MAC_OPEN_COMMAND;
222 }
223
224 // Otherwise display the dialog and ask the user to enter launching command.
225 if(command == null || command.length() == 0) {
226 ///ystem.err.println("Show Dialog");
227 // Show the dialog which forces a user to select the launch command for a certain file.
228 FileAssociationDialog dialog = new FileAssociationDialog(this);
229 command = dialog.display(extension);
230 dialog = null;
231 }
232 // Hopefully by now we have a command, or else we're never going to get one. Add the association.
233 if(command != null) {
234 // If no previous entry existed create one.
235 if(entry == null) {
236 entry = document.createElement(ENTRY_ELEMENT);
237 entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
238 document.getDocumentElement().appendChild(entry);
239 }
240 // 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
241 //MSMUtils.setValue(entry, command.replaceAll(filename, FILENAME_ARG));
242 MSMUtils.setValue(entry, command);
243 }
244 }
245 if(command != null) {
246 // We have to fix filename under windows to escape the backslashes.
247 filename = filename.replaceAll(ESCAPE, ESCAPED_ESCAPE);
248 // Replace %1 with the appropriate filename
249 command = command.replaceAll(FILENAME_ARG, filename);
250 }
251
252 entry = null;
253 extension = null;
254 filename = null;
255 }
256 return command;
257 }
258
259 public Element getCommand(String target_extension) {
260 NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
261 for(int i = 0; i < entries.getLength(); i++) {
262 Element entry = (Element) entries.item(i);
263 String extension = entry.getAttribute(EXTENSION_ATTRIBUTE);
264 if(extension.equalsIgnoreCase(target_extension)) {
265 entries = null;
266 extension = null;
267 return entry;
268 }
269 }
270 entries = null;
271 return null;
272 }
273
274 public String getCommandString(String target_extension) {
275 Element entry = getCommand(target_extension);
276 if(entry != null) {
277 return MSMUtils.getValue(entry);
278 }
279 else {
280 return "";
281 }
282 }
283
284 public String getExtension(int index) {
285 NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
286 if(0 <= index && index < entries.getLength()) {
287 Element entry = (Element) entries.item(index);
288 return entry.getAttribute(EXTENSION_ATTRIBUTE);
289 }
290 return "";
291 }
292
293 public int getRowCount() {
294 NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
295 return entries.getLength();
296 }
297
298 public Object getValueAt(int row, int column) {
299 String extension = getExtension(row);
300 switch(column) {
301 case 0:
302 return extension;
303 default:
304 return getCommandString(extension);
305 }
306 }
307
308 public void save() {
309 Utility.export(document, data_file);
310 }
311
312 public void setCommand(String extension, String command) {
313 Gatherer.println("Set Launch: " + extension + " with " + command);
314 // Retrieve any existing entry for this extension
315 Element entry = getCommand(extension);
316 // If no previous entry existed create one.
317 if(entry == null && command != null) {
318 entry = document.createElement(ENTRY_ELEMENT);
319 entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
320 document.getDocumentElement().appendChild(entry);
321 }
322
323 if(command != null) {
324 // Replace the text in this node. If the user has used filename instead of %1 then too bad.
325 MSMUtils.setValue(entry, command);
326 }
327 else {
328 // Remove the entry
329 document.getDocumentElement().removeChild(entry);
330 }
331 entry = null;
332 fireTableDataChanged(); // Can't be anymore efficient as DOM does not gareuntee ordering of new child nodes is consistant
333 }
334
335 public int size() {
336 NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
337 return entries.getLength();
338 }
339}
Note: See TracBrowser for help on using the repository browser.