1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | import org.greenstone.util.GlobalProperties;
|
---|
6 |
|
---|
7 | // XML classes
|
---|
8 | import org.w3c.dom.Node;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 | import org.w3c.dom.Document;
|
---|
11 |
|
---|
12 | // other java stuff
|
---|
13 | import java.io.File;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.*;
|
---|
16 | import java.io.StringReader;
|
---|
17 | import org.xml.sax.InputSource;
|
---|
18 | import javax.xml.parsers.DocumentBuilder;
|
---|
19 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
20 |
|
---|
21 | import java.io.*;
|
---|
22 | import org.xml.sax.*;
|
---|
23 | import javax.xml.parsers.SAXParserFactory;
|
---|
24 | import javax.xml.parsers.ParserConfigurationException;
|
---|
25 | import javax.xml.parsers.SAXParser;
|
---|
26 |
|
---|
27 | import java.io.PrintWriter;
|
---|
28 | import java.io.StringWriter;
|
---|
29 |
|
---|
30 | import org.apache.log4j.*;
|
---|
31 |
|
---|
32 | public class FormatAction extends Action {
|
---|
33 |
|
---|
34 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.FormatAction.class.getName());
|
---|
35 | XMLTransformer transformer = null;
|
---|
36 |
|
---|
37 | String tempVal = "";
|
---|
38 |
|
---|
39 | /** process a request */
|
---|
40 | public Node process (Node message_node) {
|
---|
41 |
|
---|
42 | Element message = this.converter.nodeToElement(message_node);
|
---|
43 |
|
---|
44 | // assume only one request
|
---|
45 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
46 |
|
---|
47 | String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
48 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
49 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
50 | // get the param list
|
---|
51 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
52 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
53 |
|
---|
54 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
55 |
|
---|
56 | String coll = (String)params.get(GSParams.COLLECTION); //SYSTEM_CLUSTER);
|
---|
57 | String service = (String)params.get(GSParams.SERVICE);
|
---|
58 | String classifier = (String)params.get("cl");
|
---|
59 |
|
---|
60 | String to = "";
|
---|
61 | if (coll!=null && !coll.equals("")) {
|
---|
62 | to = coll;
|
---|
63 | }
|
---|
64 |
|
---|
65 | Element mr_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
66 | Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT_STRING, to, lang, uid);
|
---|
67 |
|
---|
68 | mr_request.setAttribute("service", service);
|
---|
69 | mr_request.setAttribute("subaction", subaction);
|
---|
70 | //if(classifier != null)
|
---|
71 | mr_request.setAttribute("classifier", classifier);
|
---|
72 |
|
---|
73 | mr_request_message.appendChild(mr_request);
|
---|
74 |
|
---|
75 | String format_string = (String)params.get("data");
|
---|
76 |
|
---|
77 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
78 |
|
---|
79 | Iterator it = params.keySet().iterator();
|
---|
80 |
|
---|
81 | if(subaction.equals("saveDocument"))
|
---|
82 | {
|
---|
83 | Element format = this.doc.createElement(GSXML.FORMAT_STRING_ELEM);
|
---|
84 | try{
|
---|
85 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
86 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
87 | InputSource is = new InputSource( new StringReader( format_string ) );
|
---|
88 | Document d = (Document) builder.parse( is );
|
---|
89 | Node n1 = d.getFirstChild();
|
---|
90 |
|
---|
91 | Element format_statement = (Element) this.doc.importNode(n1, true);
|
---|
92 | format.appendChild(format_statement);
|
---|
93 | mr_request.appendChild(format);
|
---|
94 | } catch( Exception ex ) {
|
---|
95 | logger.error("There was an exception "+ex);
|
---|
96 |
|
---|
97 | StringWriter sw = new StringWriter();
|
---|
98 | PrintWriter pw = new PrintWriter(sw, true);
|
---|
99 | ex.printStackTrace(pw);
|
---|
100 | pw.flush();
|
---|
101 | sw.flush();
|
---|
102 | logger.error(sw.toString());
|
---|
103 | }
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | else
|
---|
108 | {
|
---|
109 |
|
---|
110 | try {
|
---|
111 | Document d = this.converter.getDOM(format_string);
|
---|
112 |
|
---|
113 | // Call XSLT to transform document to xml format string
|
---|
114 | XMLTransformer transformer = new XMLTransformer();
|
---|
115 | String style = GSFile.interfaceStylesheetFile(GlobalProperties.getGSDL3Home(),(String)this.config_params.get(GSConstants.INTERFACE_NAME), "formatString.xsl");
|
---|
116 | //logger.error("Style doc is "+style+", compared to /research/sjb48/greenstone3/web/interfaces/oran/transform/formatString.xsl");
|
---|
117 | Document style_doc = this.converter.getDOM(new File(style), "UTF-8");
|
---|
118 | //Document style_doc = this.converter.getDOM(new File("/research/sjb48/greenstone3/web/interfaces/oran/transform/formatString.xsl"), "UTF-8"); /*************************/
|
---|
119 |
|
---|
120 | if(style_doc == null)
|
---|
121 | logger.error("style_doc is null");
|
---|
122 |
|
---|
123 | logger.error("About to transform");
|
---|
124 | Node transformed = (Node) transformer.transform(style_doc, d);
|
---|
125 |
|
---|
126 | if(transformed.getNodeType() == Node.DOCUMENT_NODE)
|
---|
127 | transformed = ((Document)transformed).getDocumentElement();
|
---|
128 |
|
---|
129 | Element format = this.doc.createElement(GSXML.FORMAT_STRING_ELEM);
|
---|
130 | format.appendChild(this.doc.importNode(transformed,true));
|
---|
131 | mr_request.appendChild(format);
|
---|
132 | logger.error("Transformed: "+transformed);
|
---|
133 |
|
---|
134 | } catch( Exception ex ) {
|
---|
135 | logger.error("There was an exception "+ex);
|
---|
136 |
|
---|
137 | StringWriter sw = new StringWriter();
|
---|
138 | PrintWriter pw = new PrintWriter(sw, true);
|
---|
139 | ex.printStackTrace(pw);
|
---|
140 | pw.flush();
|
---|
141 | sw.flush();
|
---|
142 | logger.error(sw.toString());
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | Node response_message = this.mr.process(mr_request_message);
|
---|
147 |
|
---|
148 | result.appendChild(GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM), GSXML.RESPONSE_ELEM, true));
|
---|
149 | return result;
|
---|
150 |
|
---|
151 | }
|
---|
152 |
|
---|
153 | }
|
---|