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

Last change on this file since 13576 was 13270, checked in by shaoqun, 18 years ago

replace Category class which is deprecated with Logger class

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