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

Last change on this file since 24583 was 24583, checked in by sjm84, 13 years ago

Fixing an error with excerptid and also changed GSXML.xmlNodeToString to have a parameter that controls whether or not to print XML text

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