[23360] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
| 3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
| 4 | import org.greenstone.gsdl3.util.*;
|
---|
[23405] | 5 | import org.greenstone.util.GlobalProperties;
|
---|
[23360] | 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 |
|
---|
[23361] | 34 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.FormatAction.class.getName());
|
---|
| 35 | XMLTransformer transformer = null;
|
---|
[23360] | 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);
|
---|
[23405] | 57 | String service = (String)params.get(GSParams.SERVICE);
|
---|
| 58 | String classifier = (String)params.get("cl");
|
---|
[23360] | 59 |
|
---|
[23405] | 60 |
|
---|
| 61 | logger.error("Collection="+coll);
|
---|
| 62 | logger.error("Service="+service);
|
---|
| 63 | logger.error("Classifier="+classifier);
|
---|
| 64 |
|
---|
| 65 |
|
---|
[23360] | 66 | String to = "";
|
---|
| 67 | if (coll!=null && !coll.equals("")) {
|
---|
| 68 | to = coll;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | Element mr_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 72 | Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT_STRING, to, lang, uid);
|
---|
[23405] | 73 |
|
---|
| 74 | request.setAttribute("service", service);
|
---|
| 75 | if(classifier != null)
|
---|
| 76 | request.setAttribute("classifier", classifier);
|
---|
| 77 |
|
---|
[23360] | 78 | mr_request_message.appendChild(mr_request);
|
---|
| 79 |
|
---|
[23405] | 80 | //Element format = this.doc.createElement(GSXML.FORMAT_STRING_ELEM);
|
---|
| 81 | //mr_request.appendChild(format);
|
---|
[23360] | 82 |
|
---|
| 83 | String format_string = (String)params.get("data");
|
---|
| 84 |
|
---|
| 85 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 86 |
|
---|
| 87 | Iterator it = params.keySet().iterator();
|
---|
[23405] | 88 | //while(it.hasNext())
|
---|
| 89 | //{
|
---|
| 90 | // logger.error("Param: "+it.next());
|
---|
| 91 | //}
|
---|
[23360] | 92 |
|
---|
| 93 | try {
|
---|
| 94 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
| 95 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
| 96 | //String input = "<html><head><title></title></head><body>" + format_string + "</body></html>";
|
---|
| 97 | String input = format_string;
|
---|
| 98 | InputSource is = new InputSource( new StringReader( input ) );
|
---|
[23405] | 99 | Document d = (Document) builder.parse( is );
|
---|
| 100 | //Element e = d.getDocumentElement();
|
---|
| 101 | //Node d2 = this.doc.importNode(e,true);
|
---|
| 102 |
|
---|
| 103 | //Document format_doc = this.converter.newDOM();
|
---|
| 104 | //doc.appendChild(d2);
|
---|
| 105 |
|
---|
| 106 | //File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.INTERFACE_NAME))));
|
---|
| 107 | //if (!interface_config_file.exists()) {
|
---|
| 108 | // logger.error(" interface config file: "+interface_config_file.getPath()+" not found!");
|
---|
| 109 | //}
|
---|
| 110 | //Document config_doc = this.converter.getDOM(interface_config_file, "utf-8");
|
---|
| 111 | //if (config_doc == null) {
|
---|
| 112 | // logger.error(" could not parse interface config file: "+interface_config_file.getPath());
|
---|
| 113 | //}
|
---|
| 114 |
|
---|
| 115 | //Element config_elem = config_doc.getDocumentElement();
|
---|
| 116 | //String base_interface = config_elem.getAttribute("baseInterface");
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | // Call XSLT to transform document to xml format string
|
---|
| 120 | XMLTransformer transformer = new XMLTransformer();
|
---|
| 121 | //String stylesheet = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.SITE_NAME), coll, (String)this.config_params.get(GSConstants.INTERFACE_NAME), "oran", "FormatAction");
|
---|
| 122 | //logger.error(stylesheet);
|
---|
| 123 | Document style_doc = this.converter.getDOM(new File("/home/sam/greenstone3/web/interfaces/oran/transform/formatString.xsl"), "UTF-8");
|
---|
| 124 |
|
---|
| 125 | if(style_doc == null)
|
---|
| 126 | logger.error("style_doc is null");
|
---|
| 127 |
|
---|
| 128 | // not sure what to do here - some code from Transforming Receptionist
|
---|
| 129 | String transformed = transformer.transformToString(style_doc, d);
|
---|
| 130 | //Node transformed = transformer.transform(style_doc, d);
|
---|
| 131 |
|
---|
| 132 | Element format = this.doc.createElement(GSXML.FORMAT_STRING_ELEM);
|
---|
| 133 | GSXML.setNodeText(format, transformed);
|
---|
| 134 | //format.appendChild(this.doc.createTextNode(transformed));
|
---|
| 135 | //format.setNodeValue(transformed);
|
---|
| 136 | mr_request.appendChild(format);
|
---|
| 137 | logger.error("Transformed: "+transformed);
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | //page_response.appendChild(this.doc.importNode(e, true));
|
---|
[23360] | 141 | } catch( Exception ex ) {
|
---|
| 142 | logger.error("There was an exception "+ex);
|
---|
| 143 |
|
---|
| 144 | StringWriter sw = new StringWriter();
|
---|
| 145 | PrintWriter pw = new PrintWriter(sw, true);
|
---|
| 146 | ex.printStackTrace(pw);
|
---|
| 147 | pw.flush();
|
---|
| 148 | sw.flush();
|
---|
| 149 | logger.error(sw.toString());
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | // Call XSLT to transform document to xml format string
|
---|
[23405] | 154 | //XMLTransformer transformer = new XMLTransformer();
|
---|
[23360] | 155 |
|
---|
| 156 | // not sure what to do here - some code from Transforming Receptionist
|
---|
[23405] | 157 | //transformer.transformToString(Document stylesheet, Document source);
|
---|
[23360] | 158 |
|
---|
| 159 | // create a mesage to send to the collection object via the message router
|
---|
| 160 |
|
---|
| 161 | Node response_message = this.mr.process(mr_request_message);
|
---|
| 162 |
|
---|
| 163 | result.appendChild(GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM), GSXML.RESPONSE_ELEM, true));
|
---|
| 164 | return result;
|
---|
| 165 |
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | }
|
---|