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

Last change on this file since 26038 was 26038, checked in by ak19, 12 years ago

Updated addLinkMetaNames to deal with weblinks (for the Pointing To Docs On The Web tutorial) and equivdoc links (for the Associated Files tutorial), so that these can be referenced as other gsf:link types rather than as gsf:metadata for starting tag, anchor and closing tag. The last three were tested against the tutorials and they worked, but would like the format statements to be in a more GS3 way. For now the gsf:link type=equivdoc works with the changes in config_format, but still have issues with weblink because of the webicon. Once that's fixed, will commit config_format.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import org.greenstone.gsdl3.core.ModuleInterface;
4import org.greenstone.gsdl3.util.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Element;
9import org.w3c.dom.Document;
10
11// other java stuff
12import java.io.File;
13import java.util.Vector;
14import java.util.HashMap;
15import java.util.HashSet;
16import java.util.Iterator;
17
18import org.apache.log4j.*;
19
20/** base class for Actions */
21abstract public class Action
22{
23
24 /** the system set up variables */
25 protected HashMap<String, Comparable> config_params = null;
26 /** container Document to create XML Nodes */
27 protected Document doc = null;
28 /** a converter class to parse XML and create Docs */
29 protected XMLConverter converter = null;
30 /**
31 * a reference to the message router that it must talk to to get info. it
32 * may be a communicator acting as a proxy, but it doesn't care about that
33 */
34 protected ModuleInterface mr = null;
35
36 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
37
38 public Action()
39 {
40 this.converter = new XMLConverter();
41 this.doc = this.converter.newDOM();
42 }
43
44 /** the config variables must be set before configure is called */
45 public void setConfigParams(HashMap<String, Comparable> params)
46 {
47 this.config_params = params;
48 }
49
50 /** sets the message router */
51 public void setMessageRouter(ModuleInterface m)
52 {
53 this.mr = m;
54 }
55
56 public boolean configure()
57 {
58 // does nothing yet
59 return true;
60 }
61
62 /**
63 * process takes an xml representation of cgi args and returns the page of
64 * results - may be in html/xml/other depending on the output att of the
65 * request
66 */
67 public String process(String xml_in)
68 {
69
70 Document message_doc = this.converter.getDOM(xml_in);
71 if (message_doc == null)
72 {
73 logger.error("Couldn't parse request");
74 logger.error(xml_in);
75 return null;
76 }
77 Node result = process(message_doc);
78 return this.converter.getString(result);
79 }
80
81 /** the main process method - must be implemented in subclass */
82 abstract public Node process(Node xml_in);
83
84 /**
85 * tell the param class what its arguments are if an action has its own
86 * arguments, this should add them to the params object - particularly
87 * important for args that should not be saved
88 */
89 public boolean addActionParameters(GSParams params)
90 {
91 return true;
92 }
93
94 protected void getRequiredMetadataNames(Element format, HashSet<String> meta_names)
95 {
96 extractMetadataNames(format, meta_names);
97 addLinkMetadataNames(format, meta_names);
98 }
99
100 protected void extractMetadataNames(Element format, HashSet<String> meta_names)
101 {
102 //NodeList nodes = format.getElementsByTagNameNS("metadata", "http://www.greenstone.org/configformat");
103 NodeList metadata_nodes = format.getElementsByTagName("gsf:metadata");
104 for (int i = 0; i < metadata_nodes.getLength(); i++)
105 {
106 Element elem = (Element) metadata_nodes.item(i);
107 StringBuffer metadata = new StringBuffer();
108 String pos = elem.getAttribute("pos");
109 String name = elem.getAttribute("name");
110 String select = elem.getAttribute("select");
111 String sep = elem.getAttribute("separator");
112
113 if(pos.equals("offset")) { // offset when requested to use mdoffset
114 metadata.append("offset");
115 metadata.append(GSConstants.META_RELATION_SEP);
116 }
117 else if (!pos.equals(""))
118 {
119 metadata.append("pos" + pos); // first, last or indexing number
120 metadata.append(GSConstants.META_RELATION_SEP);
121 }
122
123 if (!select.equals(""))
124 {
125 metadata.append(select);
126 metadata.append(GSConstants.META_RELATION_SEP);
127 }
128 if (!sep.equals(""))
129 {
130 metadata.append(GSConstants.META_SEPARATOR_SEP);
131 metadata.append(sep);
132 metadata.append(GSConstants.META_SEPARATOR_SEP);
133 metadata.append(GSConstants.META_RELATION_SEP);
134 }
135
136 metadata.append(name);
137 meta_names.add(metadata.toString());
138 }
139 }
140
141 protected void addLinkMetadataNames(Element format, HashSet<String> meta_names)
142 {
143 // The XSL tranform for
144 // gsf:link type="source"
145 // makes use of 'assocfilepath' so need to make sure it's asked for
146
147 boolean getEquivLinkMeta = false;
148
149 NodeList link_nodes = format.getElementsByTagName("gsf:link");
150 for (int i = 0; i < link_nodes.getLength(); i++)
151 {
152 Element elem = (Element) link_nodes.item(i);
153 String type = elem.getAttribute("type");
154 if (type.equals("source"))
155 {
156 meta_names.add("assocfilepath");
157 meta_names.add("srclinkFile");
158 }
159 else if (type.equals("web")) {
160 meta_names.add("weblink");
161 meta_names.add("webicon");
162 meta_names.add("/weblink");
163 }
164 else if (type.equals("equivdoc")) {
165 getEquivLinkMeta = true;
166 }
167 }
168
169 // get all the metadata necessary for when the user has used "gsf:equivlink"
170 // so that we can build up the equivlink from the metadata components it needs
171 link_nodes = format.getElementsByTagName("gsf:equivlinkgs3");
172 if (getEquivLinkMeta || link_nodes.getLength() > 0)
173 {
174 String[] equivlink_metanames = { "equivDocIcon", "equivDocLink", "/equivDocLink" };
175
176 for (int i = 0; i < equivlink_metanames.length; i++)
177 {
178 StringBuffer metadata = new StringBuffer();
179 metadata.append("all"); // this means the attr multiple = true;
180 metadata.append(GSConstants.META_RELATION_SEP);
181
182 metadata.append(GSConstants.META_SEPARATOR_SEP);
183 metadata.append(','); // attr separator = ","
184 metadata.append(GSConstants.META_SEPARATOR_SEP);
185 metadata.append(GSConstants.META_RELATION_SEP);
186
187 // the name of the metadata we're retrieving
188 metadata.append(equivlink_metanames[i]);
189 meta_names.add(metadata.toString());
190 }
191 }
192
193 if (format.getElementsByTagName("gsf:image").getLength() > 0)
194 {
195 meta_names.add("Thumb");
196 meta_names.add("Screen");
197 meta_names.add("SourceFile");
198 }
199 }
200
201 protected Element createMetadataParamList(HashSet<String> metadata_names)
202 {
203 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
204
205 Element param = null;
206 Iterator<String> i = metadata_names.iterator();
207 while (i.hasNext())
208 {
209 String name = i.next();
210 param = this.doc.createElement(GSXML.PARAM_ELEM);
211 param_list.appendChild(param);
212 param.setAttribute(GSXML.NAME_ATT, "metadata");
213 param.setAttribute(GSXML.VALUE_ATT, name);
214
215 }
216 return param_list;
217 }
218
219 protected boolean processErrorElements(Element message, Element page)
220 {
221 NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
222 if (error_nodes.getLength() == 0)
223 {
224 return false;
225 }
226 Document owner = page.getOwnerDocument();
227 for (int i = 0; i < error_nodes.getLength(); i++)
228 {
229 page.appendChild(owner.importNode(error_nodes.item(i), true));
230 }
231 return true;
232 }
233
234 /**
235 * Takes an XML element and adds the metadata of the current site to it.
236 * Useful for adding the current site's metadata to a response before
237 * sending it
238 *
239 * @param element
240 * the element to add site metadata to
241 * @param lang
242 * the current language
243 * @param uid
244 * the current user id
245 */
246 protected void addSiteMetadata(Element element, UserContext userContext)
247 {
248 //ADD SITE METADATA
249 Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
250 //create a hashmap of params
251 HashMap subset_params = new HashMap(1);
252 subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
253 //create the element to put the params in
254 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
255 //put them in
256 GSXML.addParametersToList(this.doc, param_list, subset_params);
257 metadata_request.appendChild(param_list);
258 //create the message
259 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
260 metadata_message.appendChild(metadata_request);
261 //get response
262 Element metadata_response_message = (Element) this.mr.process(metadata_message);
263 //drill down to response
264 Element metadata_response = (Element) GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
265 //merge in metadata
266 GSXML.mergeMetadataLists(element, metadata_response);
267 }
268
269 protected void addInterfaceOptions(Element elem)
270 {
271 Element documentOptionList = this.doc.createElement("interfaceOptions");
272 for (Object key : this.config_params.keySet())
273 {
274 Element option = this.doc.createElement("option");
275 option.setAttribute(GSXML.NAME_ATT, (String) key);
276 option.setAttribute(GSXML.VALUE_ATT, this.config_params.get(key).toString());
277 documentOptionList.appendChild(option);
278 }
279 elem.appendChild(elem.getOwnerDocument().importNode(documentOptionList, true));
280 }
281}
Note: See TracBrowser for help on using the repository browser.