Changeset 32860 for gs3-extensions/iiif-servlet/trunk/src/gsdl-src/java/org/greenstone/gsdl3/util/IIIFXML.java
- Timestamp:
- 2019-03-05T15:30:27+13:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-extensions/iiif-servlet/trunk/src/gsdl-src/java/org/greenstone/gsdl3/util/IIIFXML.java
r32842 r32860 44 44 public static final int ACTIVATION = 1; 45 45 46 // six valid oai verbs46 // one valid iiif verb // **** 47 47 public static final String GET_RECORD = "GetRecord"; 48 /* 49 public static final String LIST_RECORDS = "ListRecords"; 50 public static final String LIST_IDENTIFIERS = "ListIdentifiers"; 51 public static final String LIST_SETS = "ListSets"; 52 public static final String LIST_METADATA_FORMATS = "ListMetadataFormats"; 53 public static final String IDENTIFY = "Identify"; 54 */ 55 56 57 // oai request parameters 48 49 // iiif request parameters 58 50 public static final String IDENTIFIER = "identifier"; 59 /*60 public static final String METADATA_PREFIX = "metadataPrefix";61 public static final String FROM = "from";62 public static final String UNTIL = "until";63 public static final String SET = "set";64 public static final String RESUMPTION_TOKEN = "resumptionToken";65 public static final String IDENTIFIER = "identifier";66 */67 51 68 52 // Error element and code att … … 70 54 public static final String CODE = "code"; 71 55 72 // OAI error codes 73 public static final String ID_DOES_NOT_EXIST = "idDoesNotExist"; 74 /* 75 public static final String BAD_ARGUMENT = "badArgument"; 76 public static final String BAD_RESUMPTION_TOKEN = "badResumptionToken"; 77 public static final String BAD_VERB = "badVerb"; 78 public static final String CANNOT_DISSEMINATE_FORMAT = "cannotDisseminateFormat"; 79 public static final String NO_METADATA_FORMATS = "noMetadataFormats"; 80 public static final String NO_RECORDS_MATCH = "noRecordsMatch"; 81 public static final String NO_SET_HIERARCHY = "noSetHierarchy"; 82 */ 83 84 // words used to compose oai responses 85 // many of these used in OAIConfig too 56 // IIIF error codes 57 public static final String ID_DOES_NOT_EXIST = "idDoesNotExist"; 86 58 87 59 // General 88 89 public static final String RESPONSE_DATE = "responseDate";60 public static final String IIIF_PMH = "IIIF-PMH"; // **** 61 public static final String RESPONSE_DATE = "responseDate"; // **** 90 62 public static final String REQUEST = "request"; 91 92 // internal "OID" identifiers in the oai-inf db93 /** represents the timestamp of the OAI collection (when its oai-inf db was first created) */94 /*95 public static final String OAI_EARLIEST_TIMESTAMP_OID = "_earliesttimestamp";96 */97 63 98 64 // Identify data 99 // OAI replacing => IIIF, maybe want baseURL and oai-identifier 100 public static final String BASE_URL = "baseURL"; 101 /* 102 public static final String ADMIN_EMAIL = "adminEmail"; 65 // OAI replacing => IIIF, maybe want baseURL and oai-identifier 103 66 public static final String BASE_URL = "baseURL"; 104 public static final String COMPRESSION = "compression";105 public static final String DELETED_RECORD = "deletedRecord";106 public static final String DESCRIPTION = "description";107 public static final String EARLIEST_DATESTAMP = "earliestDatestamp"; // taken from buildconfig used as publishing date by RSS service108 public static final String EARLIEST_OAI_DATESTAMP = "earliestOAIDatestamp"; // earliest timestamp of an OAI collection stored in oai-inf db109 public static final String GRANULARITY = "granularity";110 public static final String LAST_MODIFIED = "lastmodified";111 public static final String PROTOCOL_VERSION = "protocolVersion";112 public static final String REPOSITORY_NAME = "repositoryName";113 public static final String OAI_IDENTIFIER = "oai-identifier";114 public static final String SCHEME = "scheme";115 public static final String REPOSITORY_IDENTIFIER = "repositoryIdentifier";116 public static final String DELIMITER = "delimiter";117 public static final String SAMPLE_IDENTIFIER = "sampleIdentifier";118 */119 120 /*121 // metadata formats122 public static final String METADATA_FORMAT = "metadataFormat";123 public static final String SCHEMA = "schema";124 public static final String METADATA_NAMESPACE = "metadataNamespace";125 public static final String META_FORMAT_DC = "oai_dc";126 */127 128 67 129 68 // record response data 130 69 // SET_SPEC 131 70 public static final String RECORD = "record"; 132 /* 133 public static final String HEADER = "header"; 134 public static final String DATESTAMP = "datestamp"; 135 public static final String METADATA = "metadata"; 136 */ 137 138 /* 139 // list sets 140 // SET, 141 public static final String SET_NAME = "setName"; 142 public static final String SET_SPEC = "setSpec"; 143 public static final String SET_DESCRIPTION = "setDescription"; 144 */ 145 146 /* 147 // resumption token element 148 public static final String RESUMPTION_TOKEN_ELEM = "resumptionToken"; 149 public static final String EXPIRATION_DATE = "expirationDate"; 150 public static final String COMPLETE_LIST_SIZE = "completeListSize"; 151 public static final String CURSOR = "cursor"; 152 */ 153 154 // extra elements/attributes from OAIConfig 155 /* 156 public static final String OAI_INFO = "oaiInfo"; 157 public static final String USE_STYLESHEET = "useOAIStylesheet"; 158 public static final String STYLESHEET = "OAIStylesheet"; 159 public static final String RESUME_AFTER = "resumeAfter"; 160 public static final String RESUMPTION_TOKEN_EXPIRATION = "resumptionTokenExpiration"; 161 public static final String OAI_SUPER_SET = "oaiSuperSet"; 162 public static final String ELEMENT = "element"; 163 public static final String ELEMENTS = "elements"; 164 public static final String MAPPING = "mapping"; 165 public static final String SELECT = "select"; 166 public static final String SELECT_SINGLE_VALUE = "firstvalue"; 167 public static final String SELECT_FIRST_VALID_META = "firstvalidmetadata"; 168 public static final String SELECT_ALL_VALUES = "allvalues"; 169 // code constants 170 public static final String GS_OAI_DATE_STAMP = "gs.OAIDateStamp"; 171 public static final String GS_OAI_RESOURCE_URL = "gs.OAIResourceURL"; 172 public static final String GSF_LINK_PREFIX = "gsflink."; 173 public static final String LINK_TYPE_DOCUMENT = "document"; 174 public static final String LINK_TYPE_SOURCE = "source"; 175 public static final String ILLEGAL_OAI_VERB = "Illegal OAI verb"; 176 public static final String LASTMODIFIED = "lastmodified"; 177 // // The node id in the collection database, which contains all the OIDs in the database 178 public static final String BROWSELIST = "browselist"; 179 public static final String OAI_LASTMODIFIED = "oailastmodified"; 180 */ 181 public static final String OAIPMH = "OAIPMH"; 71 72 public static final String OAIPMH = "OAIPMH"; // **** 182 73 183 74 //public static final String OAI_SET_LIST = "oaiSetList"; // **** 184 public static final String OAI_SERVICE_UNAVAILABLE = "OAI service unavailable"; 185 public static final String OID = "OID"; 186 /* 187 // The node id in the oai-inf database of the collection, which should contain all the OIDs in the db 188 public static final String OAI_INF_STATUS = "status"; // D = Deleted; E = Existing (PD = Provisionally Deleted but PD should not be present at this stage in the oai-inf database) 189 public static final String OAI_INF_TIMESTAMP = "timestamp"; // the time of deletion/last (re)indexing of doc 190 public static final String OAI_INF_DATESTAMP = "datestamp"; // date version of timestamp 191 // oai-inf db values for OAI_INF_STATUS 192 public static final String OAI_INF_DELETED = "D"; 193 public static final String OAI_INF_EXISTS = "E"; 194 public static final String OAI_INF_PROVISIONALLY_DELETED = "PD"; 195 // header values for ListRecords/GetRecord and listIdentifiers 196 public static final String HEADER_STATUS_ATTR_DELETED = "deleted"; 197 */ 75 //public static final String OAI_SERVICE_UNAVAILABLE = "OAI service unavailable"; 76 public static final String OID = "OID"; // **** 198 77 199 78 public static final String IIIF_SERVICE_RACK = "IIIFPMH"; 200 79 //system-dependent file separator, maybe '/' or '\' 201 public static final String FILE_SEPARATOR = File.separator;80 //public static final String FILE_SEPARATOR = File.separator; 202 81 //public static final String OAI_VERSION1 = "1.0"; 203 public static final String IIIF_VERSION2 = "2.1";82 public static final String IIIF_VERSION2 = "2.1"; // **** 204 83 /*************************above are final values****************************/ 205 84 … … 208 87 public static Element iiif_config_elem = null; 209 88 210 //stores the date format "yyyy-MM-ddTHH:mm:ssZ" 211 // this is the granularity for datestamps 212 /* 213 public static String granularity = ""; 214 */ 215 216 // http://www.openarchives.org/OAI/openarchivesprotocol.html#DatestampsRequests 217 // specifies that all repositories must support YYYY-MM-DD (yyyy-MM-dd in Java) 218 // this would be in addition to the other (optional) granularity of above that 219 // a repository may additionally choose to support. 220 /* 221 public static final String default_granularity = "yyyy-MM-dd"; 222 */ 223 224 //public static long token_expiration = 7200; 225 /** which version of oai that this oaiserver supports; default is 2.0 226 * initialized in getOAIConfigXML() 227 */ 228 public static final String iiif_version = "2.1"; 89 public static final String iiif_version = "2.1"; // **** 229 90 public static String baseURL = ""; 230 91 … … 273 134 return null; 274 135 } 275 276 //initialize oai_version 277 /* 278 Element protocol_version = (Element)GSXML.getChildByTagName(iiif_config_elem, PROTOCOL_VERSION); 279 iiif_version = GSXML.getNodeText(protocol_version).trim(); 280 */ 281 136 282 137 // initialize baseURL 283 138 Element base_url_elem = (Element)GSXML.getChildByTagName(iiif_config_elem, BASE_URL); 284 139 baseURL = GSXML.getNodeText(base_url_elem); 285 140 286 /*287 //initialize token_expiration288 Element expiration = (Element)GSXML.getChildByTagName(iiif_config_elem, RESUMPTION_TOKEN_EXPIRATION);289 String expire_str = GSXML.getNodeText(expiration).trim();290 if (expiration != null && !expire_str.equals("")) {291 token_expiration = Long.parseLong(expire_str);292 }293 */294 /*295 // read granularity from the config file296 Element granu_elem = (Element)GSXML.getChildByTagName(iiif_config_elem, GRANULARITY);297 //initialize the granu_str which might be used by other methods (eg, getDate())298 granularity = GSXML.getNodeText(granu_elem).trim();299 300 //change "yyyy-MM-ddTHH:mm:ssZ" to "yyyy-MM-dd'T'HH:mm:ss'Z'"301 granularity = granularity.replaceAll("T", "'T'");302 granularity = granularity.replaceAll("Z", "'Z'");303 granularity = granularity.replaceAll("YYYY", "yyyy").replaceAll("DD", "dd").replaceAll("hh", "HH");304 */305 306 141 return iiif_config_elem; 307 142 } 308 143 309 /*310 public static String[] getMetadataMapping(Element metadata_format) {311 312 if (metadata_format == null) {313 return null;314 }315 NodeList mappings = metadata_format.getElementsByTagName(MAPPING);316 int size = mappings.getLength();317 if (size == 0) {318 logger.info("No metadata mappings are provided in IIIFConfig.xml.");319 return null;320 }321 String[] names = new String[size];322 for (int i=0; i<size; i++) {323 names[i] = GSXML.getNodeText((Element)mappings.item(i)).trim();324 }325 return names;326 327 }328 329 public static String[] getGlobalMetadataMapping(String prefix) {330 Element list_meta_formats = (Element)GSXML.getChildByTagName(iiif_config_elem, LIST_METADATA_FORMATS);331 if(list_meta_formats == null) {332 return null;333 }334 Element metadata_format = GSXML.getNamedElement(list_meta_formats, METADATA_FORMAT, METADATA_PREFIX, prefix);335 if(metadata_format == null) {336 return null;337 }338 return getMetadataMapping(metadata_format);339 }340 */341 342 /** Copies out the main info from a metadataFormat element, leaving behind the mapping stuff. This gets the bit needed for OAI response */343 /*344 public static Element getMetadataFormatShort(Document doc, Element meta_format_long) {345 Element meta_fmt = doc.createElement(OAIXML.METADATA_FORMAT);346 // Copy in the elements that we want, and ignore the rest347 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.METADATA_PREFIX), true));348 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.SCHEMA), true));349 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.METADATA_NAMESPACE), true));350 351 return meta_fmt;352 }353 */354 355 /*356 public static long getTokenExpiration() {357 return token_expiration*1000; // in milliseconds358 }359 */360 144 361 145 /** TODO: returns a basic response for appropriate oai version 362 146 * 363 147 */ 364 public static Element createBasicResponse(Document doc, String verb, String[] pairs) {148 public static Element createBasicResponse(Document doc, String verb, String[] pairs) { // **** 365 149 366 150 Element response = createResponseHeader(doc, verb); … … 385 169 386 170 Node resp_date = GSXML.getChildByTagName(response, RESPONSE_DATE); // **** 387 /*388 if (resp_date != null) {389 GSXML.setNodeText((Element)resp_date, getCurrentUTCTime());390 }391 */392 171 393 172 return response; 394 173 } 174 395 175 /** @param error_code the value of the code attribute 396 176 * @param error_text the node text of the error element … … 497 277 * escaped sequences (eg, '%3A'). 498 278 */ 499 public static String oaiEncode(String original_str) {279 public static String oaiEncode(String original_str) { // **** 500 280 logger.info("oaiEncode() " + original_str); 501 281 for (int i=0; i<special_char.length; i++) { … … 506 286 return original_str; 507 287 } 508 /** convert YYYY-MM_DDThh:mm:ssZ to yyyy-MM-ddTHH:mm:ssZ 509 */ 510 /* 511 public static String convertToJava(String oai_format) { 512 oai_format = oai_format.replaceAll("YYYY", "yyyy").replaceAll("DD", "dd").replaceAll("hh", "HH"); 513 return oai_format; 514 } 515 */ 516 /** convert yyyy-MM-ddTHH:mm:ssZ to YYYY-MM_DDThh:mm:ssZ 517 */ 518 /* 519 public static String convertToOAI(String java_format) { 520 java_format = java_format.replaceAll("yyyy", "YYYY").replaceAll("dd", "DD").replaceAll("HH", "hh"); 521 return java_format; 522 } 523 public static String getCurrentUTCTime() { 524 Date current_utc = new Date(System.currentTimeMillis()); 525 //granularity is in the form: yyyy-MM-dd'T'HH:mm:ss'Z ' 526 DateFormat formatter = new SimpleDateFormat(granularity); 527 return formatter.format(current_utc); 528 } 529 */ 530 /** get a Date object from a Date format pattern string 531 * 532 * @param pattern - in the form: 2007-06-14T16:48:25Z, for example. 533 * @return a Date object - null if the pattern is not in the specified form 534 */ 535 /* 536 public static Date getDate(String pattern) { 537 if (pattern == null || pattern.equals("")) { 538 return null; 539 } 540 Date date = null; 541 // String str = pattern.replaceAll("T", " "); 542 // str = str.replaceAll("Z", ""); 543 SimpleDateFormat sdf = null; 544 try { 545 sdf = new SimpleDateFormat(granularity); 546 date = sdf.parse(pattern); 547 } catch(Exception e) { 548 if(!default_granularity.equals(granularity)) { // try validating against default granularity 549 try { 550 date = null; 551 sdf = null; 552 sdf = new SimpleDateFormat(default_granularity); 553 date = sdf.parse(pattern); 554 } catch(Exception ex) { 555 logger.error("invalid date format: " + pattern); 556 return null; 557 } 558 } else { 559 logger.error("invalid date format: " + pattern); 560 return null; 561 } 562 } 563 return date; 564 } 565 */ 566 /** get the million second value from a string representing time in a pattern 567 * (eg, 2007-06-14T16:48:25Z) 568 */ 569 /* 570 public static long getTime(String pattern) { 571 if (pattern == null || pattern.equals("")) { 572 return -1; 573 } 574 Date date = null; 575 SimpleDateFormat sdf = null; 576 try { 577 //granularity is a global variable in the form: yyyy-MM-ddTHH:mm:ssZ 578 sdf = new SimpleDateFormat(granularity); 579 date = sdf.parse(pattern); 580 } catch(Exception e) { 581 if(!default_granularity.equals(granularity)) { // try validating against default granularity 582 try { 583 date = null; 584 sdf = null; 585 sdf = new SimpleDateFormat(default_granularity); 586 date = sdf.parse(pattern); 587 } catch(Exception ex) { 588 logger.error("invalid date format: " + pattern); 589 return -1; 590 } 591 } else { 592 logger.error("invalid date format: " + pattern); 593 return -1; 594 } 595 } 596 return date.getTime(); 597 } 598 */ 599 /** get the string representation of a time from a long value(long type) 600 */ 601 /* 602 public static String getTime(long milliseconds) { 603 Date date = new Date(milliseconds); 604 SimpleDateFormat sdf = new SimpleDateFormat(granularity); 605 return sdf.format(date); 606 } 607 */ 288 608 289 public static Element createResponseHeader(Document response_doc, String verb) { 609 290 String tag_name = (iiif_version.equals(IIIF_VERSION2))? IIIF_PMH : verb; // **** 610 291 Element oai = response_doc.createElement(tag_name); 611 Element resp_date = response_doc.createElement(RESPONSE_DATE); 292 Element resp_date = response_doc.createElement(RESPONSE_DATE); // **** 612 293 Element req = response_doc.createElement(REQUEST); 613 294 oai.appendChild(resp_date); … … 626 307 } 627 308 628 /*629 public static Element getMetadataPrefixElement(Document doc, String prefix, Element meta_format) {630 631 Element ns_elem = (Element)GSXML.getChildByTagName(meta_format, METADATA_NAMESPACE);632 String namespace = null;633 if (ns_elem != null) {634 namespace = GSXML.getNodeText(ns_elem);635 }636 if (namespace == null || namespace.equals("")) {637 logger.error("No namespace URI found in metadataFormat elemnt for "+prefix);638 logger.error(XMLConverter.getPrettyString(meta_format));639 return null;640 }641 642 Element sc_elem = (Element)GSXML.getChildByTagName(meta_format, SCHEMA);643 String schema = null;644 if (sc_elem != null) {645 schema = GSXML.getNodeText(sc_elem);646 }647 if (schema == null || schema.equals("")) {648 logger.error("No schema found in metadataFormat element for "+prefix);649 logger.error(XMLConverter.getPrettyString(meta_format));650 return null;651 }652 653 String tag_name = getMetadataTagName(prefix, oai_version);654 Element oai = doc.createElement(tag_name);655 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");656 657 if (oai_version.equals(OAI_VERSION2)) {658 oai.setAttribute("xmlns:"+prefix, namespace);659 if (prefix.equals(META_FORMAT_DC)) {660 // there seems to be an extra one for dc661 oai.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");662 }663 oai.setAttribute("xsi:schemaLocation", namespace+" \n "+schema);664 } else {665 oai.setAttribute("xmlns", "http://www.openarchives.com/OAI/1.1/");666 if (prefix.equals(META_FORMAT_DC)) {667 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/1.1/" + tag_name + ".xsd");668 } else {669 oai.setAttribute("xsi:schemaLocation", schema);670 }671 }672 673 return oai;674 675 }676 677 public static String getMetadataTagName(String prefix, String oai_version) {678 if (prefix.equals(META_FORMAT_DC)) {679 if (oai_version.equals(OAI_VERSION2)) {680 return "oai_dc:dc";681 }682 return "dc";683 }684 return prefix;685 }686 687 public static HashMap<String, Node> getChildrenMapByTagName(Node n, String tag_name) {688 689 HashMap<String, Node> map= new HashMap<String, Node>();690 Node child = n.getFirstChild();691 while (child!=null) {692 String name = child.getNodeName();693 if(name.equals(tag_name)) {694 map.put(name, child);695 }696 child = child.getNextSibling();697 }698 return map;699 }700 */701 309 702 310 public static Element createOAIIdentifierXML(Document doc, String repository_id, String sample_collection, String sample_doc_id) { … … 712 320 String xml = "<gsdl xmlns=\"http://www.greenstone.org/namespace/gsdl_oaiinfo/1.0/gsdl_oaiinfo\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://www.greenstone.org/namespace/gsdl_oaiinfo/1.0/gsdl_oaiinfo\n http://www.greenstone.org/namespace/gsdl_oaiinfo/1.0/gsdl_oaiinfo.xsd\"></gsdl>"; 713 321 Document xml_doc = converter.getDOM(xml); 714 return (Element)doc.importNode(xml_doc.getDocumentElement(), true); 715 716 717 } 718 /* 719 public static Element createSet(Document doc, String spec, String name, String description) { 720 721 Element set_elem = doc.createElement(SET); 722 Element set_spec = doc.createElement(SET_SPEC); 723 GSXML.setNodeText(set_spec, spec); 724 set_elem.appendChild(set_spec); 725 Element set_name = doc.createElement(SET_NAME); 726 GSXML.setNodeText(set_name, name); 727 set_elem.appendChild(set_name); 728 if (description != null) { 729 Element set_description = doc.createElement(SET_DESCRIPTION); 730 GSXML.setNodeText(set_description, description); 731 set_elem.appendChild(set_description); 732 } 733 return set_elem; 734 735 } 736 */ 737 738 /** returns the resumptionToken element to go into an OAI response */ 739 /* 740 public static Element createResumptionTokenElement(Document doc, String token_name, int total_size, int cursor, long expiration_time) { 741 Element token = doc.createElement(OAIXML.RESUMPTION_TOKEN); 742 if (total_size != -1) { 743 token.setAttribute(OAIXML.COMPLETE_LIST_SIZE, "" + total_size); 744 } 745 if (cursor != -1) { 746 token.setAttribute(OAIXML.CURSOR, "" + cursor); 747 } 748 if(expiration_time !=-1) { 749 token.setAttribute(OAIXML.EXPIRATION_DATE, getTime(expiration_time)); 750 } 751 752 if (token != null) { 753 GSXML.setNodeText(token, token_name); 754 } 755 return token; 756 } 757 */ 322 return (Element)doc.importNode(xml_doc.getDocumentElement(), true); 323 } 758 324 } 759 325
Note:
See TracChangeset
for help on using the changeset viewer.