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

Last change on this file since 4366 was 4366, checked in by kjdon, 21 years ago

re-tabbed the code for java

  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1package org.greenstone.gatherer.file;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.io.*;
39import java.lang.*;
40import java.nio.charset.*;
41import java.util.*;
42import javax.swing.*;
43import org.greenstone.gatherer.Gatherer;
44import org.greenstone.gatherer.gui.FileAssociationDialog;
45import org.greenstone.gatherer.msm.MSMUtils;
46import org.greenstone.gatherer.util.Utility;
47import org.greenstone.gatherer.util.WinRegistry;
48import org.w3c.dom.*;
49/** Provides a manager for relating a filetype to a command to open that filetype, and an optional icon. Under windows this will attempt to leverage the Windows Registry. If this fails, or under other platforms, the user will be prompted to enter the required details manually. This information will then be stored for any further sessions of the Gatherer. Of course the ability to edit these settings must also be provided. */
50public class FileAssociationManager
51 extends HashMap {
52
53 static private final File ASSOC_FILE = new File(Utility.BASE_DIR, "associations.xml");
54
55 public FileAssociationManager() {
56 // Attempt to load any previous file associations
57 if(ASSOC_FILE.exists()) {
58 load();
59 }
60 }
61
62 public void destroy() {
63 save();
64 }
65
66 public String getCommand(File file) {
67 String command = null;
68 String filename = file.toString();
69 // Determine extension
70 int index = -1;
71 if((index = filename.lastIndexOf(".")) != -1) {
72 String extension = filename.substring(index + 1);
73 command = getCommand(file, extension);
74 }
75 return command;
76 }
77
78 public String getCommand(File file, String extension) {
79 // We first check to see if we already know the answer to how to run this program.
80 String command = (String) get(extension);
81 if(command != null) {
82 command = command + " " + file.getAbsolutePath();
83 }
84 // If not, and we are on windows, try the registry.
85 if(command == null && Utility.isWindows()) {
86 command = WinRegistry.openCommand(file.getAbsolutePath());
87 }
88 // Failing all that prompt the user to select the program to open this with.
89 if(command == null) {
90 FileAssociationDialog dialog = new FileAssociationDialog(this);
91 command = dialog.display(extension);
92 dialog.destroy();
93 dialog = null;
94 if(command != null) {
95 put(extension, command);
96 command = command + " " + file.getAbsolutePath();
97 }
98 }
99 ///ystem.err.println("Get command: " + extension + " = " + command);
100 return command;
101 }
102
103 public String getCommandImmediately(String extension) {
104 return (String) get(extension);
105 }
106
107 public void edit() {
108 FileAssociationDialog dialog = new FileAssociationDialog(this);
109 dialog.display(null);
110 dialog.destroy();
111 dialog = null;
112 }
113
114 private void load() {
115 // Read in and parse the document.
116 Document document = Utility.parse(ASSOC_FILE, false);
117 if(document != null) {
118 // Recover all the entry nodes of the association root.
119 for(Node entry_n = document.getDocumentElement().getFirstChild(); entry_n != null; entry_n = entry_n.getNextSibling()) {
120 if(entry_n.getNodeName().equals("Entry")) {
121 Element entry_e = (Element) entry_n;
122 String extension = entry_e.getAttribute("extension");
123 String command = MSMUtils.getValue(entry_e);
124 put(extension, command);
125 extension = null;
126 command = null;
127 entry_e = null;
128 }
129 }
130 }
131 }
132
133 private void save() {
134 // Only save if there is something to save.
135 if(size() > 0) {
136 StringBuffer assoc = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
137 assoc.append("<!DOCTYPE Associations [\n");
138 assoc.append(" <!ELEMENT Associations (Entry*)>\n");
139 assoc.append(" <!ELEMENT Entry (#PCDATA)>\n");
140 assoc.append(" <!ATTLIST Entry\n");
141 assoc.append(" extension CDATA #REQUIRED>\n");
142 assoc.append("]>\n");
143 assoc.append("\n");
144 assoc.append("<Associations>\n");
145 for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
146 String extension = (String) keys.next();
147 String command = (String) get(extension);
148 assoc.append("\t<Entry extension=\"");
149 assoc.append(extension);
150 assoc.append("\">");
151 assoc.append(command);
152 assoc.append("</Entry>\n");
153 }
154 assoc.append("</Associations>");
155
156 String assoc_str = assoc.toString();
157 assoc = null;
158 try {
159 FileOutputStream fos = new FileOutputStream(ASSOC_FILE);
160 OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("UTF-8"));
161 int position = 0;
162 int BLOCK_SIZE = 1024;
163 // Write x block sized chunks to file.
164 while(position + BLOCK_SIZE < assoc_str.length()) {
165 osw.write(assoc_str, position, BLOCK_SIZE);
166 position = position + BLOCK_SIZE;
167 }
168 // Write the remainder of the buffer.
169 if(position < assoc_str.length()) {
170 osw.write(assoc_str, position, assoc_str.length() - position);
171 }
172 osw.flush();
173 osw.close();
174 osw = null;
175 fos.close();
176 fos = null;
177 assoc_str = null;
178 }
179 catch (Exception error) {
180 Gatherer.printStackTrace(error);
181 }
182 }
183 }
184
185 public void setCommand(String extension, String command) {
186 ///ystem.err.println("Set command: " + extension + " = " + command);
187 if(command == null || command.length() == 0) {
188 remove(extension);
189 }
190 else {
191 put(extension, command);
192 }
193 }
194}
195
196
197
Note: See TracBrowser for help on using the repository browser.