source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/OAIXML.java

Last change on this file was 35225, checked in by kjdon, 3 years ago

OAI identifiers should be (I think??) oai:repository_id:coll_name:doc_id. So I have updated the code to use this instead of just coll_name:doc_id

File size: 28.7 KB
Line 
1/*
2 * OAIXML.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import org.greenstone.util.GlobalProperties;
22
23import org.w3c.dom.*;
24
25import java.io.*;
26import java.net.*;
27import java.util.*;
28import java.text.DateFormat;
29import java.text.SimpleDateFormat;
30
31// import file Logger.java
32import org.apache.log4j.*;
33
34/** these constants are used for the OAI service */
35public class OAIXML {
36
37 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.OAIXML.class.getName());
38
39 // the leading keyword of oai protocol
40 public static final String VERB = "verb";
41
42 // Possible states for non-OAI/non-verb activate/deactivate requests
43 public static final int DEACTIVATION = 0;
44 public static final int ACTIVATION = 1;
45
46 // six valid oai verbs
47 public static final String GET_RECORD = "GetRecord";
48 public static final String LIST_RECORDS = "ListRecords";
49 public static final String LIST_IDENTIFIERS = "ListIdentifiers";
50 public static final String LIST_SETS = "ListSets";
51 public static final String LIST_METADATA_FORMATS = "ListMetadataFormats";
52 public static final String IDENTIFY = "Identify";
53
54 // oai request parameters
55 public static final String METADATA_PREFIX = "metadataPrefix";
56 public static final String FROM = "from";
57 public static final String UNTIL = "until";
58 public static final String SET = "set";
59 public static final String RESUMPTION_TOKEN = "resumptionToken";
60 public static final String IDENTIFIER = "identifier";
61
62 // Error element and code att
63 public static final String ERROR = "error";
64 public static final String CODE = "code";
65
66 // OAI error codes
67 public static final String BAD_ARGUMENT = "badArgument";
68 public static final String BAD_RESUMPTION_TOKEN = "badResumptionToken";
69 public static final String BAD_VERB = "badVerb";
70 public static final String CANNOT_DISSEMINATE_FORMAT = "cannotDisseminateFormat";
71 public static final String ID_DOES_NOT_EXIST = "idDoesNotExist";
72 public static final String NO_METADATA_FORMATS = "noMetadataFormats";
73 public static final String NO_RECORDS_MATCH = "noRecordsMatch";
74 public static final String NO_SET_HIERARCHY = "noSetHierarchy";
75
76
77 // words used to compose oai responses
78 // many of these used in OAIConfig too
79
80 // General
81 public static final String OAI_PMH = "OAI-PMH";
82 public static final String RESPONSE_DATE = "responseDate";
83 public static final String REQUEST = "request";
84
85 // internal "OID" identifiers in the oai-inf db
86 /** represents the timestamp of the OAI collection (when its oai-inf db was first created) */
87 public static final String OAI_EARLIEST_TIMESTAMP_OID = "_earliesttimestamp";
88
89 // Identify data
90 public static final String ADMIN_EMAIL = "adminEmail";
91 public static final String BASE_URL = "baseURL";
92 public static final String COMPRESSION = "compression";
93 public static final String DELETED_RECORD = "deletedRecord";
94 public static final String DESCRIPTION = "description";
95 public static final String EARLIEST_DATESTAMP = "earliestDatestamp"; // taken from buildconfig used as publishing date by RSS service
96 public static final String EARLIEST_OAI_DATESTAMP = "earliestOAIDatestamp"; // earliest timestamp of an OAI collection stored in oai-inf db
97 public static final String GRANULARITY = "granularity";
98 public static final String LAST_MODIFIED = "lastmodified";
99 public static final String PROTOCOL_VERSION = "protocolVersion";
100 public static final String REPOSITORY_NAME = "repositoryName";
101 public static final String OAI_IDENTIFIER = "oai-identifier";
102 public static final String SCHEME = "scheme";
103 public static final String REPOSITORY_IDENTIFIER = "repositoryIdentifier";
104 public static final String DELIMITER = "delimiter";
105 public static final String SAMPLE_IDENTIFIER = "sampleIdentifier";
106
107 // metadata formats
108 public static final String METADATA_FORMAT = "metadataFormat";
109 public static final String SCHEMA = "schema";
110 public static final String METADATA_NAMESPACE = "metadataNamespace";
111 public static final String META_FORMAT_DC = "oai_dc";
112
113 // record response data
114 // SET_SPEC
115 public static final String RECORD = "record";
116 public static final String HEADER = "header";
117 public static final String DATESTAMP = "datestamp";
118 public static final String METADATA = "metadata";
119
120 // list sets
121 // SET,
122 public static final String SET_NAME = "setName";
123 public static final String SET_SPEC = "setSpec";
124 public static final String SET_DESCRIPTION = "setDescription";
125
126 // resumption token element
127 public static final String RESUMPTION_TOKEN_ELEM = "resumptionToken";
128 public static final String EXPIRATION_DATE = "expirationDate";
129 public static final String COMPLETE_LIST_SIZE = "completeListSize";
130 public static final String CURSOR = "cursor";
131
132 // extra elements/attributes from OAIConfig
133 public static final String OAI_INFO = "oaiInfo";
134 public static final String USE_STYLESHEET = "useOAIStylesheet";
135 public static final String STYLESHEET = "OAIStylesheet";
136 public static final String RESUME_AFTER = "resumeAfter";
137 public static final String RESUMPTION_TOKEN_EXPIRATION = "resumptionTokenExpiration";
138 public static final String OAI_SUPER_SET = "oaiSuperSet";
139 public static final String ELEMENT = "element";
140 public static final String ELEMENTS = "elements";
141 public static final String MAPPING = "mapping";
142 public static final String SELECT = "select";
143 public static final String SELECT_SINGLE_VALUE = "firstvalue";
144 public static final String SELECT_FIRST_VALID_META = "firstvalidmetadata";
145 public static final String SELECT_ALL_VALUES = "allvalues";
146 // code constants
147 public static final String GS_OAI_DATE_STAMP = "gs.OAIDateStamp";
148 public static final String GS_OAI_RESOURCE_URL = "gs.OAIResourceURL";
149 public static final String GSF_LINK_PREFIX = "gsflink.";
150 public static final String LINK_TYPE_DOCUMENT = "document";
151 public static final String LINK_TYPE_SOURCE = "source";
152 public static final String LINK_TYPE_PRINT = "printdocument";
153 public static final String ILLEGAL_OAI_VERB = "Illegal OAI verb";
154 public static final String LASTMODIFIED = "lastmodified";
155 // // The node id in the collection database, which contains all the OIDs in the database
156 public static final String BROWSELIST = "browselist";
157 public static final String OAI_LASTMODIFIED = "oailastmodified";
158 public static final String OAIPMH = "OAIPMH";
159 public static final String OAI_SET_LIST = "oaiSetList";
160 public static final String OAI_SERVICE_UNAVAILABLE = "OAI service unavailable";
161 public static final String OID = "OID";
162
163 // The node id in the oai-inf database of the collection, which should contain all the OIDs in the db
164 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)
165 public static final String OAI_INF_TIMESTAMP = "timestamp"; // the time of deletion/last (re)indexing of doc
166 public static final String OAI_INF_DATESTAMP = "datestamp"; // date version of timestamp
167 // oai-inf db values for OAI_INF_STATUS
168 public static final String OAI_INF_DELETED = "D";
169 public static final String OAI_INF_EXISTS = "E";
170 public static final String OAI_INF_PROVISIONALLY_DELETED = "PD";
171 // header values for ListRecords/GetRecord and listIdentifiers
172 public static final String HEADER_STATUS_ATTR_DELETED = "deleted";
173
174 public static final String OAI_SERVICE_RACK = "OAIPMH";
175 //system-dependent file separator, maybe '/' or '\'
176 public static final String FILE_SEPARATOR = File.separator;
177 public static final String OAI_VERSION1 = "1.0";
178 public static final String OAI_VERSION2 = "2.0";
179 /*************************above are final values****************************/
180
181
182 //initialized in getOAIConfigXML()
183 public static Element oai_config_elem = null;
184
185 //stores the date format "yyyy-MM-ddTHH:mm:ssZ"
186 // this is the granularity for datestamps
187 public static String granularity = "";
188
189 // http://www.openarchives.org/OAI/openarchivesprotocol.html#DatestampsRequests
190 // specifies that all repositories must support YYYY-MM-DD (yyyy-MM-dd in Java)
191 // this would be in addition to the other (optional) granularity of above that
192 // a repository may additionally choose to support.
193 public static final String default_granularity = "yyyy-MM-dd";
194
195 public static long token_expiration = 7200;
196 /** which version of oai that this oaiserver supports; default is 2.0
197 * initialized in getOAIConfigXML()
198 */
199 public static String oai_version = "2.0";
200 public static String baseURL = "";
201
202 /** Converter for parsing files and creating Elements */
203 public static XMLConverter converter = new XMLConverter();
204
205 public static String[] special_char = {"/", "?", "#", "=", "&", ":", ";", " ", "%", "+"};
206 public static String[] escape_sequence = {"%2F", "%3F", "%23", "%3D", "%26", "%3A", "%3B", "%20", "%25", "%2B"};
207
208 public static String getOAIVersion() {
209 return oai_version;
210 }
211
212 public static String getBaseURL() {
213 return baseURL;
214 }
215
216 /** Read in OAIConfig-xxx.xml (residing web/WEB-INF/classes/) and use it to configure the receptionist etc.
217 * the oai_version and baseURL variables are also set in here.
218 * The init() method is also called in here. */
219 public static Element getOAIConfigXML(String servlet_url_name) {
220
221 File oai_config_file = null;
222
223 String config_filename = "OAIConfig-"+servlet_url_name+".xml";
224 try {
225 URL oai_config_url = Class.forName("org.greenstone.gsdl3.OAIServer").getClassLoader().getResource(config_filename);
226 if (oai_config_url == null) {
227 logger.error("couldn't find "+config_filename+" via class loader");
228 return null;
229 }
230 oai_config_file = new File(oai_config_url.toURI());
231 if (!oai_config_file.exists()) {
232 logger.error(" oai config file: "+oai_config_file.getPath()+" not found!");
233 return null;
234 }
235 } catch(Exception e) {
236 logger.error("couldn't find "+config_filename+" "+e.getMessage());
237 return null;
238 }
239
240 Document oai_config_doc = converter.getDOM(oai_config_file, "utf-8");
241 if (oai_config_doc != null) {
242 oai_config_elem = oai_config_doc.getDocumentElement();
243 } else {
244 logger.error("Failed to parse oai config file OAIConfig.xml.");
245 return null;
246 }
247
248 //initialize oai_version
249 Element protocol_version = (Element)GSXML.getChildByTagName(oai_config_elem, PROTOCOL_VERSION);
250 oai_version = GSXML.getNodeText(protocol_version).trim();
251
252 // initialize baseURL
253 Element base_url_elem = (Element)GSXML.getChildByTagName(oai_config_elem, BASE_URL);
254 baseURL = GSXML.getNodeText(base_url_elem);
255
256 //initialize token_expiration
257 Element expiration = (Element)GSXML.getChildByTagName(oai_config_elem, RESUMPTION_TOKEN_EXPIRATION);
258 String expire_str = GSXML.getNodeText(expiration).trim();
259 if (expiration != null && !expire_str.equals("")) {
260 token_expiration = Long.parseLong(expire_str);
261 }
262
263 // read granularity from the config file
264 Element granu_elem = (Element)GSXML.getChildByTagName(oai_config_elem, GRANULARITY);
265 //initialize the granu_str which might be used by other methods (eg, getDate())
266 granularity = GSXML.getNodeText(granu_elem).trim();
267
268 //change "yyyy-MM-ddTHH:mm:ssZ" to "yyyy-MM-dd'T'HH:mm:ss'Z'"
269 granularity = granularity.replaceAll("T", "'T'");
270 granularity = granularity.replaceAll("Z", "'Z'");
271 granularity = granularity.replaceAll("YYYY", "yyyy").replaceAll("DD", "dd").replaceAll("hh", "HH");
272 return oai_config_elem;
273 }
274
275 public static String[] getMetadataMapping(Element metadata_format) {
276
277 if (metadata_format == null) {
278 return null;
279 }
280 NodeList mappings = metadata_format.getElementsByTagName(MAPPING);
281 int size = mappings.getLength();
282 if (size == 0) {
283 logger.info("No metadata mappings are provided in OAIConfig.xml.");
284 return null;
285 }
286 String[] names = new String[size];
287 for (int i=0; i<size; i++) {
288 names[i] = GSXML.getNodeText((Element)mappings.item(i)).trim();
289 }
290 return names;
291
292 }
293
294 public static String[] getGlobalMetadataMapping(String prefix) {
295 Element list_meta_formats = (Element)GSXML.getChildByTagName(oai_config_elem, LIST_METADATA_FORMATS);
296 if(list_meta_formats == null) {
297 return null;
298 }
299 Element metadata_format = GSXML.getNamedElement(list_meta_formats, METADATA_FORMAT, METADATA_PREFIX, prefix);
300 if(metadata_format == null) {
301 return null;
302 }
303 return getMetadataMapping(metadata_format);
304 }
305
306 /** Copies out the main info from a metadataFormat element, leaving behind the mapping stuff. This gets the bit needed for OAI response */
307 public static Element getMetadataFormatShort(Document doc, Element meta_format_long) {
308 Element meta_fmt = doc.createElement(OAIXML.METADATA_FORMAT);
309 // Copy in the elements that we want, and ignore the rest
310 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.METADATA_PREFIX), true));
311 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.SCHEMA), true));
312 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.METADATA_NAMESPACE), true));
313
314 return meta_fmt;
315 }
316
317
318 public static long getTokenExpiration() {
319 return token_expiration*1000; // in milliseconds
320 }
321
322 /** TODO: returns a basic response for appropriate oai version
323 *
324 */
325 public static Element createBasicResponse(Document doc, String verb, String[] pairs) {
326
327 Element response = createResponseHeader(doc, verb);
328
329 //set the responseDate and request elements accordingly
330 Element request_elem = (Element)GSXML.getChildByTagName(response, REQUEST);
331 if (verb.equals("")) {
332 request_elem.setAttribute(VERB, verb);
333 }
334 int num_pairs = (pairs==null)? 0 : pairs.length;
335 for (int i=num_pairs - 1; i>=0; i--) {
336 int index = pairs[i].indexOf("=");
337 if (index != -1) {
338 String[] strs = pairs[i].split("=");
339 if(strs != null && strs.length == 2) {
340 request_elem.setAttribute(strs[0], oaiDecode(strs[1]));
341 }
342 }
343 }//end of for()
344
345 GSXML.setNodeText(request_elem, baseURL);
346
347 Node resp_date = GSXML.getChildByTagName(response, RESPONSE_DATE);
348 if (resp_date != null) {
349 GSXML.setNodeText((Element)resp_date, getCurrentUTCTime());
350 }
351
352 return response;
353 }
354 /** @param error_code the value of the code attribute
355 * @param error_text the node text of the error element
356 * @return an oai error <message><response><error>
357 */
358 public static Element createErrorMessage(String error_code, String error_text) {
359 Document doc = converter.newDOM();
360 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
361 Element resp = doc.createElement(GSXML.RESPONSE_ELEM);
362 message.appendChild(resp);
363 Element error = createErrorElement(doc, error_code, error_text);
364 resp.appendChild(error);
365 return message;
366 }
367
368 /** @param error_code the value of the code attribute
369 * @param error_text the node text of the error element
370 * @return an oai error <response><error>
371 */
372 public static Element createErrorResponse(String error_code, String error_text) {
373 Document doc = converter.newDOM();
374 Element resp = doc.createElement(GSXML.RESPONSE_ELEM);
375 Element error = createErrorElement(doc, error_code, error_text);
376 resp.appendChild(error);
377 return resp;
378 }
379
380 /** @param error_code the value of the code attribute
381 * @param error_text the node text of the error element
382 * @return an oai error <error>
383 */
384 public static Element createErrorElement(Document doc, String error_code, String error_text) {
385 Element error = doc.createElement(ERROR);
386 error.setAttribute(CODE, error_code);
387 GSXML.setNodeText(error, error_text);
388 return error;
389 }
390
391 // This is the response message sent when there's a request to activate/deactivate a non-OAI collection
392 // A request to activate a non-existent/non-OAI collection is not invalid, it's just that we won't process it.
393 // So we still return status code OK (OK status code is needed for servercontrol.pm of activate.pl to recognise
394 // that the command had been "successful" when it runs de/activate).
395 public static Element createDeActivationOfNonOAICollResponse(int activationState, String collname) {
396 Document doc = converter.newDOM();
397 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
398 response.setAttribute("status", "OK");
399 String message = "collection: " + collname + " is not enabled for OAI.";
400 if(activationState == ACTIVATION) {
401 message += " Not attempting to activate it.";
402 } else {
403 message += " Not attempting to deactivate it.";
404 }
405 GSXML.setNodeText(response, message);
406 return response;
407 }
408
409 // The response message sent when a request comes in to activate/deactivate a proper OAI collection.
410 public static Element createActivationStateResponse(boolean success, int activationState, String collname) {
411 Document doc = converter.newDOM();
412 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
413 if (success) {
414 response.setAttribute("status", "OK");
415 if(activationState == ACTIVATION) {
416 GSXML.setNodeText(response, "collection: " + collname + " activated");
417 } else {
418 GSXML.setNodeText(response, "collection: " + collname + " deactivated");
419 }
420 } else {
421 response.setAttribute("status", "FAIL");
422 if(activationState == ACTIVATION) {
423 GSXML.setNodeText(response, "Failed to activate collection " + collname);
424 } else {
425 GSXML.setNodeText(response, "Failed to deactivate collection " + collname);
426 }
427 }
428 return response;
429 }
430
431 public static Element createResetResponse(boolean success) {
432 Document doc = converter.newDOM();
433 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
434 if (success) {
435 response.setAttribute("status", "OK");
436 GSXML.setNodeText(response, "Reset OAIServer successfully");
437 } else {
438 response.setAttribute("status", "FAIL");
439 GSXML.setNodeText(response, "Failed to reset oaiserver");
440 }
441 return response;
442 }
443 /** convert the escaped sequences (eg, '%3A') of those special characters back to their
444 * original form (eg, ':').
445 */
446 public static String oaiDecode(String escaped_str) {
447 logger.info("oaiDecode() " +escaped_str);
448 for (int i=0; i<special_char.length; i++) {
449 if (escaped_str.indexOf(escape_sequence[i]) != -1) {
450 escaped_str = escaped_str.replaceAll(escape_sequence[i], special_char[i]);
451 }
452 }
453 return escaped_str;
454 }
455 /** convert those special characters (eg, ':') to their
456 * escaped sequences (eg, '%3A').
457 */
458 public static String oaiEncode(String original_str) {
459 logger.info("oaiEncode() " + original_str);
460 for (int i=0; i<special_char.length; i++) {
461 if (original_str.indexOf(special_char[i]) != -1) {
462 original_str = original_str.replaceAll(special_char[i], escape_sequence[i]);
463 }
464 }
465 return original_str;
466 }
467 /** convert YYYY-MM_DDThh:mm:ssZ to yyyy-MM-ddTHH:mm:ssZ
468 */
469 public static String convertToJava(String oai_format) {
470 oai_format = oai_format.replaceAll("YYYY", "yyyy").replaceAll("DD", "dd").replaceAll("hh", "HH");
471 return oai_format;
472 }
473 /** convert yyyy-MM-ddTHH:mm:ssZ to YYYY-MM_DDThh:mm:ssZ
474 */
475 public static String convertToOAI(String java_format) {
476 java_format = java_format.replaceAll("yyyy", "YYYY").replaceAll("dd", "DD").replaceAll("HH", "hh");
477 return java_format;
478 }
479 public static String getCurrentUTCTime() {
480 Date current_utc = new Date(System.currentTimeMillis());
481 //granularity is in the form: yyyy-MM-dd'T'HH:mm:ss'Z '
482 DateFormat formatter = new SimpleDateFormat(granularity);
483 return formatter.format(current_utc);
484 }
485 /** get a Date object from a Date format pattern string
486 *
487 * @param pattern - in the form: 2007-06-14T16:48:25Z, for example.
488 * @return a Date object - null if the pattern is not in the specified form
489 */
490
491 public static Date getDate(String pattern) {
492 if (pattern == null || pattern.equals("")) {
493 return null;
494 }
495 Date date = null;
496 // String str = pattern.replaceAll("T", " ");
497 // str = str.replaceAll("Z", "");
498 SimpleDateFormat sdf = null;
499 try {
500 sdf = new SimpleDateFormat(granularity);
501 date = sdf.parse(pattern);
502 } catch(Exception e) {
503 if(!default_granularity.equals(granularity)) { // try validating against default granularity
504 try {
505 date = null;
506 sdf = null;
507 sdf = new SimpleDateFormat(default_granularity);
508 date = sdf.parse(pattern);
509 } catch(Exception ex) {
510 logger.error("invalid date format: " + pattern);
511 return null;
512 }
513 } else {
514 logger.error("invalid date format: " + pattern);
515 return null;
516 }
517 }
518 return date;
519 }
520 /** get the million second value from a string representing time in a pattern
521 * (eg, 2007-06-14T16:48:25Z)
522 */
523 public static long getTime(String pattern) {
524 if (pattern == null || pattern.equals("")) {
525 return -1;
526 }
527 Date date = null;
528 SimpleDateFormat sdf = null;
529 try {
530 //granularity is a global variable in the form: yyyy-MM-ddTHH:mm:ssZ
531 sdf = new SimpleDateFormat(granularity);
532 date = sdf.parse(pattern);
533 } catch(Exception e) {
534 if(!default_granularity.equals(granularity)) { // try validating against default granularity
535 try {
536 date = null;
537 sdf = null;
538 sdf = new SimpleDateFormat(default_granularity);
539 date = sdf.parse(pattern);
540 } catch(Exception ex) {
541 logger.error("invalid date format: " + pattern);
542 return -1;
543 }
544 } else {
545 logger.error("invalid date format: " + pattern);
546 return -1;
547 }
548 }
549 return date.getTime();
550 }
551 /** get the string representation of a time from a long value(long type)
552 */
553 public static String getTime(long milliseconds) {
554 Date date = new Date(milliseconds);
555 SimpleDateFormat sdf = new SimpleDateFormat(granularity);
556 return sdf.format(date);
557 }
558 public static Element createResponseHeader(Document response_doc, String verb) {
559 String tag_name = (oai_version.equals(OAI_VERSION2))? OAI_PMH : verb;
560 Element oai = response_doc.createElement(tag_name);
561 Element resp_date = response_doc.createElement(RESPONSE_DATE);
562 Element req = response_doc.createElement(REQUEST);
563 oai.appendChild(resp_date);
564 oai.appendChild(req);
565
566 if(oai_version.equals(OAI_VERSION2)) {
567 oai.setAttribute("xmlns", "http://www.openarchives.org/OAI/2.0/");
568 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
569 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0/ \n http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
570 } else {
571 oai.setAttribute("xmlns", "http://www.openarchives.com/OAI/1.1/OAI_" + verb);
572 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
573 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/1.1/OAI_" + verb + "\n http://www.openarchives.org/OAI/1.1/OAI_" + verb + ".xsd");
574 }
575 return oai;
576 }
577
578 public static Element getMetadataPrefixElement(Document doc, String prefix, Element meta_format) {
579
580 Element ns_elem = (Element)GSXML.getChildByTagName(meta_format, METADATA_NAMESPACE);
581 String namespace = null;
582 if (ns_elem != null) {
583 namespace = GSXML.getNodeText(ns_elem);
584 }
585 if (namespace == null || namespace.equals("")) {
586 logger.error("No namespace URI found in metadataFormat elemnt for "+prefix);
587 logger.error(XMLConverter.getPrettyString(meta_format));
588 return null;
589 }
590
591 Element sc_elem = (Element)GSXML.getChildByTagName(meta_format, SCHEMA);
592 String schema = null;
593 if (sc_elem != null) {
594 schema = GSXML.getNodeText(sc_elem);
595 }
596 if (schema == null || schema.equals("")) {
597 logger.error("No schema found in metadataFormat element for "+prefix);
598 logger.error(XMLConverter.getPrettyString(meta_format));
599 return null;
600 }
601
602 String tag_name = getMetadataTagName(prefix, oai_version);
603 Element oai = doc.createElement(tag_name);
604 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
605
606 if (oai_version.equals(OAI_VERSION2)) {
607 oai.setAttribute("xmlns:"+prefix, namespace);
608 if (prefix.equals(META_FORMAT_DC)) {
609 // there seems to be an extra one for dc
610 oai.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
611 }
612 oai.setAttribute("xsi:schemaLocation", namespace+" \n "+schema);
613 } else {
614 oai.setAttribute("xmlns", "http://www.openarchives.com/OAI/1.1/");
615 if (prefix.equals(META_FORMAT_DC)) {
616 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/1.1/" + tag_name + ".xsd");
617 } else {
618 oai.setAttribute("xsi:schemaLocation", schema);
619 }
620 }
621
622 return oai;
623
624 }
625
626 public static String getMetadataTagName(String prefix, String oai_version) {
627 if (prefix.equals(META_FORMAT_DC)) {
628 if (oai_version.equals(OAI_VERSION2)) {
629 return "oai_dc:dc";
630 }
631 return "dc";
632 }
633 return prefix;
634 }
635
636 public static HashMap<String, Node> getChildrenMapByTagName(Node n, String tag_name) {
637
638 HashMap<String, Node> map= new HashMap<String, Node>();
639 Node child = n.getFirstChild();
640 while (child!=null) {
641 String name = child.getNodeName();
642 if(name.equals(tag_name)) {
643 map.put(name, child);
644 }
645 child = child.getNextSibling();
646 }
647 return map;
648 }
649
650 public static String createOAIIdentifier(String repository_id, String collection, String doc_id) {
651 return "oai:"+repository_id+":"+collection+":"+doc_id;
652 }
653
654 public static Element createOAIIdentifierXML(Document doc, String repository_id, String sample_collection, String sample_doc_id) {
655 String sample_id = createOAIIdentifier(repository_id,sample_collection,sample_doc_id);
656 String xml = "<oai-identifier xmlns=\"http://www.openarchives.org/OAI/2.0/oai-identifier\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai-identifier\n http://www.openarchives.org/OAI/2.0/oai-identifier.xsd\">\n <scheme>oai</scheme>\n<repositoryIdentifier>" + repository_id + "</repositoryIdentifier>\n<delimiter>:</delimiter>\n<sampleIdentifier>"+sample_id+"</sampleIdentifier>\n</oai-identifier>";
657
658 Document xml_doc = converter.getDOM(xml);
659 return (Element)doc.importNode(xml_doc.getDocumentElement(), true);
660
661
662 }
663
664 public static Element createGSDLElement(Document doc) {
665 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>";
666 Document xml_doc = converter.getDOM(xml);
667 return (Element)doc.importNode(xml_doc.getDocumentElement(), true);
668
669
670 }
671
672 public static Element createSet(Document doc, String spec, String name, String description) {
673
674 Element set_elem = doc.createElement(SET);
675 Element set_spec = doc.createElement(SET_SPEC);
676 GSXML.setNodeText(set_spec, spec);
677 set_elem.appendChild(set_spec);
678 Element set_name = doc.createElement(SET_NAME);
679 GSXML.setNodeText(set_name, name);
680 set_elem.appendChild(set_name);
681 if (description != null) {
682 Element set_description = doc.createElement(SET_DESCRIPTION);
683 GSXML.setNodeText(set_description, description);
684 set_elem.appendChild(set_description);
685 }
686 return set_elem;
687
688 }
689
690 /** returns the resumptionToken element to go into an OAI response */
691 public static Element createResumptionTokenElement(Document doc, String token_name, int total_size, int cursor, long expiration_time) {
692 Element token = doc.createElement(OAIXML.RESUMPTION_TOKEN);
693 if (total_size != -1) {
694 token.setAttribute(OAIXML.COMPLETE_LIST_SIZE, "" + total_size);
695 }
696 if (cursor != -1) {
697 token.setAttribute(OAIXML.CURSOR, "" + cursor);
698 }
699 if(expiration_time !=-1) {
700 token.setAttribute(OAIXML.EXPIRATION_DATE, getTime(expiration_time));
701 }
702
703 if (token != null) {
704 GSXML.setNodeText(token, token_name);
705 }
706 return token;
707 }
708
709}
710
711
712
713
714
715
Note: See TracBrowser for help on using the repository browser.