- Timestamp:
- 2003-11-24T14:26:53+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/SimpleNamespace.java
r5800 r5945 9 9 10 10 import java.sql.SQLException; 11 import java.sql.ResultSet; 11 12 12 13 import org.w3c.dom.Document; … … 30 31 public class SimpleNamespace extends METSNamespace 31 32 { 32 MultiMap metadataMap; 33 34 public SimpleNamespace(String name) 35 { super(name); 36 this.metadataMap = new MultiMap(); 37 System.out.println("Namespace " + name); 33 MultiMap metadataMap; 34 35 public SimpleNamespace(String name) 36 { super(name); 37 this.metadataMap = new MultiMap(); 38 System.out.println("Namespace " + name); 39 } 40 41 public SimpleNamespace(String name, METSLocation location) 42 { super(name, location); 43 this.metadataMap = new MultiMap(); 44 } 45 46 public SimpleNamespace(String name, Element mdWrapTag) 47 { super(name); 48 this.metadataMap = new MultiMap(); 49 50 NodeList childNodes = mdWrapTag.getChildNodes(); 51 for (int c = 0; c < childNodes.getLength(); c ++) 52 { // a metadata node 53 if (childNodes.item(c).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) 54 { // get the name of the metadata from the node 55 String metadataLabel = childNodes.item(c).getNodeName(); 56 57 // skip blank labels 58 // TODO: raise an error: metadata item without metadata label set 59 if (metadataLabel == null || metadataLabel.length() == 0) 60 { continue; 38 61 } 39 62 40 public SimpleNamespace(String name, METSLocation location) 41 { super(name, location); 42 this.metadataMap = new MultiMap(); 63 // build the value 64 String metadataValue = ""; 65 66 Element childElement = (Element) childNodes.item(c); 67 for (int i = 0; i < childElement.getChildNodes().getLength(); i ++) 68 { metadataValue = metadataValue + childElement.getChildNodes().item(i).toString(); 43 69 } 44 70 45 public SimpleNamespace(String name, Element mdWrapTag) 46 { super(name); 47 this.metadataMap = new MultiMap(); 48 49 NodeList childNodes = mdWrapTag.getChildNodes(); 50 for (int c = 0; c < childNodes.getLength(); c ++) 51 { // a metadata node 52 if (childNodes.item(c).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) 53 { // get the name of the metadata from the node 54 String metadataLabel = childNodes.item(c).getNodeName(); 55 56 // skip blank labels 57 // TODO: raise an error: metadata item without metadata label set 58 if (metadataLabel == null || metadataLabel.length() == 0) 59 { continue; 60 } 61 62 // build the value 63 String metadataValue = ""; 64 65 Element childElement = (Element) childNodes.item(c); 66 for (int i = 0; i < childElement.getChildNodes().getLength(); i ++) 67 { metadataValue = metadataValue + childElement.getChildNodes().item(i).toString(); 68 } 69 70 // trim any leading namespace identifiers in "namespace:label" metadata 71 if (metadataLabel.startsWith(this.name + ":")) 72 { metadataLabel = metadataLabel.substring(this.name.length() + 1); 73 } 74 75 this.addMetadata(metadataLabel, metadataValue); 76 } 77 } 71 // trim any leading namespace identifiers in "namespace:label" metadata 72 if (metadataLabel.startsWith(this.name + ":")) 73 { metadataLabel = metadataLabel.substring(this.name.length() + 1); 78 74 } 79 75 80 public boolean validate(String field, String value) 81 { return true; 82 } 83 84 /** 85 * Add a metadata item. Whether the field and value validate for this 86 * schema will also be tested. 87 * 88 * @param <code>String</code> the name of the field to be given the value 89 * @param <code>String</code> the value to be assigned 90 * 91 * @return <code>boolean</code> whether the field value was added. This 92 * would return <code>false</code> if the values did not validate, 93 * for example. 94 */ 95 public boolean addMetadata(String label, String value) 96 { if (!this.validate(label, value)) { 97 return false; 98 } 99 100 this.metadataMap.put(label, value); 101 return true; 102 } 103 104 /** 105 * Assign a metadata item. Whether the field and value validate for this 106 * schema will also be tested. Any existing metadata for that field will 107 * be destroyed if the new value validates. 108 * 109 * @param <code>String</code> the name of the field to be given the value 110 * @param <code>String</code> the value to be assigned 111 * 112 * @return <code>boolean</code> whether the field value was added. This 113 * would return <code>false</code> if the values did not validate, 114 * for example. 115 */ 116 public boolean setMetadata(String label, String value) 117 { if (!this.validate(label, value)) 118 { return false; 119 } 120 121 this.metadataMap.setOnly(label, value); 122 return true; 123 } 124 125 /** 126 * Remove all metadata values for a given field name 127 * 128 * @param <code>String</code> the field to delete 129 * 130 * @return <code>boolean</code> whether the field was actually deleted; 131 * will return <code>true</code> if the field was already empty. 132 */ 133 public boolean removeMetadata(String label) 134 { this.metadataMap.remove(label); 135 return true; 136 } 137 138 /** 139 * Remove a particular incidence of a given metadata field for a document. 140 * If an exact match for the given value is not found, nothing changes. 141 * N.B. if a value occurs twice, only the first incidence of it will be 142 * deleted from the list. 143 * 144 * @param <code>String</code> the field to have the value removed 145 * @param <code>String</code> the value to be removed from a given field 146 * 147 * @return <code>boolean</code> <code>true</code> if an actual metadata text 148 * is matched against the given value and is thus deleted. 149 */ 150 public boolean removeMetadata(String label, String value) 151 { return this.metadataMap.remove(label, value); 152 } 153 154 /** 155 * Get the metadata items for a particular label 156 * 157 * @param <code>String</code> the label to fetch values for 158 * 159 * @return <code>List</code> the list of corresponding values. May be 160 * <code>null</code> 161 */ 162 public List getMetadata(String label) 163 { return (List) this.metadataMap.get(label); 164 } 76 this.addMetadata(metadataLabel, metadataValue); 77 } 78 } 79 } 80 81 public boolean validate(String field, String value) 82 { return true; 83 } 84 85 /** 86 * Add a metadata item. Whether the field and value validate for this 87 * schema will also be tested. 88 * 89 * @param <code>String</code> the name of the field to be given the value 90 * @param <code>String</code> the value to be assigned 91 * 92 * @return <code>boolean</code> whether the field value was added. This 93 * would return <code>false</code> if the values did not validate, 94 * for example. 95 */ 96 public boolean addMetadata(String label, String value) 97 { if (!this.validate(label, value)) { 98 return false; 99 } 100 101 this.metadataMap.put(label, value); 102 return true; 103 } 104 105 /** 106 * Assign a metadata item. Whether the field and value validate for this 107 * schema will also be tested. Any existing metadata for that field will 108 * be destroyed if the new value validates. 109 * 110 * @param <code>String</code> the name of the field to be given the value 111 * @param <code>String</code> the value to be assigned 112 * 113 * @return <code>boolean</code> whether the field value was added. This 114 * would return <code>false</code> if the values did not validate, 115 * for example. 116 */ 117 public boolean setMetadata(String label, String value) 118 { if (!this.validate(label, value)) 119 { return false; 120 } 121 122 this.metadataMap.setOnly(label, value); 123 return true; 124 } 125 126 /** 127 * Remove all metadata values for a given field name 128 * 129 * @param <code>String</code> the field to delete 130 * 131 * @return <code>boolean</code> whether the field was actually deleted; 132 * will return <code>true</code> if the field was already empty. 133 */ 134 public boolean removeMetadata(String label) 135 { this.metadataMap.remove(label); 136 return true; 137 } 138 139 /** 140 * Remove a particular incidence of a given metadata field for a document. 141 * If an exact match for the given value is not found, nothing changes. 142 * N.B. if a value occurs twice, only the first incidence of it will be 143 * deleted from the list. 144 * 145 * @param <code>String</code> the field to have the value removed 146 * @param <code>String</code> the value to be removed from a given field 147 * 148 * @return <code>boolean</code> <code>true</code> if an actual metadata text 149 * is matched against the given value and is thus deleted. 150 */ 151 public boolean removeMetadata(String label, String value) 152 { return this.metadataMap.remove(label, value); 153 } 154 155 /** 156 * Get the metadata items for a particular label 157 * 158 * @param <code>String</code> the label to fetch values for 159 * 160 * @return <code>List</code> the list of corresponding values. May be 161 * <code>null</code> 162 */ 163 public List getMetadata(String label) 164 { return this.metadataMap.getAll(label); 165 } 165 166 166 167 /** … … 175 176 176 177 tag = XMLTools.addAttribute(tag, "MDType", this.name); 178 if (this.id != null) { 179 tag = XMLTools.addAttribute(tag, "ID", this.id); 180 } 177 181 writer.println(tag); 178 182 … … 197 201 tag = XMLTools.addAttribute(tag, "xlink:href", location.getLocation().toString()); 198 202 tag = XMLTools.addAttribute(tag, "MDType", this.name); 203 if (this.id != null) { 204 tag = XMLTools.addAttribute(tag, "ID", this.id); 205 } 199 206 writer.println(tag); 200 207 … … 204 211 } 205 212 206 207 208 209 210 211 {if (this.location == null)212 213 214 215 216 {String value = values.next().toString();217 218 219 220 221 222 223 224 225 226 {String tag = XMLTools.getOpenTag("mets", "mdRef");227 228 229 230 231 232 233 234 213 /** 214 * Write out a single element - this may be overloaded to provide for the 215 * appropriate formatting for this metadata. 216 */ 217 protected boolean writeItem(PrintWriter writer, String label) 218 { if (this.location == null) 219 { // just place the metadata in a simple wrapper 220 Iterator values = this.metadataMap.getAll(label).iterator(); 221 222 while (values.hasNext()) 223 { String value = values.next().toString(); 224 225 writer.write(XMLTools.getOpenTag(this.name, label)); 226 227 writer.write(value); 228 229 writer.println(XMLTools.getCloseTag(this.name, label)); 230 } 231 } 232 else 233 { String tag = XMLTools.getOpenTag("mets", "mdRef"); 234 tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType()); 235 tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString()); 236 tag = XMLTools.addAttribute(tag, "MDTYPE", this.getName()); 237 tag = XMLTools.makeSingleton(tag); 238 writer.println(tag); 239 } 240 return true; 241 } 235 242 236 243 /** … … 241 248 public boolean writeSQL(int parentId, GS3SQLConnection connection) 242 249 { // write the general stuff 243 int sqlId; 244 super.writeSQL(parentId, connection); 250 String sqlId; 251 252 if (!super.writeSQL(parentId, connection)) { 253 return false; 254 } 245 255 246 256 try { 247 257 if (this.id == null) { 248 258 GS3SQLSelect select = new GS3SQLSelect("namespaces"); 249 259 select.setWhere(new GS3SQLWhere(new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(parentId), GS3SQLField.INTEGER_TYPE))); 250 260 select.addField("NamespaceRef"); 251 System.out.println(select.toString());252 261 connection.execute(select.toString()); 253 262 254 connection.getResultSet().first(); 255 sqlId = connection.getResultSet().getInt("NamespaceRef"); 256 257 Iterator keys = this.metadataMap.keySet().iterator(); 258 while (keys.hasNext()) 259 { String thisKey = keys.next().toString(); 263 ResultSet results = connection.getResultSet(); 264 results.first(); 265 sqlId = Integer.toString(results.getInt("NamespaceRef")); 266 } 267 else { 268 sqlId = this.id; 269 } 270 271 // clear all the existing metadata items for this namespace 272 GS3SQLDelete delete = new GS3SQLDelete("mdvalues"); 273 GS3SQLWhere where = new GS3SQLWhere(new GS3SQLWhereItem("NamespaceRef", "=", sqlId, GS3SQLField.INTEGER_TYPE)); 274 delete.setWhere(where); 275 connection.execute(delete.toString()); 276 277 // write out the metadata for this namespace 278 Iterator keys = this.metadataMap.keySet().iterator(); 279 while (keys.hasNext()) 280 { String thisKey = keys.next().toString(); 260 281 261 282 Iterator values = this.metadataMap.getAll(thisKey).iterator(); 262 283 263 264 284 while (values.hasNext()) 285 { String value = values.next().toString(); 265 286 266 267 GS3SQLInsert insert = new GS3SQLInsert("mdvalues"); 268 insert.addValue("NamespaceRef", Integer.toString(sqlId), GS3SQLField.INTEGER_TYPE); 269 insert.addValue("label", thisKey); 270 insert.addValue("value", value); 271 System.out.println(insert.toString()); 272 connection.execute(insert.toString()); 273 } 287 GS3SQLInsert insert = new GS3SQLInsert("mdvalues"); 288 insert.addValue("NamespaceRef", sqlId, GS3SQLField.INTEGER_TYPE); 289 insert.addValue("label", thisKey); 290 insert.addValue("value", value); 291 connection.execute(insert.toString()); 274 292 } 293 } 275 294 } 276 295 catch (SQLException sql) { 277 278 } 279 280 281 282 283 284 285 286 287 288 289 290 291 {return (this.location == null);292 296 System.out.println(sql); 297 } 298 return true; 299 } 300 301 302 /** 303 * <p>Indicate whether this metadata is open to being changed or not.</p> 304 * <p>Metadata which is created from a distinct file cannot be changed, 305 * only those which have no associated file can be modified. 306 * 307 * @return <code>boolean</code> whether this namespace can be altered. 308 */ 309 public boolean isEditable() 310 { return (this.location == null); 311 } 293 312 }
Note:
See TracChangeset
for help on using the changeset viewer.