Ignore:
Timestamp:
2002-11-26T13:59:51+13:00 (21 years ago)
Author:
kjdon
Message:

actions and receptionist now deal with Elements, not strings. has a method to create a display element from a resource bundle.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/core/Receptionist.java

    r3512 r3570  
    1212import java.io.File;
    1313import java.util.HashMap;
    14 
     14import java.util.Enumeration;
     15
     16/** core class for web interface generation. Receives requests consisting
     17 * of an xml representation of cgi args, and returns the page of data - in
     18 * html by default. The requests are processed by the appropriate action class
     19 *
     20 * @see Action
     21 */
    1522public class Receptionist implements ModuleInterface {
    1623
     
    2330    /** container Document to create XML Nodes */
    2431    protected Document doc_=null;
     32   
    2533    /** a converter class to parse XML and create Docs */
    2634    protected XMLConverter converter_=null;
    2735
    28     /** the message router that it talks to */
     36    /** the message router that the actions will talk to */
    2937    protected ModuleInterface mr_=null;
    3038
     
    3240    protected HashMap action_map_=null;
    3341
    34     protected Translate translation_=null;
    3542    public Receptionist() {
    3643    converter_ = new XMLConverter();
    3744    doc_ = converter_.newDOM();
    3845    action_map_= new HashMap();
    39     translation_ = new Translate();
    4046    cgi_ = new CGIArgConverter();
    4147    }
     
    4450    public void setConfigVars(ConfigVars config) {
    4551    config_ = config;
    46     translation_.setConfigVars(config);
    47     }
     52    }
     53
    4854    /** sets the message router  - it should already be created and
    4955     * configured before  being passed to the receptionist*/
     
    5258    }
    5359
     60    /** configures the receptionist */
    5461    public boolean configure() {
    5562
    5663    if (config_==null) {
    57         System.err.println("You must set the config variables before calling configure");
     64        System.err.println("Receptionist Error: config variables must be set before calling configure");
    5865        return false;
    5966    }
    6067    if (mr_==null) {       
    61         System.err.println("You must set  the mr  before calling configure");
     68        System.err.println("Receptionist Error: message router must be set  before calling configure");
    6269        return false;
    6370    }
    6471
    65     // for now, assume default is en, so load that
    66     translation_.setDefaultLanguage("en");
    67     // for now, just statically add in the actions
    68     // actions may disappear if xslt is all that is needed
    69     // or they should be dynamically loaded
     72    // add in the default actions - others can be dynamically
     73    // loaded as needed
     74    // if you want an action to have a short name it needs to go here
    7075    Action a = new PageAction();
    7176    a.setConfigVars(config_);
     
    8287    action_map_.put("q", a);
    8388
    84     a = new BrowseAction();
    85     a.setConfigVars(config_);
    86     a.setCGIConverter(cgi_);
    87     a.setMessageRouter(mr_);
    88     a.configure();
    89     action_map_.put("b", a);
     89    a = new BrowseAction();
     90    a.setConfigVars(config_);
     91    a.setCGIConverter(cgi_);
     92    a.setMessageRouter(mr_);
     93    a.configure();
     94    action_map_.put("b", a);
    9095
    9196    a = new ResourceAction();
     
    102107    a.configure();
    103108    action_map_.put("a", a);
     109
     110
    104111    return true;
    105112    }
    106 
     113    /** process using strings - just calls process using Elements */
    107114    public String process(String xml_in) {
    108115
    109     Document message_doc = converter_.getDOM(xml_in);
    110     Element message = message_doc.getDocumentElement();
    111     // there should only be one request per message
     116    Element message = converter_.getDOM(xml_in).getDocumentElement();
     117    Element page = process(message);
     118    return converter_.getString(page);
     119    }
     120
     121    /** process - produce a page of data in response to a request
     122     * if something goes wrong, it returns null -
     123     * TODO:  return a suitable message to the user */
     124    public Element process(Element message) {
     125
     126    // get the request out of the message - assume that there is only one
    112127    Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
    113128    if (request == null) {
    114         return GSHTML.errorPage("no request in the message");
    115     }
    116 
     129        System.err.println("Receptionist Error: message had no request!");
     130        return null;
     131    }
     132    // check that the request type is correct
    117133    String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
    118134    if (!type.equals(GSXML.REQUEST_TYPE_ACTION)) {
    119         return GSHTML.errorPage("wrong type in request - should be action");
     135        System.err.println("Receptionist Error: request type should be '"+GSXML.REQUEST_TYPE_ACTION+"', but it was '"+type+"'!");
     136        return null;
    120137    }   
    121 
     138    // work out which action to pass to
    122139    String action = request.getAttribute(GSXML.ACTION_ATT);
    123140    if (action.equals("")) {
    124         return GSHTML.errorPage(" no action specified");
    125     }
    126     // pass to appropriate action
     141        System.err.println("Receptionist Error: no action specified in the request!");
     142        return null;
     143    }
     144    // convert cgi short names to long names in the param list
     145    Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     146    if (!cgi_.toLong(param_list)) {
     147        System.err.println("Receptionist Error: couldn't convert short names to long names in the param list!");
     148        return null;
     149    }
    127150   
     151    // pass request to appropriate action   
    128152    Action a = (Action)action_map_.get(action);
    129     if (a==null) {
     153    String action_name=null;
     154    if (a==null) { // not in the map yet
    130155        // try to load a new action
    131156        try {
    132         String action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
    133         System.out.println("new action name="+action_name);
     157        action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
    134158        Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance();
    135159        ac.setConfigVars(config_);
     
    141165        } catch (Exception e) {
    142166       
    143         return GSHTML.errorPage("no action for "+action+", and couldn't create a new one: error message:"+e.getMessage());
     167        System.err.println("Receptionist Error: a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage());
     168        return null;
    144169        }
    145170    }
    146171
    147     // add in the translation bit - should we do it here? or give the actions access to the translation object?
    148     String lang = message.getAttribute(GSXML.LANG_ATT);
    149     Element trans = translation_.getLanguageTree(lang);
    150     message.appendChild(message_doc.importNode(trans, true));
    151 
    152     // add in the system vars bit
     172    // add in the  system stuff - display and config
     173    Document message_doc = message.getOwnerDocument();
     174    String lang = request.getAttribute(GSXML.LANG_ATT);
     175    Element display = getDisplayElement(lang);
     176    message.appendChild(message_doc.importNode(display, true));
    153177    message.appendChild(message_doc.importNode(config_.config_xml_, true));
    154178
    155     String page = a.process(message);
     179    Element page = a.process(message);
    156180    return page;
    157     }
    158 
    159     public Element process(Element xml_in) {
    160     return xml_in; // for now
    161 
    162     }
    163 
     181   
     182    }
     183
     184    /** returns an xml element containing all the text strings
     185     * needed for an interface
     186     * currenlty only uses the current interface - should add in
     187     * needed ones from the default interface if current != default */
     188    protected Element getDisplayElement(String lang) {
     189
     190    Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
     191    // looks for properties files based on interface name
     192    String resource_name = "interface_"+config_.interface_name_;
     193
     194    Dictionary dict = new Dictionary(resource_name, lang);
     195    Enumeration enum = dict.getKeys();
     196    while (enum.hasMoreElements()) {
     197        String key = (String)enum.nextElement();
     198        String value = dict.get(key);
     199        Element e = GSXML.createTextElement(doc_, key, value);
     200        display.appendChild(e);
     201    }
     202    return display;
     203    }
    164204
    165205}
Note: See TracChangeset for help on using the changeset viewer.