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