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

Last change on this file since 6318 was 5581, checked in by mdewsnip, 21 years ago

Many formatting, structural and code improvements.

  • 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.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 if(name.startsWith("org/greenstone/gatherer/msm/parsers/") && name.endsWith(".class") && name.indexOf("$") == -1) {
82 name = name.substring(0, name.length() - 6);
83 name = name.replace('/', '.');
84 if(!load_these.contains(name)) {
85 load_these.add(name);
86 Gatherer.println("Loaded metadata parser: " + name);
87 }
88 }
89 name = null;
90 }
91 jar = null;
92 }
93 catch (Exception error) {
94 error.printStackTrace();
95 }
96 }
97 jar_file = null;
98 // Load create instances of the parsers found.
99 for(int i = 0; i < load_these.size(); i++) {
100 try {
101 String parser_name = (String) load_these.get(i);
102 Class custom_class = Class.forName((String)load_these.get(i));
103 MetadataParser custom_parser = (MetadataParser)custom_class.newInstance();
104 parsers.add(custom_parser);
105 custom_parser = null;
106 custom_class = null;
107 parser_name = null;
108 }
109 catch (Exception error) {
110 error.printStackTrace();
111 }
112 }
113 }
114 /** Locates and assigns metadata for the given file by sequentially applying all active metadata parsers. */
115 public boolean searchForMetadata(FileNode destination, FileNode source, boolean folder_level, boolean dummy_run) {
116 boolean dialog_cancelled = false;
117 int size = parsers.size();
118 for(int i = 0; !dialog_cancelled && i < size; i++) {
119 MetadataParser parser = (MetadataParser) parsers.get(i);
120 dialog_cancelled = parser.process(destination, source, folder_level, dummy_run);
121 parser = null;
122 }
123 return dialog_cancelled;
124 }
125}
Note: See TracBrowser for help on using the repository browser.