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

Last change on this file since 32064 was 32064, checked in by kjdon, 6 years ago

for processDocXMLGetSection we need to actually add the section to the result!

  • Property svn:executable set to *
File size: 16.1 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 protected static final String DOC_XML_CREATE_EMPTY_FILE = "DocXMLCreateEmptyFile";
59 protected static final String DOC_XML_GET_METADATA = "DocXMLGetMetadata";
60 protected static final String DOC_XML_SET_METADATA = "DocXMLSetMetadata";
61 protected static final String DOC_XML_CREATE_SECTION = "DocXMLCreateSection";
62 protected static final String DOC_XML_DELETE_SECTION = "DocXMLDeleteSection";
63 protected static final String DOC_XML_GET_SECTION = "DocXMLGetSection";
64 protected static final String DOC_XML_SET_SECTION = "DocXMLSetSection";
65 protected static final String DOC_XML_GET_TEXT = "DocXMLGetText";
66 protected static final String DOC_XML_SET_TEXT = "DocXMLSetText";
67 protected static final String DOC_XML_DELETE_TEXT = "DocXMLDeleteText";
68 /*****************************************************************/
69
70 String[] services = { DOC_XML_CREATE_EMPTY_FILE, DOC_XML_GET_METADATA, DOC_XML_SET_METADATA, DOC_XML_GET_SECTION, DOC_XML_SET_SECTION, DOC_XML_DELETE_SECTION, DOC_XML_GET_TEXT, DOC_XML_SET_TEXT };
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);
120
121 if (request == null)
122 {
123 GSXML.addError(result, DOC_XML_CREATE_EMPTY_FILE + ": 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
137 _GSDM.documentXMLCreateDocXML(oid, collection, userContext);
138 if(_GSDM.checkError(result, DOC_XML_CREATE_EMPTY_FILE))
139 {
140 return result;
141 }
142 }
143 return result;
144 }
145
146 protected Element processDocXMLGetMetadata(Element request)
147 {
148 Document result_doc = XMLConverter.newDOM();
149 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_GET_METADATA);
150
151 if (request == null)
152 {
153 GSXML.addError(result, DOC_XML_GET_METADATA + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
154 return result;
155 }
156
157 UserContext userContext = new UserContext(request);
158
159 //Go through each of the metadata items that are requested
160 NodeList metadataList = request.getElementsByTagName(GSXML.METADATA_ELEM);
161 for (int i = 0; i < metadataList.getLength(); i++)
162 {
163 Element currentMetadata = (Element) metadataList.item(i);
164 String oid = currentMetadata.getAttribute(GSXML.NODE_ID_ATT);
165 String collection = currentMetadata.getAttribute(GSXML.COLLECTION_ATT);
166 String metadataName = currentMetadata.getAttribute(GSXML.NAME_ATT);
167
168 ArrayList<Element> metadataValues = _GSDM.documentXMLGetMetadata(oid, collection, metadataName, userContext);
169 if(_GSDM.checkError(result, DOC_XML_GET_METADATA))
170 {
171 return result;
172 }
173
174 for(Element metadataValue : metadataValues)
175 {
176 Element metadataElem = result_doc.createElement(GSXML.METADATA_ELEM);
177 metadataElem.setAttribute(GSXML.NAME_ATT, metadataName);
178 metadataElem.setAttribute(GSXML.VALUE_ATT, metadataValue.getFirstChild().getNodeValue());
179 result.appendChild(metadataElem);
180 }
181 }
182
183 return result;
184 }
185
186 protected Element processDocXMLSetMetadata(Element request)
187 {
188 Document result_doc = XMLConverter.newDOM();
189 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_SET_METADATA);
190
191 if (request == null)
192 {
193 GSXML.addError(result, DOC_XML_SET_METADATA + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
194 return result;
195 }
196
197 UserContext userContext = new UserContext(request);
198
199 //Go through each of the metadata items that are requested
200 NodeList metadataList = request.getElementsByTagName(GSXML.METADATA_ELEM);
201 for (int i = 0; i < metadataList.getLength(); i++)
202 {
203 Element currentMetadata = (Element) metadataList.item(i);
204 String oid = currentMetadata.getAttribute(GSXML.NODE_ID_ATT);
205 String collection = currentMetadata.getAttribute(GSXML.COLLECTION_ATT);
206 String metadataName = currentMetadata.getAttribute(GSXML.NAME_ATT);
207 String newMetadataValue = currentMetadata.getAttribute(GSXML.VALUE_ATT);
208
209 //Optional values
210 String oldMetadataValue = currentMetadata.getAttribute("old" + GSXML.VALUE_ATT);
211 String position = currentMetadata.getAttribute("position"); //TODO: Replace "position" with a constant
212 String operation = currentMetadata.getAttribute("operation");
213
214 int op = GSDocumentModel.OPERATION_APPEND;
215 if(operation.toLowerCase().equals("insertbefore"))
216 {
217 op = GSDocumentModel.OPERATION_INSERT_BEFORE;
218 }
219 else if (operation.toLowerCase().equals("insertafter"))
220 {
221 op = GSDocumentModel.OPERATION_INSERT_AFTER;
222 }
223 else if (operation.toLowerCase().equals("replace"))
224 {
225 op = GSDocumentModel.OPERATION_REPLACE;
226 }
227
228 //If we are given a position then set the value at that position
229 if(position != null && !position.equals(""))
230 {
231 int pos = -1;
232 try
233 {
234 pos = Integer.parseInt(position);
235 }
236 catch(Exception ex)
237 {
238 GSXML.addError(result, DOC_XML_SET_METADATA + ": Error converting the position attribute to an integer", GSXML.ERROR_TYPE_SYNTAX);
239 return result;
240 }
241 _GSDM.documentXMLSetMetadata(oid, collection, metadataName, newMetadataValue, pos, op, userContext);
242 if(_GSDM.checkError(result, DOC_XML_SET_METADATA))
243 {
244 return result;
245 }
246 }
247 //If we are given a value to replace with then call the replacement method
248 else if (oldMetadataValue != null && !oldMetadataValue.equals(""))
249 {
250 _GSDM.documentXMLReplaceMetadata(oid, collection, metadataName, oldMetadataValue, newMetadataValue, userContext);
251 if(_GSDM.checkError(result, DOC_XML_SET_METADATA))
252 {
253 return result;
254 }
255 }
256 else
257 {
258 GSXML.addError(result, DOC_XML_SET_METADATA + ": A position or previous value was not given", GSXML.ERROR_TYPE_SYNTAX);
259 return result;
260 }
261 }
262
263 return result;
264 }
265
266 protected Element processDocXMLCreateSection(Element request)
267 {
268 Document result_doc = XMLConverter.newDOM();
269 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_CREATE_SECTION);
270
271 if (request == null)
272 {
273 GSXML.addError(result, DOC_XML_CREATE_SECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
274 return result;
275 }
276
277 UserContext userContext = new UserContext(request);
278
279 //Go through each of the requests
280 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM); //TODO: Replace "Section" with a constant
281 for (int i = 0; i < sectionList.getLength(); i++)
282 {
283 Element currentSection = (Element) sectionList.item(i);
284 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
285 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
286
287 _GSDM.documentXMLCreateSection(oid, collection, userContext);
288 if(_GSDM.checkError(result, DOC_XML_CREATE_SECTION))
289 {
290 return result;
291 }
292 }
293 return result;
294 }
295
296 protected Element processDocXMLDeleteSection(Element request)
297 {
298 Document result_doc = XMLConverter.newDOM();
299 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_DELETE_SECTION);
300
301 if (request == null)
302 {
303 GSXML.addError(result, DOC_XML_DELETE_SECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
304 return result;
305 }
306
307 UserContext userContext = new UserContext(request);
308
309 //Go through each of the requests
310 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM);
311 for (int i = 0; i < sectionList.getLength(); i++)
312 {
313 Element currentSection = (Element) sectionList.item(i);
314 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
315 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
316
317 _GSDM.documentXMLDeleteSection(oid, collection, userContext);
318 if(_GSDM.checkError(result, DOC_XML_DELETE_SECTION))
319 {
320 return result;
321 }
322 }
323 return result;
324 }
325
326 protected Element processDocXMLGetSection(Element request)
327 {
328 Document result_doc = XMLConverter.newDOM();
329 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_GET_SECTION);
330
331 if (request == null)
332 {
333 GSXML.addError(result, DOC_XML_GET_SECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
334 return result;
335 }
336
337 UserContext userContext = new UserContext(request);
338
339 //Go through each of the requests
340 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM);
341 for (int i = 0; i < sectionList.getLength(); i++)
342 {
343 Element currentSection = (Element) sectionList.item(i);
344 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
345 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
346
347 Element section = _GSDM.documentXMLGetSection(oid, collection, userContext);
348 if(_GSDM.checkError(result, DOC_XML_GET_SECTION))
349 {
350 logger.error("there was an error getting the archive section for " +oid);
351 return result;
352 }
353 result.appendChild(result_doc.importNode(section,true));
354 }
355 return result;
356 }
357
358 protected Element processDocXMLSetSection(Element request)
359 {
360 Document result_doc = XMLConverter.newDOM();
361 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_SET_SECTION);
362
363 if (request == null)
364 {
365 GSXML.addError(result, DOC_XML_SET_SECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
366 return result;
367 }
368
369 UserContext userContext = new UserContext(request);
370
371 //Go through each of the requests
372 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM);
373 for (int i = 0; i < sectionList.getLength(); i++)
374 {
375 Element currentSection = (Element) sectionList.item(i);
376 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
377 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
378 String operation = currentSection.getAttribute("operation");
379
380 int op = GSDocumentModel.OPERATION_REPLACE;
381 if(operation.equals("insertbefore"))
382 {
383 op = GSDocumentModel.OPERATION_INSERT_BEFORE;
384 }
385 else if (operation.equals("insertafter"))
386 {
387 op = GSDocumentModel.OPERATION_INSERT_AFTER;
388 }
389 else if (operation.equals("append"))
390 {
391 op = GSDocumentModel.OPERATION_APPEND;
392 }
393
394 _GSDM.documentXMLSetSection(oid, collection, currentSection, op, userContext);
395 if(_GSDM.checkError(result, DOC_XML_SET_SECTION))
396 {
397 return result;
398 }
399 }
400
401 return result;
402 }
403
404 protected Element processDocXMLGetText(Element request)
405 {
406 Document result_doc = XMLConverter.newDOM();
407 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_GET_TEXT);
408
409 if (request == null)
410 {
411 GSXML.addError(result, DOC_XML_GET_TEXT + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
412 return result;
413 }
414
415 UserContext userContext = new UserContext(request);
416
417 //Go through each of the requests
418 NodeList contentList = request.getElementsByTagName(GSXML.DOCXML_CONTENT_ELEM);
419 for (int i = 0; i < contentList.getLength(); i++)
420 {
421 Element currentContent = (Element) contentList.item(i);
422 String oid = currentContent.getAttribute(GSXML.NODE_ID_ATT);
423 String collection = currentContent.getAttribute(GSXML.COLLECTION_ATT);
424
425 String content = _GSDM.documentXMLGetText(oid, collection, userContext);
426 if(_GSDM.checkError(result, DOC_XML_GET_TEXT))
427 {
428 return result;
429 }
430
431 if (content == null)
432 {
433 result.appendChild(result_doc.createElement(GSXML.DOCXML_CONTENT_ELEM));
434 }
435 else
436 {
437 Element contentElem = result_doc.createElement(GSXML.DOCXML_CONTENT_ELEM);
438 Node textNode = result_doc.createTextNode(content);
439 contentElem.appendChild(textNode);
440 result.appendChild(contentElem);
441 }
442 }
443
444 return result;
445 }
446
447 protected Element processDocXMLSetText(Element request)
448 {
449 Document result_doc = XMLConverter.newDOM();
450 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_SET_TEXT);
451
452 if (request == null)
453 {
454 GSXML.addError(result, DOC_XML_SET_TEXT + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
455 return result;
456 }
457
458 UserContext userContext = new UserContext(request);
459
460 //Go through each of the requests
461 NodeList contentList = request.getElementsByTagName(GSXML.DOCXML_CONTENT_ELEM);
462 for (int i = 0; i < contentList.getLength(); i++)
463 {
464 Element currentContent = (Element) contentList.item(i);
465 String oid = currentContent.getAttribute(GSXML.NODE_ID_ATT);
466 String collection = currentContent.getAttribute(GSXML.COLLECTION_ATT);
467
468 _GSDM.documentXMLSetText(oid, collection, currentContent, userContext);
469 if(_GSDM.checkError(result, DOC_XML_SET_TEXT))
470 {
471 return result;
472 }
473 }
474
475 return result;
476 }
477}
Note: See TracBrowser for help on using the repository browser.