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;
|
---|
7 | import org.w3c.dom.NodeList;
|
---|
8 | import org.w3c.dom.Element;
|
---|
9 | import org.w3c.dom.Document;
|
---|
10 |
|
---|
11 | // other java stuff
|
---|
12 | import java.io.File;
|
---|
13 | import java.util.Vector;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.Iterator;
|
---|
17 |
|
---|
18 | import org.apache.log4j.*;
|
---|
19 |
|
---|
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 | /** the config variables must be set before configure is called */
|
---|
41 | public void setConfigParams(HashMap params) {
|
---|
42 | this.config_params = params;
|
---|
43 | }
|
---|
44 | /** sets the message router */
|
---|
45 | public void setMessageRouter(ModuleInterface m) {
|
---|
46 | this.mr = m;
|
---|
47 | }
|
---|
48 | public boolean configure() {
|
---|
49 | // does nothing yet
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** process takes an xml representation of cgi args
|
---|
54 | * and returns the page of results - may be in html/xml/other
|
---|
55 | * depending on the output att of the request */
|
---|
56 | public String process(String xml_in) {
|
---|
57 |
|
---|
58 | Document message_doc = this.converter.getDOM(xml_in);
|
---|
59 | if (message_doc == null) {
|
---|
60 | logger.error("Couldn't parse request");
|
---|
61 | logger.error(xml_in);
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 | Node result = process(message_doc);
|
---|
65 | return this.converter.getString(result);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** the main process method - must be implemented in subclass */
|
---|
69 | abstract public Node process(Node xml_in);
|
---|
70 |
|
---|
71 | /** tell the param class what its arguments are
|
---|
72 | * if an action has its own arguments, this should add them to the params
|
---|
73 | * object - particularly important for args that should not be saved */
|
---|
74 | public boolean getActionParameters(GSParams params) {
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected void extractMetadataNames(Element format, HashSet meta_names) {
|
---|
79 | //NodeList nodes = format.getElementsByTagNameNS("metadata", "http://www.greenstone.org/configformat");
|
---|
80 | NodeList nodes = format.getElementsByTagName("gsf:metadata");
|
---|
81 | for (int i=0; i<nodes.getLength(); i++) {
|
---|
82 | Element elem = (Element)nodes.item(i);
|
---|
83 | StringBuffer metadata = new StringBuffer();
|
---|
84 | String all = elem.getAttribute("multiple");
|
---|
85 | String name = elem.getAttribute("name");
|
---|
86 | String select = elem.getAttribute("select");
|
---|
87 | String sep = elem.getAttribute("separator");
|
---|
88 | if (all.equals("true")) {
|
---|
89 | metadata.append("all");
|
---|
90 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
91 | }
|
---|
92 | if (!select.equals("")) {
|
---|
93 | metadata.append(select);
|
---|
94 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
95 | }
|
---|
96 | if (!sep.equals("")) {
|
---|
97 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
98 | metadata.append(sep);
|
---|
99 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
100 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
101 | }
|
---|
102 |
|
---|
103 | metadata.append(name);
|
---|
104 | meta_names.add(metadata.toString());
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected Element createMetadataParamList(HashSet metadata_names) {
|
---|
110 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
111 |
|
---|
112 | Element param = null;
|
---|
113 | Iterator i = metadata_names.iterator();
|
---|
114 | while (i.hasNext()) {
|
---|
115 | String name = (String)i.next();
|
---|
116 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
117 | param_list.appendChild(param);
|
---|
118 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
119 | param.setAttribute(GSXML.VALUE_ATT, name);
|
---|
120 |
|
---|
121 | }
|
---|
122 | return param_list;
|
---|
123 | }
|
---|
124 |
|
---|
125 | protected boolean processErrorElements(Element message, Element page) {
|
---|
126 | NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
|
---|
127 | if (error_nodes.getLength()==0) {
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 | Document owner = page.getOwnerDocument();
|
---|
131 | for (int i=0; i<error_nodes.getLength(); i++) {
|
---|
132 | page.appendChild(owner.importNode(error_nodes.item(i), true));
|
---|
133 | }
|
---|
134 | return true;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|