/* * GSDL2ClassifierServices.java * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package org.greenstone.gsdl3.service; import org.greenstone.gdbm.*; import org.greenstone.gsdl3.util.*; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.Text; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * A ServicesImpl class for GSDL2-style Classifiers * * @author Katherine Don * @version $Revision: 3567 $ */ public class GSDL2ClassifierServices extends ServicesImpl { // the services on offer private static final String CLASSIFIER_SERVICE = "ClassifierBrowse"; /** gdbm wrapper */ private GDBMWrapper gdbm_src_=null; public GSDL2ClassifierServices() { gdbm_src_ = new GDBMWrapper(); } /** creates a display element containing all the text strings needed to display the service page, in the language specified */ protected Element createServiceDisplay(String service, String lang) { Element display = doc_.createElement(GSXML.DISPLAY_ELEM); display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, getTextString(service+".name", lang))); display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM, getTextString(service+".submit", lang))); Element param; return display; } /** processes the request */ protected Element processService(String name, Element request) { if (!name.equals(CLASSIFIER_SERVICE)) { System.err.println("GSDL2ClassifierServices:should never get here. service type wrong:"+name); return null; } // the response element Element result = doc_.createElement(GSXML.RESPONSE_ELEM); String from = GSPath.appendLink(cluster_name_, CLASSIFIER_SERVICE); result.setAttribute(GSXML.FROM_ATT, from); result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY); Element result_content = doc_.createElement(GSXML.CONTENT_ELEM); result.appendChild(result_content); // get the classifier name out of request content - assume only one for now String path = GSXML.CONTENT_ELEM; path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER); path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM); Element elem = (Element)GSXML.getNodeByPath(request, path); String elem_name = elem.getAttribute(GSXML.NAME_ATT); System.out.println("elem name="+elem_name); if (OID.needsTranslating(elem_name)) { System.out.println("translating oid"); elem_name = gdbm_src_.translateOID(elem_name); } // get the info from gdbm db DBInfo info = gdbm_src_.getInfo(elem_name); // build up the XML Element classifier = doc_.createElement(GSXML.CLASSIFIER_ELEM); String classifier_name = OID.getTop(elem_name); classifier.setAttribute(GSXML.NAME_ATT, classifier_name); result_content.appendChild(classifier); DBInfo top_info; if (elem_name.equals(classifier_name)) { top_info = info; } else { top_info = gdbm_src_.getInfo(classifier_name); } // a substitute template - should be read from config file //String extra = "coll-name***"; //Element extra_doc = converter_.getDOM(extra).getDocumentElement(); //result.appendChild(doc_.importNode(extra_doc, true)); // get the classifier attributes String type = top_info.getInfo("childtype"); if (type.equals("VList")) { classifier.setAttribute("type", "vertical"); classifier.setAttribute("interleave", "true"); classifier.setAttribute("link", "icon"); } else if (type.equals("HList")){ classifier.setAttribute("type", "horizontal"); classifier.setAttribute("interleave", "false"); classifier.setAttribute("link", "name"); } else { System.out.println("unknown type!!! check this"); classifier.setAttribute("type", "vertical"); classifier.setAttribute("interleave", "true"); classifier.setAttribute("link", "icon"); } Element this_node; if (elem_name.equals(classifier_name)) { this_node = classifier; } else { // have to build up the parents // the current node this_node = doc_.createElement("node"); this_node.setAttribute("name", elem_name); type = info.getInfo("childtype"); if (type.equals("VList")) { this_node.setAttribute("type", "vertical"); } else { this_node.setAttribute("type", "horzizontal"); } String title = info.getInfo("Title"); Element meta_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER); GSXML.addMetadata(doc_, meta_list, "Title", title); this_node.appendChild(meta_list); System.out.println("classifier_name="+classifier_name); System.out.println("this name="+elem_name); // the parents String middle_name = OID.getParent(elem_name); Element middle_node = this_node; while (!middle_name.equals(classifier_name)) { System.out.println("middle_name="+middle_name); // add in a new node Element temp_node = doc_.createElement("node"); temp_node.setAttribute("name", middle_name); DBInfo i = gdbm_src_.getInfo(middle_name); type = i.getInfo("childtype"); if (type.equals("VList")) { temp_node.setAttribute("type", "vertical"); } else { temp_node.setAttribute("type", "horzizontal"); } title = i.getInfo("Title"); meta_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER); GSXML.addMetadata(doc_, meta_list, "Title", title); temp_node.appendChild(meta_list); // link into the tree temp_node.appendChild(middle_node); middle_node = temp_node; middle_name = OID.getParent(middle_name); } classifier.appendChild(middle_node); } // get the child nodes String contains = info.getInfo("contains"); // replace the " with parent name contains = OID.translateParent(contains, elem_name); String [] children = contains.split(";"); for (int i=0; i