1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 |
|
---|
6 | // XML classes
|
---|
7 | import org.w3c.dom.Node;
|
---|
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.HashMap;
|
---|
14 | import java.util.*;
|
---|
15 | import java.io.StringReader;
|
---|
16 | import org.xml.sax.InputSource;
|
---|
17 | import javax.xml.parsers.DocumentBuilder;
|
---|
18 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
19 |
|
---|
20 | import java.io.*;
|
---|
21 | import org.xml.sax.*;
|
---|
22 | import javax.xml.parsers.SAXParserFactory;
|
---|
23 | import javax.xml.parsers.ParserConfigurationException;
|
---|
24 | import javax.xml.parsers.SAXParser;
|
---|
25 |
|
---|
26 | import java.io.PrintWriter;
|
---|
27 | import java.io.StringWriter;
|
---|
28 |
|
---|
29 | import org.apache.log4j.*;
|
---|
30 |
|
---|
31 | public class FormatAction extends Action {
|
---|
32 |
|
---|
33 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.FormatAction.class.getName());
|
---|
34 |
|
---|
35 | String tempVal = "";
|
---|
36 |
|
---|
37 | /** process a request */
|
---|
38 | public Node process (Node message_node) {
|
---|
39 |
|
---|
40 | Element message = this.converter.nodeToElement(message_node);
|
---|
41 |
|
---|
42 | // assume only one request
|
---|
43 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
44 |
|
---|
45 | String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
46 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
47 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
48 | // get the param list
|
---|
49 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
50 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
51 |
|
---|
52 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
53 |
|
---|
54 | String coll = (String)params.get(GSParams.COLLECTION); //SYSTEM_CLUSTER);
|
---|
55 |
|
---|
56 | String to = "";
|
---|
57 | if (coll!=null && !coll.equals("")) {
|
---|
58 | to = coll;
|
---|
59 | }
|
---|
60 |
|
---|
61 | Element mr_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
62 | Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT_STRING, to, lang, uid);
|
---|
63 | mr_request_message.appendChild(mr_request);
|
---|
64 |
|
---|
65 | Element format = this.doc.createElement(GSXML.FORMAT_STRING_ELEM);
|
---|
66 | mr_request.appendChild(format);
|
---|
67 |
|
---|
68 | String format_string = (String)params.get("data");
|
---|
69 |
|
---|
70 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
71 |
|
---|
72 | Iterator it = params.keySet().iterator();
|
---|
73 | while(it.hasNext())
|
---|
74 | {
|
---|
75 | logger.error("Param: "+it.next());
|
---|
76 | }
|
---|
77 |
|
---|
78 | try {
|
---|
79 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
80 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
81 | //String input = "<html><head><title></title></head><body>" + format_string + "</body></html>";
|
---|
82 | String input = format_string;
|
---|
83 | InputSource is = new InputSource( new StringReader( input ) );
|
---|
84 | Document d = builder.parse( is );
|
---|
85 | Element e = d.getDocumentElement();
|
---|
86 |
|
---|
87 | page_response.appendChild(this.doc.importNode(e, true));
|
---|
88 | } catch( Exception ex ) {
|
---|
89 | logger.error("There was an exception "+ex);
|
---|
90 |
|
---|
91 | StringWriter sw = new StringWriter();
|
---|
92 | PrintWriter pw = new PrintWriter(sw, true);
|
---|
93 | ex.printStackTrace(pw);
|
---|
94 | pw.flush();
|
---|
95 | sw.flush();
|
---|
96 | logger.error(sw.toString());
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | // Call XSLT to transform document to xml format string
|
---|
101 | this.transformer = new XMLTransformer();
|
---|
102 |
|
---|
103 | // not sure what to do here - some code from Transforming Receptionist
|
---|
104 |
|
---|
105 | // create a mesage to send to the collection object via the message router
|
---|
106 |
|
---|
107 | Node response_message = this.mr.process(mr_request_message);
|
---|
108 |
|
---|
109 | result.appendChild(GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM), GSXML.RESPONSE_ELEM, true));
|
---|
110 | return result;
|
---|
111 |
|
---|
112 | }
|
---|
113 |
|
---|
114 | }
|
---|