- Timestamp:
- 2004-11-30T11:18:55+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSDescriptive.java
r8461 r8702 36 36 37 37 public class METSDescriptive 38 { MultiMap map; 39 String ID; 40 String name; 41 42 public METSDescriptive(String ID, String name) 43 { this.map = new MultiMap(); 44 this.ID = ID; 45 this.name = name; 46 } 47 48 public String getID() 49 { return this.ID; 50 } 51 52 public String getName() 53 { return this.name; 54 } 55 56 public void addNamespace(METSNamespace namespace) 57 { this.map.put(namespace.getName(), namespace); 58 } 59 60 public METSNamespace getNamespace(String namespace) 61 { return (METSNamespace) this.map.get(namespace); 62 } 63 64 /** 65 * Get the version of a metadata namespace that can be used for 66 * adding new values to the document. Remember, each namespace may 67 * occur more than once, but only one of the occurrences can be 'open'. 68 * 69 * @param <code>String</code> the name of the namespace 70 */ 71 public METSNamespace getOpenNamespace(String namespace) 72 { if (this.map.getAll(namespace) == null) 73 { return null; 74 } 75 76 Iterator namespaces = ((List) this.map.getAll(namespace)).iterator(); 77 78 while (namespaces.hasNext()) 79 { METSNamespace namespaceData = (METSNamespace) namespaces.next(); 80 81 if (namespaceData.isEditable()) 82 { return namespaceData; 83 } 84 } 85 return null; 86 } 87 88 /** 89 * Add a piece of metadata to this document. Existing values are preserved. 90 * 91 * @param <code>String</code> the namespace in which the metadata occurs 92 * @param <code>String</code> the label of the metadata 93 * @param <code>String</code> the value of the metadata 94 */ 95 public void addMetadata(String namespace, String label, String value) 96 { METSNamespace namespaceData = this.getOpenNamespace(namespace); 97 98 if (namespaceData == null || 99 namespaceData.isEditable() == false) 100 { namespaceData = NamespaceFactory.initNamespace(namespace); 101 this.addNamespace(namespaceData); 102 } 103 namespaceData.addMetadata(label, value); 104 } 105 106 /** 107 * Add a piece of metadata to this document. Existing values in editable 108 * parts of the document's metadata are destroyed. 109 * 110 * @param <code>String</code> the namespace in which the metadata occurs 111 * @param <code>String</code> the label of the metadata 112 * @param <code>String</code> the value of the metadata 113 */ 114 public void setMetadata(String namespace, String label, String value) 115 { METSNamespace namespaceData = this.getOpenNamespace(namespace); 116 117 if (namespaceData == null || 118 namespaceData.isEditable() == false) 119 { namespaceData = NamespaceFactory.initNamespace(namespace); 120 this.addNamespace(namespaceData); 121 } 122 namespaceData.setMetadata(label, value); 123 } 124 125 /** 126 * Remove the metadata for a particular label. 127 * 128 * @param <code>String</code> the namespace in which the metadata occurs 129 * @param <code>String</code> the label of the metadata 130 */ 131 public void removeMetadata(String namespace, String label) 132 { METSNamespace namespaceData = this.getOpenNamespace(namespace); 133 134 if (namespaceData != null && namespaceData.isEditable() == true) 135 { namespaceData.removeMetadata(label); 136 } 137 } 138 139 /** 140 * Fetch a <code>List</code> of the values matching the given label in 141 * the requisite namespace. 142 * 143 * @param <code>String</code> namespace 144 * @param <code>String</code> label of metadata to match 145 * 146 * @return <code>List</code> the corresponding values found. 147 */ 148 public List getMetadata(String namespace, String label) 149 { 150 // Simple case - no metadata 151 if (!this.map.containsKey(namespace)) 152 { return null; 153 } 154 155 // if there's just one instance of the requisite namespace, 156 // then again it is a simple case 157 if (this.map.getAll(namespace).size() == 1) 158 { return this.getNamespace(namespace).getMetadata(label); 159 } 160 161 // otherwise, step through and accumulate 162 List accumulatedValues = new ArrayList(); 163 Iterator namespaceIterator = this.map.getAll(namespace).iterator(); 164 while (namespaceIterator.hasNext()) 165 { METSNamespace localNamespace = (METSNamespace) namespaceIterator.next(); 166 167 List localList = localNamespace.getMetadata(label); 168 if (localList != null && localList.size() > 0) 169 { accumulatedValues.addAll(localList); 170 } 171 } 172 173 // if the result of the accumulation is empty, return a null item 174 if (accumulatedValues.size() == 0) 175 { return null; 176 } 177 178 return accumulatedValues; 179 } 180 38 { 39 MultiMap map; 40 String ID; 41 String name; 42 43 public METSDescriptive(String ID, String name) 44 { 45 this.map = new MultiMap(); 46 this.ID = ID; 47 this.name = name; 48 } 49 50 public String getID() 51 { 52 return this.ID; 53 } 54 55 public String getName() 56 { 57 return this.name; 58 } 59 60 public void addNamespace(METSNamespace namespace) 61 { 62 this.map.put(namespace.getName(), namespace); 63 } 64 65 public METSNamespace getNamespace(String namespace) 66 { 67 return (METSNamespace) this.map.get(namespace); 68 } 69 70 /** 71 * Get the version of a metadata namespace that can be used for 72 * adding new values to the document. Remember, each namespace may 73 * occur more than once, but only one of the occurrences can be 'open'. 74 * 75 * @param <code>String</code> the name of the namespace 76 */ 77 public METSNamespace getOpenNamespace(String namespace) 78 { 79 if (this.map.getAll(namespace) == null) { 80 return null; 81 } 82 83 Iterator namespaces = ((List) this.map.getAll(namespace)).iterator(); 84 85 while (namespaces.hasNext()) { 86 METSNamespace namespaceData = (METSNamespace) namespaces.next(); 87 88 if (namespaceData.isEditable()){ 89 return namespaceData; 90 } 91 } 92 return null; 93 } 94 95 /** 96 * Add a piece of metadata to this document. Existing values are preserved. 97 * 98 * @param <code>String</code> the namespace in which the metadata occurs 99 * @param <code>String</code> the label of the metadata 100 * @param <code>String</code> the value of the metadata 101 */ 102 public void addMetadata(String namespace, String label, String value) 103 { 104 METSNamespace namespaceData = this.getOpenNamespace(namespace); 105 106 if (namespaceData == null || 107 namespaceData.isEditable() == false) { 108 namespaceData = NamespaceFactory.initNamespace(namespace); 109 this.addNamespace(namespaceData); 110 } 111 namespaceData.addMetadata(label, value); 112 } 113 114 /** 115 * Add a piece of metadata to this document. Existing values in editable 116 * parts of the document's metadata are destroyed. 117 * 118 * @param <code>String</code> the namespace in which the metadata occurs 119 * @param <code>String</code> the label of the metadata 120 * @param <code>String</code> the value of the metadata 121 */ 122 public void setMetadata(String namespace, String label, String value) 123 { 124 METSNamespace namespaceData = this.getOpenNamespace(namespace); 125 126 if (namespaceData == null || 127 namespaceData.isEditable() == false) { 128 namespaceData = NamespaceFactory.initNamespace(namespace); 129 this.addNamespace(namespaceData); 130 } 131 namespaceData.setMetadata(label, value); 132 } 133 134 /** 135 * Remove the metadata for a particular label. 136 * 137 * @param <code>String</code> the namespace in which the metadata occurs 138 * @param <code>String</code> the label of the metadata 139 */ 140 public void removeMetadata(String namespace, String label) 141 { 142 METSNamespace namespaceData = this.getOpenNamespace(namespace); 143 144 if (namespaceData != null && namespaceData.isEditable() == true) { 145 namespaceData.removeMetadata(label); 146 } 147 } 148 149 /** 150 * Fetch a <code>List</code> of the values matching the given label in 151 * the requisite namespace. 152 * 153 * @param <code>String</code> namespace 154 * @param <code>String</code> label of metadata to match 155 * 156 * @return <code>List</code> the corresponding values found. 157 */ 158 public List getMetadata(String namespace, String label) 159 { 160 // Simple case - no metadata 161 if (!this.map.containsKey(namespace)){ 162 return null; 163 } 164 165 // if there's just one instance of the requisite namespace, 166 // then again it is a simple case 167 if (this.map.getAll(namespace).size() == 1){ 168 return this.getNamespace(namespace).getMetadata(label); 169 } 170 171 // otherwise, step through and accumulate 172 List accumulatedValues = new ArrayList(); 173 Iterator namespaceIterator = this.map.getAll(namespace).iterator(); 174 while (namespaceIterator.hasNext()){ 175 METSNamespace localNamespace = (METSNamespace) namespaceIterator.next(); 176 177 List localList = localNamespace.getMetadata(label); 178 if (localList != null && localList.size() > 0){ 179 accumulatedValues.addAll(localList); 180 } 181 } 182 183 // if the result of the accumulation is empty, return a null item 184 if (accumulatedValues.size() == 0){ 185 return null; 186 } 187 188 return accumulatedValues; 189 } 190 181 191 protected void parse_xmlData(METSNamespace namespace, Element element) 182 192 { … … 190 200 continue; 191 201 } 192 202 193 203 Element child = (Element) children.item(c); 194 204 String childName = child.getNodeName(); … … 204 214 } 205 215 } 206 216 207 217 public void parse_mdWrap(Element element) 208 218 { … … 217 227 continue; 218 228 } 219 229 220 230 Element child = (Element) children.item(c); 221 231 String childName = child.getNodeName(); … … 274 284 } 275 285 276 277 /**278 * Parse an XML Element as a METS Descriptive Metadata section279 *280 * @param <code>Element</code> the XML element which represents the dmdSec itself281 */286 287 /** 288 * Parse an XML Element as a METS Descriptive Metadata section 289 * 290 * @param <code>Element</code> the XML element which represents the dmdSec itself 291 */ 282 292 /* public static METSDescriptive parseXML(Element element) 283 284 285 286 287 288 289 290 291 292 293 { // Note: no parsing of attributes required, we just move onto the metadata 294 // namespaces/sections themselves 295 String ID = element.getAttribute("ID"); 296 String label = element.getAttribute("GROUPID"); 297 METSDescriptive thisDescriptive = new METSDescriptive(ID, label); 298 299 NodeList children = element.getChildNodes(); 300 301 for (int c = 0; c < children.getLength(); c ++) 302 { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) { 293 303 continue; 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 304 } 305 306 Element childElement = (Element) children.item(c); 307 if (childElement.getNodeName().equals("mets:mdRef")) 308 { METSNamespace namespace = NamespaceFactory.parseXML(element); 309 } 310 else if (childElement.getNodeName().equals("mets:mdWrap")) 311 { METSNamespace namespace = NamespaceFactory.parseXML(element); 312 } 313 else 314 { // TODO: raise an error! 315 } 316 } 317 return thisDescriptive; 318 } 309 319 */ 310 320 311 /** 312 * Write the document metadata to a <code>PrintWriter</code> in METS 313 * XML format. 314 * 315 * @param <code>PrintWriter</code> the writer to use for output 316 */ 317 public void write(PrintWriter output) 318 { String tag = XMLTools.getOpenTag("mets", "dmdSec"); 319 tag = XMLTools.addAttribute(tag, "ID", this.ID); 320 tag = XMLTools.addAttribute(tag, "GROUPID", this.name); 321 output.println(tag); 322 323 Iterator keys = this.map.keySet().iterator(); 324 325 // get the keys one by one 326 while (keys.hasNext()) 327 { Object nextKey = keys.next(); 328 if (nextKey == null) 329 { continue; 330 } 331 String key = nextKey.toString(); 332 333 // get every copy of the current namespace name - namespaces may 334 // occur more than once, so this is a List 335 Iterator namespaces = this.map.getAll(key).iterator(); 336 337 // namespaces will write themselves... 338 while (namespaces.hasNext()) 339 { METSNamespace namespace = (METSNamespace) namespaces.next(); 340 341 namespace.write(output); 342 } 343 } 344 345 tag = XMLTools.getCloseTag("mets", "dmdSec"); 346 output.println(tag); 347 } 348 321 /** 322 * Write the document metadata to a <code>PrintWriter</code> in METS 323 * XML format. 324 * 325 * @param <code>PrintWriter</code> the writer to use for output 326 */ 327 public void write(PrintWriter output) 328 { 329 String tag = XMLTools.getOpenTag("mets", "dmdSec"); 330 tag = XMLTools.addAttribute(tag, "ID", this.ID); 331 tag = XMLTools.addAttribute(tag, "GROUPID", this.name); 332 output.println(tag); 333 334 Iterator keys = this.map.keySet().iterator(); 335 336 // get the keys one by one 337 while (keys.hasNext()){ 338 Object nextKey = keys.next(); 339 if (nextKey == null){ 340 continue; 341 } 342 String key = nextKey.toString(); 343 344 // get every copy of the current namespace name - namespaces may 345 // occur more than once, so this is a List 346 Iterator namespaces = this.map.getAll(key).iterator(); 347 348 // namespaces will write themselves... 349 while (namespaces.hasNext()){ 350 METSNamespace namespace = (METSNamespace) namespaces.next(); 351 352 namespace.write(output); 353 } 354 } 355 356 tag = XMLTools.getCloseTag("mets", "dmdSec"); 357 output.println(tag); 358 } 359 349 360 public void copy_metadata(METSDescriptive dst) 350 361 { 351 362 Iterator keys = this.map.keySet().iterator(); 352 363 353 364 // get the keys one by one 354 365 while (keys.hasNext()) { … … 358 369 } 359 370 String key = nextKey.toString(); 360 361 System.err.println("*** Namespace = " + key);362 371 363 372 // get every copy of the current namespace name - namespaces may … … 390 399 391 400 392 public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection) 393 { // check if this node is in the 394 int sqlId = -1; 395 396 // System.out.println("Writing " + connection.toString()); 397 398 GS3SQLSelect select = new GS3SQLSelect("metadata"); 399 select.addField("*"); 400 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetaID", "=", this.ID); 401 GS3SQLWhereItem item = new GS3SQLWhereItem("DocID", "=", document.getID().toString()); 402 GS3SQLWhere where = new GS3SQLWhere(whereItem); 403 where.add(item); 404 select.setWhere(where); 405 406 connection.execute(select.toString()); 407 408 try { 409 GS3SQLAction action; 410 411 ResultSet resultSet = connection.getResultSet(); 412 413 if (resultSet != null && 414 resultSet.first()) { 415 // the node already exists - no need to do anything as such 416 GS3SQLUpdate update = new GS3SQLUpdate("metadata"); 417 update.setWhere(where); 418 action = update; 419 } 420 else 421 { // Set result set to null, just in case next() didn't work above... 422 resultSet = null; 423 424 // It is a new node and needs writing 425 action = new GS3SQLInsert("metadata"); 426 427 action.addValue("DocID", document.getID().toString()); 428 action.addValue("MetaID", this.ID); 429 } 430 431 // always set the group identifier 432 action.addValue("GroupID", this.name); 433 434 // execute the action as required 435 connection.execute(action.toString()); 436 437 // create a new resultset if necessary 438 if (resultSet == null) { 439 // now execute the select statement again to get the new identifier 440 // 'MetadataRef' 441 // System.out.println(select.toString()); 401 public boolean writeSQL(DocumentInterface document, GS3SQLConnection connection) 402 { 403 // check if this node is in the 404 int sqlId = -1; 405 406 // System.out.println("Writing " + connection.toString()); 407 408 GS3SQLSelect select = new GS3SQLSelect("metadata"); 409 select.addField("*"); 410 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetaID", "=", this.ID); 411 GS3SQLWhereItem item = new GS3SQLWhereItem("DocID", "=", document.getID().toString()); 412 GS3SQLWhere where = new GS3SQLWhere(whereItem); 413 where.add(item); 414 select.setWhere(where); 415 442 416 connection.execute(select.toString()); 443 444 resultSet = connection.getResultSet(); 445 resultSet.first(); 446 } 447 448 // get the reference for this item... 449 sqlId = resultSet.getInt("MetadataRef"); 450 } 451 catch (SQLException sql) 452 { System.out.println(sql); 453 } 454 455 Iterator keys = this.map.keySet().iterator(); 456 457 // get the keys one by one 458 while (keys.hasNext()) 459 { Object nextKey = keys.next(); 460 if (nextKey == null) 461 { continue; 462 } 463 String key = nextKey.toString(); 464 465 // get every current namespace - namespaces may 466 // occur more than once, so this is a List 467 Iterator namespaces = this.map.getAll(key).iterator(); 468 469 // namespaces will write themselves... 470 while (namespaces.hasNext()) 471 { METSNamespace namespace = (METSNamespace) namespaces.next(); 472 473 if (!namespace.writeSQL(sqlId, connection)) 474 { return false; 475 } 476 } 477 } 478 return true; 479 } 480 481 public static METSDescriptive readSQL(DocumentInterface document, GS3SQLConnection connection, 482 ResultSet resultSet) 483 { 484 try { 485 String ID = resultSet.getString("MetaID"); 486 String name = resultSet.getString("GroupID"); 487 488 // create the metadata block object 489 METSDescriptive descriptive = new METSDescriptive(ID, name); 490 491 // get its metadata reference to retrieve namespaces 492 int metadataRef = resultSet.getInt("MetadataRef"); 493 494 // query the database for matching namespaces for this metadata block 495 GS3SQLSelect select = new GS3SQLSelect("namespaces"); 496 select.addField("*"); 497 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(metadataRef), 498 GS3SQLField.INTEGER_TYPE); 499 GS3SQLWhere where = new GS3SQLWhere(whereItem); 500 select.setWhere(where); 501 502 connection.execute(select.toString()); 503 504 // parse through the namespaces, calling the namespace class to create instances 505 // as it will 506 ResultSet namespaceSet = connection.getResultSet(); 507 if (namespaceSet != null && namespaceSet.first()) { 508 do { 509 METSNamespace namespace = METSNamespace.readSQL(connection, namespaceSet); 510 if (namespace != null) { 511 descriptive.addNamespace(namespace); 512 } 513 else { 514 System.out.println("Null namespace output"); 515 } 516 } 517 while (namespaceSet.next()); 518 } 519 520 return descriptive; 521 } 522 catch (SQLException sqlEx) 523 { System.out.println(sqlEx); 524 System.exit(1); 525 } 526 return null; 527 } 417 418 try { 419 GS3SQLAction action; 420 421 ResultSet resultSet = connection.getResultSet(); 422 423 if (resultSet != null && 424 resultSet.first()) { 425 // the node already exists - no need to do anything as such 426 GS3SQLUpdate update = new GS3SQLUpdate("metadata"); 427 update.setWhere(where); 428 action = update; 429 } 430 else 431 { // Set result set to null, just in case next() didn't work above... 432 resultSet = null; 433 434 // It is a new node and needs writing 435 action = new GS3SQLInsert("metadata"); 436 437 action.addValue("DocID", document.getID().toString()); 438 action.addValue("MetaID", this.ID); 439 } 440 441 // always set the group identifier 442 action.addValue("GroupID", this.name); 443 444 // execute the action as required 445 connection.execute(action.toString()); 446 447 // create a new resultset if necessary 448 if (resultSet == null) { 449 // now execute the select statement again to get the new identifier 450 // 'MetadataRef' 451 // System.out.println(select.toString()); 452 connection.execute(select.toString()); 453 454 resultSet = connection.getResultSet(); 455 resultSet.first(); 456 } 457 458 // get the reference for this item... 459 sqlId = resultSet.getInt("MetadataRef"); 460 } 461 catch (SQLException sql){ 462 System.out.println(sql); 463 } 464 465 Iterator keys = this.map.keySet().iterator(); 466 467 // get the keys one by one 468 while (keys.hasNext()){ 469 Object nextKey = keys.next(); 470 if (nextKey == null){ 471 continue; 472 } 473 String key = nextKey.toString(); 474 475 // get every current namespace - namespaces may 476 // occur more than once, so this is a List 477 Iterator namespaces = this.map.getAll(key).iterator(); 478 479 // namespaces will write themselves... 480 while (namespaces.hasNext()){ 481 METSNamespace namespace = (METSNamespace) namespaces.next(); 482 483 if (!namespace.writeSQL(sqlId, connection)){ 484 return false; 485 } 486 } 487 } 488 return true; 489 } 490 491 public static METSDescriptive readSQL(DocumentInterface document, GS3SQLConnection connection, 492 ResultSet resultSet) 493 { 494 try { 495 String ID = resultSet.getString("MetaID"); 496 String name = resultSet.getString("GroupID"); 497 498 // create the metadata block object 499 METSDescriptive descriptive = new METSDescriptive(ID, name); 500 501 // get its metadata reference to retrieve namespaces 502 int metadataRef = resultSet.getInt("MetadataRef"); 503 504 // query the database for matching namespaces for this metadata block 505 GS3SQLSelect select = new GS3SQLSelect("namespaces"); 506 select.addField("*"); 507 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("MetadataRef", "=", Integer.toString(metadataRef), 508 GS3SQLField.INTEGER_TYPE); 509 GS3SQLWhere where = new GS3SQLWhere(whereItem); 510 select.setWhere(where); 511 512 connection.execute(select.toString()); 513 514 // parse through the namespaces, calling the namespace class to create instances 515 // as it will 516 ResultSet namespaceSet = connection.getResultSet(); 517 if (namespaceSet != null && namespaceSet.first()) { 518 do { 519 METSNamespace namespace = METSNamespace.readSQL(connection, namespaceSet); 520 if (namespace != null) { 521 descriptive.addNamespace(namespace); 522 } 523 else { 524 System.out.println("Null namespace output"); 525 } 526 } 527 while (namespaceSet.next()); 528 } 529 530 return descriptive; 531 } 532 catch (SQLException sqlEx){ 533 System.out.println(sqlEx); 534 System.exit(1); 535 } 536 return null; 537 } 528 538 }
Note:
See TracChangeset
for help on using the changeset viewer.