[8164] | 1 | /**
|
---|
| 2 | *############################################################################
|
---|
| 3 | * A component of the Greenstone Librarian Interface, part of the Greenstone
|
---|
| 4 | * digital library suite from the New Zealand Digital Library Project at the
|
---|
| 5 | * University of Waikato, New Zealand.
|
---|
| 6 | *
|
---|
| 7 | * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
|
---|
| 8 | *
|
---|
| 9 | * Copyright (C) 2004 New Zealand Digital Library Project
|
---|
| 10 | *
|
---|
| 11 | * This program is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * This program is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with this program; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | *############################################################################
|
---|
| 25 | */
|
---|
| 26 |
|
---|
[7822] | 27 | package org.greenstone.gatherer.metadata;
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | import java.io.*;
|
---|
| 31 | import java.util.*;
|
---|
[13337] | 32 | import org.greenstone.gatherer.DebugStream;
|
---|
[34394] | 33 | import org.greenstone.gatherer.util.Utility;
|
---|
[7822] | 34 |
|
---|
| 35 | /** This class is a static class that manages the doc.xml files */
|
---|
| 36 | public class DocXMLFileManager
|
---|
| 37 | {
|
---|
[7978] | 38 | static private ArrayList doc_xml_files = new ArrayList();
|
---|
[7822] | 39 |
|
---|
[7978] | 40 | static public void clearDocXMLFiles()
|
---|
| 41 | {
|
---|
| 42 | doc_xml_files.clear();
|
---|
| 43 | }
|
---|
[7822] | 44 |
|
---|
| 45 |
|
---|
[7978] | 46 | static public ArrayList getMetadataExtractedFromFile(File file)
|
---|
| 47 | {
|
---|
[34394] | 48 | // Work out relative file path and its hex encoded value here,
|
---|
| 49 | // avoids making DocXMLFile.java recalculate these each time
|
---|
| 50 | String file_relative_path = file.getAbsolutePath();
|
---|
| 51 | int import_index = file_relative_path.indexOf("import");
|
---|
| 52 | if (import_index != -1) {
|
---|
| 53 | file_relative_path = file_relative_path.substring(import_index + "import".length() + 1);
|
---|
| 54 | }
|
---|
[34507] | 55 |
|
---|
[7978] | 56 | // Build up a list of metadata values extracted from this file
|
---|
| 57 | ArrayList metadata_values = new ArrayList();
|
---|
[7822] | 58 |
|
---|
[7978] | 59 | // Look at each loaded doc.xml file to see if any have extracted metadata for this file
|
---|
| 60 | for (int i = 0; i < doc_xml_files.size(); i++) {
|
---|
| 61 | DocXMLFile doc_xml_file = (DocXMLFile) doc_xml_files.get(i);
|
---|
[34394] | 62 | ///System.err.println("@@@@ Looking at doc.xml file: " + doc_xml_files.get(i));
|
---|
[34507] | 63 | metadata_values.addAll(doc_xml_file.getMetadataExtractedFromFile(file, file_relative_path));
|
---|
[7978] | 64 | }
|
---|
[7822] | 65 |
|
---|
[7978] | 66 | return metadata_values;
|
---|
| 67 | }
|
---|
[7822] | 68 |
|
---|
| 69 |
|
---|
[17009] | 70 | static public void loadDocXMLFiles(File directory, String filename_match)
|
---|
[7978] | 71 | {
|
---|
[8013] | 72 | // Make sure the directory (archives) exists
|
---|
| 73 | if (directory.exists() == false) {
|
---|
| 74 | return;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[7978] | 77 | // Look recursively at each subfile of the directory for doc.xml files
|
---|
| 78 | File[] directory_files = directory.listFiles();
|
---|
| 79 | for (int i = 0; i < directory_files.length; i++) {
|
---|
| 80 | File child_file = directory_files[i];
|
---|
| 81 | if (child_file.isDirectory()) {
|
---|
[17009] | 82 | loadDocXMLFiles(child_file,filename_match);
|
---|
[7978] | 83 | }
|
---|
[17009] | 84 | else if (child_file.getName().equals(filename_match)) {
|
---|
| 85 | // e.g. doc.xml (for regular Greenstone, docmets.xml for Fedora)
|
---|
| 86 |
|
---|
| 87 | loadDocXMLFile(child_file,filename_match);
|
---|
[7978] | 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
[7822] | 91 |
|
---|
| 92 |
|
---|
[17009] | 93 | static private void loadDocXMLFile(File doc_xml_file_file,String filename_match)
|
---|
[7978] | 94 | {
|
---|
[17009] | 95 | String file = doc_xml_file_file.getAbsolutePath();
|
---|
| 96 |
|
---|
[17101] | 97 | // Need to do typecasts in the following to keep Java 1.4 happy
|
---|
[17009] | 98 | DocXMLFile doc_xml_file
|
---|
[17101] | 99 | = (filename_match.equals("docmets.xml"))
|
---|
| 100 | ? (DocXMLFile) new DocMetsXMLFile(file)
|
---|
| 101 | : (DocXMLFile) new DocGAFile(file);
|
---|
[17009] | 102 |
|
---|
| 103 | try {
|
---|
[13337] | 104 | doc_xml_file.skimFile();
|
---|
| 105 | doc_xml_files.add(doc_xml_file);
|
---|
| 106 | }
|
---|
| 107 | catch (Exception exception) {
|
---|
| 108 | // We catch any exceptions here so errors in doc.xml files don't stop the collection from loading
|
---|
| 109 | System.err.println("Error: Could not skim doc.xml file " + doc_xml_file.getAbsolutePath());
|
---|
| 110 | DebugStream.printStackTrace(exception);
|
---|
| 111 | }
|
---|
[7978] | 112 | }
|
---|
[7822] | 113 | }
|
---|