source: tags/gsdl-2_41-fiji/gli/src/org/greenstone/gatherer/msm/GDMDocument.new@ 5932

Last change on this file since 5932 was 5932, checked in by (none), 20 years ago

This commit was manufactured by cvs2svn to create tag
'gsdl-2_41-fiji'.

  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 KB
Line 
1package org.greenstone.gatherer.msm;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.io.*;
39import java.util.*;
40import org.greenstone.gatherer.Gatherer;
41import org.greenstone.gatherer.msm.ElementWrapper;
42import org.greenstone.gatherer.msm.GDMManager;
43import org.greenstone.gatherer.msm.Metadata;
44import org.greenstone.gatherer.msm.MetadataSetManager;
45import org.greenstone.gatherer.msm.MSMUtils;
46import org.greenstone.gatherer.util.ArrayTools;
47import org.greenstone.gatherer.util.Utility;
48import org.greenstone.gatherer.valuetree.GValueModel;
49import org.greenstone.gatherer.valuetree.GValueNode;
50import org.w3c.dom.*;
51/** This class wraps around a DOM Document providing methods for accessing the data within. In this case the DOM represents a Greenstone Directory metadata file. It provides the necessary functionality to create a new metadata.xml file. */
52public class GDMDocument {
53 /** The document this class sources its data from. */
54 private Document document;
55 /** The pattern to match when searching for directory level assignments. */
56 static final private String DIRECTORY_FILENAME = ".*";
57 /** @deprecated */
58 public GDMDocument(Gatherer gatherer, File file) {}
59 /** @deprecated */
60 public GDMDocument(Gatherer gatherer, File file, Document document) {}
61 /** Constructor which creates a brand new metadata.xml document. */
62 public GDMDocument() {
63 // Create new document. We do this by loading a copy of the template. */
64 this.document = Utility.parse(Utility.GREENSTONEDIRECTORYMETADATA_TEMPLATE, true);
65 }
66 /** Constructor which parses an existing metadata.xml document. */
67 public GDMDocument(File file) {
68 try {
69 this.document = Utility.parse(file.getAbsolutePath(), false);
70 }
71 catch (Exception error) {
72 // Poorly formed, or completely invalid metadata.xml file!
73 }
74 }
75 /** Constructor which wraps around an existing metadata.xml document. */
76 public GDMDocument(Document document) {
77 this.document = document;
78 }
79 /** Add this piece of directory level metadata. */
80 public void addDirectoryMetadata(Metadata metadata) {
81 addMetadata(DIRECTORY_FILENAME, metadata);
82 }
83 /** Add a new piece of metadata to this document, for the given filename. */
84 public void addMetadata(String filename, Metadata metadata) {
85 try {
86 Element fileset_element = getFileSet(filename);
87 // Retrieve the Metadata Element for this fileset.
88 Element description_element = (Element) MSMUtils.getNodeFromNamed(fileset_element, "Description");
89 // Add the new element.
90 Element metadata_element = document.createElement("Metadata");
91 metadata_element.setAttribute("name", metadata.getElement().getName());
92 // What mode we use depends on whether this is the first metadata assignment of this type. If so we use overwrite to ensure that it overrides any directory level metadata (overwrite is the default mode). If this isn't the first metadata of this type we explicitly set accumulate.
93 if(existsMetadata(description_element, metadata.getElement().toString())) {
94 metadata_element.setAttribute("mode", "accumulate");
95 }
96 // What actually gets written at the node depends on whether the element above is a hierarchy based one.
97 GValueModel model = MetadataSetManager.self.getValueTree(metadata.getElement());
98 Text metadata_element_value;
99 if(model != null && model.isHierarchy()) {
100 metadata_element_value = document.createTextNode(model.getHIndex(metadata.getAbsoluteValue()));
101 }
102 else {
103 metadata_element_value = document.createTextNode(metadata.getValue());
104 }
105 metadata_element.appendChild(metadata_element_value);
106 }
107 catch (Exception error) {
108 Gatherer.printStackTrace(error);
109 }
110 }
111
112 /** @deprecated */
113 public void destroy() {}
114
115 /** Retrieve the document this class is wrapping. */
116 public Document getDocument() {
117 return document;
118 }
119
120 /** @deprecated */
121 public File getFile() {
122 return null;
123 }
124
125 /** Get all of the metadata, including directory level, associated with this file. If remove is true remove any metadata we find. */
126 public ArrayList getMetadata(String filename, boolean remove) {
127 Metadata[] metadatum = null;
128 try {
129 // Retrieve the document element.
130 Element directorymetadata_element = document.getDocumentElement();
131 // Iterate through the filesets, checking the FileName child element against the target file's name using regular expression matching.
132 NodeList filesets = directorymetadata_element.getElementsByTagName("FileSet");
133 for(int i = 0; i < filesets.getLength(); i++) {
134 Element fileset_element = (Element) filesets.item(i);
135 Element filename_element = (Element) MSMUtils.getNodeFromNamed(fileset_element, "FileName");
136 String current_filename = MSMUtils.getValue(filename_element);
137 if((filename != null && current_filename.matches(filename)) || current_filename.equals(DIRECTORY_FILENAME)) {
138 // If they match add all of the metadata found in the Description child element, remembering to abide by desired mode (accumulate vs. overwrite).
139 Element description_element = (Element)MSMUtils.getNodeFromNamed(fileset_element, "Description");
140 NodeList metadatas = description_element.getElementsByTagName("Metadata");
141 for(int j = 0; j < metadatas.getLength(); j++) {
142 Element metadata_element = (Element) metadatas.item(j);
143 String element_str = metadata_element.getAttribute("name");
144 //String language = metadata_element.getAttribute("language");
145 String mode = metadata_element.getAttribute("mode");
146 // If mode is overwrite, then remove any previous values for this metadata element.
147 if(metadatum != null) {
148 for(int k = metadatum.length - 1; k >= 0; k--) {
149 if(metadatum[k].getElement().toString().equals(element_str)) {
150 metadatum = ArrayTools.remove(metadatum, k);
151 }
152 }
153 }
154 // Add the new metadata.
155 String value_str = MSMUtils.getValue(metadata_element);
156 // Using the element string and value, retrieve a matching Metadata object from the cache
157 Metadata metadata = (Metadata) GDMManager.self.metadata_cache.get(element_str, value_str);
158 if(metadata != null) {
159 // Retrieve the appropriate element and value Elements
160 ElementWrapper element = MetadataSetManager.self.getElement(element_str);
161 GValueModel model = MetadataSetManager.self.getValueTree(element);
162 GValueNode value;
163 if(model != null) {
164 value = model.getValue(value_str);
165 }
166 else {
167 value = new GValueNode(element_str, value_str);
168 }
169 metadata = new Metadata(element, value);
170 GDMManager.self.metadata_cache.put(element_str, value_str, metadata);
171 }
172 metadatum = ArrayTools.add(metadatum, metadata);
173 }
174 }
175 }
176 }
177 catch (Exception error) {
178 Gatherer.self.printStackTrace(error);
179 }
180
181 //return metadatum;
182
183 // Change the Metadata[] into an ArrayList
184 ArrayList result = new ArrayList();
185 for(int i = 0; i < metadatum.length; i++) {
186 result.add(metadatum[i]);
187 }
188 return result;
189 }
190 /** Determine is this is a valid Greenstone Directory Metadata file. It may of course just be some xml file with the name metadata.xml. */
191 public boolean isValid() {
192 // Just determine if the doctype is GreenstoneDirectoryMetadata and root node is called DirectoryMetadata.
193 String doctype_name = document.getDoctype().getName();
194 String root_name = document.getDocumentElement().getTagName();
195 return (doctype_name.equals("GreenstoneDirectoryMetadata") && root_name.equals("DirectoryMetadata"));
196 }
197 /** Remove the given directory level metadata from this document. All directory level metadata is available under the FileSet with filename '.*' */
198 public void removeDirectoryMetadata(Metadata metadata) {
199 try {
200 Element fileset_element = getFileSet(DIRECTORY_FILENAME);
201 // Retrieve the Metadata Element for this fileset, and iterate through them looking for the one which we are to remove.
202 Element description_element = (Element) MSMUtils.getNodeFromNamed(fileset_element, "Description");
203 NodeList metadatas = description_element.getElementsByTagName("Metadata");
204 for(int j = 0; j < metadatas.getLength(); j++) {
205 Element metadata_element = (Element) metadatas.item(j);
206 String element = metadata_element.getAttribute("name");
207 String value = MSMUtils.getValue(metadata_element);
208 // See if this is the metadata we wish to remove
209 if(element.equals(metadata.getElement().toString()) && value.equals(metadata.getAbsoluteValue())) {
210 // Remove it
211 description_element.removeChild(metadata_element);
212 }
213 }
214 }
215 catch (Exception error) {
216 Gatherer.printStackTrace(error);
217 }
218 }
219
220 /** Remove the given metadata from the file specified within this document. */
221 public void removeMetadata(String filename, Metadata metadata) {
222
223 }
224
225 /** @deprecated */
226 public void save() {}
227
228 /** @deprecated */
229 public void setUpToDate(boolean state) {}
230
231 /** Retrieves the fileset with the desired filename pattern, or creates it if none is available. */
232 private Element getFileSet(String pattern) {
233 Element fileset;
234 // Retrieve the document element.
235 Element directorymetadata_element = document.getDocumentElement();
236 // Iterate through the filesets looking for the directory level one.
237 NodeList filesets = directorymetadata_element.getElementsByTagName("FileSet");
238 for(int i = 0; i < filesets.getLength(); i++) {
239 Element fileset_element = (Element) filesets.item(i);
240 Element filename_element = (Element) MSMUtils.getNodeFromNamed(fileset_element, "FileName");
241 String filename = MSMUtils.getValue(filename_element);
242 if(name.matches(pattern)) {
243 fileset = fileset_element;
244 }
245 filename = null;
246 filename_element = null;
247 fileset_element = null;
248 }
249 filesets = null;
250 // It doesn't already exist. Add our own.
251 if(fileset == null) {
252 Element filename = document.createElement("FileName");
253 filename.appendChild(document.createTextNode(pattern));
254 Element description = document.createElement("Description");
255 fileset = document.createElement("FileSet");
256 fileset.appendChild(filename);
257 fileset.appendChild(description);
258 if(directorymetadata_element.hasChildNodes()) {
259 directorymetadata_element.insertBefore(fileset, directorymetadata_element.getFirstChild());
260 }
261 else {
262 directorymetadata_element.appendChild(fileset);
263 }
264 description = null;
265 filename = null;
266 }
267 directorymetadata_element = null;
268 return fileset;
269 }
270
271 /** Determines if there is already a metadata assignment with the given element name within the given description element. */
272 public boolean existsMetadata(Element description, String element_name) {
273 boolean result = false;
274 NodeList metadatas = description.getElementsByTagName("Metadata");
275 for(int i = 0; !result && i < metadatas.getLength(); i++) {
276 Element metadata = (Element) metadatas.item(i);
277 if(metadata.getAttribute("name").equals(element_name)) {
278 result = true;
279 }
280 }
281 return result;
282 }
283}
Note: See TracBrowser for help on using the repository browser.