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