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