source: trunk/gli/src/org/greenstone/gatherer/msm/ExistingMetadataLoader.java@ 8236

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.msm;
38
39import java.io.*;
40import java.util.*;
41import java.util.jar.*;
42import org.greenstone.gatherer.DebugStream;
43import org.greenstone.gatherer.Gatherer;
44import org.greenstone.gatherer.file.FileNode;
45import org.greenstone.gatherer.msm.MetadataParser;
46import org.greenstone.gatherer.util.Utility;
47/** Attempts to locate and import any metadata already attatched to files about to be imported. This entire function is part of a critical file copy and so must be fast and reliable (or at least recover nicely when things go wrong). Futhermore this process must be aware of concerns such as previous greenstone collections having metadata sets that could be immediately imported, or hierarchy files needed to turn indexes into something more meaningful. We also must allow for the extensibility of this class to include custom metadata objects such as MARK records etc.
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.3
50 */
51public class ExistingMetadataLoader {
52 /** A list of currently loaded metadata parsers. */
53 private ArrayList parsers = null;
54 /** Constructor.
55 * @see org.greenstone.gatherer.msm.MetadataParser
56 * @see org.greenstone.gatherer.util.Utility
57 */
58 public ExistingMetadataLoader() {
59 this.parsers = new ArrayList();
60 ArrayList load_these = new ArrayList();
61 // Find any metadata parsers found in parsers directory
62 File file = new File(Utility.BASE_DIR + "classes" + File.separator + "org" + File.separator + "greenstone" + File.separator + "gatherer" + File.separator + "msm" + File.separator + "parsers");
63 File children[] = file.listFiles();
64 for(int i = 0; children != null && i < children.length; i++) {
65 String name = children[i].getName();
66 if(name.endsWith(".class") && name.indexOf("$") == -1) {
67 name = name.substring(0, name.length() - 6);
68 load_these.add("org.greenstone.gatherer.msm.parsers." + name);
69 DebugStream.println("Loaded metadata parser: " + name);
70 }
71 name = null;
72 }
73 children = null;
74 file = null;
75 // Find any metadata parsers found in the jar file (if present)
76 File jar_file = new File(Utility.GLI_ARCHIVE);
77 if(jar_file.exists()) {
78 try {
79 JarFile jar = new JarFile(jar_file);
80 for(Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
81 String name = entries.nextElement().toString();
82 // !! THIS CLASS USES parsers/GreenstoneMetadataParser.java !!
83 if(name.startsWith("org/greenstone/gatherer/msm/parsers/") && name.endsWith(".class") && name.indexOf("$") == -1) {
84 name = name.substring(0, name.length() - 6);
85 name = name.replace('/', '.');
86 if(!load_these.contains(name)) {
87 load_these.add(name);
88 DebugStream.println("Loaded metadata parser: " + name);
89 }
90 }
91 name = null;
92 }
93 jar = null;
94 }
95 catch (Exception error) {
96 error.printStackTrace();
97 }
98 }
99 jar_file = null;
100 // Load create instances of the parsers found.
101 for(int i = 0; i < load_these.size(); i++) {
102 try {
103 String parser_name = (String) load_these.get(i);
104 Class custom_class = Class.forName((String)load_these.get(i));
105 MetadataParser custom_parser = (MetadataParser)custom_class.newInstance();
106 parsers.add(custom_parser);
107 custom_parser = null;
108 custom_class = null;
109 parser_name = null;
110 }
111 catch (Exception error) {
112 error.printStackTrace();
113 }
114 }
115 }
116 /** Locates and assigns metadata for the given file by sequentially applying all active metadata parsers. */
117 public boolean searchForMetadata(FileNode destination, FileNode source, boolean folder_level, boolean dummy_run) {
118 boolean dialog_cancelled = false;
119 int size = parsers.size();
120 for(int i = 0; !dialog_cancelled && i < size; i++) {
121 MetadataParser parser = (MetadataParser) parsers.get(i);
122 dialog_cancelled = parser.process(destination, source, folder_level, dummy_run);
123 parser = null;
124 }
125 return dialog_cancelled;
126 }
127}
Note: See TracBrowser for help on using the repository browser.