source: trunk/gli/src/org/greenstone/gatherer/msm/MSMProfiler.java@ 4595

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

tidied up gatherer.debug - > gatherer.println

  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1package org.greenstone.gatherer.msm;
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.File;
39import java.io.FileInputStream;
40import java.io.FileOutputStream;
41import java.io.InputStream;
42import java.io.OutputStreamWriter;
43import java.util.ArrayList;
44import java.util.HashMap;
45import java.util.Iterator;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.msm.Declarations;
48import org.greenstone.gatherer.msm.MSMAdapter;
49import org.greenstone.gatherer.util.HashMap3D;
50import org.greenstone.gatherer.util.Utility;
51import org.w3c.dom.Document;
52import org.w3c.dom.Element;
53import org.w3c.dom.NodeList;
54/** This class is essentially records what actions a user took when merging two metadata sets, so we can avoid having to get the user to repeat the proceedure.
55 * @author John Thompson, Greenstone Digital Library, University of Waikato
56 * @version 2.2
57 */
58public class MSMProfiler
59 extends MSMAdapter {
60 /** The file the profile should be loaded from and saved to. */
61 private File profile_file = null;
62 /** A mapping of known profile actions. */
63 private HashMap3D profiles = null;
64 static final private String IGNORE = "\nIGNORE\n";
65 /** The constructor loads the previous profile for this collection if there is one.
66 * @see org.greenstone.gatherer.collection.CollectionManager
67 * @see org.greenstone.gatherer.msm.MetadataSetManager
68 */
69 public MSMProfiler() {
70 this.profile_file = new File(Gatherer.c_man.getCollectionMetadata(), "profile.xml");
71 this.profiles = new HashMap3D();
72 // Load an existing profile if there is one.
73 if(profile_file.exists()) {
74 Document document = Utility.parse(profile_file, false);
75 if(document != null) {
76 load(document);
77 document = null;
78 }
79 }
80 }
81 /** Adds a new action mapping to the profile. Such a mapping records that for a certain collection (file) and metadata element a specific action must occur. The action is either a renaming to a new metadata element name, also provided, or an instruction to ignore this particular metadata element, is the target is null.
82 * @param collection_file The aboslute path name to the collection where the metadata was sourced, as a <strong>String</strong>.
83 * @param source A <strong>String</strong> containing the fully qualified name of the source metadata element.
84 * @param target Another <strong>String</strong> which is either the fully qualified name of the target element, or <i>null</i> if this is actually an ignore action addition.
85 */
86 public void addAction(String collection_file, String source, String target) {
87 if(target == null) {
88 target = IGNORE;
89 }
90 profiles.put(collection_file, source, target);
91 }
92
93 public void addSource(String collection_file) {
94 profiles.put(collection_file, new HashMap());
95 }
96
97 /** Determine if an action exists for the given collection and source.
98 * @param collection_file The aboslute path name to the collection where the metadata was sourced, as a <strong>String</strong>.
99 * @param source A <strong>String</strong> containing the fully qualified name of the source metadata element.
100 * @return <i>true</i> if such an action exists, <i>false</i> otherwise.
101 */
102 public boolean containsAction(String collection_file, String source) {
103 return profiles.contains(collection_file, source);
104 }
105
106 public boolean containsSource(String collection_file) {
107 return profiles.containsKey(collection_file);
108 }
109
110 /** Destructor to ensure that no memory leaks. */
111 public void destroy() {
112 //save();
113 profiles.clear();
114 profile_file = null;
115 profiles = null;
116 }
117 /** Method that is called whenever an element within a set is changed or modified, in which case we must modify the action profiles to suit. If an element was removed, remove all action profiles with this element as the target. If an elements name changes, update all matching targets to reflect new name.
118 * @param event A <strong>MSMEvent</strong> containing details of the event that caused this message to be fired.
119 */
120 public void elementChanged(MSMEvent event) {
121 Gatherer.println("Element changed: " + event);
122 String new_name = null;
123 String old_name = event.getValue();
124 ElementWrapper element = event.getElement();
125 boolean delete = false;
126 boolean rename = false;
127 // First we check if the element has been removed.
128 if(element == null) {
129 delete = true;
130 }
131 // Next we determine if its name has changed
132 else if(!(new_name = element.toString()).equals(old_name)) {
133 rename = true;
134 }
135 // Perform any action we need to.
136 if(delete || rename) {
137 Iterator iterator_value_one = profiles.values().iterator();
138 while(iterator_value_one.hasNext()) {
139 HashMap map = (HashMap) iterator_value_one.next();
140 Iterator iterator_key_two = map.keySet().iterator();
141 while(iterator_key_two.hasNext()) {
142 String key_two = (String) iterator_key_two.next();
143 String value = (String) map.get(key_two);
144 if(value.equals(old_name)) {
145 if(delete) {
146 map.remove(key_two);
147 }
148 else {
149 map.put(key_two, new_name);
150 }
151 }
152 key_two = null;
153 value = null;
154 }
155 map = null;
156 iterator_key_two = null;
157 }
158 iterator_value_one = null;
159 }
160 // Otherwise nothing for us to do (must be an add, or possibly a move if I can ever get round to the editor).
161 element = null;
162 old_name = null;
163 new_name = null;
164 }
165 /** Search the profilerer for any previous actions regarding the indicated collection and metadata element.
166 * @param collection_file The absolute path name to the collection where the metadata was sourced, as a <strong>String</strong>.
167 * @param source A <strong>String</strong> containing the fully qualified name of the source metadata element.
168 * @return The fully qualified name of the target metadata element, as a <strong>String</strong>, or <i>null</i> if we are to ignore this metadata. Note that the target elements name may be exactly the source elements one.
169 * @see org.greenstone.gatherer.msm.Declarations
170 */
171 public String getAction(String collection_file, String source) {
172 ///atherer.println("Get action.");
173 String result = (String) profiles.get(collection_file, source);
174 if(result.equals(IGNORE)) {
175 result = null;
176 }
177 return result;
178 }
179
180 public HashMap getActions(String collection_file) {
181 return (HashMap) profiles.get(collection_file);
182 }
183
184 public ArrayList getCollections() {
185 return new ArrayList(profiles.keySet());
186 }
187
188 public ArrayList getSources(String collection_file) {
189 ArrayList result = new ArrayList();
190 HashMap map = (HashMap) profiles.get(collection_file);
191 if(map != null) {
192 result.addAll(map.keySet());
193 }
194 return result;
195 }
196
197 public void removeProfile(String collection_file) {
198 profiles.remove(collection_file);
199 }
200
201 public void removeAction(String collection_file, String source) {
202 HashMap map = (HashMap) profiles.get(collection_file);
203 if(map != null) {
204 map.remove(source);
205 }
206 }
207
208 /** Save the profile document. */
209 public void save() {
210 // While we're at it save the profiles.
211 try {
212 // Create backup.
213 if(profile_file.exists()) {
214 File backup = new File(profile_file.getAbsolutePath() + "~");
215 backup.deleteOnExit();
216 if(!profile_file.renameTo(backup)) {
217 Gatherer.println("Error in MSMProfiler.save(): FileRenamedException");
218 }
219 backup = null;
220 }
221
222 // read in the default profile file - has all the dtd in it
223 Document document = Utility.parse(Utility.PROFILE_TEMPLATE, true);
224 if(document == null) {
225 Gatherer.println("Error in MSMProfiler.save(): couldn't find and parse the profile template file!");
226 return;
227 }
228
229
230 // Add the current profile info into the document
231 Element profile_elem = document.getDocumentElement();
232
233 Iterator iterator_key_one = profiles.keySet().iterator();
234 while(iterator_key_one.hasNext()) {
235 String key_one = (String) iterator_key_one.next();
236 HashMap map = (HashMap) profiles.get(key_one);
237 Iterator iterator_key_two = map.keySet().iterator();
238 while(iterator_key_two.hasNext()) {
239 String key_two = (String) iterator_key_two.next();
240 String value = (String) map.get(key_two);
241 Element action = document.createElement("Action");
242 profile_elem.appendChild(action);
243 action.setAttribute("collection", key_one);
244 action.setAttribute("source", key_two);
245 action.setAttribute("target", value);
246 key_two = null;
247 value = null;
248 }
249 key_one = null;
250 map = null;
251 iterator_key_two = null;
252 }
253 iterator_key_one = null;
254
255 // Now write it to file.
256 Utility.export(document, profile_file);
257 }
258 catch (Exception error) {
259 Gatherer.printStackTrace(error);
260 }
261 }
262 /** Reload the mapping information stored in this document.
263 * @param document The <strong>Document</strong> to parse.
264 */
265 private void load(Document document) {
266 Element root = document.getDocumentElement();
267 NodeList actions = root.getElementsByTagName("Action");
268 for(int i = 0; i < actions.getLength(); i++) {
269 Element action = (Element) actions.item(i);
270 String collection = action.getAttribute("collection");
271 String source = action.getAttribute("source");
272 String target = action.getAttribute("target");
273 if(collection.length() > 0 && source.length() > 0) {
274 if(target.length() == 0) {
275 target = null;
276 }
277 profiles.put(collection, source, target);
278 }
279 action = null;
280 collection = null;
281 source = null;
282 target = null;
283 }
284 root = null;
285 actions = null;
286 }
287}
Note: See TracBrowser for help on using the repository browser.