source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/OrderedNamespace.java@ 5945

Last change on this file since 5945 was 5945, checked in by cs025, 20 years ago

Extensive additions to metadata

  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3import java.io.PrintWriter;
4import java.util.Iterator;
5import java.util.List;
6import java.util.Map;
7import java.util.ArrayList;
8import java.util.HashMap;
9
10import org.greenstone.gsdl3.gs3build.util.MultiMap;
11import org.greenstone.gsdl3.gs3build.util.XMLTools;
12
13/**
14 * Implement a generic, ordered namespace in METS.
15 *
16 * @see: org.greenstone.gsdl3.gs3build.metadata.METSNamespace
17 */
18
19public class OrderedNamespace extends METSNamespace
20{
21 List metadataList;
22
23 public OrderedNamespace(String name)
24 { super(name);
25 this.metadataList = new ArrayList();
26 }
27
28 public OrderedNamespace(String name, METSLocation location)
29 { super(name, location);
30 this.metadataList = new ArrayList();
31 }
32
33 public boolean validate(String field, String value)
34 { return true;
35 }
36
37 /**
38 * Add a metadata item. Whether the field and value validate for this
39 * schema will also be tested.
40 *
41 * @param <code>String</code> the name of the field to be given the value
42 * @param <code>String</code> the value to be assigned
43 *
44 * @return <code>boolean</code> whether the field value was added. This
45 * would return <code>false</code> if the values did not validate,
46 * for example.
47 */
48 public boolean addMetadata(String label, String value)
49 { if (!this.validate(label, value)) {
50 return false;
51 }
52
53 this.metadataList.add(new NamespaceItem(label, value));
54 return true;
55 }
56
57 /**
58 * Assign a metadata item. Whether the field and value validate for this
59 * schema will also be tested. Any existing metadata for that field will
60 * be destroyed if the new value validates.
61 *
62 * @param <code>String</code> the name of the field to be given the value
63 * @param <code>String</code> the value to be assigned
64 *
65 * @return <code>boolean</code> whether the field value was added. This
66 * would return <code>false</code> if the values did not validate,
67 * for example.
68 */
69 public boolean setMetadata(String label, String value)
70 { if (!this.validate(label, value))
71 { return false;
72 }
73
74 // TODO: this.metadataMap.setOnly(label, value);
75 int itemNo = this.findItem(label);
76 if (itemNo >= 0)
77 { this.metadataList.set(itemNo, new NamespaceItem(label, value));
78 return true;
79 }
80 return false;
81 }
82
83 /**
84 * Remove all metadata values for a given field name
85 *
86 * @param <code>String</code> the field to delete
87 *
88 * @return <code>boolean</code> whether the field was actually deleted;
89 * will return <code>true</code> if the field was already empty.
90 */
91 public boolean removeMetadata(String label)
92 { int itemNo;
93
94 itemNo = this.findItem(label);
95 while (itemNo >= 0)
96 { this.metadataList.remove(itemNo);
97 itemNo = this.findItem(label);
98 }
99 return true;
100 }
101
102 /**
103 * Remove a particular incidence of a given metadata field for a document.
104 * If an exact match for the given value is not found, nothing changes.
105 * N.B. if a value occurs twice, only the first incidence of it will be
106 * deleted from the list.
107 *
108 * @param <code>String</code> the field to have the value removed
109 * @param <code>String</code> the value to be removed from a given field
110 *
111 * @return <code>boolean</code> <code>true</code> if an actual metadata text
112 * is matched against the given value and is thus deleted.
113 */
114 public boolean removeMetadata(String label, String value)
115 { int itemNo;
116
117 itemNo = this.findItem(label);
118 if (itemNo >= 0)
119 { this.metadataList.remove(itemNo);
120 return true;
121 }
122 return false;
123 }
124
125 /**
126 * Get the metadata items for a particular label
127 *
128 * @param <code>String</code> the label to fetch values for - must be devoid
129 * of namespace prologue (i.e. "title" rather than
130 * e.g. "dc:title").
131 *
132 * @return <code>List</code> the list of corresponding values. May be
133 * <code>null</code> if no values are found for the metadata
134 */
135 public List getMetadata(String label)
136 { List resultList = new ArrayList();
137
138 for (int i = 0; i < this.metadataList.size(); i ++)
139 { NamespaceItem item = (NamespaceItem) this.metadataList.get(i);
140
141 if (item.getLabel().equals(label))
142 { resultList.add(item.getValue());
143 }
144 }
145 if (resultList.size() > 0)
146 { return resultList;
147 }
148 return null;
149 }
150
151 private int findItem(String label)
152 { for (int i = 0; i < this.metadataList.size(); i ++)
153 { if (((NamespaceItem) this.metadataList.get(i)).getLabel().equals(label))
154 { return i;
155 }
156 }
157 return -1;
158 }
159
160 private int findItem(String label, String value)
161 { for (int i = 0; i < this.metadataList.size(); i ++)
162 { NamespaceItem item = (NamespaceItem) this.metadataList.get(i);
163 if (item.getLabel().equals(label) && item.getValue().equals(value))
164 { return i;
165 }
166 }
167 return -1;
168 }
169
170 /**
171 * Write out the metadata to an XML file through a <code>PrintWriter</code>.
172 *
173 * @param <code>PrintWriter</code> the writer to use.
174 */
175 public boolean write(PrintWriter writer)
176 { // if this is a non-file block of metadata, write it out in long hand
177 if (this.location == null)
178 { String tag = XMLTools.getOpenTag("mets", "mdWrap");
179 tag = XMLTools.addAttribute(tag, "MDType", this.name);
180 if (this.id != null) {
181 tag = XMLTools.addAttribute(tag, "ID", this.id);
182 }
183 writer.println(tag);
184
185 Iterator items = this.metadataList.iterator();
186
187 while (items.hasNext())
188 { NamespaceItem item = (NamespaceItem) items.next();
189 this.writeItem(writer, item);
190 }
191 writer.println("</mets:mdWrap>");
192 }
193 // otherwise, drop the metadata out in a simplified file-reference
194 // form only
195 else
196 { String tag = XMLTools.getOpenTag("mets", "mdRef");
197 tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
198 tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
199 tag = XMLTools.addAttribute(tag, "MDTYPE", this.name);
200 if (this.id != null) {
201 tag = XMLTools.addAttribute(tag, "ID", this.id);
202 }
203 tag = XMLTools.makeSingleton(tag);
204 }
205 return true;
206 }
207
208 /**
209 * Write out a single element - this may be overloaded to provide for the
210 * appropriate formatting for this metadata.
211 */
212 protected boolean writeItem(PrintWriter writer, NamespaceItem item)
213 { // Do some default sillinesses
214 writer.write(XMLTools.getOpenTag(this.name, item.getLabel()));
215
216 writer.write(item.getValue());
217
218 writer.write(XMLTools.getCloseTag(this.name, item.getLabel()));
219 return true;
220 }
221
222 /**
223 * <p>Indicate whether this metadata is open to being changed or not.</p>
224 * <p>Metadata which is created from a distinct file cannot be changed,
225 * only those which have no associated file can be modified.
226 *
227 * @return <code>boolean</code> whether this namespace can be altered.
228 */
229 public boolean isEditable()
230 { return (this.location == null);
231 }
232}
Note: See TracBrowser for help on using the repository browser.