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