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

Last change on this file since 32212 was 32212, checked in by kjdon, 6 years ago

added in strings for gsflink.document/source

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