1 | package org.greenstone.gsdl3.core;
|
---|
2 |
|
---|
3 | import org.greenstone.util.GlobalProperties;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | import org.greenstone.gsdl3.action.*;
|
---|
6 | // XML classes
|
---|
7 | import org.w3c.dom.Node;
|
---|
8 | import org.w3c.dom.NodeList;
|
---|
9 | import org.w3c.dom.Comment;
|
---|
10 | import org.w3c.dom.Text;
|
---|
11 | import org.w3c.dom.Document;
|
---|
12 | import org.w3c.dom.Element;
|
---|
13 | import org.xml.sax.InputSource;
|
---|
14 | import org.w3c.dom.NamedNodeMap;
|
---|
15 |
|
---|
16 | // other java classes
|
---|
17 | import java.io.File;
|
---|
18 | import java.io.StringWriter;
|
---|
19 | import java.io.FileReader;
|
---|
20 | import java.io.FileNotFoundException;
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.Enumeration;
|
---|
23 |
|
---|
24 | import javax.xml.parsers.*;
|
---|
25 | import javax.xml.transform.*;
|
---|
26 | import javax.xml.transform.dom.*;
|
---|
27 | import javax.xml.transform.stream.*;
|
---|
28 | import org.apache.log4j.*;
|
---|
29 | import org.apache.xerces.dom.*;
|
---|
30 | import org.apache.xerces.parsers.DOMParser;
|
---|
31 |
|
---|
32 | import org.apache.commons.lang3.StringUtils;
|
---|
33 |
|
---|
34 | /** A receptionist that uses xslt to transform the page_data before returning it. . Receives requests consisting
|
---|
35 | * of an xml representation of cgi args, and returns the page of data - in
|
---|
36 | * html by default. The requests are processed by the appropriate action class
|
---|
37 | *
|
---|
38 | * @see Action
|
---|
39 | */
|
---|
40 | public class TransformingReceptionist extends Receptionist{
|
---|
41 |
|
---|
42 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.TransformingReceptionist.class.getName());
|
---|
43 |
|
---|
44 | /** The preprocess.xsl file is in a fixed location */
|
---|
45 | static final String preprocess_xsl_filename = GlobalProperties.getGSDL3Home() + File.separatorChar
|
---|
46 | + "ui" + File.separatorChar + "xslt" + File.separatorChar + "preProcess.xsl";
|
---|
47 |
|
---|
48 | /** the list of xslt to use for actions */
|
---|
49 | protected HashMap xslt_map = null;
|
---|
50 |
|
---|
51 | /** a transformer class to transform xml using xslt */
|
---|
52 | protected XMLTransformer transformer=null;
|
---|
53 |
|
---|
54 | protected TransformerFactory transformerFactory=null;
|
---|
55 | protected DOMParser parser = null;
|
---|
56 | public TransformingReceptionist() {
|
---|
57 | super();
|
---|
58 | this.xslt_map = new HashMap();
|
---|
59 | this.transformer = new XMLTransformer();
|
---|
60 | try {
|
---|
61 | transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
|
---|
62 | this.converter = new XMLConverter();
|
---|
63 | //transformerFactory.setURIResolver(new MyUriResolver()) ;
|
---|
64 |
|
---|
65 | parser = new DOMParser();
|
---|
66 | parser.setFeature("http://xml.org/sax/features/validation", false);
|
---|
67 | // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up.
|
---|
68 | parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
---|
69 | // a performance test showed that having this on lead to increased
|
---|
70 | // memory use for small-medium docs, and not much gain for large
|
---|
71 | // docs.
|
---|
72 | // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html
|
---|
73 | parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
|
---|
74 | parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
|
---|
75 | // setting a handler for when fatal errors, errors or warnings happen during xml parsing
|
---|
76 | // call XMLConverter's getParseErrorMessage() to get the errorstring that can be rendered as web page
|
---|
77 | this.parser.setErrorHandler(new XMLConverter.ParseErrorHandler());
|
---|
78 | }
|
---|
79 | catch (Exception e) {
|
---|
80 | e.printStackTrace();
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** configures the receptionist - overwrite this to set up the xslt map*/
|
---|
86 | public boolean configure() {
|
---|
87 |
|
---|
88 | if (this.config_params==null) {
|
---|
89 | logger.error(" config variables must be set before calling configure");
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | if (this.mr==null) {
|
---|
93 | logger.error(" message router must be set before calling configure");
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 |
|
---|
97 | // find the config file containing a list of actions
|
---|
98 | File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.INTERFACE_NAME))));
|
---|
99 | if (!interface_config_file.exists()) {
|
---|
100 | logger.error(" interface config file: "+interface_config_file.getPath()+" not found!");
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 | Document config_doc = this.converter.getDOM(interface_config_file, "utf-8");
|
---|
104 | if (config_doc == null) {
|
---|
105 | logger.error(" could not parse interface config file: "+interface_config_file.getPath());
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 | Element config_elem = config_doc.getDocumentElement();
|
---|
109 | String base_interface = config_elem.getAttribute("baseInterface");
|
---|
110 | setUpBaseInterface(base_interface);
|
---|
111 | setUpInterfaceOptions(config_elem);
|
---|
112 |
|
---|
113 | Element action_list = (Element)GSXML.getChildByTagName(config_elem, GSXML.ACTION_ELEM+GSXML.LIST_MODIFIER);
|
---|
114 | NodeList actions = action_list.getElementsByTagName(GSXML.ACTION_ELEM);
|
---|
115 |
|
---|
116 | for (int i=0; i<actions.getLength(); i++) {
|
---|
117 | Element action = (Element) actions.item(i);
|
---|
118 | String class_name = action.getAttribute("class");
|
---|
119 | String action_name = action.getAttribute("name");
|
---|
120 | Action ac = null;
|
---|
121 | try {
|
---|
122 | ac = (Action)Class.forName("org.greenstone.gsdl3.action."+class_name).newInstance();
|
---|
123 | } catch (Exception e) {
|
---|
124 | logger.error(" couldn't load in action "+class_name);
|
---|
125 | e.printStackTrace();
|
---|
126 | continue;
|
---|
127 | }
|
---|
128 | ac.setConfigParams(this.config_params);
|
---|
129 | ac.setMessageRouter(this.mr);
|
---|
130 | ac.configure();
|
---|
131 | ac.getActionParameters(this.params);
|
---|
132 | this.action_map.put(action_name, ac);
|
---|
133 |
|
---|
134 | // now do the xslt map
|
---|
135 | String xslt = action.getAttribute("xslt");
|
---|
136 | if (!xslt.equals("")) {
|
---|
137 | this.xslt_map.put(action_name, xslt);
|
---|
138 | }
|
---|
139 | NodeList subactions = action.getElementsByTagName(GSXML.SUBACTION_ELEM);
|
---|
140 | for (int j=0; j<subactions.getLength(); j++) {
|
---|
141 | Element subaction = (Element)subactions.item(j);
|
---|
142 | String subname = subaction.getAttribute(GSXML.NAME_ATT);
|
---|
143 | String subxslt = subaction.getAttribute("xslt");
|
---|
144 |
|
---|
145 | String map_key = action_name+":"+subname;
|
---|
146 | logger.debug("adding in to xslt map, "+map_key+"->"+subxslt);
|
---|
147 | this.xslt_map.put(map_key, subxslt);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | Element lang_list = (Element)GSXML.getChildByTagName(config_elem, "languageList");
|
---|
151 | if (lang_list == null) {
|
---|
152 | logger.error(" didn't find a language list in the config file!!");
|
---|
153 | } else {
|
---|
154 | this.language_list = (Element) this.doc.importNode(lang_list, true);
|
---|
155 | }
|
---|
156 |
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | protected Node postProcessPage(Element page) {
|
---|
162 | // might need to add some data to the page
|
---|
163 | addExtraInfo(page);
|
---|
164 | // transform the page using xslt
|
---|
165 | Node transformed_page = transformPage(page);
|
---|
166 |
|
---|
167 | // if the user has specified they want only a part of the full page then subdivide it
|
---|
168 | boolean subdivide = false;
|
---|
169 | String excerptID = null;
|
---|
170 | String excerptTag = null;
|
---|
171 | Element request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
|
---|
172 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
173 | if (cgi_param_list != null) {
|
---|
174 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
175 | if((excerptID = (String)params.get(GSParams.EXCERPT_ID)) != null)
|
---|
176 | {
|
---|
177 | subdivide = true;
|
---|
178 | }
|
---|
179 | if((excerptTag = (String)params.get(GSParams.EXCERPT_TAG)) != null)
|
---|
180 | {
|
---|
181 | subdivide = true;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | if(subdivide)
|
---|
186 | {
|
---|
187 | Node subdivided_page = subdivide(transformed_page, excerptID, excerptTag);
|
---|
188 | if(subdivided_page != null)
|
---|
189 | {
|
---|
190 | return subdivided_page;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | return transformed_page;
|
---|
195 | }
|
---|
196 |
|
---|
197 | protected Node subdivide(Node transformed_page, String excerptID, String excerptTag)
|
---|
198 | {
|
---|
199 | if(excerptID != null)
|
---|
200 | {
|
---|
201 | Node selectedElement = getNodeByIdRecursive(transformed_page, excerptID);
|
---|
202 | modifyNodesByTagRecursive(selectedElement, "a");
|
---|
203 | return selectedElement;
|
---|
204 | }
|
---|
205 | else if(excerptTag != null)
|
---|
206 | {
|
---|
207 | /*
|
---|
208 | // define a list
|
---|
209 |
|
---|
210 | Node selectedElement = modifyNodesByTagRecursive(transformed_page, excerptTag);
|
---|
211 | */
|
---|
212 |
|
---|
213 | Node selectedElement = getNodeByTagRecursive(transformed_page, excerptTag);
|
---|
214 | return selectedElement;
|
---|
215 |
|
---|
216 | }
|
---|
217 | return transformed_page;
|
---|
218 | }
|
---|
219 |
|
---|
220 | protected Node getNodeByIdRecursive(Node parent, String id)
|
---|
221 | {
|
---|
222 | if(parent.hasAttributes() && ((Element)parent).getAttribute("id").equals(id))
|
---|
223 | {
|
---|
224 | return parent;
|
---|
225 | }
|
---|
226 |
|
---|
227 | NodeList children = parent.getChildNodes();
|
---|
228 | for(int i = 0; i < children.getLength(); i++)
|
---|
229 | {
|
---|
230 | Node result = null;
|
---|
231 | if((result = getNodeByIdRecursive(children.item(i), id)) != null)
|
---|
232 | {
|
---|
233 | return result;
|
---|
234 | }
|
---|
235 | }
|
---|
236 | return null;
|
---|
237 | }
|
---|
238 |
|
---|
239 | protected Node getNodeByTagRecursive(Node parent, String tag)
|
---|
240 | {
|
---|
241 | if(parent.getNodeType() == Node.ELEMENT_NODE && ((Element)parent).getTagName().equals(tag))
|
---|
242 | {
|
---|
243 | return parent;
|
---|
244 | }
|
---|
245 |
|
---|
246 | NodeList children = parent.getChildNodes();
|
---|
247 | for(int i = 0; i < children.getLength(); i++)
|
---|
248 | {
|
---|
249 | Node result = null;
|
---|
250 | if((result = getNodeByTagRecursive(children.item(i), tag)) != null)
|
---|
251 | {
|
---|
252 | return result;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | return null;
|
---|
256 | }
|
---|
257 |
|
---|
258 | protected Node modifyNodesByTagRecursive(Node parent, String tag)
|
---|
259 | {
|
---|
260 | if(parent.getNodeType() == Node.ELEMENT_NODE && ((Element)parent).getTagName().equals(tag))
|
---|
261 | {
|
---|
262 | return parent;
|
---|
263 | }
|
---|
264 |
|
---|
265 | NodeList children = parent.getChildNodes();
|
---|
266 | for(int i = 0; i < children.getLength(); i++)
|
---|
267 | {
|
---|
268 | Node result = null;
|
---|
269 | if((result = modifyNodesByTagRecursive(children.item(i), tag)) != null)
|
---|
270 | {
|
---|
271 | //return result;
|
---|
272 | //logger.error("Modify node value = "+result.getNodeValue()); //NamedItem("href"););
|
---|
273 | logger.error("BEFORE Modify node attribute = "+result.getAttributes().getNamedItem("href").getNodeValue());
|
---|
274 | String url = result.getAttributes().getNamedItem("href").getNodeValue();
|
---|
275 | //url = url + "&excerptid=results";
|
---|
276 | result.getAttributes().getNamedItem("href").setNodeValue(url);
|
---|
277 | logger.error("AFTER Modify node attribute = "+result.getAttributes().getNamedItem("href").getNodeValue());
|
---|
278 |
|
---|
279 | }
|
---|
280 | }
|
---|
281 | return null;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /** overwrite this to add any extra info that might be needed in the page before transformation */
|
---|
285 | protected void addExtraInfo(Element page) {}
|
---|
286 |
|
---|
287 | /** transform the page using xslt
|
---|
288 | * we need to get any format element out of the page and add it to the xslt
|
---|
289 | * before transforming */
|
---|
290 | protected Node transformPage(Element page) {
|
---|
291 |
|
---|
292 | boolean allowsClientXSLT = (Boolean)config_params.get(GSConstants.ALLOW_CLIENT_SIDE_XSLT);
|
---|
293 | //System.out.println("Client side transforms allowed? " + allowsClientXSLT);
|
---|
294 |
|
---|
295 | String currentInterface = (String)config_params.get(GSConstants.INTERFACE_NAME);
|
---|
296 |
|
---|
297 | Element request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
|
---|
298 | String output = request.getAttribute(GSXML.OUTPUT_ATT);
|
---|
299 |
|
---|
300 | //System.out.println("Current output mode is: " + output + ", current interface name is: " + currentInterface);
|
---|
301 |
|
---|
302 | if(allowsClientXSLT) {
|
---|
303 | if(!currentInterface.endsWith(GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX) && output.equals("html"))
|
---|
304 | {
|
---|
305 | System.out.println("output is html and we are not currently using a client side version, switching");
|
---|
306 | // Switch the interface
|
---|
307 | config_params.put(GSConstants.INTERFACE_NAME, currentInterface.concat(GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX));
|
---|
308 | }
|
---|
309 | else if((currentInterface.endsWith(GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX) && !output.equals("html")) || output.equals("server"))
|
---|
310 | {
|
---|
311 | // The reverse needs to happen too
|
---|
312 | config_params.put(GSConstants.INTERFACE_NAME, currentInterface.substring(0, currentInterface.length() - GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX.length()));
|
---|
313 | }
|
---|
314 | }
|
---|
315 | else if (currentInterface.endsWith(GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX))
|
---|
316 | {
|
---|
317 | config_params.put(GSConstants.INTERFACE_NAME, currentInterface.substring(0, currentInterface.length() - GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX.length()));
|
---|
318 | }
|
---|
319 |
|
---|
320 | // DocType defaults in case the skin doesn't have an "xsl:output" element
|
---|
321 | String qualifiedName = "html";
|
---|
322 | String publicID = "-//W3C//DTD HTML 4.01 Transitional//EN";
|
---|
323 | String systemID = "http://www.w3.org/TR/html4/loose.dtd";
|
---|
324 |
|
---|
325 | // We need to create an empty document with a predefined DocType,
|
---|
326 | // that will then be used for the transformation by the DOMResult
|
---|
327 | Document docWithDoctype = converter.newDOM(qualifiedName, publicID, systemID);
|
---|
328 |
|
---|
329 | if(output.equals("xsltclient")) {
|
---|
330 | // If you're just getting the client-side transform page, why bother with the rest of this?
|
---|
331 | Element html = docWithDoctype.createElement("html");
|
---|
332 | Element img = docWithDoctype.createElement("img");
|
---|
333 | img.setAttribute("src", "interfaces/default/images/loading.gif"); // Make it dynamic
|
---|
334 | img.setAttribute("alt", "Please wait...");
|
---|
335 | Text title_text = docWithDoctype.createTextNode("Please wait..."); // Make this language dependent
|
---|
336 | Element head = docWithDoctype.createElement("head");
|
---|
337 | Element title = docWithDoctype.createElement("title");
|
---|
338 | title.appendChild(title_text);
|
---|
339 | Element body = docWithDoctype.createElement("body");
|
---|
340 | Element script = docWithDoctype.createElement("script");
|
---|
341 | Element jquery = docWithDoctype.createElement("script");
|
---|
342 | jquery.setAttribute("src", "jquery.js");
|
---|
343 | jquery.setAttribute("type", "text/javascript");
|
---|
344 | Comment jquery_comment = docWithDoctype.createComment("jQuery");
|
---|
345 | Comment script_comment = docWithDoctype.createComment("Filler for browser");
|
---|
346 | script.setAttribute("src", "test.js");
|
---|
347 | script.setAttribute("type", "text/javascript");
|
---|
348 | Element pagevar = docWithDoctype.createElement("script");
|
---|
349 | Element style = docWithDoctype.createElement("style");
|
---|
350 | style.setAttribute("type", "text/css");
|
---|
351 | Text style_text = docWithDoctype.createTextNode("body { text-align: center; padding: 50px; font: 14pt Arial, sans-serif; font-weight: bold; }");
|
---|
352 | pagevar.setAttribute("type", "text/javascript");
|
---|
353 | Text page_var_text = docWithDoctype.createTextNode("var placeholder = true;");
|
---|
354 |
|
---|
355 | html.appendChild(head);
|
---|
356 | head.appendChild(title);
|
---|
357 | head.appendChild(style);
|
---|
358 | style.appendChild(style_text);
|
---|
359 | html.appendChild(body);
|
---|
360 | head.appendChild(pagevar);
|
---|
361 | head.appendChild(jquery);
|
---|
362 | head.appendChild(script);
|
---|
363 | pagevar.appendChild(page_var_text);
|
---|
364 | jquery.appendChild(jquery_comment);
|
---|
365 | script.appendChild(script_comment);
|
---|
366 | body.appendChild(img);
|
---|
367 | docWithDoctype.appendChild(html);
|
---|
368 |
|
---|
369 | return (Node)docWithDoctype;
|
---|
370 | }
|
---|
371 |
|
---|
372 | // Passing in the pretty string here means it needs to be generated even when not debugging; so use custom function to return blank when debug is off
|
---|
373 | logger.debug("page before transforming:");
|
---|
374 | logger.debug(this.converter.getPrettyStringLogger(page, logger));
|
---|
375 |
|
---|
376 | String action = request.getAttribute(GSXML.ACTION_ATT);
|
---|
377 | String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
378 |
|
---|
379 | // we should choose how to transform the data based on output, eg diff
|
---|
380 | // choice for html, and wml??
|
---|
381 | // for now, if output=xml, we don't transform the page, we just return
|
---|
382 | // the page xml
|
---|
383 | Document theXML = null;
|
---|
384 |
|
---|
385 | if (output.equals("xml") || output.equals("clientside")) {
|
---|
386 | // Append some bits and pieces first...
|
---|
387 | theXML = converter.newDOM();
|
---|
388 | // Import into new document first!
|
---|
389 | Node newPage = theXML.importNode(page, true);
|
---|
390 | theXML.appendChild(newPage);
|
---|
391 | Element root = theXML.createElement("xsltparams");
|
---|
392 | newPage.appendChild(root);
|
---|
393 |
|
---|
394 | Element libname = theXML.createElement("param");
|
---|
395 | libname.setAttribute("name", "library_name");
|
---|
396 | Text libnametext = theXML.createTextNode((String)config_params.get(GSConstants.LIBRARY_NAME));
|
---|
397 | libname.appendChild(libnametext);
|
---|
398 |
|
---|
399 | Element intname = theXML.createElement("param");
|
---|
400 | intname.setAttribute("name", "interface_name");
|
---|
401 | Text intnametext = theXML.createTextNode((String)config_params.get(GSConstants.INTERFACE_NAME));
|
---|
402 | intname.appendChild(intnametext);
|
---|
403 |
|
---|
404 | Element filepath = theXML.createElement("param");
|
---|
405 | filepath.setAttribute("name", "filepath");
|
---|
406 | Text filepathtext = theXML.createTextNode(GlobalProperties.getGSDL3Home());
|
---|
407 | filepath.appendChild(filepathtext);
|
---|
408 |
|
---|
409 | root.appendChild(libname);
|
---|
410 | root.appendChild(intname);
|
---|
411 | root.appendChild(filepath);
|
---|
412 |
|
---|
413 | if(output.equals("xml"))
|
---|
414 | return theXML.getDocumentElement();
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
419 | String collection = "";
|
---|
420 | if (cgi_param_list != null) {
|
---|
421 | // Don't waste time getting all the parameters
|
---|
422 | HashMap params = GSXML.extractParams(cgi_param_list, false, GSParams.COLLECTION);
|
---|
423 | collection = (String)params.get(GSParams.COLLECTION);
|
---|
424 | if (collection == null) collection = "";
|
---|
425 | }
|
---|
426 |
|
---|
427 | String xslt_file = getXSLTFileName(action, subaction, collection);
|
---|
428 | if (xslt_file==null) {
|
---|
429 | // returning file not found error page to indicate which file is missing
|
---|
430 | return fileNotFoundErrorPage(xslt_file);
|
---|
431 | }
|
---|
432 |
|
---|
433 | Document style_doc = this.converter.getDOM(new File(xslt_file), "UTF-8");
|
---|
434 | String errorPage = this.converter.getParseErrorMessage();
|
---|
435 | if(errorPage != null) {
|
---|
436 | return XMLTransformer.constructErrorXHTMLPage("Cannot parse the xslt file: " + xslt_file + "\n" + errorPage);
|
---|
437 | }
|
---|
438 | if (style_doc == null) {
|
---|
439 | logger.error(" cant parse the xslt file needed, so returning the original page!");
|
---|
440 | return page;
|
---|
441 | }
|
---|
442 |
|
---|
443 |
|
---|
444 | // put the page into a document - this is necessary for xslt to get
|
---|
445 | // the paths right if you have paths relative to the document root
|
---|
446 | // eg /page.
|
---|
447 | Document doc = this.converter.newDOM();
|
---|
448 | doc.appendChild(doc.importNode(page, true));
|
---|
449 | Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
|
---|
450 | Element format_elem = (Element)GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM);
|
---|
451 | if (output.equals("formatelem")) {
|
---|
452 | return format_elem;
|
---|
453 | }
|
---|
454 | if (format_elem != null) {
|
---|
455 | //page_response.removeChild(format_elem);
|
---|
456 | logger.debug("format elem="+this.converter.getPrettyStringLogger(format_elem, logger));
|
---|
457 | // need to transform the format info
|
---|
458 | String configStylesheet_file = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.SITE_NAME), collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, "config_format.xsl");
|
---|
459 | Document configStylesheet_doc = this.converter.getDOM(new File(configStylesheet_file));
|
---|
460 |
|
---|
461 | if (configStylesheet_doc != null) {
|
---|
462 | Document format_doc = this.converter.newDOM();
|
---|
463 | format_doc.appendChild(format_doc.importNode(format_elem, true));
|
---|
464 | Node result = this.transformer.transform(configStylesheet_doc, format_doc); // Needs addressing <-
|
---|
465 |
|
---|
466 | // Since we started creating documents with DocTypes, we can end up with
|
---|
467 | // Document objects here. But we will be working with an Element instead,
|
---|
468 | // so we grab the DocumentElement() of the Document object in such a case.
|
---|
469 | Element new_format;
|
---|
470 | if(result.getNodeType() == Node.DOCUMENT_NODE) {
|
---|
471 | new_format = ((Document)result).getDocumentElement();
|
---|
472 | } else {
|
---|
473 | new_format = (Element)result;
|
---|
474 | }
|
---|
475 | logger.debug("new format elem="+this.converter.getPrettyStringLogger(new_format, logger));
|
---|
476 | if (output.equals("newformat")) {
|
---|
477 | return new_format;
|
---|
478 | }
|
---|
479 |
|
---|
480 | // add extracted GSF statements in to the main stylesheet
|
---|
481 | GSXSLT.mergeStylesheets(style_doc, new_format);
|
---|
482 | //System.out.println("added extracted GSF statements into the main stylesheet") ;
|
---|
483 |
|
---|
484 | // add extracted GSF statements in to the debug test stylesheet
|
---|
485 | //GSXSLT.mergeStylesheets(oldStyle_doc, new_format);
|
---|
486 | } else {
|
---|
487 | logger.error(" couldn't parse the config_format stylesheet, adding the format info as is");
|
---|
488 | GSXSLT.mergeStylesheets(style_doc, format_elem);
|
---|
489 | //GSXSLT.mergeStylesheets(oldStyle_doc, format_elem);
|
---|
490 | }
|
---|
491 | logger.debug("the converted stylesheet is:");
|
---|
492 | logger.debug(this.converter.getPrettyStringLogger(style_doc.getDocumentElement(), logger));
|
---|
493 | }
|
---|
494 |
|
---|
495 | //for debug purposes only
|
---|
496 | Document oldStyle_doc = style_doc;
|
---|
497 |
|
---|
498 |
|
---|
499 | Document preprocessingXsl;
|
---|
500 | try {
|
---|
501 | preprocessingXsl = getPreprocessDoc();
|
---|
502 | String errMsg = ((XMLConverter.ParseErrorHandler)parser.getErrorHandler()).getErrorMessage();
|
---|
503 | if(errMsg != null) {
|
---|
504 | return XMLTransformer.constructErrorXHTMLPage("error loading preprocess xslt file: "
|
---|
505 | + preprocess_xsl_filename + "\n" + errMsg);
|
---|
506 | }
|
---|
507 | } catch (java.io.FileNotFoundException e) {
|
---|
508 | return fileNotFoundErrorPage(e.getMessage());
|
---|
509 | } catch (Exception e) {
|
---|
510 | e.printStackTrace() ;
|
---|
511 | System.out.println("error loading preprocess xslt") ;
|
---|
512 | return XMLTransformer.constructErrorXHTMLPage("error loading preprocess xslt\n" + e.getMessage());
|
---|
513 | }
|
---|
514 |
|
---|
515 | Document libraryXsl = null;
|
---|
516 | try {
|
---|
517 | libraryXsl = getLibraryDoc() ;
|
---|
518 | String errMsg = ((XMLConverter.ParseErrorHandler)parser.getErrorHandler()).getErrorMessage();
|
---|
519 | if(errMsg != null) {
|
---|
520 | return XMLTransformer.constructErrorXHTMLPage("Error loading xslt file: "
|
---|
521 | + this.getLibraryXSLFilename() + "\n" + errMsg);
|
---|
522 | }
|
---|
523 | } catch (java.io.FileNotFoundException e) {
|
---|
524 | return fileNotFoundErrorPage(e.getMessage());
|
---|
525 | } catch (Exception e) {
|
---|
526 | e.printStackTrace() ;
|
---|
527 | System.out.println("error loading library xslt") ;
|
---|
528 | return XMLTransformer.constructErrorXHTMLPage("error loading library xslt\n" + e.getMessage()) ;
|
---|
529 | }
|
---|
530 |
|
---|
531 | // Combine the skin file and library variables/templates into one document.
|
---|
532 | // Please note: We dont just use xsl:import because the preprocessing stage
|
---|
533 | // needs to know what's available in the library.
|
---|
534 |
|
---|
535 | Document skinAndLibraryXsl = null ;
|
---|
536 | Document skinAndLibraryDoc = converter.newDOM();
|
---|
537 | try {
|
---|
538 |
|
---|
539 | skinAndLibraryXsl = converter.newDOM();
|
---|
540 | Element root = skinAndLibraryXsl.createElement("skinAndLibraryXsl") ;
|
---|
541 | skinAndLibraryXsl.appendChild(root) ;
|
---|
542 |
|
---|
543 | Element s = skinAndLibraryXsl.createElement("skinXsl") ;
|
---|
544 | s.appendChild(skinAndLibraryXsl.importNode(style_doc.getDocumentElement(), true)) ;
|
---|
545 | root.appendChild(s) ;
|
---|
546 |
|
---|
547 | Element l = skinAndLibraryXsl.createElement("libraryXsl") ;
|
---|
548 | Element libraryXsl_el = libraryXsl.getDocumentElement();
|
---|
549 | l.appendChild(skinAndLibraryXsl.importNode(libraryXsl_el, true)) ;
|
---|
550 | root.appendChild(l) ;
|
---|
551 | //System.out.println("Skin and Library XSL are now together") ;
|
---|
552 |
|
---|
553 |
|
---|
554 | //System.out.println("Pre-processing the skin file...") ;
|
---|
555 |
|
---|
556 | //pre-process the skin style sheet
|
---|
557 | //In other words, apply the preProcess.xsl to 'skinAndLibraryXsl' in order to
|
---|
558 | //expand all GS-Lib statements into complete XSL statements and also to create
|
---|
559 | //a valid xsl style sheet document.
|
---|
560 |
|
---|
561 | Transformer preProcessor = transformerFactory.newTransformer(new DOMSource(preprocessingXsl));
|
---|
562 | preProcessor.setErrorListener(new XMLTransformer.TransformErrorListener());
|
---|
563 | DOMResult result = new DOMResult();
|
---|
564 | result.setNode(skinAndLibraryDoc);
|
---|
565 | preProcessor.transform(new DOMSource(skinAndLibraryXsl), result);
|
---|
566 | //System.out.println("GS-Lib statements are now expanded") ;
|
---|
567 |
|
---|
568 | }
|
---|
569 | catch (TransformerException e) {
|
---|
570 | e.printStackTrace() ;
|
---|
571 | System.out.println("TransformerException while preprocessing the skin xslt") ;
|
---|
572 | return XMLTransformer.constructErrorXHTMLPage(e.getMessage()) ;
|
---|
573 | }
|
---|
574 | catch (Exception e) {
|
---|
575 | e.printStackTrace() ;
|
---|
576 | System.out.println("Error while preprocessing the skin xslt") ;
|
---|
577 | return XMLTransformer.constructErrorXHTMLPage(e.getMessage()) ;
|
---|
578 | }
|
---|
579 |
|
---|
580 | //The following code is to be uncommented if we need to append the extracted GSF statements
|
---|
581 | //after having extracted the GSLib elements. In case of a problem during postprocessing.
|
---|
582 | /*
|
---|
583 | // put the page into a document - this is necessary for xslt to get
|
---|
584 | // the paths right if you have paths relative to the document root
|
---|
585 | // eg /page.
|
---|
586 | Document doc = this.converter.newDOM();
|
---|
587 | doc.appendChild(doc.importNode(page, true));
|
---|
588 | Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
|
---|
589 | Element format_elem = (Element)GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM);
|
---|
590 | if (output.equals("formatelem")) {
|
---|
591 | return format_elem;
|
---|
592 | }
|
---|
593 | if (format_elem != null) {
|
---|
594 | //page_response.removeChild(format_elem);
|
---|
595 | logger.debug("format elem="+this.converter.getPrettyString(format_elem));
|
---|
596 | // need to transform the format info
|
---|
597 | String configStylesheet_file = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.SITE_NAME), collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, "config_format.xsl");
|
---|
598 | Document configStylesheet_doc = this.converter.getDOM(new File(configStylesheet_file));
|
---|
599 | if (configStylesheet_doc != null) {
|
---|
600 | Document format_doc = this.converter.newDOM();
|
---|
601 | format_doc.appendChild(format_doc.importNode(format_elem, true));
|
---|
602 | Node result = this.transformer.transform(configStylesheet_doc, format_doc);
|
---|
603 |
|
---|
604 | // Since we started creating documents with DocTypes, we can end up with
|
---|
605 | // Document objects here. But we will be working with an Element instead,
|
---|
606 | // so we grab the DocumentElement() of the Document object in such a case.
|
---|
607 | Element new_format;
|
---|
608 | if(result.getNodeType() == Node.DOCUMENT_NODE) {
|
---|
609 | new_format = ((Document)result).getDocumentElement();
|
---|
610 | } else {
|
---|
611 | new_format = (Element)result;
|
---|
612 | }
|
---|
613 | logger.debug("new format elem="+this.converter.getPrettyString(new_format));
|
---|
614 | if (output.equals("newformat")) {
|
---|
615 | return new_format;
|
---|
616 | }
|
---|
617 |
|
---|
618 | // add extracted GSF statements in to the main stylesheet
|
---|
619 | GSXSLT.mergeStylesheets(skinAndLibraryDoc, new_format);
|
---|
620 | //System.out.println("added extracted GSF statements into the main stylesheet") ;
|
---|
621 |
|
---|
622 | // add extracted GSF statements in to the debug test stylesheet
|
---|
623 | //GSXSLT.mergeStylesheets(oldStyle_doc, new_format);
|
---|
624 | } else {
|
---|
625 | logger.error(" couldn't parse the config_format stylesheet, adding the format info as is");
|
---|
626 | GSXSLT.mergeStylesheets(skinAndLibraryDoc, format_elem);
|
---|
627 | // GSXSLT.mergeStylesheets(oldStyle_doc, format_elem);
|
---|
628 | }
|
---|
629 | logger.debug("the converted stylesheet is:");
|
---|
630 | logger.debug(this.converter.getPrettyString(skinAndLibraryDoc.getDocumentElement()));
|
---|
631 | }
|
---|
632 | */
|
---|
633 |
|
---|
634 | // there is a thing called a URIResolver which you can set for a
|
---|
635 | // transformer or transformer factory. may be able to use this
|
---|
636 | // instead of this absoluteIncludepaths hack
|
---|
637 |
|
---|
638 | GSXSLT.absoluteIncludePaths(skinAndLibraryDoc, GlobalProperties.getGSDL3Home(),
|
---|
639 | (String)this.config_params.get(GSConstants.SITE_NAME),
|
---|
640 | collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME),
|
---|
641 | base_interfaces);
|
---|
642 |
|
---|
643 |
|
---|
644 | //Same but for the debug version when we want the do the transformation like we use to do
|
---|
645 | //without any gslib elements.
|
---|
646 | GSXSLT.absoluteIncludePaths(oldStyle_doc, GlobalProperties.getGSDL3Home(),
|
---|
647 | (String)this.config_params.get(GSConstants.SITE_NAME),
|
---|
648 | collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME),
|
---|
649 | base_interfaces);
|
---|
650 |
|
---|
651 | //Send different stages of the skin xslt to the browser for debug purposes only
|
---|
652 | //using &o=skindoc or &o=skinandlib etc...
|
---|
653 | if (output.equals("skindoc")) {
|
---|
654 | return converter.getDOM(getStringFromDocument(style_doc));
|
---|
655 | }
|
---|
656 | if (output.equals("skinandlib")) {
|
---|
657 | return converter.getDOM(getStringFromDocument(skinAndLibraryXsl));
|
---|
658 | }
|
---|
659 | if (output.equals("skinandlibdoc") || output.equals("clientside")) {
|
---|
660 |
|
---|
661 | Node skinAndLib = converter.getDOM(getStringFromDocument(skinAndLibraryDoc));
|
---|
662 |
|
---|
663 | if(output.equals("skinandlibdoc")) {
|
---|
664 | return skinAndLib;
|
---|
665 | } else {
|
---|
666 | // Send XML and skinandlibdoc down the line together
|
---|
667 | Document finalDoc = converter.newDOM();
|
---|
668 | Node finalDocSkin = finalDoc.importNode(skinAndLibraryDoc.getDocumentElement(), true);
|
---|
669 | Node finalDocXML = finalDoc.importNode(theXML.getDocumentElement(), true);
|
---|
670 | Element root = finalDoc.createElement("skinlibPlusXML");
|
---|
671 | root.appendChild(finalDocSkin);
|
---|
672 | root.appendChild(finalDocXML);
|
---|
673 | finalDoc.appendChild(root);
|
---|
674 | return (Node)finalDoc.getDocumentElement();
|
---|
675 | }
|
---|
676 | }
|
---|
677 | if (output.equals("oldskindoc")) {
|
---|
678 | return converter.getDOM(getStringFromDocument(oldStyle_doc));
|
---|
679 | }
|
---|
680 |
|
---|
681 | // Try to get the system and public ID from the current skin xsl document
|
---|
682 | // otherwise keep the default values.
|
---|
683 | Element root = skinAndLibraryDoc.getDocumentElement();
|
---|
684 | NodeList nodes = root.getElementsByTagName("xsl:output");
|
---|
685 | // If there is at least one "xsl:output" command in the final xsl then...
|
---|
686 | if(nodes.getLength() != 0) {
|
---|
687 | // There should be only one element called xsl:output,
|
---|
688 | // but if this is not the case get the last one
|
---|
689 | Element xsl_output = (Element)nodes.item(nodes.getLength()-1);
|
---|
690 | if (xsl_output != null) {
|
---|
691 | // Qualified name will always be html even for xhtml pages
|
---|
692 | //String attrValue = xsl_output.getAttribute("method");
|
---|
693 | //qualifiedName = attrValue.equals("") ? qualifiedName : attrValue;
|
---|
694 |
|
---|
695 | String attrValue = xsl_output.getAttribute("doctype-system");
|
---|
696 | systemID = attrValue.equals("") ? systemID : attrValue;
|
---|
697 |
|
---|
698 | attrValue = xsl_output.getAttribute("doctype-public");
|
---|
699 | publicID = attrValue.equals("") ? publicID : attrValue;
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | //System.out.println(converter.getPrettyString(docWithDoctype));
|
---|
704 | //System.out.println("Doctype vals: " + qualifiedName + " " + publicID + " " + systemID) ;
|
---|
705 |
|
---|
706 | docWithDoctype = converter.newDOM(qualifiedName, publicID, systemID);
|
---|
707 |
|
---|
708 | //System.out.println("Generate final HTML from current skin") ;
|
---|
709 | //Transformation of the XML message from the receptionist to HTML with doctype
|
---|
710 |
|
---|
711 | return this.transformer.transform(skinAndLibraryDoc, doc, config_params, docWithDoctype); // The default
|
---|
712 |
|
---|
713 | // The line below will do the transformation like we use to do before having Skin++ implemented,
|
---|
714 | // it will not contain any GS-Lib statements expanded, and the result will not contain any doctype.
|
---|
715 |
|
---|
716 | //return (Element)this.transformer.transform(style_doc, doc, config_params);
|
---|
717 | //return null; // For now - change later
|
---|
718 | }
|
---|
719 |
|
---|
720 |
|
---|
721 | // method to convert Document to a proper XML string for debug purposes only
|
---|
722 | protected String getStringFromDocument(Document doc)
|
---|
723 | {
|
---|
724 | String content = "";
|
---|
725 | try
|
---|
726 | {
|
---|
727 | DOMSource domSource = new DOMSource(doc);
|
---|
728 | StringWriter writer = new StringWriter();
|
---|
729 | StreamResult result = new StreamResult(writer);
|
---|
730 | TransformerFactory tf = TransformerFactory.newInstance();
|
---|
731 | Transformer transformer = tf.newTransformer();
|
---|
732 | transformer.transform(domSource, result);
|
---|
733 | content = writer.toString();
|
---|
734 | System.out.println("Change the & to &Amp; for proper debug dispay") ;
|
---|
735 | content = StringUtils.replace(content, "&", "&");
|
---|
736 | writer.flush();
|
---|
737 | }
|
---|
738 | catch(TransformerException ex)
|
---|
739 | {
|
---|
740 | ex.printStackTrace();
|
---|
741 | return null;
|
---|
742 | }
|
---|
743 | return content;
|
---|
744 | }
|
---|
745 |
|
---|
746 |
|
---|
747 | protected Document getPreprocessDoc() throws Exception {
|
---|
748 |
|
---|
749 | File xslt_file = new File(preprocess_xsl_filename) ;
|
---|
750 |
|
---|
751 | FileReader reader = new FileReader(xslt_file);
|
---|
752 | InputSource xml_source = new InputSource(reader);
|
---|
753 | this.parser.parse(xml_source);
|
---|
754 | Document doc = this.parser.getDocument();
|
---|
755 |
|
---|
756 | return doc ;
|
---|
757 | }
|
---|
758 |
|
---|
759 | protected Document getLibraryDoc() throws Exception {
|
---|
760 | Document doc = null;
|
---|
761 | File xslt_file = new File(this.getLibraryXSLFilename()) ;
|
---|
762 |
|
---|
763 | FileReader reader = new FileReader(xslt_file);
|
---|
764 | InputSource xml_source = new InputSource(reader);
|
---|
765 | this.parser.parse(xml_source);
|
---|
766 |
|
---|
767 | doc = this.parser.getDocument();
|
---|
768 | return doc ;
|
---|
769 | }
|
---|
770 |
|
---|
771 | protected String getXSLTFileName(String action, String subaction,
|
---|
772 | String collection) {
|
---|
773 |
|
---|
774 | String name = null;
|
---|
775 | if (!subaction.equals("")) {
|
---|
776 | String key = action+":"+subaction;
|
---|
777 | name = (String) this.xslt_map.get(key);
|
---|
778 | }
|
---|
779 | // try the action by itself
|
---|
780 | if (name==null) {
|
---|
781 | name = (String) this.xslt_map.get(action);
|
---|
782 | }
|
---|
783 | // now find the absolute path
|
---|
784 | String stylesheet = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.SITE_NAME), collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, name);
|
---|
785 | if (stylesheet==null) {
|
---|
786 | logger.info(" Can't find stylesheet for "+name);
|
---|
787 | }
|
---|
788 | logger.debug("Stylesheet: "+stylesheet);
|
---|
789 | return stylesheet;
|
---|
790 | }
|
---|
791 |
|
---|
792 | // returns the library.xsl path of the library file that is applicable for the current interface
|
---|
793 | protected String getLibraryXSLFilename() {
|
---|
794 | return GSFile.xmlTransformDir(GSFile.interfaceHome(
|
---|
795 | GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.INTERFACE_NAME)))
|
---|
796 | + File.separatorChar + "library.xsl";
|
---|
797 | }
|
---|
798 |
|
---|
799 | // Call this when a FileNotFoundException could be thrown when loading an xsl (xml) file.
|
---|
800 | // Returns an error xhtml page indicating which xsl (or other xml) file is missing.
|
---|
801 | protected Document fileNotFoundErrorPage(String filenameMessage) {
|
---|
802 | String errorMessage = "ERROR missing file: " + filenameMessage;
|
---|
803 | Element errPage = XMLTransformer.constructErrorXHTMLPage(errorMessage);
|
---|
804 | logger.error(errorMessage);
|
---|
805 | return errPage.getOwnerDocument();
|
---|
806 | }
|
---|
807 | }
|
---|