package org.greenstone.gatherer.msm;
/**
*#########################################################################
*
* A component of the Gatherer application, part of the Greenstone digital
* library suite from the New Zealand Digital Library Project at the
* University of Waikato, New Zealand.
*
*
*
* Author: John Thompson, Greenstone Digital Library, University of Waikato
*
*
*
* Copyright (C) 1999 New Zealand Digital Library Project
*
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*########################################################################
*/
import java.io.*;
import java.net.*;
import java.util.*;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.collection.Collection;
import org.greenstone.gatherer.collection.CollectionManager;
import org.greenstone.gatherer.file.FileNode;
import org.greenstone.gatherer.msm.ElementWrapper;
import org.greenstone.gatherer.msm.MetadataSet;
import org.greenstone.gatherer.msm.MetadataSetManager;
import org.greenstone.gatherer.msm.MSMUtils;
import org.greenstone.gatherer.shell.GShell;
import org.greenstone.gatherer.shell.GShellProgressMonitor;
import org.greenstone.gatherer.util.Utility;
import org.greenstone.gatherer.valuetree.GValueModel;
import org.greenstone.gatherer.valuetree.GValueNode;
import org.w3c.dom.*;
public class GreenstoneArchiveParser {
private GShell shell;
static final String ignore_list[] = {"assocfilepath","gsdl","Identifier","Source","URL"};
public GreenstoneArchiveParser(GShellProgressMonitor progress, GShell shell) {
// We can only extract metadata if an extracted metadata set exists in our collection.
if(Gatherer.c_man.msm.getSet("") != null) {
this.shell = shell;
// Determine the collection archive directory.
File archive_directory = new File(Gatherer.c_man.getCollectionArchive());
// For each of the hash coded directories within.
File document_directories[] = archive_directory.listFiles();
for(int i = 0; i < document_directories.length; i++) {
// Find the doc.xml file within
if(document_directories[i].isDirectory()) {
File document_file = new File(document_directories[i], "doc.xml");
// Then extract the metadata from it.
if(document_file.exists()) {
extractMetadata(document_file);
// Display a pretty progress message.
shell.fireMessage(GShell.IMPORT, shell.typeAsString(GShell.IMPORT) + "> " + Gatherer.dictionary.get("GShell.Extracted", document_directories[i].getName()), GShell.OK);
progress.increment();
}
}
}
}
// All done. Outta here like a bald man.
}
private void extractMetadata(File file) {
// Retrieve the DOM of the file.
Document document = Utility.parse(file, false);
// If we successfully parsed the document, then it is time to search through the DOM for the Metadata tags.
if(document != null) {
String file_path = null;
Element archive_element = document.getDocumentElement();
// Retrieve all of the Metadata sections.
NodeList metadata_elements = archive_element.getElementsByTagName("Metadata");
// Now for each Metadata entry retrieved...
for(int i = 0; i < metadata_elements.getLength(); i++) {
Element metadata_element = (Element) metadata_elements.item(i);
String name = metadata_element.getAttribute("name");
// There is a special case when the metadata name is gsdlsourcefilename, as we use this to find the FileRecord we want to add metadata to.
if(name.equals("gsdlsourcefilename")) {
file_path = MSMUtils.getValue(metadata_element);
}
else {
// Check if its name starts with, or is equal to, one of the values in our ignore list, and if so ignore this metadata.
boolean ignore = (name.indexOf(".") != -1);
for(int j = 0; !ignore && j < ignore_list.length; j++) {
ignore = name.startsWith(ignore_list[j]);
}
// Otherwise ensure the metadata is present in our collection.
if(!ignore && file_path != null) {
// If we successfully retrieved a record we can continue.
if(file_path != null) {
// 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).
ElementWrapper element = Gatherer.c_man.msm.getElement(name);
if(element == null) {
MetadataSet extracted_mds = Gatherer.c_man.msm.getSet("ex");
if(extracted_mds != null) {
element = extracted_mds.addElement(name);
}
}
// If we successfully retrieved an element (and we should have) we can continue.
if(element != null) {
// Retrieve the metadata for the current file
File target_file = new File(file_path);
ArrayList metadatum = Gatherer.c_man.getCollection().gdm.getMetadata(target_file);
// If no metadata exists for the current element, add it
boolean found = false;
for(int k = 0; !found && k < metadatum.size(); k++) {
Metadata sibling = (Metadata) metadatum.get(k);
found = element.equals(sibling.getElement());
}
metadatum = null;
if(!found) {
String value = "";
try {
value = Utility.decodeGreenstone(URLDecoder.decode(MSMUtils.getValue(metadata_element), "UTF-8"));
}
catch(UnsupportedEncodingException error) {
Gatherer.printStackTrace(error);
}
// If we successfully retrieved a value we can continue.
if(value != null) {
// Create a new metadata object.
GValueModel value_tree = Gatherer.c_man.msm.getValueTree(element);
GValueNode value_node = null;
if(value_tree != null) {
value_node = value_tree.getValue(value);
}
else {
value_node = new GValueNode(element.toString(), value);
}
Metadata metadata = new Metadata(element, value_node);
Gatherer.c_man.getCollection().gdm.metadataChanged(new MSMEvent(this, System.currentTimeMillis(), target_file, null, metadata));
// All done. On to next metadata.
}
}
target_file = null;
}
else {
Gatherer.println("Cannot retrieve metadata element " + name);
}
}
}
}
}
}
}
}