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

Last change on this file since 31911 was 31911, checked in by ak19, 7 years ago

First part of commit after using earliestDatestamp from oai-inf.db rather than build conf: 1. skip output of OAI records for internal 'OID' stored in oai-inf.db, whose entry represents the collection's earliest timestamp info. 2. Now prefer doc's lastmod date from oai-inf.db to doc's oailastmod date in indedb. So for all records, not just deleted records that only have entries in oaiinfdb and not indexdb.

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