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

Last change on this file since 24993 was 24993, checked in by sjm84, 12 years ago

Adding UserContext to replace the use of lang and uid

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