source: trunk/gli/src/org/greenstone/gatherer/msm/GreenstoneArchiveParser.java@ 6539

Last change on this file since 6539 was 6539, checked in by jmt12, 20 years ago

Heres a bunch of other changed files. If it wasn't a Friday afternoon I might be bothered finding out what I actually changed in them. Such changes include: a new option or three on preferences, a bug fix for the GDM classes, several changes to CDM to allow for G2.39 configuration files, a fix to Codec to allow for quotes in format strings and more work on CommandTokenizer to allow for stupid, stupid, stupid collectionextra's starting with speech marks then a new line. Plus other stuff. And things. Peace Out.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.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.net.*;
41import java.util.*;
42import org.greenstone.gatherer.Configuration;
43import org.greenstone.gatherer.Dictionary;
44import org.greenstone.gatherer.Gatherer;
45import org.greenstone.gatherer.collection.Collection;
46import org.greenstone.gatherer.collection.CollectionManager;
47import org.greenstone.gatherer.file.FileNode;
48import org.greenstone.gatherer.msm.ElementWrapper;
49import org.greenstone.gatherer.msm.MetadataSet;
50import org.greenstone.gatherer.msm.MetadataSetManager;
51import org.greenstone.gatherer.msm.MSMUtils;
52import org.greenstone.gatherer.shell.GShell;
53import org.greenstone.gatherer.shell.GShellProgressMonitor;
54import org.greenstone.gatherer.util.StaticStrings;
55import org.greenstone.gatherer.util.Utility;
56import org.greenstone.gatherer.valuetree.GValueModel;
57import org.greenstone.gatherer.valuetree.GValueNode;
58import org.w3c.dom.*;
59
60public class GreenstoneArchiveParser {
61
62 private GShell shell;
63
64 static final String ignore_list[] = {"assocfilepath", "gsdl", "Identifier", "URL"}; //"Source",
65
66 public GreenstoneArchiveParser(GShellProgressMonitor progress, GShell shell) {
67 // We can only extract metadata if an extracted metadata set exists in our collection.
68 if(Gatherer.c_man.msm.getSet(Utility.EXTRACTED_METADATA_NAMESPACE) != null) {
69 this.shell = shell;
70 // The very firstist thing we do is remove any existing extracted metadata
71 if(Gatherer.c_man != null && Gatherer.c_man.getCollection() != null) {
72 Gatherer.c_man.getCollection().getGDM().removeExtractedMetadata();
73 }
74
75 // Determine the collection archive directory.
76 File archive_directory = new File(Gatherer.c_man.getCollectionArchive());
77 // For each of the hash coded directories within.
78 File document_directories[] = archive_directory.listFiles();
79 for(int i = 0; i < document_directories.length; i++) {
80 // Find the doc.xml file within
81 if(document_directories[i].isDirectory()) {
82 File document_file = new File(document_directories[i], "doc.xml");
83 // Then extract the metadata from it.
84 if(document_file.exists()) {
85 int count = extractMetadata(document_file);
86 // Display a pretty progress message.
87 String[] args = new String[2];
88 args[0] = document_directories[i].getName();
89 args[1] = String.valueOf(count);
90 shell.fireMessage(GShell.IMPORT, shell.typeAsString(GShell.IMPORT) + "> " + Dictionary.get("GShell.Extracted", args), GShell.OK, null);
91 args = null;
92 progress.increment();
93 }
94 }
95 }
96 }
97 // All done. Outta here like a bald man.
98 }
99
100 private int extractMetadata(File file) {
101 int count = 0;
102 // Retrieve the DOM of the file.
103 Document document = Utility.parse(file, false);
104
105 Gatherer.println("Parsed greenstone archive document: " + file.getAbsolutePath());
106 // If we successfully parsed the document, then it is time to search through the DOM for the Metadata tags.
107 if(document != null) {
108 String file_path = null;
109 Element archive_element = document.getDocumentElement();
110 // Retrieve all of the Metadata sections.
111 NodeList metadata_elements = archive_element.getElementsByTagName("Metadata");
112 // We first zip through the retrieved metadata, and if we encounter the element 'SourceSegment' - a sure sign this collection came from a bibliographic type file - we break out of extracted metadata parsing as no sense could be made of the data extracted anyway (plus we suffer a death of thirty-thousand pointy bits of metadata!)
113 for(int i = 0; i < metadata_elements.getLength(); i++) {
114 Element metadata_element = (Element) metadata_elements.item(i);
115 String name = metadata_element.getAttribute("name");
116 if(name.equalsIgnoreCase(StaticStrings.SOURCESEGMENT_VALUE)) {
117 return 0;
118 }
119 }
120 // Now for each Metadata entry retrieved...
121 for(int i = 0; i < metadata_elements.getLength(); i++) {
122 Element metadata_element = (Element) metadata_elements.item(i);
123 String name = metadata_element.getAttribute("name");
124 // There is also a special case when the metadata name is gsdlsourcefilename, as we use this to find the FileRecord we want to add metadata to.
125 if(name.equals("gsdlsourcefilename")) {
126 file_path = MSMUtils.getValue(metadata_element);
127 }
128 else {
129 // Check if its name starts with, or is equal to, one of the values in our ignore list, and if so ignore this metadata.
130 boolean ignore = false;
131 for(int j = 0; !ignore && j < ignore_list.length; j++) {
132 ignore = name.startsWith(ignore_list[j]);
133 }
134 // Otherwise ensure the metadata is present in our collection.
135 if(!ignore && file_path != null) {
136 // If we successfully retrieved a record we can continue.
137 if(file_path != null) {
138 // We now retrieve the appropriate element. If no such element exists we create a new one in the greenstone mds. Remember that no element in the greenstone mds has an associated value tree, so it is perfect for metadata elements with a small number of repeated values but where the values have no relation between files (such as encoding, where many files will be iso_8859_1, but if you change one you don't intend to change them all).
139 ElementWrapper element = Gatherer.c_man.msm.getElement(name);
140 if(element == null) {
141 MetadataSet extracted_mds = Gatherer.c_man.msm.getSet(Utility.EXTRACTED_METADATA_NAMESPACE);
142 if(extracted_mds != null) {
143 element = extracted_mds.addElement(name, Gatherer.config.getLanguage());
144 }
145 }
146 // If we successfully retrieved an element (and we should have) we can continue.
147 // WARNING!! There is one known exception - MARC records. Adding the extracted elements is all good, but adding the extracted metadata causes the whole thing to collapse in a pile of unhappy.
148 if(element != null && !file_path.endsWith(StaticStrings.MARC_EXTENSION) && (element.getNamespace().equals("") || element.getNamespace().equals(Utility.EXTRACTED_METADATA_NAMESPACE))) {
149 // Retrieve the metadata for the current file
150 File target_file = new File(file_path);
151 String value = "";
152 try {
153 value = Utility.decodeGreenstone(URLDecoder.decode(MSMUtils.getValue(metadata_element), "UTF-8"));
154 }
155 catch(UnsupportedEncodingException error) {
156 Gatherer.printStackTrace(error);
157 }
158 // If we successfully retrieved a value we can continue.
159 if(value != null) {
160 // Create a new metadata object.
161 GValueModel value_tree = Gatherer.c_man.msm.getValueTree(element);
162 GValueNode value_node = null;
163 if(value_tree != null) {
164 value_node = value_tree.getValue(value);
165 }
166 else {
167 value_node = new GValueNode(element.toString(), value);
168 }
169 Metadata metadata = new Metadata(element, value_node);
170 element.inc();
171 ///ystem.err.println("Adding extracted metadata: " + metadata);
172 Gatherer.c_man.getCollection().msm.fireMetadataChanged(new MSMEvent(this, System.currentTimeMillis(), target_file, null, metadata));
173 count++;
174 // All done. On to next metadata.
175 }
176 value = null;
177 target_file = null;
178 }
179 else {
180 Gatherer.println("Cannot retrieve metadata element " + name);
181 }
182 }
183 }
184 }
185 }
186 }
187 return count;
188 }
189
190 static final String metadata_ignore_list[] = {"assocfilepath", "gsdl", "Identifier","URL"};
191
192 static public ArrayList extractMetadataElements(File archive_directory) {
193 ArrayList extracted_metadata_elements = new ArrayList();
194 File document_directories[] = archive_directory.listFiles();
195 for(int i = 0; i < document_directories.length; i++) {
196 // Find the doc.xml file within
197 if(document_directories[i].isDirectory()) {
198 File document_file = new File(document_directories[i], "doc.xml");
199 // Then extract the metadata from it.
200 if(document_file.exists()) {
201 try {
202 Document document = Utility.parse(document_file, false);
203 // Retrieve all of the Metadata sections.
204 Element archive_element = document.getDocumentElement();
205 NodeList metadata_elements = archive_element.getElementsByTagName("Metadata");
206 // Now for each Metadata entry retrieved...
207 for(int j = 0; j < metadata_elements.getLength(); j++) {
208 Element metadata_element = (Element) metadata_elements.item(j);
209 String name = metadata_element.getAttribute("name");
210 // Check if its name starts with, or is equal to, one of the values in our ignore list, and if so ignore this metadata.
211 boolean ignore = false;
212 for(int k = 0; !ignore && k < metadata_ignore_list.length; k++) {
213 ignore = name.startsWith(metadata_ignore_list[k]);
214 }
215 if(!ignore && !extracted_metadata_elements.contains(name)) {
216 extracted_metadata_elements.add(name);
217 }
218 name = null;
219 metadata_element = null;
220 }
221 metadata_elements = null;
222 archive_element = null;
223 document = null;
224 }
225 catch (Exception error) {
226 Gatherer.printStackTrace(error);
227 }
228 }
229 document_file = null;
230 }
231 }
232 document_directories = null;
233 return extracted_metadata_elements;
234 }
235}
Note: See TracBrowser for help on using the repository browser.