source: tags/ant-install-branch-merged-1/gsdl3/src/java/org/greenstone/gsdl3/action/Action.java@ 9873

Last change on this file since 9873 was 9873, checked in by (none), 19 years ago

This commit was manufactured by cvs2svn to create tag
'ant-install-branch-merged-1'.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 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
18/** base class for Actions */
19abstract public class Action {
20
21 /** the system set up variables */
22 protected HashMap config_params = null;
23 /** container Document to create XML Nodes */
24 protected Document doc=null;
25 /** a converter class to parse XML and create Docs */
26 protected XMLConverter converter=null;
27 /** a reference to the message router that it must talk to to
28 * get info. it may be a communicator acting as a proxy, but it
29 doesn't care about that */
30 protected ModuleInterface mr=null;
31
32 public Action() {
33 this.converter = new XMLConverter();
34 this.doc = this.converter.newDOM();
35 }
36 /** the config variables must be set before configure is called */
37 public void setConfigParams(HashMap params) {
38 this.config_params = params;
39 }
40 /** sets the message router */
41 public void setMessageRouter(ModuleInterface m) {
42 this.mr = m;
43 }
44 public boolean configure() {
45 // does nothing yet
46 return true;
47 }
48
49 /** process takes an xml representation of cgi args
50 * and returns the page of results - may be in html/xml/other
51 * depending on the output att of the request */
52 public String process(String xml_in) {
53
54 Document message_doc = this.converter.getDOM(xml_in);
55 if (message_doc == null) {
56 System.err.println("Action.process(String) Error: Couldn't parse request");
57 System.err.println(xml_in);
58 return null;
59 }
60 Element result = process(message_doc.getDocumentElement());
61 return this.converter.getString(result);
62 }
63
64 /** the main process method - must be implemented in subclass */
65 abstract public Element process(Element xml_in);
66
67 /** tell the param class what its arguments are
68 * if an action has its own arguments, this should add them to the params
69 * object - particularly important for args that should not be saved */
70 public boolean getActionParameters(GSParams params) {
71 return true;
72 }
73
74 protected void extractMetadataNames(Element format, HashSet meta_names) {
75 //NodeList nodes = format.getElementsByTagNameNS("metadata", "http://www.greenstone.org/configformat");
76 NodeList nodes = format.getElementsByTagName("gsf:metadata");
77 for (int i=0; i<nodes.getLength(); i++) {
78 Element elem = (Element)nodes.item(i);
79 StringBuffer metadata = new StringBuffer();
80 String all = elem.getAttribute("multiple");
81 String name = elem.getAttribute("name");
82 String select = elem.getAttribute("select");
83 String sep = elem.getAttribute("separator");
84 if (all.equals("true")) {
85 metadata.append("all");
86 metadata.append(GSConstants.META_RELATION_SEP);
87 }
88 if (!select.equals("")) {
89 metadata.append(select);
90 metadata.append(GSConstants.META_RELATION_SEP);
91 }
92 if (!sep.equals("")) {
93 metadata.append(GSConstants.META_SEPARATOR_SEP);
94 metadata.append(sep);
95 metadata.append(GSConstants.META_SEPARATOR_SEP);
96 metadata.append(GSConstants.META_RELATION_SEP);
97 }
98
99 metadata.append(name);
100 meta_names.add(metadata.toString());
101 }
102
103 }
104
105 protected Element createMetadataParamList(HashSet metadata_names) {
106 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
107
108 Element param = null;
109 Iterator i = metadata_names.iterator();
110 while (i.hasNext()) {
111 String name = (String)i.next();
112 param = this.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 NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
123 if (error_nodes.getLength()==0) {
124 return false;
125 }
126 Document owner = page.getOwnerDocument();
127 for (int i=0; i<error_nodes.getLength(); i++) {
128 page.appendChild(owner.importNode(error_nodes.item(i), true));
129 }
130 return true;
131 }
132}
133
134
135
136
Note: See TracBrowser for help on using the repository browser.