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

Last change on this file since 4670 was 4670, checked in by jmt12, 21 years ago

Reimplemented file associations to properly handle %1 arguments

  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 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 org.greenstone.gatherer.Gatherer;
31import org.greenstone.gatherer.gui.FileAssociationDialog;
32import org.greenstone.gatherer.msm.MSMUtils;
33import org.greenstone.gatherer.util.Utility;
34import org.greenstone.gatherer.util.WinRegistry;
35import org.w3c.dom.*;
36
37public class FileAssociationManager {
38 static final public String FILENAME_ARG = "%1";
39 static final private String DATA_FILENAME = "associations.xml";
40 static final private String ENTRY_ELEMENT = "Entry";
41 static final private String ESCAPE = "\\\\"; // '\'
42 static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\'
43 static final private String EXTENSION_ATTRIBUTE = "extension";
44 private Document document;
45 private File data_file;
46
47 public FileAssociationManager() {
48 // If a associations.xml is available in the GLI install directory load it.
49 data_file = new File(Utility.BASE_DIR + DATA_FILENAME);
50 if(data_file.exists()) {
51 document = Utility.parse(data_file, true);
52 }
53 // Load the default associations xml data file. This can be done using the classloader.
54 else {
55 document = Utility.parse(Utility.XML_DIRECTORY + DATA_FILENAME, true);
56 }
57 // Initialize the associations. This involves looking through all current associations searching for those with a command of "".
58 if(document != null) {
59 NodeList entries = (document.getDocumentElement()).getElementsByTagName(ENTRY_ELEMENT);
60 for(int i = 0; i < entries.getLength(); i++) {
61 Element entry = (Element) entries.item(i);
62 String extension = entry.getAttribute(EXTENSION_ATTRIBUTE);
63 String command = MSMUtils.getValue(entry);
64 // If we encounter a command of "", and we are on windows, we try to automatically set this command.
65 if(command.length() == 0 && Utility.isWindows()) {
66 // Create a dummy filename with the appropriate extension
67 String dummy_file = "dummy." + extension;
68 command = WinRegistry.openCommand(dummy_file);
69 // If this succeeded add the association.
70 if(command != null) {
71 // Remember to replace the dummy filename with %1
72 command = command.replaceAll(dummy_file, FILENAME_ARG);
73 // Replace the text in this node.
74 MSMUtils.setValue(entry, command);
75 }
76 dummy_file = null;
77 }
78 command = null;
79 extension = null;
80 entry = null;
81 }
82 entries = null;
83 }
84 else {
85 Gatherer.println("Didn't parse anything. About to crash.");
86 }
87 }
88
89 public void edit() {
90 FileAssociationDialog dialog = new FileAssociationDialog(this);
91 dialog.display(null);
92 dialog = null;
93 }
94
95 public String getCommand(File file) {
96 String command = null;
97 if(file.isFile()) {
98 // Determine extension
99 String filename = file.getAbsolutePath();
100 String extension = filename.substring(filename.lastIndexOf(".") + 1);
101 // Try to retrieve a value from cache
102 Element entry = getCommand(extension);
103 if(entry != null) {
104 command = MSMUtils.getValue(entry);
105 if(command == null || command.length() == 0) {
106 // If command is null, and we are on windows try searching the registry.
107 if(Utility.isWindows()) {
108 command = WinRegistry.openCommand(filename);
109 }
110 // Otherwise display the dialog and ask the user to enter launching command.
111 if(command == null || command.length() == 0) {
112 // Show the dialog which forces a user to select the launch command for a certain file.
113 FileAssociationDialog dialog = new FileAssociationDialog(this);
114 command = dialog.display(extension);
115 dialog = null;
116 }
117 // Hopefully by now we have a command, or else we're never going to get one. Add the association.
118 if(command != null) {
119 // If no previous entry existed create one.
120 if(entry == null) {
121 entry = document.createElement(ENTRY_ELEMENT);
122 entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
123 document.getDocumentElement().appendChild(entry);
124 }
125 // Replace the text in this node. Remember to replace the dummy filename with %1
126 MSMUtils.setValue(entry, command.replaceAll(filename, FILENAME_ARG));
127 }
128 }
129 if(command != null) {
130 // We have to fix filename under windows to escape the backslashes.
131 filename = filename.replaceAll(ESCAPE, ESCAPED_ESCAPE);
132 // Replace %1 with the appropriate filename
133 command = command.replaceAll(FILENAME_ARG, filename);
134 }
135 }
136 entry = null;
137 extension = null;
138 filename = null;
139 }
140 return command;
141 }
142
143 public Element getCommand(String target_extension) {
144 NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
145 for(int i = 0; i < entries.getLength(); i++) {
146 Element entry = (Element) entries.item(i);
147 String extension = entry.getAttribute(EXTENSION_ATTRIBUTE);
148 if(extension.equalsIgnoreCase(target_extension)) {
149 entries = null;
150 extension = null;
151 return entry;
152 }
153 }
154 entries = null;
155 return null;
156 }
157
158 public String getCommandString(String target_extension) {
159 Element entry = getCommand(target_extension);
160 if(entry != null) {
161 return MSMUtils.getValue(entry);
162 }
163 else {
164 return "";
165 }
166 }
167
168 public String getExtension(int index) {
169 NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
170 if(0 <= index && index < entries.getLength()) {
171 Element entry = (Element) entries.item(index);
172 return entry.getAttribute(EXTENSION_ATTRIBUTE);
173 }
174 return "";
175 }
176
177 public void save() {
178 Utility.export(document, data_file);
179 }
180
181 public void setCommand(String extension, String command) {
182 Gatherer.println("Set Launch: " + extension + " with " + command);
183 // Retrieve any existing entry for this extension
184 Element entry = getCommand(extension);
185 // If no previous entry existed create one.
186 if(entry == null) {
187 entry = document.createElement(ENTRY_ELEMENT);
188 entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
189 document.getDocumentElement().appendChild(entry);
190 }
191 // Replace the text in this node. If the user has used filename instead of %1 then too bad.
192 MSMUtils.setValue(entry, command);
193 entry = null;
194 }
195
196 public int size() {
197 NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
198 return entries.getLength();
199 }
200}
Note: See TracBrowser for help on using the repository browser.