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 subaction = (String)params.get(GSParams.SUBACTION);
|
---|
58 | String service = (String)params.get(GSParams.SERVICE);
|
---|
59 | String classifier = (String)params.get("cl");
|
---|
60 |
|
---|
61 |
|
---|
62 | logger.error("Collection="+coll);
|
---|
63 | logger.error("Subaction="+subaction);
|
---|
64 | logger.error("Service="+service);
|
---|
65 | logger.error("Classifier="+classifier);
|
---|
66 |
|
---|
67 |
|
---|
68 | String to = "";
|
---|
69 | if (coll!=null && !coll.equals("")) {
|
---|
70 | to = coll;
|
---|
71 | }
|
---|
72 |
|
---|
73 | Element mr_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
74 | Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT_STRING, to, lang, uid);
|
---|
75 |
|
---|
76 | mr_request.setAttribute("service", service);
|
---|
77 | mr_request.setAttribute("subaction", subaction);
|
---|
78 | //if(classifier != null)
|
---|
79 | mr_request.setAttribute("classifier", classifier);
|
---|
80 |
|
---|
81 | mr_request_message.appendChild(mr_request);
|
---|
82 |
|
---|
83 | //Element format = this.doc.createElement(GSXML.FORMAT_STRING_ELEM);
|
---|
84 | //mr_request.appendChild(format);
|
---|
85 |
|
---|
86 | String format_string = (String)params.get("data");
|
---|
87 | //logger.error("Original format string");
|
---|
88 | //logger.error(format_string);
|
---|
89 |
|
---|
90 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
91 |
|
---|
92 | Iterator it = params.keySet().iterator();
|
---|
93 | //while(it.hasNext())
|
---|
94 | //{
|
---|
95 | // logger.error("Param: "+it.next());
|
---|
96 | //}
|
---|
97 |
|
---|
98 | try {
|
---|
99 | //DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
100 | //DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
101 | //String input = "<html><head><title></title></head><body>" + format_string + "</body></html>";
|
---|
102 | //String input = format_string;
|
---|
103 | //InputSource is = new InputSource( new StringReader( input ) );
|
---|
104 | //Document d = (Document) builder.parse( is );
|
---|
105 | Document d = this.converter.getDOM(format_string); //(Document) builder.parse( is );
|
---|
106 | //Node n1 = d.getFirstChild();
|
---|
107 | //Document d2 = (Document) this.doc.importNode(e, true);
|
---|
108 |
|
---|
109 | //Element format_statement = this.doc.importNode(d, true);
|
---|
110 |
|
---|
111 | // Call XSLT to transform document to xml format string
|
---|
112 | XMLTransformer transformer = new XMLTransformer();
|
---|
113 | // HOW DO I DO THIS PROPERLY?
|
---|
114 | //String style = stylesheetFile(String gsdl3_home, String site_name, String collection, String interface_name, ArrayList base_interfaces, String filename);
|
---|
115 | //Document style_doc = this.converter.getDOM(new File(style), "UTF-8"); //"/home/sam/greenstone3/web/interfaces/oran/transform/formatString.xsl"), "UTF-8"); /*************************/
|
---|
116 | Document style_doc = this.converter.getDOM(new File("/home/sam/greenstone3/web/interfaces/oran/transform/formatString.xsl"), "UTF-8"); /*************************/
|
---|
117 |
|
---|
118 | if(style_doc == null)
|
---|
119 | logger.error("style_doc is null");
|
---|
120 |
|
---|
121 | // not sure what to do here - some code from Transforming Receptionist
|
---|
122 | //String transformed = transformer.transformToString(style_doc, d);
|
---|
123 | logger.error("About to transform");
|
---|
124 | Node transformed = (Node) transformer.transform(style_doc, d); // Failing org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. ; SystemID: file:///home/sam/greenstone3/packages/tomcat/bin/dummy.xsl
|
---|
125 |
|
---|
126 | logger.error("Transform successful?");
|
---|
127 |
|
---|
128 | if(transformed==null) // not null
|
---|
129 | logger.error("TRANSFORMED IS NULL");
|
---|
130 |
|
---|
131 | if(transformed.getNodeType() == Node.DOCUMENT_NODE)
|
---|
132 | transformed = ((Document)transformed).getDocumentElement();
|
---|
133 | logger.error("Node type: "+transformed.getNodeType());
|
---|
134 |
|
---|
135 | //logger.error("begin import");
|
---|
136 | //Node imported = this.doc.importNode(transformed, true); // There was an exception org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation.
|
---|
137 | //logger.error("finished import");
|
---|
138 |
|
---|
139 | //String format_string2 = GSXML.xmlNodeToString(imported); // null pointer exception occuring here
|
---|
140 | //logger.error("format string="+format_string2);
|
---|
141 |
|
---|
142 | Element format = this.doc.createElement(GSXML.FORMAT_STRING_ELEM);
|
---|
143 | //GSXML.setNodeText(format, transformed);
|
---|
144 | format.appendChild(this.doc.importNode(transformed,true));
|
---|
145 | //format.setNodeValue(transformed);
|
---|
146 | mr_request.appendChild(format);
|
---|
147 | logger.error("Transformed: "+transformed);
|
---|
148 |
|
---|
149 |
|
---|
150 | //page_response.appendChild(this.doc.importNode(e, true));
|
---|
151 | } catch( Exception ex ) {
|
---|
152 | logger.error("There was an exception "+ex);
|
---|
153 |
|
---|
154 | StringWriter sw = new StringWriter();
|
---|
155 | PrintWriter pw = new PrintWriter(sw, true);
|
---|
156 | ex.printStackTrace(pw);
|
---|
157 | pw.flush();
|
---|
158 | sw.flush();
|
---|
159 | logger.error(sw.toString());
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | // Call XSLT to transform document to xml format string
|
---|
164 | //XMLTransformer transformer = new XMLTransformer();
|
---|
165 |
|
---|
166 | // not sure what to do here - some code from Transforming Receptionist
|
---|
167 | //transformer.transformToString(Document stylesheet, Document source);
|
---|
168 |
|
---|
169 | // create a mesage to send to the collection object via the message router
|
---|
170 |
|
---|
171 | Node response_message = this.mr.process(mr_request_message);
|
---|
172 |
|
---|
173 | result.appendChild(GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM), GSXML.RESPONSE_ELEM, true));
|
---|
174 | return result;
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | }
|
---|