/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson, Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer.file; import java.io.*; import java.util.regex.*; import javax.swing.table.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.DebugStream; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.gui.FileAssociationDialog; import org.greenstone.gatherer.gui.PreviewCommandDialog; import org.greenstone.gatherer.util.SafeProcess; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Utility; import org.greenstone.gatherer.util.XMLTools; import org.w3c.dom.*; public class FileAssociationManager extends AbstractTableModel { static final public String FILENAME_ARG = "%1"; static final private String ESCAPE = "\\\\"; // '\' static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\' static final private String SPACE = " "; static final private String ESCAPED_SPACE = "\\\\ "; private Element associations_element; private File data_file; public FileAssociationManager() { // Retrieve the associations_element from the config associations_element = Configuration.getFileAssociations(); // Initialize the associations. This involves looking through all current associations searching for those with a command of "". if(associations_element != null) { NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT); for(int i = 0; i < entries.getLength(); i++) { Element entry = (Element) entries.item(i); String command = XMLTools.getValue(entry); // If we encounter a command of ""... if(command.length() == 0) { // if we are on windows, we default to the start command if(Utility.isWindows()) { if (Utility.isWindows9x()) { XMLTools.setValue(entry, StaticStrings.WIN_9X_OPEN_COMMAND); } else { XMLTools.setValue(entry, StaticStrings.WIN_OPEN_COMMAND); } } // and if we are on mac, we default to the open program else if(Utility.isMac()) { XMLTools.setValue(entry, StaticStrings.MAC_OPEN_COMMAND); } //else { XMLTools.setValue(entry, StaticStrings.LINUX_OPEN_COMMAND); } // assume linux? } command = null; entry = null; } entries = null; } else { DebugStream.println("Didn't parse anything. About to crash."); } } public void edit() { FileAssociationDialog dialog = new FileAssociationDialog(this); dialog.display(null); dialog = null; } public String getBrowserCommand(String url) { DebugStream.println("Get browser command: " + url); // First off we try to retrieve one from the configuration String command = Configuration.getPreviewCommand(); // If that worked, substitute in the url and return if(command != null && command.length() > 0) { command = command.replaceAll("%1", url); DebugStream.println("Result = " + command); return command; } command = null; // Failing that we have a guess at a sensible default if(Utility.isWindows()) { // we use cmd and start if (Utility.isWindows9x()) { command = StaticStrings.WIN_9X_OPEN_COMMAND;//"command.com /c start \""+url+"\""; } else { command = StaticStrings.WIN_OPEN_COMMAND;//"cmd.exe /c start \"\" \""+url+"\""; } } else if (Utility.isMac()) { command = StaticStrings.MAC_OPEN_COMMAND; // "open %1" } else { // we try to look for a browser String [] browsers = new String [] {"mozilla", "netscape", "firefox"}; for (int i=0; i does not work if there are spaces in the // file path %1, when running the Process with a command array. // Need <"start \"window\" \"%1\""> to be an element in the command array: String[] tmp = commands; int index = 0; for(int i = 0; i < commands.length; i++) { if(commands[i].indexOf("start") != -1) { index = i; } } commands = new String[index+1]; for(int i = 0; i < index; i++) { commands[i] = tmp[i]; } commands[index] = tmp[index]; for(int i = index+1; i < tmp.length; i++) { commands[index] = commands[index] + " " + tmp[i]; } } for (int i=0; i