1 | package org.greenstone.gatherer.metadata;
|
---|
2 |
|
---|
3 |
|
---|
4 | import java.io.*;
|
---|
5 | import java.util.*;
|
---|
6 | import org.greenstone.gatherer.util.Utility;
|
---|
7 | import org.greenstone.gatherer.util.XMLTools;
|
---|
8 | import org.w3c.dom.*;
|
---|
9 |
|
---|
10 |
|
---|
11 | /** This class represents one doc.xml file */
|
---|
12 | public class DocXMLFile
|
---|
13 | extends File
|
---|
14 | {
|
---|
15 | static final private String ARCHIVE_ELEMENT = "Archive";
|
---|
16 | static final private String DESCRIPTION_ELEMENT = "Description";
|
---|
17 | static final private String METADATA_ELEMENT = "Metadata";
|
---|
18 | static final private String SECTION_ELEMENT = "Section";
|
---|
19 |
|
---|
20 | private ArrayList files_in_doc_xml_file = new ArrayList();
|
---|
21 |
|
---|
22 |
|
---|
23 | public DocXMLFile(String doc_xml_file_path)
|
---|
24 | {
|
---|
25 | super(doc_xml_file_path);
|
---|
26 |
|
---|
27 | MetadataSet extracted_metadata_set = MetadataSetManager.getMetadataSet(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE);
|
---|
28 |
|
---|
29 | // Skim the doc.xml file as quickly as possible (don't parse as XML), looking at the Metadata elements
|
---|
30 | System.err.println("Skimming doc.xml file " + this + "...");
|
---|
31 | try {
|
---|
32 | BufferedReader buffered_reader = new BufferedReader(new FileReader(this));
|
---|
33 | String line = null;
|
---|
34 | while ((line = buffered_reader.readLine()) != null) {
|
---|
35 | // This line doesn't contain a metadata element
|
---|
36 | if (line.indexOf("<Metadata ") == -1) {
|
---|
37 | continue;
|
---|
38 | }
|
---|
39 |
|
---|
40 | // Extract the metadata element name
|
---|
41 | int name_index = line.indexOf(" name=\"") + " name=\"".length();
|
---|
42 | String metadata_element_name_full = line.substring(name_index, line.indexOf("\"", name_index));
|
---|
43 |
|
---|
44 | // If the metadata has a namespace it isn't extracted metadata, so we're not interested
|
---|
45 | String metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
|
---|
46 | if (!metadata_set_namespace.equals("")) {
|
---|
47 | continue;
|
---|
48 | }
|
---|
49 |
|
---|
50 | // Extracted metadata!
|
---|
51 | String metadata_element_name = metadata_element_name_full;
|
---|
52 |
|
---|
53 | // Note which file this doc.xml is for
|
---|
54 | if (metadata_element_name.equals("gsdlsourcefilename")) {
|
---|
55 | // Extract the gsdlsourcefilename element value
|
---|
56 | int value_index = line.indexOf(">", name_index) + ">".length();
|
---|
57 | String gsdlsourcefilename_value = line.substring(value_index, line.indexOf("<", value_index));
|
---|
58 |
|
---|
59 | // We're only interested in the path relative to the import folder
|
---|
60 | int import_index = gsdlsourcefilename_value.indexOf("import");
|
---|
61 | if (import_index != -1) {
|
---|
62 | gsdlsourcefilename_value = gsdlsourcefilename_value.substring(import_index + "import".length());
|
---|
63 |
|
---|
64 | boolean is_unix_path = gsdlsourcefilename_value.startsWith("/");
|
---|
65 | gsdlsourcefilename_value = gsdlsourcefilename_value.substring(1);
|
---|
66 |
|
---|
67 | // Make sure the path matches the OS that is running
|
---|
68 | if (is_unix_path && Utility.isWindows()) {
|
---|
69 | // Convert path from Unix to Windows
|
---|
70 | gsdlsourcefilename_value = gsdlsourcefilename_value.replaceAll("/", File.separator);
|
---|
71 | }
|
---|
72 | if (!is_unix_path && !Utility.isWindows()) {
|
---|
73 | // Convert path from Windows to Unix
|
---|
74 | gsdlsourcefilename_value = gsdlsourcefilename_value.replaceAll("\\\\", File.separator);
|
---|
75 | }
|
---|
76 |
|
---|
77 | // Remember this for quick access later
|
---|
78 | if (gsdlsourcefilename_value != null) {
|
---|
79 | files_in_doc_xml_file.add(gsdlsourcefilename_value);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | else {
|
---|
83 | // We don't really know what is going on...
|
---|
84 | System.err.println("Warning: Could not understand gsdlsourcefilename " + gsdlsourcefilename_value);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Ignore lower-case metadata elements (gsdlsourcefilename, gsdlassocfile etc.)
|
---|
89 | // and those starting with '/' (/srclink)
|
---|
90 | char first_character = metadata_element_name.charAt(0);
|
---|
91 | if (Character.isLowerCase(first_character) || first_character == '/') {
|
---|
92 | continue;
|
---|
93 | }
|
---|
94 |
|
---|
95 | MetadataElement metadata_element = extracted_metadata_set.getMetadataElement(metadata_element_name);
|
---|
96 | if (metadata_element == null) {
|
---|
97 | // This element isn't defined in ex.mds, so create it for this session
|
---|
98 | System.err.println("Extracted metadata element not defined: " + metadata_element_name);
|
---|
99 | extracted_metadata_set.addMetadataElementForThisSession(metadata_element_name);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | catch (Exception ex) {
|
---|
104 | System.err.println("Exception: " + ex);
|
---|
105 | ex.printStackTrace();
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | public ArrayList getMetadataExtractedFromFile(File file)
|
---|
111 | {
|
---|
112 | // Build up a list of metadata extracted from this file
|
---|
113 | ArrayList metadata_values = new ArrayList();
|
---|
114 |
|
---|
115 | String file_relative_path = file.getAbsolutePath();
|
---|
116 | int import_index = file_relative_path.indexOf("import");
|
---|
117 | if (import_index != -1) {
|
---|
118 | file_relative_path = file_relative_path.substring(import_index + "import".length() + 1);
|
---|
119 | }
|
---|
120 |
|
---|
121 | // Check whether this doc.xml file contains extracted metadata for the specified file
|
---|
122 | boolean contains_extracted_metadata_for_file = false;
|
---|
123 | for (int i = 0; i < files_in_doc_xml_file.size(); i++) {
|
---|
124 | if (file_relative_path.equals(files_in_doc_xml_file.get(i))) {
|
---|
125 | contains_extracted_metadata_for_file = true;
|
---|
126 | // System.err.println("Found extracted metadata in file " + getAbsolutePath());
|
---|
127 | break;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | // ...it doesn't
|
---|
132 | if (!contains_extracted_metadata_for_file) {
|
---|
133 | return metadata_values;
|
---|
134 | }
|
---|
135 |
|
---|
136 | // Parse the doc.xml file
|
---|
137 | Document document = XMLTools.parseXMLFile(this);
|
---|
138 | if (document == null) {
|
---|
139 | System.err.println("Error: Could not parse doc.xml file " + getAbsolutePath());
|
---|
140 | return metadata_values;
|
---|
141 | }
|
---|
142 |
|
---|
143 | MetadataSet extracted_metadata_set = MetadataSetManager.getMetadataSet(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE);
|
---|
144 |
|
---|
145 | // Read all the Archive elements in the file
|
---|
146 | NodeList archive_elements_nodelist = document.getElementsByTagName(ARCHIVE_ELEMENT);
|
---|
147 | for (int i = 0; i < archive_elements_nodelist.getLength(); i++) {
|
---|
148 | Element current_archive_element = (Element) archive_elements_nodelist.item(i);
|
---|
149 |
|
---|
150 | // Read the child Section elements of the archive (but not all descendants)
|
---|
151 | ArrayList child_section_elements = XMLTools.getChildElementsByTagName(current_archive_element, SECTION_ELEMENT);
|
---|
152 | for (int j = 0; j < child_section_elements.size(); j++) {
|
---|
153 | Element current_section_element = (Element) child_section_elements.get(j);
|
---|
154 |
|
---|
155 | // Read the Description elements of this section only (not child sections as well)
|
---|
156 | ArrayList child_description_elements = XMLTools.getChildElementsByTagName(current_section_element, DESCRIPTION_ELEMENT);
|
---|
157 | for (int k = 0; k < child_description_elements.size(); k++) {
|
---|
158 | Element current_description_element = (Element) child_description_elements.get(k);
|
---|
159 |
|
---|
160 | // Read all the Metadata elements in this description element
|
---|
161 | NodeList metadata_elements_nodelist = current_description_element.getElementsByTagName(METADATA_ELEMENT);
|
---|
162 | for (int l = 0; l < metadata_elements_nodelist.getLength(); l++) {
|
---|
163 | Element current_metadata_element = (Element) metadata_elements_nodelist.item(l);
|
---|
164 | String metadata_element_name_full = current_metadata_element.getAttribute("name");
|
---|
165 |
|
---|
166 | // If the metadata has a namespace it isn't extracted metadata, so we're not interested
|
---|
167 | String metadata_set_namespace = MetadataTools.getMetadataSetNamespace(metadata_element_name_full);
|
---|
168 | if (!metadata_set_namespace.equals("")) {
|
---|
169 | continue;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // Extracted metadata!
|
---|
173 | String metadata_element_name = metadata_element_name_full;
|
---|
174 |
|
---|
175 | // Ignore lower-case metadata elements (gsdlsourcefilename, gsdlassocfile etc.)
|
---|
176 | // and those starting with '/' (/srclink)
|
---|
177 | char first_character = metadata_element_name.charAt(0);
|
---|
178 | if (Character.isLowerCase(first_character) || first_character == '/') {
|
---|
179 | continue;
|
---|
180 | }
|
---|
181 |
|
---|
182 | MetadataElement metadata_element = extracted_metadata_set.getMetadataElement(metadata_element_name);
|
---|
183 |
|
---|
184 | // Value trees are not stored for extracted metadata, so create a new value tree node now
|
---|
185 | String current_metadata_element_value = XMLTools.getElementTextValue(current_metadata_element);
|
---|
186 | metadata_element.addMetadataValue(current_metadata_element_value);
|
---|
187 | MetadataValueTreeNode metadata_value_tree_node = metadata_element.getMetadataValueTreeNode(current_metadata_element_value);
|
---|
188 |
|
---|
189 | // Add the new metadata value to the list
|
---|
190 | MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
|
---|
191 | metadata_values.add(metadata_value);
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | return metadata_values;
|
---|
198 | }
|
---|
199 | }
|
---|