source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2Browse.java@ 10651

Last change on this file since 10651 was 10651, checked in by kjdon, 19 years ago

made all index/gdbm db paths use indexstem instead of cluster_name

  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/*
2 * GS2Browse.java
3 * Copyright (C) 2005 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 */
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
22import org.greenstone.gsdl3.util.OID;
23import org.greenstone.gsdl3.util.GSXML;
24import org.greenstone.gsdl3.util.GSFile;
25import org.greenstone.gsdl3.util.MacroResolver;
26import org.greenstone.gsdl3.util.GS2MacroResolver;
27import org.greenstone.gsdl3.util.GDBMWrapper;
28import org.greenstone.gsdl3.util.DBInfo;
29// XML classes
30import org.w3c.dom.Document;
31import org.w3c.dom.Element;
32import org.w3c.dom.NodeList;
33
34// General Java classes
35import java.util.ArrayList;
36import java.util.StringTokenizer;
37import java.util.Set;
38import java.util.Iterator;
39
40/** Greenstone 2 collection classifier service
41 *
42 * @author <a href="mailto:[email protected]">Katherine Don</a>
43 * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
44 */
45
46public class GS2Browse
47 extends AbstractBrowse
48{
49
50 protected GDBMWrapper gdbm_src = null;
51
52 public GS2Browse()
53 {
54 this.gdbm_src = new GDBMWrapper();
55 this.macro_resolver = new GS2MacroResolver(this.gdbm_src);
56 }
57
58 public void cleanUp() {
59 super.cleanUp();
60 this.gdbm_src.closeDatabase();
61 }
62
63 public boolean configure(Element info, Element extra_info)
64 {
65 if (!super.configure(info, extra_info)){
66 return false;
67 }
68
69 System.err.println("Configuring GS2Browse...");
70 // the index stem is either specified in the config file or is the collection name
71 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
72 String index_stem = null;
73 if (index_stem_elem != null) {
74 index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
75 }
76 if (index_stem == null || index_stem.equals("")) {
77 index_stem = this.cluster_name;
78 }
79
80 // Open GDBM database for querying
81 String gdbm_db_file = GSFile.GDBMDatabaseFile(this.site_home, this.cluster_name, index_stem);
82 if (!this.gdbm_src.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
83 System.err.println("GS2Browse Error: Could not open GDBM database!");
84 return false;
85 }
86 return true;
87 }
88
89 /** if id ends in .fc, .pc etc, then translate it to the correct id */
90 protected String translateId(String node_id) {
91 return this.gdbm_src.translateOID(node_id);
92 }
93
94 /** returns the document type of the doc that the specified node
95 belongs to. should be one of
96 GSXML.DOC_TYPE_SIMPLE,
97 GSXML.DOC_TYPE_PAGED,
98 GSXML.DOC_TYPE_HIERARCHY
99 */
100 protected String getDocType(String node_id) {
101 DBInfo info = this.gdbm_src.getInfo(node_id);
102 if (info == null) {
103 return GSXML.DOC_TYPE_SIMPLE;
104 }
105 String doc_type = info.getInfo("doctype");
106 if (!doc_type.equals("")&&!doc_type.equals("doc")) {
107 return doc_type;
108 }
109
110 String top_id = OID.getTop(node_id);
111 boolean is_top = (top_id.equals(node_id) ? true : false);
112
113 String children = info.getInfo("contains");
114 boolean is_leaf = (children.equals("") ? true : false);
115
116 if (is_top && is_leaf) { // a single section document
117 return GSXML.DOC_TYPE_SIMPLE;
118 }
119
120 // now we just check the top node
121 if (!is_top) { // we need to look at the top info
122 info = this.gdbm_src.getInfo(top_id);
123 }
124 if (info == null) {
125 return GSXML.DOC_TYPE_HIERARCHY;
126 }
127
128 String childtype = info.getInfo("childtype");
129 if (childtype.equals("Paged")) {
130 return GSXML.DOC_TYPE_PAGED;
131 }
132 return GSXML.DOC_TYPE_HIERARCHY;
133
134 }
135
136 /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
137 protected String getRootId(String node_id) {
138 return OID.getTop(node_id);
139 }
140 /** returns a list of the child ids in order, null if no children */
141 protected ArrayList getChildrenIds(String node_id) {
142 DBInfo info = this.gdbm_src.getInfo(node_id);
143 if (info == null) {
144 return null;
145 }
146
147 ArrayList children = new ArrayList();
148
149 String contains = info.getInfo("contains");
150 StringTokenizer st = new StringTokenizer(contains, ";");
151 while (st.hasMoreTokens()) {
152 String child_id = st.nextToken().replaceAll("\"", node_id);
153 children.add(child_id);
154 }
155 return children;
156
157 }
158 /** returns the node id of the parent node, null if no parent */
159 protected String getParentId(String node_id){
160 String parent = OID.getParent(node_id);
161 if (parent.equals(node_id)) {
162 return null;
163 }
164 return parent;
165 }
166
167 /** get the metadata for the classifier node node_id
168 * returns a metadataList element:
169 * <metadataList><metadata name="xxx">value</metadata></metadataList>
170 * if all_metadata is true, returns all available metadata, otherwise just
171 * returns requested metadata
172 */
173 // assumes only one value per metadata
174 protected Element getMetadataList(String node_id, boolean all_metadata,
175 ArrayList metadata_names) {
176 String lang = "en";
177 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
178 DBInfo info = this.gdbm_src.getInfo(node_id);
179 if (info == null) {
180 return null;
181 }
182 if (all_metadata) {
183 // return everything out of the database
184 Set keys = info.getKeys();
185 Iterator it = keys.iterator();
186 while(it.hasNext()) {
187 String key = (String)it.next();
188 String value = info.getInfo(key);
189 GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id));
190 }
191
192 } else {
193 for (int i=0; i<metadata_names.size(); i++) {
194 String meta_name = (String) metadata_names.get(i);
195 String value = (String)info.getInfo(meta_name);
196 GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
197 }
198 }
199 return metadata_list;
200 }
201
202 /** returns the structural information asked for.
203 * info_type may be one of
204 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
205 */
206 protected String getStructureInfo(String doc_id, String info_type) {
207 String value="";
208 if (info_type.equals(INFO_NUM_SIBS)) {
209 String parent_id = OID.getParent(doc_id);
210 if (parent_id.equals(doc_id)) {
211 value="0";
212 } else {
213 value = String.valueOf(getNumChildren(parent_id));
214 }
215 return value;
216 }
217
218 if (info_type.equals(INFO_NUM_CHILDREN)) {
219 return String.valueOf(getNumChildren(doc_id));
220 }
221
222
223 if (info_type.equals(INFO_SIB_POS)) {
224 String parent_id = OID.getParent(doc_id);
225 if (parent_id.equals(doc_id)) {
226 return "-1";
227 }
228
229 DBInfo info = this.gdbm_src.getInfo(parent_id);
230 if (info==null) {
231 return "-1";
232 }
233
234 String contains = info.getInfo("contains");
235 contains = contains.replaceAll("\"", parent_id);
236 String [] children = contains.split(";");
237 for (int i=0;i<children.length;i++) {
238 String child_id = children[i];
239 if (child_id.equals(doc_id)) {
240 return String.valueOf(i+1); // make it from 1 to length
241
242 }
243 }
244
245 return "-1";
246 } else {
247 return null;
248 }
249
250 }
251
252 protected int getNumChildren(String node_id) {
253 DBInfo info = this.gdbm_src.getInfo(node_id);
254 if (info == null) {
255 return 0;
256 }
257 String contains = info.getInfo("contains");
258 if (contains.equals("")) {
259 return 0;
260 }
261 String [] children = contains.split(";");
262 return children.length;
263 }
264
265 /** returns true if the id refers to a document (rather than
266 * a classifier node)
267 */
268 protected boolean isDocumentId(String node_id){
269 if (node_id.startsWith("CL")) {
270 return false;
271 }
272 return true;
273 }
274
275}
Note: See TracBrowser for help on using the repository browser.