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

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

Adding gs3build

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