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

Last change on this file since 28966 was 28966, checked in by kjdon, 10 years ago

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

  • Property svn:executable set to *
File size: 15.9 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 _GSDM.documentXMLGetSection(oid, collection, userContext);
348 if(_GSDM.checkError(result, DOC_XML_GET_SECTION))
349 {
350 return result;
351 }
352 }
353 return result;
354 }
355
356 protected Element processDocXMLSetSection(Element request)
357 {
358 Document result_doc = XMLConverter.newDOM();
359 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_SET_SECTION);
360
361 if (request == null)
362 {
363 GSXML.addError(result, DOC_XML_SET_SECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
364 return result;
365 }
366
367 UserContext userContext = new UserContext(request);
368
369 //Go through each of the requests
370 NodeList sectionList = request.getElementsByTagName(GSXML.DOCXML_SECTION_ELEM);
371 for (int i = 0; i < sectionList.getLength(); i++)
372 {
373 Element currentSection = (Element) sectionList.item(i);
374 String oid = currentSection.getAttribute(GSXML.NODE_ID_ATT);
375 String collection = currentSection.getAttribute(GSXML.COLLECTION_ATT);
376 String operation = currentSection.getAttribute("operation");
377
378 int op = GSDocumentModel.OPERATION_REPLACE;
379 if(operation.equals("insertbefore"))
380 {
381 op = GSDocumentModel.OPERATION_INSERT_BEFORE;
382 }
383 else if (operation.equals("insertafter"))
384 {
385 op = GSDocumentModel.OPERATION_INSERT_AFTER;
386 }
387 else if (operation.equals("append"))
388 {
389 op = GSDocumentModel.OPERATION_APPEND;
390 }
391
392 _GSDM.documentXMLSetSection(oid, collection, currentSection, op, userContext);
393 if(_GSDM.checkError(result, DOC_XML_SET_SECTION))
394 {
395 return result;
396 }
397 }
398
399 return result;
400 }
401
402 protected Element processDocXMLGetText(Element request)
403 {
404 Document result_doc = XMLConverter.newDOM();
405 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_GET_TEXT);
406
407 if (request == null)
408 {
409 GSXML.addError(result, DOC_XML_GET_TEXT + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
410 return result;
411 }
412
413 UserContext userContext = new UserContext(request);
414
415 //Go through each of the requests
416 NodeList contentList = request.getElementsByTagName(GSXML.DOCXML_CONTENT_ELEM);
417 for (int i = 0; i < contentList.getLength(); i++)
418 {
419 Element currentContent = (Element) contentList.item(i);
420 String oid = currentContent.getAttribute(GSXML.NODE_ID_ATT);
421 String collection = currentContent.getAttribute(GSXML.COLLECTION_ATT);
422
423 String content = _GSDM.documentXMLGetText(oid, collection, userContext);
424 if(_GSDM.checkError(result, DOC_XML_GET_TEXT))
425 {
426 return result;
427 }
428
429 if (content == null)
430 {
431 result.appendChild(result_doc.createElement(GSXML.DOCXML_CONTENT_ELEM));
432 }
433 else
434 {
435 Element contentElem = result_doc.createElement(GSXML.DOCXML_CONTENT_ELEM);
436 Node textNode = result_doc.createTextNode(content);
437 contentElem.appendChild(textNode);
438 result.appendChild(contentElem);
439 }
440 }
441
442 return result;
443 }
444
445 protected Element processDocXMLSetText(Element request)
446 {
447 Document result_doc = XMLConverter.newDOM();
448 Element result = GSXML.createBasicResponse(result_doc, DOC_XML_SET_TEXT);
449
450 if (request == null)
451 {
452 GSXML.addError(result, DOC_XML_SET_TEXT + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
453 return result;
454 }
455
456 UserContext userContext = new UserContext(request);
457
458 //Go through each of the requests
459 NodeList contentList = request.getElementsByTagName(GSXML.DOCXML_CONTENT_ELEM);
460 for (int i = 0; i < contentList.getLength(); i++)
461 {
462 Element currentContent = (Element) contentList.item(i);
463 String oid = currentContent.getAttribute(GSXML.NODE_ID_ATT);
464 String collection = currentContent.getAttribute(GSXML.COLLECTION_ATT);
465
466 _GSDM.documentXMLSetText(oid, collection, currentContent, userContext);
467 if(_GSDM.checkError(result, DOC_XML_SET_TEXT))
468 {
469 return result;
470 }
471 }
472
473 return result;
474 }
475}
Note: See TracBrowser for help on using the repository browser.