source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/Action.java@ 32063

Last change on this file since 32063 was 32063, checked in by kjdon, 6 years ago

added xmltransformer to base action for any actions that need it. will be used by documentaction.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Iterator;
6
7import org.apache.log4j.Logger;
8import org.greenstone.gsdl3.core.ModuleInterface;
9import org.greenstone.gsdl3.util.Dictionary;
10import org.greenstone.gsdl3.util.GSConstants;
11import org.greenstone.gsdl3.util.GSParams;
12import org.greenstone.gsdl3.util.GSXML;
13import org.greenstone.gsdl3.util.GSXSLT;
14import org.greenstone.gsdl3.util.UserContext;
15import org.greenstone.gsdl3.util.XMLConverter;
16import org.greenstone.gsdl3.util.XMLTransformer;
17import org.w3c.dom.Document;
18import org.w3c.dom.Element;
19import org.w3c.dom.Node;
20import org.w3c.dom.NodeList;
21
22/** base class for Actions */
23abstract public class Action
24{
25
26 /** the system set up variables */
27 protected HashMap<String, Object> config_params = null;
28
29
30 /** a converter class to parse XML and create Docs */
31 protected XMLConverter converter = null;
32 /** a transformer class in case the action wants to run XSLT itself */
33 protected XMLTransformer transformer = null;
34 /**
35 * a reference to the message router that it must talk to to get info. it
36 * may be a communicator acting as a proxy, but it doesn't care about that
37 */
38 protected ModuleInterface mr = null;
39
40 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
41
42 public Action()
43 {
44 this.converter = new XMLConverter();
45 this.transformer = new XMLTransformer();
46 }
47
48 /** the config variables must be set before configure is called */
49 public void setConfigParams(HashMap<String, Object> params)
50 {
51 this.config_params = params;
52 }
53
54 /** sets the message router */
55 public void setMessageRouter(ModuleInterface m)
56 {
57 this.mr = m;
58 }
59
60 public boolean configure()
61 {
62 // does nothing yet
63 return true;
64 }
65
66 /**
67 * process takes an xml representation of cgi args and returns the page of
68 * results - may be in html/xml/other depending on the output att of the
69 * request
70 */
71 public String process(String xml_in)
72 {
73
74 Document message_doc = this.converter.getDOM(xml_in);
75 if (message_doc == null)
76 {
77 logger.error("Couldn't parse request");
78 logger.error(xml_in);
79 return null;
80 }
81 Node result = process(message_doc);
82 return this.converter.getString(result);
83 }
84
85 /** the main process method - must be implemented in subclass */
86 abstract public Node process(Node xml_in);
87
88 /**
89 * tell the param class what its arguments are if an action has its own
90 * arguments, this should add them to the params object - particularly
91 * important for args that should not be saved
92 */
93 public boolean addActionParameters(GSParams params)
94 {
95 return true;
96 }
97
98 protected void getRequiredMetadataNames(Element format, HashSet<String> meta_names) {
99 GSXSLT.findExtraMetadataNames(format, meta_names);
100 }
101
102
103 protected Element createMetadataParamList(Document doc, HashSet<String> metadata_names)
104 {
105 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
106
107 Element param = null;
108 Iterator<String> i = metadata_names.iterator();
109 while (i.hasNext())
110 {
111 String name = i.next();
112 param = doc.createElement(GSXML.PARAM_ELEM);
113 param_list.appendChild(param);
114 param.setAttribute(GSXML.NAME_ATT, "metadata");
115 param.setAttribute(GSXML.VALUE_ATT, name);
116
117 }
118 return param_list;
119 }
120
121 protected boolean processErrorElements(Element message, Element page)
122 {
123 NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
124 if (error_nodes.getLength() == 0)
125 {
126 return false;
127 }
128 Document owner = page.getOwnerDocument();
129 for (int i = 0; i < error_nodes.getLength(); i++)
130 {
131 page.appendChild(owner.importNode(error_nodes.item(i), true));
132 }
133 return true;
134 }
135
136 /**
137 * Takes an XML element and adds the metadata of the current site to it.
138 * Useful for adding the current site's metadata to a response before
139 * sending it
140 *
141 * @param element
142 * the element to add site metadata to
143 * @param lang
144 * the current language
145 * @param uid
146 * the current user id
147 */
148 protected void addSiteMetadata(Element element, UserContext userContext)
149 {
150 Document doc = element.getOwnerDocument();
151
152 //ADD SITE METADATA
153 Element metadata_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
154 //create the element to put the params in
155 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
156 // want to get metadataList and displayItemList
157 GSXML.addParameterToList(param_list, GSXML.SUBSET_PARAM , GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
158 GSXML.addParameterToList(param_list, GSXML.SUBSET_PARAM , GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
159
160 metadata_request.appendChild(param_list);
161 //create the message
162 Element metadata_message = doc.createElement(GSXML.MESSAGE_ELEM);
163 metadata_message.appendChild(metadata_request);
164 //get response
165 Element metadata_response_message = (Element) this.mr.process(metadata_message);
166 //drill down to response
167 Element metadata_response = (Element) GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
168 //merge in metadata
169 // *************** need to merge the displayItem lists too
170 GSXML.mergeMetadataLists(element, metadata_response);
171 GSXML.mergeSpecifiedLists(element, metadata_response, GSXML.DISPLAY_TEXT_ELEM);
172 }
173
174 protected void addInterfaceOptions(Element elem)
175 {
176 Document doc = elem.getOwnerDocument();
177
178 Element documentOptionList = doc.createElement("interfaceOptions");
179 for (Object key : this.config_params.keySet())
180 {
181 Element option = doc.createElement("option");
182 option.setAttribute(GSXML.NAME_ATT, (String) key);
183 option.setAttribute(GSXML.VALUE_ATT, this.config_params.get(key).toString());
184 documentOptionList.appendChild(option);
185 }
186 elem.appendChild(elem.getOwnerDocument().importNode(documentOptionList, true));
187 }
188
189 protected Element getFormatInfo(String to, UserContext userContext)
190 {
191 // Eclipse call hierarchy shows the element returned from this method is
192 // subsequently used in a 'importNode'. For this reason it is safe here
193 // for call up our own document DOM.
194 //
195 // If this pattern changes for any reason, then the DOM will need to be
196 // passed in as a parameter
197
198 Document doc = XMLConverter.newDOM();
199
200 Element mr_format_message = doc.createElement(GSXML.MESSAGE_ELEM);
201 Element mr_format_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
202 mr_format_message.appendChild(mr_format_request);
203
204 // process the message
205 Element mr_response_message = (Element) this.mr.process(mr_format_message);
206 // the response
207
208 Element format_response = (Element) GSXML.getChildByTagName(mr_response_message, GSXML.RESPONSE_ELEM);
209
210 Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
211 if (format_elem != null)
212 {
213 Element global_format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.GLOBAL_FORMAT_ELEM);
214 if (global_format_elem != null)
215 {
216 GSXSLT.mergeFormatElements(format_elem, global_format_elem, false);
217 }
218 }
219 return format_elem;
220 }
221
222 protected String getTextString(String key, String lang, String dictionary, String[] args)
223 {
224 logger.error("lang = "+lang);
225 if (dictionary != null)
226 {
227 // just try the one specified dictionary
228 Dictionary dict = new Dictionary(dictionary, lang);
229 String result = dict.get(key, args);
230 if (result == null)
231 { // not found
232 return "_" + key + "_";
233 }
234 return result;
235 }
236
237 // otherwise we try class names for dictionary names
238 String class_name = this.getClass().getName();
239 class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
240 Dictionary dict = new Dictionary(class_name, lang);
241 String result = dict.get(key, args);
242 if (result != null)
243 {
244 return result;
245 }
246
247 // we have to try super classes
248 Class c = this.getClass().getSuperclass();
249 while (result == null && c != null)
250 {
251 class_name = c.getName();
252 class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
253 if (class_name.equals("ServiceRack"))
254 {
255 // this is as far as we go
256 break;
257 }
258 dict = new Dictionary(class_name, lang);
259 result = dict.get(key, args);
260 c = c.getSuperclass();
261 }
262 if (result == null)
263 {
264 return "_" + key + "_";
265 }
266 return result;
267
268 }
269
270}
Note: See TracBrowser for help on using the repository browser.