source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/DocXMLUtil.java@ 38154

Last change on this file since 38154 was 37177, checked in by davidb, 17 months ago

Introduction of new optional parameter docVersion. If null (or equal to the empty string), then code works as before. Designed to work with the file-level document-version history mechanism, if non-empty, then this value is used to change where doc.xml on the file system is read from

  • Property svn:executable set to *
File size: 17.4 KB
Line 
1/*
2 * DocXMLUtil.java
3 * Used to manipulate archive doc.xml files
4 *
5 * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21package org.greenstone.gsdl3.service;
22
23import org.greenstone.gsdl3.util.GSDocumentModel;
24import org.greenstone.gsdl3.util.GSPath;
25import org.greenstone.gsdl3.util.GSXML;
26import org.greenstone.gsdl3.util.UserContext;
27import org.greenstone.gsdl3.util.XMLConverter;
28
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31import org.w3c.dom.Node;
32import org.w3c.dom.NodeList;
33
34import org.apache.log4j.*;
35
36import java.io.BufferedWriter;
37import java.io.File;
38import java.io.FileWriter;
39import java.util.ArrayList;
40import java.util.HashMap;
41
42import javax.xml.parsers.DocumentBuilderFactory;
43import javax.xml.parsers.DocumentBuilder;
44import javax.xml.transform.Result;
45import javax.xml.transform.Transformer;
46import javax.xml.transform.TransformerFactory;
47import javax.xml.transform.dom.DOMSource;
48import javax.xml.transform.stream.StreamResult;
49
50public class DocXMLUtil extends ServiceRack
51{
52 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.DocXMLUtil.class.getName());
53 GSDocumentModel _GSDM = null;
54
55 /******************************************************************
56 * The list of services the doc.xml utility service rack supports *
57 *****************************************************************/
58 public static final String DOC_XML_CREATE_EMPTY_FILE_SERVICE = "DocXMLCreateEmptyFile";
59 public static final String DOC_XML_GET_METADATA_SERVICE = "DocXMLGetMetadata";
60 public static final String DOC_XML_SET_METADATA_SERVICE = "DocXMLSetMetadata";
61 public static final String DOC_XML_CREATE_SECTION_SERVICE = "DocXMLCreateSection";
62 public static final String DOC_XML_DELETE_SECTION_SERVICE = "DocXMLDeleteSection";
63 public static final String DOC_XML_GET_SECTION_SERVICE = "DocXMLGetSection";
64 public static final String DOC_XML_SET_SECTION_SERVICE = "DocXMLSetSection";
65 public static final String DOC_XML_GET_TEXT_SERVICE = "DocXMLGetText";
66 public static final String DOC_XML_SET_TEXT_SERVICE = "DocXMLSetText";
67 public static final String DOC_XML_DELETE_TEXT_SERVICE = "DocXMLDeleteText";
68 /*****************************************************************/
69
70 String[] services = { DOC_XML_CREATE_EMPTY_FILE_SERVICE, DOC_XML_GET_METADATA_SERVICE, DOC_XML_SET_METADATA_SERVICE, DOC_XML_GET_SECTION_SERVICE, DOC_XML_SET_SECTION_SERVICE, DOC_XML_DELETE_SECTION_SERVICE, DOC_XML_GET_TEXT_SERVICE, DOC_XML_SET_TEXT_SERVICE };
71
72 /** configure this service */
73 public boolean configure(Element info, Element extra_info)
74 {
75 if (!super.configure(info, extra_info))
76 {
77 return false;
78 }
79
80 logger.info("Configuring DocXMLUtil...");
81 this.config_info = info;
82
83 for (int i = 0; i < services.length; i++)
84 {
85 Element service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
86 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
87 service.setAttribute(GSXML.NAME_ATT, services[i]);
88 this.short_service_info.appendChild(service);
89 }
90
91 _GSDM = new GSDocumentModel(this.site_home, this.router);
92
93 return true;
94 }
95
96 protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
97 {
98 for (int i = 0; i < services.length; i++)
99 {
100 if (service_id.equals(services[i]))
101 {
102 Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
103 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
104 service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
105 return service_elem;
106 }
107 }
108
109 return null;
110 }
111
112 /************
113 * Services *
114 ***********/
115
116 protected Element processDocXMLCreateEmptyFile(Element request)
117 {
118 Document result_doc = XMLConverter.newDOM();
119 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_CREATE_EMPTY_FILE_SERVICE);
120
121 if (request == null)
122 {
123 GSXML.addError(result, DOC_XML_CREATE_EMPTY_FILE_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
124 return result;
125 }
126
127 UserContext userContext = new UserContext(request);
128
129 //Go through each of the document items that are requested
130 NodeList docList = request.getElementsByTagName(GSXML.DOCUMENT_ELEM);
131 for (int i = 0; i < docList.getLength(); i++)
132 {
133 Element currentDoc = (Element) docList.item(i);
134 String oid = currentDoc.getAttribute(GSXML.NODE_ID_ATT);
135 String collection = currentDoc.getAttribute(GSXML.COLLECTION_ATT);
136 //String optDocVersion = (String) currentDoc.getAttribute(GSXML.DOC_VERSION_ATT);
137
138 _GSDM.documentXMLCreateNewImportDocXML(oid, collection, userContext);
139 if(_GSDM.checkError(result, DOC_XML_CREATE_EMPTY_FILE_SERVICE))
140 {
141 return result;
142 }
143 }
144 return result;
145 }
146
147 protected Element processDocXMLGetMetadata(Element request)
148 {
149 Document result_doc = XMLConverter.newDOM();
150 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_GET_METADATA_SERVICE);
151
152 if (request == null)
153 {
154 GSXML.addError(result, DOC_XML_GET_METADATA_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
155 return result;
156 }
157
158 UserContext userContext = new UserContext(request);
159
160 //Go through each of the metadata items that are requested
161 NodeList metadataList = request.getElementsByTagName(GSXML.METADATA_ELEM);
162 for (int i = 0; i < metadataList.getLength(); i++)
163 {
164 Element currentMetadata = (Element) metadataList.item(i);
165 String oid = currentMetadata.getAttribute(GSXML.NODE_ID_ATT);
166 String optDocVersion = currentMetadata.getAttribute(GSXML.DOC_VERSION_ATT);
167 String collection = currentMetadata.getAttribute(GSXML.COLLECTION_ATT);
168 String metadataName = currentMetadata.getAttribute(GSXML.NAME_ATT);
169
170 ArrayList<Element> metadataValues = _GSDM.documentXMLGetMetadata(oid, optDocVersion, collection, metadataName, userContext);
171 if(_GSDM.checkError(result, DOC_XML_GET_METADATA_SERVICE))
172 {
173 return result;
174 }
175
176 for(Element metadataValue : metadataValues)
177 {
178 Element metadataElem = result_doc.createElement(GSXML.METADATA_ELEM);
179 metadataElem.setAttribute(GSXML.NAME_ATT, metadataName);
180 metadataElem.setAttribute(GSXML.VALUE_ATT, metadataValue.getFirstChild().getNodeValue());
181 result.appendChild(metadataElem);
182 }
183 }
184
185 return result;
186 }
187
188 protected Element processDocXMLSetMetadata(Element request)
189 {
190 Document result_doc = XMLConverter.newDOM();
191 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_SET_METADATA_SERVICE);
192
193 if (request == null)
194 {
195 GSXML.addError(result, DOC_XML_SET_METADATA_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
196 return result;
197 }
198
199 UserContext userContext = new UserContext(request);
200
201 //Go through each of the metadata items that are requested
202 NodeList metadataList = request.getElementsByTagName(GSXML.METADATA_ELEM);
203 for (int i = 0; i < metadataList.getLength(); i++)
204 {
205 Element currentMetadata = (Element) metadataList.item(i);
206 String oid = currentMetadata.getAttribute(GSXML.NODE_ID_ATT);
207 String optDocVersion = currentMetadata.getAttribute(GSXML.DOC_VERSION_ATT);
208 String collection = currentMetadata.getAttribute(GSXML.COLLECTION_ATT);
209 String metadataName = currentMetadata.getAttribute(GSXML.NAME_ATT);
210 String newMetadataValue = currentMetadata.getAttribute(GSXML.VALUE_ATT);
211
212 //Optional values
213 String oldMetadataValue = currentMetadata.getAttribute("old" + GSXML.VALUE_ATT);
214 String position = currentMetadata.getAttribute("position"); //TODO: Replace "position" with a constant
215 String operation = currentMetadata.getAttribute("operation");
216
217 int op = GSDocumentModel.OPERATION_APPEND;
218 if(operation.toLowerCase().equals("insertbefore"))
219 {
220 op = GSDocumentModel.OPERATION_INSERT_BEFORE;
221 }
222 else if (operation.toLowerCase().equals("insertafter"))
223 {
224 op = GSDocumentModel.OPERATION_INSERT_AFTER;
225 }
226 else if (operation.toLowerCase().equals("replace"))
227 {
228 op = GSDocumentModel.OPERATION_REPLACE;
229 }
230
231 //If we are given a position then set the value at that position
232 if(position != null && !position.equals(""))
233 {
234 int pos = -1;
235 try
236 {
237 pos = Integer.parseInt(position);
238 }
239 catch(Exception ex)
240 {
241 GSXML.addError(result, DOC_XML_SET_METADATA_SERVICE + ": Error converting the position attribute to an integer", GSXML.ERROR_TYPE_SYNTAX);
242 return result;
243 }
244 _GSDM.documentXMLSetMetadata(oid, optDocVersion, collection, metadataName, newMetadataValue, pos, op, userContext);
245 if(_GSDM.checkError(result, DOC_XML_SET_METADATA_SERVICE))
246 {
247 return result;
248 }
249 }
250 //If we are given a value to replace with then call the replacement method
251 else if (oldMetadataValue != null && !oldMetadataValue.equals(""))
252 {
253 _GSDM.documentXMLReplaceMetadata(oid, optDocVersion, collection, metadataName, oldMetadataValue, newMetadataValue, userContext);
254 if(_GSDM.checkError(result, DOC_XML_SET_METADATA_SERVICE))
255 {
256 return result;
257 }
258 }
259 else
260 {
261 GSXML.addError(result, DOC_XML_SET_METADATA_SERVICE + ": A position or previous value was not given", GSXML.ERROR_TYPE_SYNTAX);
262 return result;
263 }
264 }
265
266 return result;
267 }
268
269 protected Element processDocXMLCreateSection(Element request)
270 {
271 Document result_doc = XMLConverter.newDOM();
272 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_CREATE_SECTION_SERVICE);
273
274 if (request == null)
275 {
276 GSXML.addError(result, DOC_XML_CREATE_SECTION_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
277 return result;
278 }
279
280 UserContext userContext = new UserContext(request);
281
282 //Go through each of the requests
283 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM); //TODO: Replace "Section" with a constant
284 for (int i = 0; i < sectionList.getLength(); i++)
285 {
286 Element currentSection = (Element) sectionList.item(i);
287 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
288 String optDocVersion = currentSection.getAttribute(GSXML.DOC_VERSION_ATT);
289 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
290
291 _GSDM.documentXMLCreateSection(oid, optDocVersion, collection, userContext);
292 if(_GSDM.checkError(result, DOC_XML_CREATE_SECTION_SERVICE))
293 {
294 return result;
295 }
296 }
297 return result;
298 }
299
300 protected Element processDocXMLDeleteSection(Element request)
301 {
302 Document result_doc = XMLConverter.newDOM();
303 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_DELETE_SECTION_SERVICE);
304
305 if (request == null)
306 {
307 GSXML.addError(result, DOC_XML_DELETE_SECTION_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
308 return result;
309 }
310
311 UserContext userContext = new UserContext(request);
312
313 //Go through each of the requests
314 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM);
315 for (int i = 0; i < sectionList.getLength(); i++)
316 {
317 Element currentSection = (Element) sectionList.item(i);
318 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
319 String optDocVersion = currentSection.getAttribute(GSXML.DOC_VERSION_ATT);
320 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
321
322 _GSDM.documentXMLDeleteSection(oid, optDocVersion, collection, userContext);
323 if(_GSDM.checkError(result, DOC_XML_DELETE_SECTION_SERVICE))
324 {
325 return result;
326 }
327 }
328 return result;
329 }
330
331 protected Element processDocXMLGetSection(Element request)
332 {
333 Document result_doc = XMLConverter.newDOM();
334 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_GET_SECTION_SERVICE);
335
336 if (request == null)
337 {
338 GSXML.addError(result, DOC_XML_GET_SECTION_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
339 return result;
340 }
341
342 UserContext userContext = new UserContext(request);
343
344 //Go through each of the requests
345 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM);
346 for (int i = 0; i < sectionList.getLength(); i++)
347 {
348 Element currentSection = (Element) sectionList.item(i);
349 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
350 String optDocVersion = currentSection.getAttribute(GSXML.DOC_VERSION_ATT);
351 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
352
353 Element section = _GSDM.documentXMLGetSection(oid, optDocVersion, collection, userContext);
354 if(_GSDM.checkError(result, DOC_XML_GET_SECTION_SERVICE))
355 {
356 logger.error("there was an error getting the archive section for " +oid);
357 return result;
358 }
359 result.appendChild(result_doc.importNode(section,true));
360 }
361 return result;
362 }
363
364 protected Element processDocXMLSetSection(Element request)
365 {
366 Document result_doc = XMLConverter.newDOM();
367 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_SET_SECTION_SERVICE);
368
369 if (request == null)
370 {
371 GSXML.addError(result, DOC_XML_SET_SECTION_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
372 return result;
373 }
374
375 UserContext userContext = new UserContext(request);
376
377 //Go through each of the requests
378 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM);
379 for (int i = 0; i < sectionList.getLength(); i++)
380 {
381 Element currentSection = (Element) sectionList.item(i);
382 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
383 String optDocVersion = currentSection.getAttribute(GSXML.DOC_VERSION_ATT);
384 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
385
386 String operation = currentSection.getAttribute("operation");
387
388 int op = GSDocumentModel.OPERATION_REPLACE;
389 if(operation.equals("insertbefore"))
390 {
391 op = GSDocumentModel.OPERATION_INSERT_BEFORE;
392 }
393 else if (operation.equals("insertafter"))
394 {
395 op = GSDocumentModel.OPERATION_INSERT_AFTER;
396 }
397 else if (operation.equals("append"))
398 {
399 op = GSDocumentModel.OPERATION_APPEND;
400 }
401
402 _GSDM.documentXMLSetSection(oid, optDocVersion, collection, currentSection, op, userContext);
403 if(_GSDM.checkError(result, DOC_XML_SET_SECTION_SERVICE))
404 {
405 return result;
406 }
407 }
408
409 return result;
410 }
411
412 protected Element processDocXMLGetText(Element request)
413 {
414 Document result_doc = XMLConverter.newDOM();
415 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_GET_TEXT_SERVICE);
416
417 if (request == null)
418 {
419 GSXML.addError(result, DOC_XML_GET_TEXT_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
420 return result;
421 }
422
423 UserContext userContext = new UserContext(request);
424
425 //Go through each of the requests
426 NodeList contentList = request.getElementsByTagName(GSXML.DOCXML_CONTENT_ELEM);
427 for (int i = 0; i < contentList.getLength(); i++)
428 {
429 Element currentContent = (Element) contentList.item(i);
430 String oid = currentContent.getAttribute(GSXML.NODE_ID_ATT);
431 String optDocVersion = currentContent.getAttribute(GSXML.DOC_VERSION_ATT);
432 String collection = currentContent.getAttribute(GSXML.COLLECTION_ATT);
433
434 String content = _GSDM.documentXMLGetText(oid, optDocVersion, collection, userContext);
435 if(_GSDM.checkError(result, DOC_XML_GET_TEXT_SERVICE))
436 {
437 return result;
438 }
439
440 if (content == null)
441 {
442 result.appendChild(result_doc.createElement(GSXML.DOCXML_CONTENT_ELEM));
443 }
444 else
445 {
446 Element contentElem = result_doc.createElement(GSXML.DOCXML_CONTENT_ELEM);
447 Node textNode = result_doc.createTextNode(content);
448 contentElem.appendChild(textNode);
449 result.appendChild(contentElem);
450 }
451 }
452
453 return result;
454 }
455
456 protected Element processDocXMLSetText(Element request)
457 {
458 Document result_doc = XMLConverter.newDOM();
459 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_SET_TEXT_SERVICE);
460
461 if (request == null)
462 {
463 GSXML.addError(result, DOC_XML_SET_TEXT_SERVICE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
464 return result;
465 }
466
467 UserContext userContext = new UserContext(request);
468
469 //Go through each of the requests
470 NodeList contentList = request.getElementsByTagName(GSXML.DOCXML_CONTENT_ELEM);
471 for (int i = 0; i < contentList.getLength(); i++)
472 {
473 Element currentContent = (Element) contentList.item(i);
474 String oid = currentContent.getAttribute(GSXML.NODE_ID_ATT);
475 String optDocVersion = currentContent.getAttribute(GSXML.DOC_VERSION_ATT);
476 String collection = currentContent.getAttribute(GSXML.COLLECTION_ATT);
477
478 _GSDM.documentXMLSetText(oid, optDocVersion, collection, currentContent, userContext);
479 if(_GSDM.checkError(result, DOC_XML_SET_TEXT_SERVICE))
480 {
481 return result;
482 }
483 }
484
485 return result;
486 }
487}
Note: See TracBrowser for help on using the repository browser.