source: trunk/gsdl3/src/java/org/greenstone/gsdl3/core/Receptionist.java@ 3570

Last change on this file since 3570 was 3570, checked in by kjdon, 22 years ago

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

  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.gsdl3.util.*;
4import org.greenstone.gsdl3.action.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11// other java classes
12import java.io.File;
13import java.util.HashMap;
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 */
22public class Receptionist implements ModuleInterface {
23
24 /** the set up variables */
25 protected ConfigVars config_=null;
26
27 /** the cgi args converter */
28 protected CGIArgConverter cgi_=null;
29
30 /** container Document to create XML Nodes */
31 protected Document doc_=null;
32
33 /** a converter class to parse XML and create Docs */
34 protected XMLConverter converter_=null;
35
36 /** the message router that the actions will talk to */
37 protected ModuleInterface mr_=null;
38
39 /** the list of actions */
40 protected HashMap action_map_=null;
41
42 public Receptionist() {
43 converter_ = new XMLConverter();
44 doc_ = converter_.newDOM();
45 action_map_= new HashMap();
46 cgi_ = new CGIArgConverter();
47 }
48
49 /** the set up vars must be set before configure called*/
50 public void setConfigVars(ConfigVars config) {
51 config_ = config;
52 }
53
54 /** sets the message router - it should already be created and
55 * configured before being passed to the receptionist*/
56 public void setMessageRouter(ModuleInterface m) {
57 mr_ = m;
58 }
59
60 /** configures the receptionist */
61 public boolean configure() {
62
63 if (config_==null) {
64 System.err.println("Receptionist Error: config variables must be set before calling configure");
65 return false;
66 }
67 if (mr_==null) {
68 System.err.println("Receptionist Error: message router must be set before calling configure");
69 return false;
70 }
71
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
75 Action a = new PageAction();
76 a.setConfigVars(config_);
77 a.setMessageRouter(mr_);
78 a.setCGIConverter(cgi_);
79 a.configure();
80 action_map_.put("p", a);
81
82 a = new QueryAction();
83 a.setConfigVars(config_);
84 a.setCGIConverter(cgi_);
85 a.setMessageRouter(mr_);
86 a.configure();
87 action_map_.put("q", a);
88
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);
95
96 a = new ResourceAction();
97 a.setConfigVars(config_);
98 a.setCGIConverter(cgi_);
99 a.setMessageRouter(mr_);
100 a.configure();
101 action_map_.put("r", a);
102
103 a = new AppletAction();
104 a.setConfigVars(config_);
105 a.setCGIConverter(cgi_);
106 a.setMessageRouter(mr_);
107 a.configure();
108 action_map_.put("a", a);
109
110
111 return true;
112 }
113 /** process using strings - just calls process using Elements */
114 public String process(String xml_in) {
115
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
127 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
128 if (request == null) {
129 System.err.println("Receptionist Error: message had no request!");
130 return null;
131 }
132 // check that the request type is correct
133 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
134 if (!type.equals(GSXML.REQUEST_TYPE_ACTION)) {
135 System.err.println("Receptionist Error: request type should be '"+GSXML.REQUEST_TYPE_ACTION+"', but it was '"+type+"'!");
136 return null;
137 }
138 // work out which action to pass to
139 String action = request.getAttribute(GSXML.ACTION_ATT);
140 if (action.equals("")) {
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 }
150
151 // pass request to appropriate action
152 Action a = (Action)action_map_.get(action);
153 String action_name=null;
154 if (a==null) { // not in the map yet
155 // try to load a new action
156 try {
157 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
158 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance();
159 ac.setConfigVars(config_);
160 ac.setMessageRouter(mr_);
161 ac.setCGIConverter(cgi_);
162 ac.configure();
163 action_map_.put(action, ac);
164 a = ac;
165 } catch (Exception e) {
166
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;
169 }
170 }
171
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));
177 message.appendChild(message_doc.importNode(config_.config_xml_, true));
178
179 Element page = a.process(message);
180 return page;
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 }
204
205}
Note: See TracBrowser for help on using the repository browser.