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

Last change on this file since 21780 was 21780, checked in by kjdon, 14 years ago

removed getCollectionConfigXML as it should not be here. added a coupld more strings

File size: 29.1 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.w3c.dom.*;
22import java.io.*;
23import java.net.*;
24import java.util.*;
25import java.text.DateFormat;
26import java.text.SimpleDateFormat;
27import org.apache.xerces.parsers.*;
28import org.apache.xml.serialize.*;
29
30// SAX
31import org.xml.sax.XMLReader;
32import org.xml.sax.SAXException;
33import org.xml.sax.SAXParseException;
34import org.xml.sax.helpers.DefaultHandler;
35import org.xml.sax.InputSource;
36
37// JAXP
38import javax.xml.parsers.FactoryConfigurationError;
39import javax.xml.parsers.ParserConfigurationException;
40import javax.xml.parsers.SAXParser;
41import javax.xml.parsers.SAXParserFactory;
42
43// import file Logger.java
44import org.apache.log4j.*;
45
46/** these constants are used for the OAI service */
47public class OAIXML {
48
49 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSXML.class.getName());
50
51 // the leading keyword of oai protocol
52 public static final String VERB = "verb";
53
54 // six valid oai verbs
55 public static final String GET_RECORD = "GetRecord";
56 public static final String LIST_RECORDS = "ListRecords";
57 public static final String LIST_IDENTIFIERS = "ListIdentifiers";
58 public static final String LIST_SETS = "ListSets";
59 public static final String LIST_METADATA_FORMATS = "ListMetadataFormats";
60 public static final String IDENTIFY = "Identify";
61
62 // other valid oai parameters
63 public static final String OAI_METADATAFORMAT = "OAIMetadataFormat";
64 public static final String METADATA_NAMESPACE = "metadataNamespace";
65 public static final String OAI_DC = "oai_dc";
66 public static final String DC = "dc";
67 public static final String METADATA_PREFIX = "metadataPrefix";
68 public static final String FROM = "from";
69 public static final String UNTIL = "until";
70 public static final String SET = "set";
71 public static final String RESUMPTION_TOKEN = "resumptionToken";
72 public static final String RESUMPTION_TOKEN_EXPIRATION = "resumptionTokenExpiration";
73 public static final String IDENTIFIER = "identifier";
74
75 public static final String USE_STYLESHEET = "useOAIStylesheet";
76 public static final String STYLESHEET = "OAIStylesheet";
77 // words used to compose oai responses
78 public static final String ADMIN_EMAIL = "adminEmail";
79 public static final String BAD_ARGUMENT = "badArgument";
80 public static final String BAD_RESUMPTION_TOKEN = "badResumptionToken";
81 public static final String BAD_VERB = "badVerb";
82 public static final String BASE_URL = "baseURL";
83 public static final String CANNOT_DISSEMINATE_FORMAT = "cannotDisseminateFormat";
84 public static final String CODE = "code";
85 public static final String COLLECTION = "collection";
86 public static final String COLLECTION_LIST = "collectionList";
87 public static final String COMPLETE_LIST_SIZE = "completeListSize";
88 public static final String COMPRESSION = "compression";
89 public static final String CURSOR = "cursor";
90 public static final String DATESTAMP = "datestamp";
91 public static final String DC_METADATA_NAMES = "DCMetadataNames";
92 public static final String DELETED_RECORD = "deletedRecord";
93 public static final String DESCRIPTION = "description";
94 public static final String EARLIEST_DATESTAMP = "earliestDatestamp";
95 public static final String ERROR = "error";
96 public static final String EXPIRATION_DATE = "expirationDate";
97 public static final String GRANULARITY = "granularity";
98 public static final String GS3OAI = "GS3OAI";
99 public static final String HAS_OAI = "hasOAI";
100 public static final String HEADER = "header";
101 public static final String ILLEGAL_OAI_VERB = "Illegal OAI verb";
102 public static final String INDEX_STEM = "indexStem";
103 public static final String LASTMODIFIED = "lastmodified";
104 public static final String MAPPING = "mapping";
105 public static final String MAPPING_LIST = "mappingList";
106 public static final String MESSAGE = "message";
107 public static final String METADATA = "metadata";
108 public static final String METADATA_FORMAT = "metadataFormat";
109 public static final String NAME = "name";
110 public static final String NO_RECORDS_MATCH = "noRecordsMatch";
111 public static final String OAI = "OAI";
112 public static final String OAI_DASH_PMH = "OAI-PMH";
113 public static final String OAIPMH = "OAIPMH";
114 public static final String OAI_RESUMPTION_TOKENS = "OAIResumptionTokens";
115 public static final String OAI_SERVICE = "oaiService";
116 public static final String OAI_SET_LIST = "oaiSetList";
117 public static final String OAI_SERVICE_UNAVAILABLE = "OAI service unavailable";
118 public static final String OID = "OID";
119 public static final String PARAM = "param";
120 public static final String PARAM_LIST = "paramList";
121 public static final String PROTOCOL_VERSION = "protocolVersion";
122 public static final String RECORD = "record";
123 public static final String REQUEST = "request";
124 public static final String REPOSITORY_NAME = "repositoryName";
125 public static final String RESPONSE = "response";
126 public static final String RESPONSE_DATE = "responseDate";
127 public static final String RESUME_AFTER = "resumeAfter";
128 public static final String SCHEMA = "schema";
129 public static final String SERVICE = "service";
130 public static final String SERVICE_UNAVAILABLE = "service unavailable";
131 public static final String SET_SPEC = "setSpec";
132 public static final String SET_NAME = "setName";
133 public static final String SET_DESCRIPTION = "setDescription";
134 public static final String SITE = "site";
135 public static final String TO = "to";
136 public static final String TYPE = "type";
137 public static final String VALUE = "value";
138
139 //Two error and exception conditions for the verb 'ListMetadataFormats'
140 public static final String ID_DOES_NOT_EXIST = "idDoesNotExist";
141 public static final String NO_METADATA_FORMATS = "noMetadataFormats";
142
143 // The node id in the collection database, which contains all the OIDs in the database
144 public static final String BROWSELIST = "browselist";
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 public static Element resumption_token_elem = null;
153 //used when saving the token file
154 public static File resumption_token_file = null;
155 //public static ArrayList token_list = new ArrayList();
156
157 //initialized in getOAIConfigXML()
158 public static Element oai_config_elem = null;
159
160 //stores the date format "yyyy-MM-ddTHH:mm:ssZ"
161 public static String granularity = "";
162 //this value is overriden in getOAIConfigXML()
163 public static long token_expiration = 7200;
164
165 /** which version of oai that this oaiserver supports; default is 2.0
166 * initialized in getOAIConfigXML()
167 */
168 public static String oai_version = "2.0";
169
170 /**response owner document */
171 public static Document response_doc = new XMLConverter().newDOM();
172
173 public static String[] special_char = {"/", "?", "#", "=", "&", ":", ";", " ", "%", "+"};
174 public static String[] escape_sequence = {"%2F", "%3F", "%23", "%3D", "%26", "%3A", "%3B", "%20", "%25", "%2B"};
175// /** key=special character; value=escaped sequence */
176// public static HashMap encode_map = new HashMap();
177// /** key=escaped sequence; value=special character */
178// public static HashMap decode_map = new HashMap();
179
180 public static void init() {
181 resumption_token_elem = getOAIResumptionTokenXML();
182 }
183 public static String getOAIVersion() {
184 return oai_version;
185 }
186 public static Element createElement(String tag_name) {
187 return response_doc.createElement(tag_name);
188 }
189 /**Compose a response element used when OAIPMH service sending responses thru
190 * ServiceCluster and MessageRouter, as they automatically wrap a message element
191 * on this response element
192 */
193 public static Element getResponse(Element core_msg) {
194 Element res = createElement(RESPONSE);
195 res.appendChild(response_doc.importNode(core_msg, true));
196 return res;
197 }
198 /** Read in OAIResumptionToken.xml (residing web/WEB-INF/classes/) */
199 public static Element getOAIResumptionTokenXML() {
200
201 // The system environment variable $GSDL3HOME(ends ../web) does not contain the file separator
202 resumption_token_file = new File(GlobalProperties.getGSDL3Home() + FILE_SEPARATOR +
203 "WEB-INF" + FILE_SEPARATOR + "classes" +FILE_SEPARATOR + "OAIResumptionToken.xml");
204 if (resumption_token_file.exists()) {
205 Document token_doc = parseXMLFile(resumption_token_file);
206 if (token_doc != null) {
207 resumption_token_elem = token_doc.getDocumentElement();
208 } else {
209 logger.error("Fail to parse resumption token file OAIReceptionToken.xml.");
210 return null;
211 }
212 //remove all expired tokens
213 clearExpiredTokens();
214 return resumption_token_elem;
215 }
216 //if resumption_token_file does not exist
217 logger.info("resumption token file: "+ resumption_token_file.getPath()+" not found! create an empty one.");
218 resumption_token_elem = createElement(OAI_RESUMPTION_TOKENS);
219 saveOAIResumptionTokenXML(resumption_token_elem);
220 return resumption_token_elem;
221 }
222 public static void saveOAIResumptionTokenXML(Element token_elem) {
223 if(writeXMLFile(resumption_token_file, token_elem.getOwnerDocument()) == false) {
224 logger.error("Fail to save the resumption token file");
225 }
226 }
227 public static void clearExpiredTokens() {
228 boolean token_deleted = false;
229 NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, RESUMPTION_TOKEN);
230 for (int i=0; i<tokens.getLength(); i++) {
231 Element token_elem = (Element)tokens.item(i);
232 String expire_str = token_elem.getAttribute(EXPIRATION_DATE);
233 long datestamp = getTime(expire_str);
234 if(datestamp < System.currentTimeMillis()) {
235 resumption_token_elem.removeChild(token_elem);
236 token_elem = null;
237 token_deleted = true;
238 }
239 }
240
241 if(token_deleted) {
242 saveOAIResumptionTokenXML(resumption_token_elem);
243 }
244 }
245 public static boolean containsToken(String token) {
246 NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, OAIXML.RESUMPTION_TOKEN);
247 for (int i=0; i<tokens.getLength(); i++) {
248 if(token.equals(GSXML.getNodeText((Element)tokens.item(i)).trim() ))
249 return true;
250 }
251 return false;
252 }
253 public static void addToken(Element token) {
254 Document doc = resumption_token_elem.getOwnerDocument();
255 resumption_token_elem.appendChild(duplicateElement(doc, token, true));
256 saveOAIResumptionTokenXML(resumption_token_elem);
257 }
258 public static void addToken(String token) {
259 Element te = resumption_token_elem.getOwnerDocument().createElement(OAIXML.RESUMPTION_TOKEN);
260 //add expiration att
261 resumption_token_elem.appendChild(te);
262 saveOAIResumptionTokenXML(resumption_token_elem);
263 }
264 public static boolean removeToken(String token) {
265 NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, OAIXML.RESUMPTION_TOKEN);
266 int num_tokens = tokens.getLength();
267 for (int i=0; i<num_tokens; i++) {
268 Element e = (Element)(tokens.item(i));
269 if(token.equals(GSXML.getNodeText(e))) {
270 resumption_token_elem.removeChild(e);
271 saveOAIResumptionTokenXML(resumption_token_elem);
272 return true;
273 }
274 }
275 return false;
276 }
277 /** Read in OAIConfig.xml (residing web/WEB-INF/classes/) and use it to configure the receptionist etc.
278 * the oai_version variable is also set in here.
279 * The init() method is also called in here. */
280 public static Element getOAIConfigXML() {
281 init();
282
283 // The system environment variable $GSDL3HOME(ends ../web) does not contain the file separator
284 File oai_config_file = new File(GlobalProperties.getGSDL3Home() + FILE_SEPARATOR +
285 "WEB-INF" + FILE_SEPARATOR + "classes" +FILE_SEPARATOR + "OAIConfig.xml");
286 if (!oai_config_file.exists()) {
287 logger.error(" oai config file: "+oai_config_file.getPath()+" not found!");
288 return null;
289 }
290 Document oai_config_doc = parseXMLFile(oai_config_file);
291 if (oai_config_doc != null) {
292 oai_config_elem = oai_config_doc.getDocumentElement();
293 } else {
294 logger.error("Fail to parse oai config file OAIConfig.xml.");
295 return null;
296 }
297
298 //initialize oai_version
299 Element protocol_version = (Element)GSXML.getChildByTagName(oai_config_elem, PROTOCOL_VERSION);
300 oai_version = GSXML.getNodeText(protocol_version).trim();
301
302 //initialize token_expiration
303 Element expiration = (Element)GSXML.getChildByTagName(oai_config_elem, RESUMPTION_TOKEN_EXPIRATION);
304 String expire_str = GSXML.getNodeText(expiration).trim();
305 if (expiration != null && !expire_str.equals("")) {
306 token_expiration = Long.parseLong(expire_str);
307 }
308
309 // read granularity from the config file
310 Element granu_elem = (Element)GSXML.getChildByTagName(oai_config_elem, GRANULARITY);
311 //initialize the granu_str which might be used by other methods (eg, getDate())
312 granularity = GSXML.getNodeText(granu_elem).trim();
313 //change "yyyy-MM-ddTHH:mm:ssZ" to "yyyy-MM-dd'T'HH:mm:ss'Z'"
314 granularity = granularity.replaceAll("T", "'T'");
315 granularity = granularity.replaceAll("Z", "'Z'");
316 granularity = granularity.replaceAll("YYYY", "yyyy").replaceAll("DD", "dd").replaceAll("hh", "HH");
317 return oai_config_elem;
318 }
319 public static String[] getGlobalMetadataMapping(String prefix) {
320 Element list_meta_formats = (Element)GSXML.getChildByTagName(oai_config_elem, LIST_METADATA_FORMATS);
321 if(list_meta_formats == null) {
322 return null;
323 }
324 Element metadata_format = GSXML.getNamedElement(list_meta_formats, METADATA_FORMAT, METADATA_PREFIX, prefix);
325 if(metadata_format == null) {
326 return null;
327 }
328 NodeList mappings = metadata_format.getElementsByTagName(MAPPING);
329 int size = mappings.getLength();
330 if (size == 0) {
331 logger.info("No metadata mappings are provided in OAIConfig.xml.");
332 return null;
333 }
334 String[] names = new String[size];
335 for (int i=0; i<size; i++) {
336 names[i] = GSXML.getNodeText((Element)mappings.item(i)).trim();
337 }
338 return names;
339 }
340 public static String[] getDublinCoreNames() {
341 // read the standard Dublin Core metadata names
342 //<DCmetadataNames>dc.Title,dc.Creator,dc.Subject,dc.Description,dc.Publisher,dc.Contributor,dc.Date,dc.Type,dc.Format,dc.Identifier,dc.Source,dc.Language,dc.Relation,dc.Coverage,dc.Rights</DCmetadataNames>
343 Element dc_metadata_names = (Element)GSXML.getChildByTagName(oai_config_elem, DC_METADATA_NAMES);
344 if(dc_metadata_names == null) {
345 logger.error("Dublin Core metadata names are not provided.");
346 return null;
347 }
348 String names = GSXML.getNodeText(dc_metadata_names).trim();
349 return names.split(",");
350// String[] str = {"dc.Title","dc.Creator","dc.Subject","dc.Description","dc.Publisher","dc.Contributor","dc.Date","dc.Type","dc.Format","dc.Identifier","dc.Source","dc.Language","dc.Relation","dc.Coverage","dc.Rights"};
351// return str;
352 }
353
354 public static long getTokenExpiration() {
355 return token_expiration*1000;
356 }
357
358 /** TODO: returns a basic response for appropriate oai version
359 *
360 */
361 public static Element createBasicResponse(String verb, String[] pairs) {
362
363 Element response = createResponseHeader(verb);
364
365 //set the responseDate and request elements accordingly
366 Element request_elem = (Element)GSXML.getChildByTagName(response, REQUEST);
367 if (verb.equals("")) {
368 request_elem.setAttribute(VERB, verb);
369 }
370 int num_pairs = (pairs==null)? 0 : pairs.length;
371 for (int i=num_pairs - 1; i>=0; i--) {
372 int index = pairs[i].indexOf("=");
373 if (index != -1) {
374 String[] strs = pairs[i].split("=");
375 if(strs != null && strs.length == 2) {
376 request_elem.setAttribute(strs[0], oaiDecode(strs[1]));
377 }
378 }
379 }//end of for()
380 Element base_url_elem = (Element)GSXML.getChildByTagName(oai_config_elem, BASE_URL);
381 String base_url = GSXML.getNodeText(base_url_elem);
382 GSXML.setNodeText(request_elem, base_url);
383
384 Node resp_date = GSXML.getChildByTagName(response, RESPONSE_DATE);
385 if (resp_date != null) {
386 GSXML.setNodeText((Element)resp_date, getCurrentUTCTime());
387 }
388
389 return response;
390 }
391 /** @param error_code the value of the code attribute
392 * @param error_text the node text of the error element
393 * @return an oai error element
394 * Used by receptionist
395 */
396 public static Element createErrorElement(String error_code, String error_text) {
397 Element error = createElement(ERROR);
398 error.setAttribute(CODE, error_code);
399 GSXML.setNodeText(error, error_text);
400 return error;
401 }
402
403 /** convert the escaped sequences (eg, '%3A') of those special characters back to their
404 * original form (eg, ':').
405 */
406 public static String oaiDecode(String escaped_str) {
407 logger.info("oaiDecode() " +escaped_str);
408 for (int i=0; i<special_char.length; i++) {
409 if (escaped_str.indexOf(escape_sequence[i]) != -1) {
410 escaped_str = escaped_str.replaceAll(escape_sequence[i], special_char[i]);
411 }
412 }
413 //escaped_str = escaped_str.replaceAll("%3A", ":");
414 return escaped_str;
415 }
416 /** convert those special characters (eg, ':') to their
417 * escaped sequences (eg, '%3A').
418 */
419 public static String oaiEncode(String original_str) {
420 logger.info("oaiEncode() " + original_str);
421 for (int i=0; i<special_char.length; i++) {
422 if (original_str.indexOf(special_char[i]) != -1) {
423 original_str = original_str.replaceAll(special_char[i], escape_sequence[i]);
424 }
425 }
426 //original_str = original_str.replaceAll(":", "%3A");
427 return original_str;
428 }
429 /** convert YYYY-MM_DDThh:mm:ssZ to yyyy-MM-ddTHH:mm:ssZ
430 */
431 public static String convertToJava(String oai_format) {
432 oai_format = oai_format.replaceAll("YYYY", "yyyy").replaceAll("DD", "dd").replaceAll("hh", "HH");
433 return oai_format;
434 }
435 /** convert yyyy-MM-ddTHH:mm:ssZ to YYYY-MM_DDThh:mm:ssZ
436 */
437 public static String convertToOAI(String java_format) {
438 java_format = java_format.replaceAll("yyyy", "YYYY").replaceAll("dd", "DD").replaceAll("HH", "hh");
439 return java_format;
440 }
441 public static String getCurrentUTCTime() {
442 Date current_utc = new Date(System.currentTimeMillis());
443 //granularity is in the form: yyyy-MM-dd'T'HH:mm:ss'Z '
444 DateFormat formatter = new SimpleDateFormat(granularity);
445 return formatter.format(current_utc);
446 }
447 /** get a Date object from a Date format pattern string
448 *
449 * @param pattern - in the form: 2007-06-14T16:48:25Z, for example.
450 * @return a Date object - null if the pattern is not in the specified form
451 */
452
453 public static Date getDate(String pattern) {
454 if (pattern == null || pattern.equals("")) {
455 return null;
456 }
457 Date date = null;
458// String str = pattern.replaceAll("T", " ");
459// str = str.replaceAll("Z", "");
460 SimpleDateFormat sdf = null;
461 try {
462 sdf = new SimpleDateFormat(granularity);
463 date = sdf.parse(pattern);
464 } catch(Exception e) {
465 logger.error("invalid date format: " + pattern);
466 return null;
467 }
468 return date;
469 }
470 /** get the million second value from a string representing time in a pattern
471 * (eg, 2007-06-14T16:48:25Z)
472 */
473 public static long getTime(String pattern) {
474 if (pattern == null || pattern.equals("")) {
475 return -1;
476 }
477 Date date = null;
478 SimpleDateFormat sdf = null;
479 try {
480 //granularity is a global variable in the form: yyyy-MM-ddTHH:mm:ssZ
481 sdf = new SimpleDateFormat(granularity);
482 date = sdf.parse(pattern);
483 } catch(Exception e) {
484 logger.error("invalid date format: " + pattern);
485 return -1;
486 }
487 return date.getTime();
488 }
489 /** get the string representation of a time from a long value(long type)
490 */
491 public static String getTime(long seconds) {
492 Date date = new Date(seconds);
493 SimpleDateFormat sdf = new SimpleDateFormat(granularity);
494 return sdf.format(date);
495 }
496 public static Element createResponseHeader(String verb) {
497 String tag_name = (oai_version.equals(OAI_VERSION2))? OAI_DASH_PMH : verb;
498 Element oai = response_doc.createElement(tag_name);
499 Element resp_date = response_doc.createElement(RESPONSE_DATE);
500 Element req = response_doc.createElement(REQUEST);
501 oai.appendChild(resp_date);
502 oai.appendChild(req);
503
504 if(oai_version.equals(OAI_VERSION2)) {
505 oai.setAttribute("xmlns", "http://www.openarchives.org/OAI/2.0/");
506 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
507 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0 \n http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
508 } else {
509 oai.setAttribute("xmlns", "http://www.openarchives.com/OAI/1.1/OAI_" + verb);
510 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
511 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/1.1/OAI_" + verb + "\n http://www.openarchives.org/OAI/1.1/OAI_" + verb + ".xsd");
512 }
513 return oai;
514 }
515 public static Element getMetadataPrefixElement(String tag_name, String version) {
516 //examples of tag_name: dc, oai_dc:dc, etc.
517 Element oai = response_doc.createElement(tag_name);
518 if (version.equals(OAI_VERSION2)) {
519 oai.setAttribute("xmlns", "http://www.openarchives.org/OAI/2.0/");
520 oai.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
521 oai.setAttribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0 \n http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
522 } else {
523 oai.setAttribute("xmlns", "ttp://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
528 return oai;
529 }
530 public static HashMap getChildrenMapByTagName(Node n, String tag_name) {
531
532 HashMap map= new HashMap();
533 Node child = n.getFirstChild();
534 while (child!=null) {
535 String name = child.getNodeName();
536 if(name.equals(tag_name)) {
537 map.put(name, child);
538 }
539 child = child.getNextSibling();
540 }
541 return map;
542 }
543
544 /** Duplicates an element */
545 public static Element duplicateElement (Document owner, Element element, boolean with_attributes) {
546 return duplicateElementNS (owner, element, null, with_attributes);
547 }
548
549 /** Duplicates an element */
550 public static Element duplicateElementNS (Document owner,
551 Element element,
552 String namespace_uri,
553 boolean with_attributes) {
554 Element duplicate;
555 if (namespace_uri == null) {
556 duplicate = owner.createElement (element.getTagName ());
557 } else {
558 duplicate = owner.createElementNS (namespace_uri, element.getTagName ());
559 }
560 // Copy element attributes
561 if (with_attributes) {
562 NamedNodeMap attributes = element.getAttributes ();
563 for (int i = 0; i < attributes.getLength (); i++) {
564 Node attribute = attributes.item (i);
565 duplicate.setAttribute (attribute.getNodeName (), attribute.getNodeValue ());
566 }
567 }
568
569 // Copy element children
570 NodeList children = element.getChildNodes ();
571 for (int i = 0; i < children.getLength (); i++) {
572 Node child = children.item (i);
573 duplicate.appendChild (owner.importNode (child, true));
574 }
575
576 return duplicate;
577 }
578
579 public static void copyElement(Element to, Element from, String elem_name) {
580
581 Document to_doc = to.getOwnerDocument();
582 Node child = from.getFirstChild();
583 while (child != null) {
584 if (child.getNodeName().equals(elem_name)) {
585 to.appendChild(to_doc.importNode(child, true));
586 return;
587 }
588 child = child.getNextSibling();
589 }
590 }
591 public static HashMap getParamMap(NodeList params) {
592 HashMap map = new HashMap();
593 for(int i=0; i<params.getLength(); i++) {
594 Element param = (Element)params.item(i);
595 String param_name = param.getAttribute(OAIXML.NAME);
596 String param_value = param.getAttribute(OAIXML.VALUE);
597 map.put(param_name, param_value);
598 }
599 return map;
600 }
601 /** Parse an XML document from a given file */
602 static public Document parseXMLFile (File xml_file) {
603 // No file? No point trying!
604 if (xml_file.exists () == false) {
605 return null;
606 }
607 Document doc = null;
608 try {
609 doc = parseXML (new FileInputStream (xml_file));
610 }
611 catch (Exception exception) {
612 logger.error(exception.toString());
613 return null;
614 }
615 return doc;
616 }
617
618
619 /** Parse an XML document from a given input stream */
620 static public Document parseXML (InputStream xml_input_stream) {
621 Document document = null;
622
623 try {
624 InputStreamReader isr = new InputStreamReader (xml_input_stream, "UTF-8");
625 Reader xml_reader = new BufferedReader (isr);
626 document = parseXML (xml_reader);
627 isr.close ();
628 xml_input_stream.close ();
629 }
630 catch (Exception exception) {
631 logger.error(exception.toString());
632 }
633
634 return document;
635 }
636
637 /** Parse an XML document from a given reader */
638 static public Document parseXML (Reader xml_reader) {
639 Document document = null;
640
641 try {
642 InputSource isc = new InputSource (xml_reader);
643 DOMParser parser = new DOMParser ();
644 parser.setFeature ("http://xml.org/sax/features/validation", false);
645 parser.setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
646 // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
647 parser.setFeature ("http://apache.org/xml/features/dom/defer-node-expansion", true);
648 parser.setFeature ("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
649 parser.parse (isc);
650 document = parser.getDocument ();
651 }
652 catch (SAXException exception) {
653 System.err.println ("SAX exception: " + exception.getMessage ());
654 logger.error(exception.toString());
655 }
656 catch (Exception exception) {
657 logger.error(exception.toString());
658 }
659
660 return document;
661 }
662 /** Write an XML document to a given file */
663 static public boolean writeXMLFile (File xml_file, Document document) {
664 try {
665 OutputStream os = new FileOutputStream (xml_file);
666 // Create an output format for our document.
667 OutputFormat f = new OutputFormat (document);
668 f.setEncoding ("UTF-8");
669 f.setIndenting (true);
670 f.setLineWidth (0); // Why isn't this working!
671 f.setPreserveSpace (false);
672 // Create the necessary writer stream for serialization.
673 OutputStreamWriter osw = new OutputStreamWriter (os, "UTF-8");
674 Writer w = new BufferedWriter (osw);
675 // Generate a new serializer from the above.
676 XMLSerializer s = new XMLSerializer (w, f);
677 s.asDOMSerializer ();
678 // Finally serialize the document to file.
679 s.serialize (document);
680 // And close.
681 os.close ();
682 return true;
683 }
684 catch (Exception exception) {
685 logger.error(exception.toString());
686 return false;
687 }
688 }
689
690
691}
692
693
694
695
696
697
Note: See TracBrowser for help on using the repository browser.