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