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

Last change on this file since 14185 was 13962, checked in by shaoqun, 17 years ago

added a method to get a particular metadata(indicated by key) of a classifer

  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 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 protected String getMetadata(String node_id, String key){
172 DBInfo info = this.gdbm_src.getInfo(node_id);
173 if (info == null) {
174 return "";
175 }
176
177 Set keys = info.getKeys();
178 Iterator it = keys.iterator();
179 while(it.hasNext()) {
180 String key_in = (String)it.next();
181 String value = info.getInfo(key);
182 if (key_in.equals(key)){
183 return value;
184 }
185 }
186
187 return "";
188
189 }
190
191 /** get the metadata for the classifier node node_id
192 * returns a metadataList element:
193 * <metadataList><metadata name="xxx">value</metadata></metadataList>
194 * if all_metadata is true, returns all available metadata, otherwise just
195 * returns requested metadata
196 */
197 // assumes only one value per metadata
198 protected Element getMetadataList(String node_id, boolean all_metadata,
199 ArrayList metadata_names) {
200 String lang = "en";
201 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
202 DBInfo info = this.gdbm_src.getInfo(node_id);
203 if (info == null) {
204 return null;
205 }
206 if (all_metadata) {
207 // return everything out of the database
208 Set keys = info.getKeys();
209 Iterator it = keys.iterator();
210 while(it.hasNext()) {
211 String key = (String)it.next();
212 String value = info.getInfo(key);
213 GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id));
214 }
215
216 } else {
217 for (int i=0; i<metadata_names.size(); i++) {
218 String meta_name = (String) metadata_names.get(i);
219 String value = (String)info.getInfo(meta_name);
220 GSXML.addMetadata(this.doc, metadata_list, meta_name, value);
221 }
222 }
223 return metadata_list;
224 }
225
226 /** returns the structural information asked for.
227 * info_type may be one of
228 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
229 */
230 protected String getStructureInfo(String doc_id, String info_type) {
231 String value="";
232 if (info_type.equals(INFO_NUM_SIBS)) {
233 String parent_id = OID.getParent(doc_id);
234 if (parent_id.equals(doc_id)) {
235 value="0";
236 } else {
237 value = String.valueOf(getNumChildren(parent_id));
238 }
239 return value;
240 }
241
242 if (info_type.equals(INFO_NUM_CHILDREN)) {
243 return String.valueOf(getNumChildren(doc_id));
244 }
245
246
247 if (info_type.equals(INFO_SIB_POS)) {
248 String parent_id = OID.getParent(doc_id);
249 if (parent_id.equals(doc_id)) {
250 return "-1";
251 }
252
253 DBInfo info = this.gdbm_src.getInfo(parent_id);
254 if (info==null) {
255 return "-1";
256 }
257
258 String contains = info.getInfo("contains");
259 contains = contains.replaceAll("\"", parent_id);
260 String [] children = contains.split(";");
261 for (int i=0;i<children.length;i++) {
262 String child_id = children[i];
263 if (child_id.equals(doc_id)) {
264 return String.valueOf(i+1); // make it from 1 to length
265
266 }
267 }
268
269 return "-1";
270 } else {
271 return null;
272 }
273
274 }
275
276 protected int getNumChildren(String node_id) {
277 DBInfo info = this.gdbm_src.getInfo(node_id);
278 if (info == null) {
279 return 0;
280 }
281 String contains = info.getInfo("contains");
282 if (contains.equals("")) {
283 return 0;
284 }
285 String [] children = contains.split(";");
286 return children.length;
287 }
288
289 /** returns true if the id refers to a document (rather than
290 * a classifier node)
291 */
292 protected boolean isDocumentId(String node_id){
293 if (node_id.startsWith("CL")) {
294 return false;
295 }
296 return true;
297 }
298
299}
Note: See TracBrowser for help on using the repository browser.