Changeset 25690
- Timestamp:
- 2012-05-29T11:06:31+12:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Browse.java
r25635 r25690 29 29 // XML classes 30 30 import org.w3c.dom.Document; 31 import org.w3c.dom.Element; 31 import org.w3c.dom.Element; 32 32 import org.w3c.dom.NodeList; 33 33 … … 40 40 import org.apache.log4j.*; 41 41 42 /** Greenstone 2 collection classifier service 43 * 42 /** 43 * Greenstone 2 collection classifier service 44 * 44 45 * @author <a href="mailto:[email protected]">Katherine Don</a> 45 46 * @author <a href="mailto:[email protected]">Michael Dewsnip</a> 46 47 */ 47 48 48 public class GS2Browse 49 extends AbstractBrowse 49 public class GS2Browse extends AbstractBrowse 50 50 { 51 51 52 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2Browse.class.getName()); 53 54 protected SimpleCollectionDatabase coll_db = null; 55 56 public GS2Browse() 57 { 58 } 59 60 public void cleanUp() { 61 super.cleanUp(); 62 this.coll_db.closeDatabase(); 63 } 64 65 public boolean configure(Element info, Element extra_info) 66 { 67 if (!super.configure(info, extra_info)){ 68 return false; 69 } 70 71 logger.info("Configuring GS2Browse..."); 72 // the index stem is either specified in the config file or is the collection name 73 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM); 74 String index_stem = null; 75 if (index_stem_elem != null) { 76 index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT); 77 } 78 if (index_stem == null || index_stem.equals("")) { 79 index_stem = this.cluster_name; 80 } 81 82 // find out what kind of database we have 83 Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM); 84 String database_type = null; 85 if (database_type_elem != null) { 86 database_type = database_type_elem.getAttribute(GSXML.NAME_ATT); 87 } 88 89 if (database_type == null || database_type.equals("")) { 90 database_type = "gdbm"; // the default 91 } 92 coll_db = new SimpleCollectionDatabase(database_type); 93 if (!coll_db.databaseOK()) { 94 logger.error("Couldn't create the collection database of type "+database_type); 95 return false; 96 } 97 98 // Open database for querying 99 String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, index_stem, database_type); 100 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) { 101 logger.error("Could not open collection database!"); 102 return false; 103 } 104 this.macro_resolver = new GS2MacroResolver(this.coll_db); 105 return true; 106 } 107 108 /** if id ends in .fc, .pc etc, then translate it to the correct id */ 109 protected String translateId(String node_id) { 110 return OID.translateOID(this.coll_db, node_id); //return this.coll_db.translateOID(node_id); 111 } 112 113 /** returns the document type of the doc that the specified node 114 belongs to. should be one of 115 GSXML.DOC_TYPE_SIMPLE, 116 GSXML.DOC_TYPE_PAGED, 117 GSXML.DOC_TYPE_HIERARCHY 118 */ 119 protected String getDocType(String node_id) { 120 DBInfo info = this.coll_db.getInfo(node_id); 121 if (info == null) { 122 return GSXML.DOC_TYPE_SIMPLE; 123 } 124 String doc_type = info.getInfo("doctype"); 125 if (!doc_type.equals("")&&!doc_type.equals("doc")) { 126 return doc_type; 127 } 128 129 String top_id = OID.getTop(node_id); 130 boolean is_top = (top_id.equals(node_id) ? true : false); 131 132 String children = info.getInfo("contains"); 133 boolean is_leaf = (children.equals("") ? true : false); 134 135 if (is_top && is_leaf) { // a single section document 136 return GSXML.DOC_TYPE_SIMPLE; 137 } 138 139 // now we just check the top node 140 if (!is_top) { // we need to look at the top info 141 info = this.coll_db.getInfo(top_id); 142 } 143 if (info == null) { 144 return GSXML.DOC_TYPE_HIERARCHY; 145 } 146 147 String childtype = info.getInfo("childtype"); 148 if (childtype.equals("Paged")) { 149 return GSXML.DOC_TYPE_PAGED; 150 } 151 return GSXML.DOC_TYPE_HIERARCHY; 152 153 } 154 155 /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */ 156 protected String getRootId(String node_id) { 157 return OID.getTop(node_id); 158 } 159 /** returns a list of the child ids in order, null if no children */ 160 protected ArrayList<String> getChildrenIds(String node_id) { 161 DBInfo info = this.coll_db.getInfo(node_id); 162 if (info == null) { 163 return null; 164 } 165 166 ArrayList<String> children = new ArrayList<String>(); 167 168 String contains = info.getInfo("contains"); 169 StringTokenizer st = new StringTokenizer(contains, ";"); 170 while (st.hasMoreTokens()) { 171 String child_id = st.nextToken().replaceAll("\"", node_id); 172 children.add(child_id); 173 } 174 return children; 175 176 } 177 /** returns the node id of the parent node, null if no parent */ 178 protected String getParentId(String node_id){ 179 String parent = OID.getParent(node_id); 180 if (parent.equals(node_id)) { 181 return null; 182 } 183 return parent; 184 } 185 186 protected String getMetadata(String node_id, String key){ 187 DBInfo info = this.coll_db.getInfo(node_id); 188 if (info == null) { 189 return ""; 190 } 191 192 Set<String> keys = info.getKeys(); 193 Iterator<String> it = keys.iterator(); 194 while(it.hasNext()) { 195 String key_in = it.next(); 196 String value = info.getInfo(key); 197 if (key_in.equals(key)){ 198 return value; 199 } 200 } 201 202 return ""; 203 204 } 205 206 /** get the metadata for the classifier node node_id 207 * returns a metadataList element: 208 * <metadataList><metadata name="xxx">value</metadata></metadataList> 209 * if all_metadata is true, returns all available metadata, otherwise just 210 * returns requested metadata 211 */ 212 // assumes only one value per metadata 213 protected Element getMetadataList(String node_id, boolean all_metadata, 214 ArrayList<String> metadata_names) { 215 String lang = "en"; 216 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER); 217 DBInfo info = this.coll_db.getInfo(node_id); 218 if (info == null) { 219 return null; 220 } 221 if (all_metadata) { 222 // return everything out of the database 223 Set<String> keys = info.getKeys(); 224 Iterator<String> it = keys.iterator(); 225 while(it.hasNext()) { 226 String key = it.next(); 227 String value = info.getInfo(key); 228 GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id)); 229 } 230 231 } else { 232 for (int i=0; i<metadata_names.size(); i++) { 233 String meta_name = metadata_names.get(i); 234 String value = (String)info.getInfo(meta_name); 235 GSXML.addMetadata(this.doc, metadata_list, meta_name, value); 236 } 237 } 238 return metadata_list; 239 } 240 241 /** returns the structural information asked for. 242 * info_type may be one of 243 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS 244 */ 245 protected String getStructureInfo(String doc_id, String info_type) { 246 String value=""; 247 if (info_type.equals(INFO_NUM_SIBS)) { 248 String parent_id = OID.getParent(doc_id); 249 if (parent_id.equals(doc_id)) { 250 value="0"; 251 } else { 252 value = String.valueOf(getNumChildren(parent_id)); 253 } 254 return value; 255 } 256 257 if (info_type.equals(INFO_NUM_CHILDREN)) { 258 return String.valueOf(getNumChildren(doc_id)); 259 } 260 261 262 if (info_type.equals(INFO_SIB_POS)) { 263 String parent_id = OID.getParent(doc_id); 264 if (parent_id.equals(doc_id)) { 265 return "-1"; 266 } 267 268 DBInfo info = this.coll_db.getInfo(parent_id); 269 if (info==null) { 270 return "-1"; 271 } 272 273 String contains = info.getInfo("contains"); 274 contains = contains.replaceAll("\"", parent_id); 275 String [] children = contains.split(";"); 276 for (int i=0;i<children.length;i++) { 277 String child_id = children[i]; 278 if (child_id.equals(doc_id)) { 279 return String.valueOf(i+1); // make it from 1 to length 280 281 } 282 } 283 284 return "-1"; 285 } else { 286 return null; 287 } 288 289 } 290 291 protected int getNumChildren(String node_id) { 292 DBInfo info = this.coll_db.getInfo(node_id); 293 if (info == null) { 294 return 0; 295 } 296 String contains = info.getInfo("contains"); 297 if (contains.equals("")) { 298 return 0; 299 } 300 String [] children = contains.split(";"); 301 return children.length; 302 } 303 304 /** returns true if the id refers to a document (rather than 305 * a classifier node) 306 */ 307 protected boolean isDocumentId(String node_id){ 308 if (node_id.startsWith("CL")) { 309 return false; 310 } 311 return true; 312 } 52 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2Browse.class.getName()); 53 54 protected SimpleCollectionDatabase coll_db = null; 55 56 public GS2Browse() 57 { 58 } 59 60 public void cleanUp() 61 { 62 super.cleanUp(); 63 this.coll_db.closeDatabase(); 64 } 65 66 public boolean configure(Element info, Element extra_info) 67 { 68 if (!super.configure(info, extra_info)) 69 { 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 { 79 index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT); 80 } 81 if (index_stem == null || index_stem.equals("")) 82 { 83 index_stem = this.cluster_name; 84 } 85 86 // find out what kind of database we have 87 Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM); 88 String database_type = null; 89 if (database_type_elem != null) 90 { 91 database_type = database_type_elem.getAttribute(GSXML.NAME_ATT); 92 } 93 94 if (database_type == null || database_type.equals("")) 95 { 96 database_type = "gdbm"; // the default 97 } 98 coll_db = new SimpleCollectionDatabase(database_type); 99 if (!coll_db.databaseOK()) 100 { 101 logger.error("Couldn't create the collection database of type " + database_type); 102 return false; 103 } 104 105 // Open database for querying 106 String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, index_stem, database_type); 107 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) 108 { 109 logger.error("Could not open collection database!"); 110 return false; 111 } 112 this.macro_resolver = new GS2MacroResolver(this.coll_db); 113 return true; 114 } 115 116 /** if id ends in .fc, .pc etc, then translate it to the correct id */ 117 protected String translateId(String node_id) 118 { 119 return OID.translateOID(this.coll_db, node_id); //return this.coll_db.translateOID(node_id); 120 } 121 122 /** 123 * returns the document type of the doc that the specified node belongs to. 124 * should be one of GSXML.DOC_TYPE_SIMPLE, GSXML.DOC_TYPE_PAGED, 125 * GSXML.DOC_TYPE_HIERARCHY 126 */ 127 protected String getDocType(String node_id) 128 { 129 DBInfo info = this.coll_db.getInfo(node_id); 130 if (info == null) 131 { 132 return GSXML.DOC_TYPE_SIMPLE; 133 } 134 String doc_type = info.getInfo("doctype"); 135 if (!doc_type.equals("") && !doc_type.equals("doc")) 136 { 137 return doc_type; 138 } 139 140 String top_id = OID.getTop(node_id); 141 boolean is_top = (top_id.equals(node_id) ? true : false); 142 143 String children = info.getInfo("contains"); 144 boolean is_leaf = (children.equals("") ? true : false); 145 146 if (is_top && is_leaf) 147 { // a single section document 148 return GSXML.DOC_TYPE_SIMPLE; 149 } 150 151 // now we just check the top node 152 if (!is_top) 153 { // we need to look at the top info 154 info = this.coll_db.getInfo(top_id); 155 } 156 if (info == null) 157 { 158 return GSXML.DOC_TYPE_HIERARCHY; 159 } 160 161 String childtype = info.getInfo("childtype"); 162 if (childtype.equals("Paged")) 163 { 164 return GSXML.DOC_TYPE_PAGED; 165 } 166 return GSXML.DOC_TYPE_HIERARCHY; 167 168 } 169 170 /** 171 * returns the id of the root node of the document containing node node_id. 172 * . may be the same as node_id 173 */ 174 protected String getRootId(String node_id) 175 { 176 return OID.getTop(node_id); 177 } 178 179 /** returns a list of the child ids in order, null if no children */ 180 protected ArrayList<String> getChildrenIds(String node_id) 181 { 182 DBInfo info = this.coll_db.getInfo(node_id); 183 if (info == null) 184 { 185 return null; 186 } 187 188 ArrayList<String> children = new ArrayList<String>(); 189 190 String contains = info.getInfo("contains"); 191 StringTokenizer st = new StringTokenizer(contains, ";"); 192 while (st.hasMoreTokens()) 193 { 194 String child_id = st.nextToken().replaceAll("\"", node_id); 195 children.add(child_id); 196 } 197 return children; 198 199 } 200 201 /** returns the node id of the parent node, null if no parent */ 202 protected String getParentId(String node_id) 203 { 204 String parent = OID.getParent(node_id); 205 if (parent.equals(node_id)) 206 { 207 return null; 208 } 209 return parent; 210 } 211 212 protected String getMetadata(String node_id, String key) 213 { 214 DBInfo info = this.coll_db.getInfo(node_id); 215 if (info == null) 216 { 217 return ""; 218 } 219 220 Set<String> keys = info.getKeys(); 221 Iterator<String> it = keys.iterator(); 222 while (it.hasNext()) 223 { 224 String key_in = it.next(); 225 String value = info.getInfo(key); 226 if (key_in.equals(key)) 227 { 228 return value; 229 } 230 } 231 232 return ""; 233 234 } 235 236 /** 237 * get the metadata for the classifier node node_id returns a metadataList 238 * element: <metadataList><metadata 239 * name="xxx">value</metadata></metadataList> if all_metadata is true, 240 * returns all available metadata, otherwise just returns requested metadata 241 */ 242 // assumes only one value per metadata 243 protected Element getMetadataList(String node_id, boolean all_metadata, ArrayList<String> metadata_names) 244 { 245 String lang = "en"; 246 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER); 247 DBInfo info = this.coll_db.getInfo(node_id); 248 if (info == null) 249 { 250 return null; 251 } 252 if (all_metadata) 253 { 254 // return everything out of the database 255 Set<String> keys = info.getKeys(); 256 Iterator<String> it = keys.iterator(); 257 while (it.hasNext()) 258 { 259 String key = it.next(); 260 String value = info.getInfo(key); 261 GSXML.addMetadata(this.doc, metadata_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id)); 262 } 263 264 } 265 else 266 { 267 for (int i = 0; i < metadata_names.size(); i++) 268 { 269 String meta_name = metadata_names.get(i); 270 String value = (String) info.getInfo(meta_name); 271 GSXML.addMetadata(this.doc, metadata_list, meta_name, value); 272 } 273 } 274 return metadata_list; 275 } 276 277 /** 278 * returns the structural information asked for. info_type may be one of 279 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS 280 */ 281 protected String getStructureInfo(String doc_id, String info_type) 282 { 283 String value = ""; 284 if (info_type.equals(INFO_NUM_SIBS)) 285 { 286 String parent_id = OID.getParent(doc_id); 287 if (parent_id.equals(doc_id)) 288 { 289 value = "0"; 290 } 291 else 292 { 293 value = String.valueOf(getNumChildren(parent_id)); 294 } 295 return value; 296 } 297 298 if (info_type.equals(INFO_NUM_CHILDREN)) 299 { 300 return String.valueOf(getNumChildren(doc_id)); 301 } 302 303 if (info_type.equals(INFO_SIB_POS)) 304 { 305 String parent_id = OID.getParent(doc_id); 306 if (parent_id.equals(doc_id)) 307 { 308 return "-1"; 309 } 310 311 DBInfo info = this.coll_db.getInfo(parent_id); 312 if (info == null) 313 { 314 return "-1"; 315 } 316 317 String contains = info.getInfo("contains"); 318 contains = contains.replaceAll("\"", parent_id); 319 String[] children = contains.split(";"); 320 for (int i = 0; i < children.length; i++) 321 { 322 String child_id = children[i]; 323 if (child_id.equals(doc_id)) 324 { 325 return String.valueOf(i + 1); // make it from 1 to length 326 327 } 328 } 329 330 return "-1"; 331 } 332 else 333 { 334 return null; 335 } 336 337 } 338 339 protected int getNumChildren(String node_id) 340 { 341 DBInfo info = this.coll_db.getInfo(node_id); 342 if (info == null) 343 { 344 return 0; 345 } 346 String contains = info.getInfo("contains"); 347 if (contains.equals("")) 348 { 349 return 0; 350 } 351 String[] children = contains.split(";"); 352 return children.length; 353 } 354 355 /** 356 * returns true if the id refers to a document (rather than a classifier 357 * node) 358 */ 359 protected boolean isDocumentId(String node_id) 360 { 361 if (node_id.startsWith("CL")) 362 { 363 return false; 364 } 365 return true; 366 } 313 367 314 368 }
Note:
See TracChangeset
for help on using the changeset viewer.