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

Last change on this file since 32941 was 32941, checked in by kjdon, 5 years ago

gsflink.printdocument is a print friendly version of the document - basically most of the header is removed

File size: 28.4 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.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() {
220
221 File oai_config_file = null;
222
223 try {
224 URL oai_config_url = Class.forName("org.greenstone.gsdl3.OAIServer").getClassLoader().getResource("OAIConfig.xml");
225 if (oai_config_url == null) {
226 logger.error("couldn't find OAIConfig.xml via class loader");
227 return null;
228 }
229 oai_config_file = new File(oai_config_url.toURI());
230 if (!oai_config_file.exists()) {
231 logger.error(" oai config file: "+oai_config_file.getPath()+" not found!");
232 return null;
233 }
234 } catch(Exception e) {
235 logger.error("couldn't find OAIConfig.xml "+e.getMessage());
236 return null;
237 }
238
239 Document oai_config_doc = converter.getDOM(oai_config_file, "utf-8");
240 if (oai_config_doc != null) {
241 oai_config_elem = oai_config_doc.getDocumentElement();
242 } else {
243 logger.error("Failed to parse oai config file OAIConfig.xml.");
244 return null;
245 }
246
247 //initialize oai_version
248 Element protocol_version = (Element)GSXML.getChildByTagName(oai_config_elem, PROTOCOL_VERSION);
249 oai_version = GSXML.getNodeText(protocol_version).trim();
250
251 // initialize baseURL
252 Element base_url_elem = (Element)GSXML.getChildByTagName(oai_config_elem, BASE_URL);
253 baseURL = GSXML.getNodeText(base_url_elem);
254
255 //initialize token_expiration
256 Element expiration = (Element)GSXML.getChildByTagName(oai_config_elem, RESUMPTION_TOKEN_EXPIRATION);
257 String expire_str = GSXML.getNodeText(expiration).trim();
258 if (expiration != null && !expire_str.equals("")) {
259 token_expiration = Long.parseLong(expire_str);
260 }
261
262 // read granularity from the config file
263 Element granu_elem = (Element)GSXML.getChildByTagName(oai_config_elem, GRANULARITY);
264 //initialize the granu_str which might be used by other methods (eg, getDate())
265 granularity = GSXML.getNodeText(granu_elem).trim();
266
267 //change "yyyy-MM-ddTHH:mm:ssZ" to "yyyy-MM-dd'T'HH:mm:ss'Z'"
268 granularity = granularity.replaceAll("T", "'T'");
269 granularity = granularity.replaceAll("Z", "'Z'");
270 granularity = granularity.replaceAll("YYYY", "yyyy").replaceAll("DD", "dd").replaceAll("hh", "HH");
271 return oai_config_elem;
272 }
273
274 public static String[] getMetadataMapping(Element metadata_format) {
275
276 if (metadata_format == null) {
277 return null;
278 }
279 NodeList mappings = metadata_format.getElementsByTagName(MAPPING);
280 int size = mappings.getLength();
281 if (size == 0) {
282 logger.info("No metadata mappings are provided in OAIConfig.xml.");
283 return null;
284 }
285 String[] names = new String[size];
286 for (int i=0; i<size; i++) {
287 names[i] = GSXML.getNodeText((Element)mappings.item(i)).trim();
288 }
289 return names;
290
291 }
292
293 public static String[] getGlobalMetadataMapping(String prefix) {
294 Element list_meta_formats = (Element)GSXML.getChildByTagName(oai_config_elem, LIST_METADATA_FORMATS);
295 if(list_meta_formats == null) {
296 return null;
297 }
298 Element metadata_format = GSXML.getNamedElement(list_meta_formats, METADATA_FORMAT, METADATA_PREFIX, prefix);
299 if(metadata_format == null) {
300 return null;
301 }
302 return getMetadataMapping(metadata_format);
303 }
304
305 /** Copies out the main info from a metadataFormat element, leaving behind the mapping stuff. This gets the bit needed for OAI response */
306 public static Element getMetadataFormatShort(Document doc, Element meta_format_long) {
307 Element meta_fmt = doc.createElement(OAIXML.METADATA_FORMAT);
308 // Copy in the elements that we want, and ignore the rest
309 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.METADATA_PREFIX), true));
310 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.SCHEMA), true));
311 meta_fmt.appendChild(doc.importNode(GSXML.getChildByTagName(meta_format_long, OAIXML.METADATA_NAMESPACE), true));
312
313 return meta_fmt;
314 }
315
316
317 public static long getTokenExpiration() {
318 return token_expiration*1000; // in milliseconds
319 }
320
321 /** TODO: returns a basic response for appropriate oai version
322 *
323 */
324 public static Element createBasicResponse(Document doc, String verb, String[] pairs) {
325
326 Element response = createResponseHeader(doc, verb);
327
328 //set the responseDate and request elements accordingly
329 Element request_elem = (Element)GSXML.getChildByTagName(response, REQUEST);
330 if (verb.equals("")) {
331 request_elem.setAttribute(VERB, verb);
332 }
333 int num_pairs = (pairs==null)? 0 : pairs.length;
334 for (int i=num_pairs - 1; i>=0; i--) {
335 int index = pairs[i].indexOf("=");
336 if (index != -1) {
337 String[] strs = pairs[i].split("=");
338 if(strs != null && strs.length == 2) {
339 request_elem.setAttribute(strs[0], oaiDecode(strs[1]));
340 }
341 }
342 }//end of for()
343
344 GSXML.setNodeText(request_elem, baseURL);
345
346 Node resp_date = GSXML.getChildByTagName(response, RESPONSE_DATE);
347 if (resp_date != null) {
348 GSXML.setNodeText((Element)resp_date, getCurrentUTCTime());
349 }
350
351 return response;
352 }
353 /** @param error_code the value of the code attribute
354 * @param error_text the node text of the error element
355 * @return an oai error <message><response><error>
356 */
357 public static Element createErrorMessage(String error_code, String error_text) {
358 Document doc = converter.newDOM();
359 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
360 Element resp = doc.createElement(GSXML.RESPONSE_ELEM);
361 message.appendChild(resp);
362 Element error = createErrorElement(doc, error_code, error_text);
363 resp.appendChild(error);
364 return message;
365 }
366
367 /** @param error_code the value of the code attribute
368 * @param error_text the node text of the error element
369 * @return an oai error <response><error>
370 */
371 public static Element createErrorResponse(String error_code, String error_text) {
372 Document doc = converter.newDOM();
373 Element resp = doc.createElement(GSXML.RESPONSE_ELEM);
374 Element error = createErrorElement(doc, error_code, error_text);
375 resp.appendChild(error);
376 return resp;
377 }
378
379 /** @param error_code the value of the code attribute
380 * @param error_text the node text of the error element
381 * @return an oai error <error>
382 */
383 public static Element createErrorElement(Document doc, String error_code, String error_text) {
384 Element error = doc.createElement(ERROR);
385 error.setAttribute(CODE, error_code);
386 GSXML.setNodeText(error, error_text);
387 return error;
388 }
389
390 // This is the response message sent when there's a request to activate/deactivate a non-OAI collection
391 // A request to activate a non-existent/non-OAI collection is not invalid, it's just that we won't process it.
392 // So we still return status code OK (OK status code is needed for servercontrol.pm of activate.pl to recognise
393 // that the command had been "successful" when it runs de/activate).
394 public static Element createDeActivationOfNonOAICollResponse(int activationState, String collname) {
395 Document doc = converter.newDOM();
396 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
397 response.setAttribute("status", "OK");
398 String message = "collection: " + collname + " is not enabled for OAI.";
399 if(activationState == ACTIVATION) {
400 message += " Not attempting to activate it.";
401 } else {
402 message += " Not attempting to deactivate it.";
403 }
404 GSXML.setNodeText(response, message);
405 return response;
406 }
407
408 // The response message sent when a request comes in to activate/deactivate a proper OAI collection.
409 public static Element createActivationStateResponse(boolean success, int activationState, String collname) {
410 Document doc = converter.newDOM();
411 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
412 if (success) {
413 response.setAttribute("status", "OK");
414 if(activationState == ACTIVATION) {
415 GSXML.setNodeText(response, "collection: " + collname + " activated");
416 } else {
417 GSXML.setNodeText(response, "collection: " + collname + " deactivated");
418 }
419 } else {
420 response.setAttribute("status", "FAIL");
421 if(activationState == ACTIVATION) {
422 GSXML.setNodeText(response, "Failed to activate collection " + collname);
423 } else {
424 GSXML.setNodeText(response, "Failed to deactivate collection " + collname);
425 }
426 }
427 return response;
428 }
429
430 public static Element createResetResponse(boolean success) {
431 Document doc = converter.newDOM();
432 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
433 if (success) {
434 response.setAttribute("status", "OK");
435 GSXML.setNodeText(response, "Reset OAIServer successfully");
436 } else {
437 response.setAttribute("status", "FAIL");
438 GSXML.setNodeText(response, "Failed to reset oaiserver");
439 }
440 return response;
441 }
442 /** convert the escaped sequences (eg, '%3A') of those special characters back to their
443 * original form (eg, ':').
444 */
445 public static String oaiDecode(String escaped_str) {
446 logger.info("oaiDecode() " +escaped_str);
447 for (int i=0; i<special_char.length; i++) {
448 if (escaped_str.indexOf(escape_sequence[i]) != -1) {
449 escaped_str = escaped_str.replaceAll(escape_sequence[i], special_char[i]);
450 }
451 }
452 return escaped_str;
453 }
454 /** convert those special characters (eg, ':') to their
455 * escaped sequences (eg, '%3A').
456 */
457 public static String oaiEncode(String original_str) {
458 logger.info("oaiEncode() " + original_str);
459 for (int i=0; i<special_char.length; i++) {
460 if (original_str.indexOf(special_char[i]) != -1) {
461 original_str = original_str.replaceAll(special_char[i], escape_sequence[i]);
462 }
463 }
464 return original_str;
465 }
466 /** convert YYYY-MM_DDThh:mm:ssZ to yyyy-MM-ddTHH:mm:ssZ
467 */
468 public static String convertToJava(String oai_format) {
469 oai_format = oai_format.replaceAll("YYYY", "yyyy").replaceAll("DD", "dd").replaceAll("hh", "HH");
470 return oai_format;
471 }
472 /** convert yyyy-MM-ddTHH:mm:ssZ to YYYY-MM_DDThh:mm:ssZ
473 */
474 public static String convertToOAI(String java_format) {
475 java_format = java_format.replaceAll("yyyy", "YYYY").replaceAll("dd", "DD").replaceAll("HH", "hh");
476 return java_format;
477 }
478 public static String getCurrentUTCTime() {
479 Date current_utc = new Date(System.currentTimeMillis());
480 //granularity is in the form: yyyy-MM-dd'T'HH:mm:ss'Z '
481 DateFormat formatter = new SimpleDateFormat(granularity);
482 return formatter.format(current_utc);
483 }
484 /** get a Date object from a Date format pattern string
485 *
486 * @param pattern - in the form: 2007-06-14T16:48:25Z, for example.
487 * @return a Date object - null if the pattern is not in the specified form
488 */
489
490 public static Date getDate(String pattern) {
491 if (pattern == null || pattern.equals("")) {
492 return null;
493 }
494 Date date = null;
495 // String str = pattern.replaceAll("T", " ");
496 // str = str.replaceAll("Z", "");
497 SimpleDateFormat sdf = null;
498 try {
499 sdf = new SimpleDateFormat(granularity);
500 date = sdf.parse(pattern);
501 } catch(Exception e) {
502 if(!default_granularity.equals(granularity)) { // try validating against default granularity
503 try {
504 date = null;
505 sdf = null;
506 sdf = new SimpleDateFormat(default_granularity);
507 date = sdf.parse(pattern);
508 } catch(Exception ex) {
509 logger.error("invalid date format: " + pattern);
510 return null;
511 }
512 } else {
513 logger.error("invalid date format: " + pattern);
514 return null;
515 }
516 }
517 return date;
518 }
519 /** get the million second value from a string representing time in a pattern
520 * (eg, 2007-06-14T16:48:25Z)
521 */
522 public static long getTime(String pattern) {
523 if (pattern == null || pattern.equals("")) {
524 return -1;
525 }
526 Date date = null;
527 SimpleDateFormat sdf = null;
528 try {
529 //granularity is a global variable in the form: yyyy-MM-ddTHH:mm:ssZ
530 sdf = new SimpleDateFormat(granularity);
531 date = sdf.parse(pattern);
532 } catch(Exception e) {
533 if(!default_granularity.equals(granularity)) { // try validating against default granularity
534 try {
535 date = null;
536 sdf = null;
537 sdf = new SimpleDateFormat(default_granularity);
538 date = sdf.parse(pattern);
539 } catch(Exception ex) {
540 logger.error("invalid date format: " + pattern);
541 return -1;
542 }
543 } else {
544 logger.error("invalid date format: " + pattern);
545 return -1;
546 }
547 }
548 return date.getTime();
549 }
550 /** get the string representation of a time from a long value(long type)
551 */
552 public static String getTime(long milliseconds) {
553 Date date = new Date(milliseconds);
554 SimpleDateFormat sdf = new SimpleDateFormat(granularity);
555 return sdf.format(date);
556 }
557 public static Element createResponseHeader(Document response_doc, String verb) {
558 String tag_name = (oai_version.equals(OAI_VERSION2))? OAI_PMH : verb;
559 Element oai = response_doc.createElement(tag_name);
560 Element resp_date = response_doc.createElement(RESPONSE_DATE);
561 Element req = response_doc.createElement(REQUEST);
562 oai.appendChild(resp_date);
563 oai.appendChild(req);
564
565 if(oai_version.equals(OAI_VERSION2)) {
566 oai.setAttribute("xmlns", "http://www.openarchives.org/OAI/2.0/");
567 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
568 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0/ \n http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
569 } else {
570 oai.setAttribute("xmlns", "http://www.openarchives.com/OAI/1.1/OAI_" + verb);
571 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
572 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/1.1/OAI_" + verb + "\n http://www.openarchives.org/OAI/1.1/OAI_" + verb + ".xsd");
573 }
574 return oai;
575 }
576
577 public static Element getMetadataPrefixElement(Document doc, String prefix, Element meta_format) {
578
579 Element ns_elem = (Element)GSXML.getChildByTagName(meta_format, METADATA_NAMESPACE);
580 String namespace = null;
581 if (ns_elem != null) {
582 namespace = GSXML.getNodeText(ns_elem);
583 }
584 if (namespace == null || namespace.equals("")) {
585 logger.error("No namespace URI found in metadataFormat elemnt for "+prefix);
586 logger.error(XMLConverter.getPrettyString(meta_format));
587 return null;
588 }
589
590 Element sc_elem = (Element)GSXML.getChildByTagName(meta_format, SCHEMA);
591 String schema = null;
592 if (sc_elem != null) {
593 schema = GSXML.getNodeText(sc_elem);
594 }
595 if (schema == null || schema.equals("")) {
596 logger.error("No schema found in metadataFormat element for "+prefix);
597 logger.error(XMLConverter.getPrettyString(meta_format));
598 return null;
599 }
600
601 String tag_name = getMetadataTagName(prefix, oai_version);
602 Element oai = doc.createElement(tag_name);
603 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
604
605 if (oai_version.equals(OAI_VERSION2)) {
606 oai.setAttribute("xmlns:"+prefix, namespace);
607 if (prefix.equals(META_FORMAT_DC)) {
608 // there seems to be an extra one for dc
609 oai.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
610 }
611 oai.setAttribute("xsi:schemaLocation", namespace+" \n "+schema);
612 } else {
613 oai.setAttribute("xmlns", "http://www.openarchives.com/OAI/1.1/");
614 if (prefix.equals(META_FORMAT_DC)) {
615 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/1.1/" + tag_name + ".xsd");
616 } else {
617 oai.setAttribute("xsi:schemaLocation", schema);
618 }
619 }
620
621 return oai;
622
623 }
624
625 public static String getMetadataTagName(String prefix, String oai_version) {
626 if (prefix.equals(META_FORMAT_DC)) {
627 if (oai_version.equals(OAI_VERSION2)) {
628 return "oai_dc:dc";
629 }
630 return "dc";
631 }
632 return prefix;
633 }
634
635 public static HashMap<String, Node> getChildrenMapByTagName(Node n, String tag_name) {
636
637 HashMap<String, Node> map= new HashMap<String, Node>();
638 Node child = n.getFirstChild();
639 while (child!=null) {
640 String name = child.getNodeName();
641 if(name.equals(tag_name)) {
642 map.put(name, child);
643 }
644 child = child.getNextSibling();
645 }
646 return map;
647 }
648
649 public static Element createOAIIdentifierXML(Document doc, String repository_id, String sample_collection, String sample_doc_id) {
650 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>oai:"+repository_id+":"+sample_collection+":"+sample_doc_id+"</sampleIdentifier>\n</oai-identifier>";
651
652 Document xml_doc = converter.getDOM(xml);
653 return (Element)doc.importNode(xml_doc.getDocumentElement(), true);
654
655
656 }
657
658 public static Element createGSDLElement(Document doc) {
659 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>";
660 Document xml_doc = converter.getDOM(xml);
661 return (Element)doc.importNode(xml_doc.getDocumentElement(), true);
662
663
664 }
665
666 public static Element createSet(Document doc, String spec, String name, String description) {
667
668 Element set_elem = doc.createElement(SET);
669 Element set_spec = doc.createElement(SET_SPEC);
670 GSXML.setNodeText(set_spec, spec);
671 set_elem.appendChild(set_spec);
672 Element set_name = doc.createElement(SET_NAME);
673 GSXML.setNodeText(set_name, name);
674 set_elem.appendChild(set_name);
675 if (description != null) {
676 Element set_description = doc.createElement(SET_DESCRIPTION);
677 GSXML.setNodeText(set_description, description);
678 set_elem.appendChild(set_description);
679 }
680 return set_elem;
681
682 }
683
684 /** returns the resumptionToken element to go into an OAI response */
685 public static Element createResumptionTokenElement(Document doc, String token_name, int total_size, int cursor, long expiration_time) {
686 Element token = doc.createElement(OAIXML.RESUMPTION_TOKEN);
687 if (total_size != -1) {
688 token.setAttribute(OAIXML.COMPLETE_LIST_SIZE, "" + total_size);
689 }
690 if (cursor != -1) {
691 token.setAttribute(OAIXML.CURSOR, "" + cursor);
692 }
693 if(expiration_time !=-1) {
694 token.setAttribute(OAIXML.EXPIRATION_DATE, getTime(expiration_time));
695 }
696
697 if (token != null) {
698 GSXML.setNodeText(token, token_name);
699 }
700 return token;
701 }
702
703}
704
705
706
707
708
709
Note: See TracBrowser for help on using the repository browser.