[3340] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
| 3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
| 4 | import org.greenstone.gsdl3.util.*;
|
---|
| 5 | // XML classes
|
---|
| 6 | import org.w3c.dom.Node;
|
---|
[4257] | 7 | import org.w3c.dom.NodeList;
|
---|
[3363] | 8 | import org.w3c.dom.Element;
|
---|
[3340] | 9 | import org.w3c.dom.Document;
|
---|
| 10 |
|
---|
[3935] | 11 | // other java stuff
|
---|
| 12 | import java.io.File;
|
---|
[4257] | 13 | import java.util.Vector;
|
---|
[4695] | 14 | import java.util.HashMap;
|
---|
[4856] | 15 | import java.util.HashSet;
|
---|
| 16 | import java.util.Iterator;
|
---|
[4695] | 17 |
|
---|
[13124] | 18 | import org.apache.log4j.*;
|
---|
| 19 |
|
---|
[3568] | 20 | /** base class for Actions */
|
---|
[3363] | 21 | abstract public class Action {
|
---|
[3340] | 22 |
|
---|
[4695] | 23 | /** the system set up variables */
|
---|
| 24 | protected HashMap config_params = null;
|
---|
[3340] | 25 | /** container Document to create XML Nodes */
|
---|
[5155] | 26 | protected Document doc=null;
|
---|
[3340] | 27 | /** a converter class to parse XML and create Docs */
|
---|
[5155] | 28 | protected XMLConverter converter=null;
|
---|
[3340] | 29 | /** a reference to the message router that it must talk to to
|
---|
| 30 | * get info. it may be a communicator acting as a proxy, but it
|
---|
| 31 | doesn't care about that */
|
---|
[5155] | 32 | protected ModuleInterface mr=null;
|
---|
[3340] | 33 |
|
---|
[13270] | 34 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
|
---|
[13124] | 35 |
|
---|
[3340] | 36 | public Action() {
|
---|
[20402] | 37 | this.converter = new XMLConverter();
|
---|
| 38 | this.doc = this.converter.newDOM();
|
---|
[3340] | 39 | }
|
---|
[20402] | 40 |
|
---|
[4695] | 41 | /** the config variables must be set before configure is called */
|
---|
| 42 | public void setConfigParams(HashMap params) {
|
---|
[20402] | 43 | this.config_params = params;
|
---|
[3340] | 44 | }
|
---|
| 45 | /** sets the message router */
|
---|
| 46 | public void setMessageRouter(ModuleInterface m) {
|
---|
[20402] | 47 | this.mr = m;
|
---|
[3340] | 48 | }
|
---|
[8832] | 49 | public boolean configure() {
|
---|
[20402] | 50 | // does nothing yet
|
---|
| 51 | return true;
|
---|
[3340] | 52 | }
|
---|
[4257] | 53 |
|
---|
[3340] | 54 | /** process takes an xml representation of cgi args
|
---|
| 55 | * and returns the page of results - may be in html/xml/other
|
---|
| 56 | * depending on the output att of the request */
|
---|
[3363] | 57 | public String process(String xml_in) {
|
---|
| 58 |
|
---|
[20402] | 59 | Document message_doc = this.converter.getDOM(xml_in);
|
---|
| 60 | if (message_doc == null) {
|
---|
| 61 | logger.error("Couldn't parse request");
|
---|
| 62 | logger.error(xml_in);
|
---|
| 63 | return null;
|
---|
| 64 | }
|
---|
| 65 | Node result = process(message_doc);
|
---|
| 66 | return this.converter.getString(result);
|
---|
[3363] | 67 | }
|
---|
[4257] | 68 |
|
---|
[3645] | 69 | /** the main process method - must be implemented in subclass */
|
---|
[16688] | 70 | abstract public Node process(Node xml_in);
|
---|
[3893] | 71 |
|
---|
[4980] | 72 | /** tell the param class what its arguments are
|
---|
| 73 | * if an action has its own arguments, this should add them to the params
|
---|
| 74 | * object - particularly important for args that should not be saved */
|
---|
| 75 | public boolean getActionParameters(GSParams params) {
|
---|
[20402] | 76 | return true;
|
---|
[4980] | 77 | }
|
---|
| 78 |
|
---|
[24087] | 79 | protected void extractMetadataNames(Element format, HashSet meta_names) {
|
---|
[20402] | 80 | //NodeList nodes = format.getElementsByTagNameNS("metadata", "http://www.greenstone.org/configformat");
|
---|
[24087] | 81 | NodeList metadata_nodes = format.getElementsByTagName("gsf:metadata");
|
---|
| 82 | for (int i=0; i<metadata_nodes.getLength(); i++) {
|
---|
| 83 | Element elem = (Element)metadata_nodes.item(i);
|
---|
[20402] | 84 | StringBuffer metadata = new StringBuffer();
|
---|
| 85 | String all = elem.getAttribute("multiple");
|
---|
| 86 | String name = elem.getAttribute("name");
|
---|
| 87 | String select = elem.getAttribute("select");
|
---|
| 88 | String sep = elem.getAttribute("separator");
|
---|
| 89 | if (all.equals("true")) {
|
---|
| 90 | metadata.append("all");
|
---|
| 91 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
| 92 | }
|
---|
| 93 | if (!select.equals("")) {
|
---|
| 94 | metadata.append(select);
|
---|
| 95 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
| 96 | }
|
---|
| 97 | if (!sep.equals("")) {
|
---|
| 98 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
| 99 | metadata.append(sep);
|
---|
| 100 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
| 101 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
| 102 | }
|
---|
[6248] | 103 |
|
---|
[20402] | 104 | metadata.append(name);
|
---|
| 105 | meta_names.add(metadata.toString());
|
---|
| 106 | }
|
---|
[24219] | 107 |
|
---|
| 108 | // The XSL tranform for
|
---|
| 109 | // gsf:link type="source"
|
---|
| 110 | // makes use of 'assocfilepath' so need to make sure it's asked for
|
---|
| 111 |
|
---|
| 112 | NodeList link_nodes = format.getElementsByTagName("gsf:link");
|
---|
| 113 | for (int i=0; i<link_nodes.getLength(); i++) {
|
---|
| 114 | Element elem = (Element)link_nodes.item(i);
|
---|
| 115 | String type = elem.getAttribute("type");
|
---|
| 116 | if (type.equals("source")) {
|
---|
| 117 | meta_names.add("assocfilepath");
|
---|
| 118 | } else if (type.equals("sourcelinkfile")) {
|
---|
| 119 | meta_names.add("assocfilepath");
|
---|
| 120 | //meta_names.add("srclinkFile");
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
[24087] | 123 |
|
---|
| 124 |
|
---|
[24219] | 125 | // get all the metadata necessary for when the user has used "gsf:equivlink"
|
---|
| 126 | // so that we can build up the equivlink from the metadata components it needs
|
---|
| 127 | link_nodes = format.getElementsByTagName("gsf:equivlinkgs3");
|
---|
| 128 | if(link_nodes != null) {
|
---|
[24087] | 129 |
|
---|
[24219] | 130 | String[] equivlink_metanames = {"equivDocIcon", "equivDocLink", "/equivDocLink"};
|
---|
| 131 |
|
---|
| 132 | for(int i = 0; i < equivlink_metanames.length; i++) {
|
---|
| 133 | StringBuffer metadata = new StringBuffer();
|
---|
| 134 | metadata.append("all"); // this means the attr multiple = true;
|
---|
| 135 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
| 136 |
|
---|
| 137 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
| 138 | metadata.append(','); // attr separator = ","
|
---|
| 139 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
| 140 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
| 141 |
|
---|
| 142 | // the name of the metadata we're retrieving
|
---|
| 143 | metadata.append(equivlink_metanames[i]);
|
---|
| 144 | meta_names.add(metadata.toString());
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[4856] | 147 | }
|
---|
| 148 |
|
---|
| 149 | protected Element createMetadataParamList(HashSet metadata_names) {
|
---|
[20402] | 150 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 151 |
|
---|
| 152 | Element param = null;
|
---|
| 153 | Iterator i = metadata_names.iterator();
|
---|
| 154 | while (i.hasNext()) {
|
---|
| 155 | String name = (String)i.next();
|
---|
| 156 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
| 157 | param_list.appendChild(param);
|
---|
| 158 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
| 159 | param.setAttribute(GSXML.VALUE_ATT, name);
|
---|
| 160 |
|
---|
| 161 | }
|
---|
| 162 | return param_list;
|
---|
[4856] | 163 | }
|
---|
| 164 |
|
---|
[9874] | 165 | protected boolean processErrorElements(Element message, Element page) {
|
---|
[20402] | 166 | NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
|
---|
| 167 | if (error_nodes.getLength()==0) {
|
---|
| 168 | return false;
|
---|
| 169 | }
|
---|
| 170 | Document owner = page.getOwnerDocument();
|
---|
| 171 | for (int i=0; i<error_nodes.getLength(); i++) {
|
---|
| 172 | page.appendChild(owner.importNode(error_nodes.item(i), true));
|
---|
| 173 | }
|
---|
| 174 | return true;
|
---|
[9874] | 175 | }
|
---|
[19984] | 176 |
|
---|
[20402] | 177 | /**
|
---|
| 178 | * Takes an XML element and adds the metadata of the current site to it.
|
---|
| 179 | * Useful for adding the current site's metadata to a response before sending it
|
---|
| 180 | *
|
---|
| 181 | * @param element the element to add site metadata to
|
---|
| 182 | * @param lang the current language
|
---|
| 183 | * @param uid the current user id
|
---|
| 184 | */
|
---|
[22134] | 185 | protected void addSiteMetadata( Element element, String lang, String uid ) {
|
---|
[20402] | 186 | //ADD SITE METADATA
|
---|
| 187 | Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid);
|
---|
| 188 | //create a hashmap of params
|
---|
| 189 | HashMap subset_params = new HashMap(1);
|
---|
| 190 | subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 191 | //create the element to put the params in
|
---|
| 192 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 193 | //put them in
|
---|
| 194 | GSXML.addParametersToList( this.doc, param_list, subset_params );
|
---|
| 195 | metadata_request.appendChild(param_list);
|
---|
| 196 | //create the message
|
---|
| 197 | Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 198 | metadata_message.appendChild(metadata_request);
|
---|
| 199 | //get response
|
---|
| 200 | Element metadata_response_message = (Element)this.mr.process(metadata_message);
|
---|
| 201 | //drill down to response
|
---|
| 202 | Element metadata_response = (Element)GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
|
---|
| 203 | //merge in metadata
|
---|
| 204 | GSXML.mergeMetadataLists(element,metadata_response);
|
---|
| 205 | }
|
---|
[19984] | 206 |
|
---|
[3340] | 207 | }
|
---|
| 208 |
|
---|
[3935] | 209 |
|
---|
| 210 |
|
---|
| 211 |
|
---|