1 | /*
|
---|
2 | * GS2MGRetrieve.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.service;
|
---|
20 |
|
---|
21 |
|
---|
22 | // Greenstone classes
|
---|
23 | import org.greenstone.mg.*;
|
---|
24 | import org.greenstone.gsdl3.util.*;
|
---|
25 |
|
---|
26 | // XML classes
|
---|
27 | import org.w3c.dom.Element;
|
---|
28 |
|
---|
29 | // General Java classes
|
---|
30 | import java.io.File;
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | *
|
---|
35 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
36 | * @version $Revision: 5098 $
|
---|
37 | */
|
---|
38 |
|
---|
39 | public class GS2MGRetrieve
|
---|
40 | extends GS2Retrieve {
|
---|
41 |
|
---|
42 | // Parameters used
|
---|
43 | private static final String INDEX_PARAM = "index";
|
---|
44 |
|
---|
45 | // Elements used in the config file that are specific to this class
|
---|
46 | private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
|
---|
47 |
|
---|
48 | private MGWrapper mg_src = null;
|
---|
49 |
|
---|
50 | private String default_index = null;
|
---|
51 |
|
---|
52 |
|
---|
53 | /** constructor */
|
---|
54 | public GS2MGRetrieve()
|
---|
55 | {
|
---|
56 | this.mg_src = new MGWrapper();
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /** configure this service */
|
---|
61 | public boolean configure(Element info, Element extra_info)
|
---|
62 | {
|
---|
63 | // Do specific configuration
|
---|
64 | System.out.println("Configuring GS2MGRetrieve...");
|
---|
65 | // System.out.println("info:\n" + converter_.getString(info));
|
---|
66 | // System.out.println("extra_info:\n" + converter_.getString(extra_info));
|
---|
67 |
|
---|
68 | // Get the default index out of <defaultIndex> (buildConfig.xml)
|
---|
69 | Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM);
|
---|
70 | if (def != null) {
|
---|
71 | this.default_index = def.getAttribute(GSXML.NAME_ATT);
|
---|
72 | }
|
---|
73 | if (this.default_index == null || this.default_index.equals("")) {
|
---|
74 | System.err.println("Error: default index not specified!");
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 | // System.out.println("Default index: " + this.default_index);
|
---|
78 |
|
---|
79 | // Do generic configuration
|
---|
80 | return super.configure(info, extra_info);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /** Retrieve the content of a document */
|
---|
85 | protected Element processDocumentContentRetrieve(Element request)
|
---|
86 | {
|
---|
87 | // Create a new (empty) result message
|
---|
88 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
89 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
|
---|
90 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
91 |
|
---|
92 | // Get the parameters of the request - no parameters at this stage
|
---|
93 | //Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
94 |
|
---|
95 | // Get the request content
|
---|
96 |
|
---|
97 | Element query_doc_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
98 | if (query_doc_list == null) {
|
---|
99 | System.err.println("Error: DocumentContentRetrieve request specified no doc nodes.\n");
|
---|
100 | return result;
|
---|
101 | }
|
---|
102 |
|
---|
103 | Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
104 | result.appendChild(doc_list);
|
---|
105 |
|
---|
106 | // The location of the MG index and text files
|
---|
107 | String basedir = GSFile.collectionBaseDir(this.site_home, this.cluster_name)
|
---|
108 | + File.separatorChar; // Needed by MG
|
---|
109 | String textdir = GSFile.collectionTextPath(this.cluster_name);
|
---|
110 | // index is only needed to start up MG, not used so just use the default index
|
---|
111 | String indexpath = GSFile.collectionIndexPath(this.cluster_name, this.default_index);
|
---|
112 | this.mg_src.setIndex(indexpath);
|
---|
113 |
|
---|
114 | // Get the documents
|
---|
115 | String[] doc_ids = GSXML.getAttributeValuesFromList(query_doc_list,
|
---|
116 | GSXML.NODE_ID_ATT);
|
---|
117 | for (int i = 0; i < doc_ids.length; i++) {
|
---|
118 | String doc_id = doc_ids[i];
|
---|
119 | if (OID.needsTranslating(doc_id)) {
|
---|
120 | doc_id = this.gdbm_src.translateOID(doc_id);
|
---|
121 | }
|
---|
122 | long doc_num = this.gdbm_src.oid2Docnum(doc_id);
|
---|
123 | String doc_content = this.mg_src.getDocument(basedir, textdir, doc_num);
|
---|
124 | // remove any ctrl-c or ctrl-b (I hope these are the right codes)
|
---|
125 | doc_content = doc_content.replaceAll("\u0002|\u0003", "");
|
---|
126 | // replace _httpimg_ with the correct address
|
---|
127 | doc_content = resolveImages(doc_content, doc_id);
|
---|
128 | // Stick it in a text node
|
---|
129 | Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
130 | doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
|
---|
131 | GSXML.addDocText(this.doc, doc, doc_content);
|
---|
132 | doc_list.appendChild(doc);
|
---|
133 | }
|
---|
134 |
|
---|
135 | return result;
|
---|
136 | }
|
---|
137 | }
|
---|