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

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

Added a reminder so no-one tries to remove GreenstoneMetadataParser.java again.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 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.Gatherer;
43import org.greenstone.gatherer.file.FileNode;
44import org.greenstone.gatherer.msm.MetadataParser;
45import org.greenstone.gatherer.util.Utility;
46/** 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.
47 * @author John Thompson, Greenstone Digital Library, University of Waikato
48 * @version 2.3
49 */
50public class ExistingMetadataLoader {
51 /** A list of currently loaded metadata parsers. */
52 private ArrayList parsers = null;
53 /** Constructor.
54 * @see org.greenstone.gatherer.msm.MetadataParser
55 * @see org.greenstone.gatherer.util.Utility
56 */
57 public ExistingMetadataLoader() {
58 this.parsers = new ArrayList();
59 ArrayList load_these = new ArrayList();
60 // Find any metadata parsers found in parsers directory
61 File file = new File(Utility.BASE_DIR + "classes" + File.separator + "org" + File.separator + "greenstone" + File.separator + "gatherer" + File.separator + "msm" + File.separator + "parsers");
62 File children[] = file.listFiles();
63 for(int i = 0; children != null && i < children.length; i++) {
64 String name = children[i].getName();
65 if(name.endsWith(".class") && name.indexOf("$") == -1) {
66 name = name.substring(0, name.length() - 6);
67 load_these.add("org.greenstone.gatherer.msm.parsers." + name);
68 Gatherer.println("Loaded metadata parser: " + name);
69 }
70 name = null;
71 }
72 children = null;
73 file = null;
74 // Find any metadata parsers found in the jar file (if present)
75 File jar_file = new File(Utility.GLI_ARCHIVE);
76 if(jar_file.exists()) {
77 try {
78 JarFile jar = new JarFile(jar_file);
79 for(Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
80 String name = entries.nextElement().toString();
81 // !!!! THIS CLASS USES parsers/GreenstoneMetadataParser.java !!!!
82 if(name.startsWith("org/greenstone/gatherer/msm/parsers/") && name.endsWith(".class") && name.indexOf("$") == -1) {
83 name = name.substring(0, name.length() - 6);
84 name = name.replace('/', '.');
85 if(!load_these.contains(name)) {
86 load_these.add(name);
87 Gatherer.println("Loaded metadata parser: " + name);
88 }
89 }
90 name = null;
91 }
92 jar = null;
93 }
94 catch (Exception error) {
95 error.printStackTrace();
96 }
97 }
98 jar_file = null;
99 // Load create instances of the parsers found.
100 for(int i = 0; i < load_these.size(); i++) {
101 try {
102 String parser_name = (String) load_these.get(i);
103 Class custom_class = Class.forName((String)load_these.get(i));
104 MetadataParser custom_parser = (MetadataParser)custom_class.newInstance();
105 parsers.add(custom_parser);
106 custom_parser = null;
107 custom_class = null;
108 parser_name = null;
109 }
110 catch (Exception error) {
111 error.printStackTrace();
112 }
113 }
114 }
115 /** Locates and assigns metadata for the given file by sequentially applying all active metadata parsers. */
116 public boolean searchForMetadata(FileNode destination, FileNode source, boolean folder_level, boolean dummy_run) {
117 boolean dialog_cancelled = false;
118 int size = parsers.size();
119 for(int i = 0; !dialog_cancelled && i < size; i++) {
120 MetadataParser parser = (MetadataParser) parsers.get(i);
121 dialog_cancelled = parser.process(destination, source, folder_level, dummy_run);
122 parser = null;
123 }
124 return dialog_cancelled;
125 }
126}
Note: See TracBrowser for help on using the repository browser.