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