source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/TransformingReceptionist.java@ 23270

Last change on this file since 23270 was 23270, checked in by kjdon, 13 years ago

use GSParams static strings for excerptid/tag strings

  • Property svn:keywords set to Author Date Id Revision
File size: 25.4 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.util.GlobalProperties;
4import org.greenstone.gsdl3.util.*;
5import org.greenstone.gsdl3.action.*;
6// XML classes
7import org.w3c.dom.Node;
8import org.w3c.dom.NodeList;
9import org.w3c.dom.Document;
10import org.w3c.dom.Element;
11import org.xml.sax.InputSource;
12
13// other java classes
14import java.io.File;
15import java.io.StringWriter;
16import java.io.FileReader;
17import java.io.FileNotFoundException;
18import java.util.HashMap;
19import java.util.Enumeration;
20
21import javax.xml.parsers.*;
22import javax.xml.transform.*;
23import javax.xml.transform.dom.*;
24import javax.xml.transform.stream.*;
25import org.apache.log4j.*;
26import org.apache.xerces.dom.*;
27import 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 */
35public 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 return selectedElement;
198 }
199 else if(excerptTag != null)
200 {
201 Node selectedElement = getNodeByTagRecursive(transformed_page, excerptTag);
202 return selectedElement;
203 }
204 return transformed_page;
205 }
206
207 protected Node getNodeByIdRecursive(Node parent, String id)
208 {
209 if(parent.hasAttributes() && ((Element)parent).getAttribute("id").equals(id))
210 {
211 return parent;
212 }
213
214 NodeList children = parent.getChildNodes();
215 for(int i = 0; i < children.getLength(); i++)
216 {
217 Node result = null;
218 if((result = getNodeByIdRecursive(children.item(i), id)) != null)
219 {
220 return result;
221 }
222 }
223 return null;
224 }
225
226 protected Node getNodeByTagRecursive(Node parent, String tag)
227 {
228 if(parent.getNodeType() == Node.ELEMENT_NODE && ((Element)parent).getTagName().equals(tag))
229 {
230 return parent;
231 }
232
233 NodeList children = parent.getChildNodes();
234 for(int i = 0; i < children.getLength(); i++)
235 {
236 Node result = null;
237 if((result = getNodeByTagRecursive(children.item(i), tag)) != null)
238 {
239 return result;
240 }
241 }
242 return null;
243 }
244
245 /** overwrite this to add any extra info that might be needed in the page before transformation */
246 protected void addExtraInfo(Element page) {}
247
248 /** transform the page using xslt
249 * we need to get any format element out of the page and add it to the xslt
250 * before transforming */
251 protected Node transformPage(Element page) {
252
253 logger.debug("page before transforming:");
254 logger.debug(this.converter.getPrettyString(page));
255
256 Element request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
257 String action = request.getAttribute(GSXML.ACTION_ATT);
258 String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
259
260 String output = request.getAttribute(GSXML.OUTPUT_ATT);
261 // we should choose how to transform the data based on output, eg diff
262 // choice for html, and wml??
263 // for now, if output=xml, we don't transform the page, we just return
264 // the page xml
265 if (output.equals("xml")) {
266 return page;
267 }
268
269
270 Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
271 String collection = "";
272 if (cgi_param_list != null) {
273 HashMap params = GSXML.extractParams(cgi_param_list, false);
274 collection = (String)params.get(GSParams.COLLECTION);
275 if (collection == null) collection = "";
276 }
277
278 String xslt_file = getXSLTFileName(action, subaction, collection);
279 if (xslt_file==null) {
280 // returning file not found error page to indicate which file is missing
281 return fileNotFoundErrorPage(xslt_file);
282 }
283
284 Document style_doc = this.converter.getDOM(new File(xslt_file), "UTF-8");
285 String errorPage = this.converter.getParseErrorMessage();
286 if(errorPage != null) {
287 return XMLTransformer.constructErrorXHTMLPage(
288 "Cannot parse the xslt file: " + xslt_file + "\n" + errorPage);
289 }
290 if (style_doc == null) {
291 logger.error(" cant parse the xslt file needed, so returning the original page!");
292 return page;
293 }
294
295
296 // put the page into a document - this is necessary for xslt to get
297 // the paths right if you have paths relative to the document root
298 // eg /page.
299 Document doc = this.converter.newDOM();
300 doc.appendChild(doc.importNode(page, true));
301 Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
302 Element format_elem = (Element)GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM);
303 if (output.equals("formatelem")) {
304 return format_elem;
305 }
306 if (format_elem != null) {
307 //page_response.removeChild(format_elem);
308 logger.debug("format elem="+this.converter.getPrettyString(format_elem));
309 // need to transform the format info
310 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");
311 Document configStylesheet_doc = this.converter.getDOM(new File(configStylesheet_file));
312 if (configStylesheet_doc != null) {
313 Document format_doc = this.converter.newDOM();
314 format_doc.appendChild(format_doc.importNode(format_elem, true));
315 Node result = this.transformer.transform(configStylesheet_doc, format_doc);
316
317 // Since we started creating documents with DocTypes, we can end up with
318 // Document objects here. But we will be working with an Element instead,
319 // so we grab the DocumentElement() of the Document object in such a case.
320 Element new_format;
321 if(result.getNodeType() == Node.DOCUMENT_NODE) {
322 new_format = ((Document)result).getDocumentElement();
323 } else {
324 new_format = (Element)result;
325 }
326 logger.debug("new format elem="+this.converter.getPrettyString(new_format));
327 if (output.equals("newformat")) {
328 return new_format;
329 }
330
331 // add extracted GSF statements in to the main stylesheet
332 GSXSLT.mergeStylesheets(style_doc, new_format);
333 //System.out.println("added extracted GSF statements into the main stylesheet") ;
334
335 // add extracted GSF statements in to the debug test stylesheet
336 //GSXSLT.mergeStylesheets(oldStyle_doc, new_format);
337 } else {
338 logger.error(" couldn't parse the config_format stylesheet, adding the format info as is");
339 GSXSLT.mergeStylesheets(style_doc, format_elem);
340 //GSXSLT.mergeStylesheets(oldStyle_doc, format_elem);
341 }
342 logger.debug("the converted stylesheet is:");
343 logger.debug(this.converter.getPrettyString(style_doc.getDocumentElement()));
344 }
345
346 //for debug purposes only
347 Document oldStyle_doc = style_doc;
348
349
350 Document preprocessingXsl ;
351 try {
352 preprocessingXsl = getPreprocessDoc();
353 String errMsg = ((XMLConverter.ParseErrorHandler)parser.getErrorHandler()).getErrorMessage();
354 if(errMsg != null) {
355 return XMLTransformer.constructErrorXHTMLPage("error loading preprocess xslt file: "
356 + preprocess_xsl_filename + "\n" + errMsg);
357 }
358 } catch (java.io.FileNotFoundException e) {
359 return fileNotFoundErrorPage(e.getMessage());
360 } catch (Exception e) {
361 e.printStackTrace() ;
362 System.out.println("error loading preprocess xslt") ;
363 return XMLTransformer.constructErrorXHTMLPage("error loading preprocess xslt\n" + e.getMessage());
364 }
365
366 Document libraryXsl = null;
367 try {
368 libraryXsl = getLibraryDoc() ;
369 String errMsg = ((XMLConverter.ParseErrorHandler)parser.getErrorHandler()).getErrorMessage();
370 if(errMsg != null) {
371 return XMLTransformer.constructErrorXHTMLPage("Error loading xslt file: "
372 + this.getLibraryXSLFilename() + "\n" + errMsg);
373 }
374 } catch (java.io.FileNotFoundException e) {
375 return fileNotFoundErrorPage(e.getMessage());
376 } catch (Exception e) {
377 e.printStackTrace() ;
378 System.out.println("error loading library xslt") ;
379 return XMLTransformer.constructErrorXHTMLPage("error loading library xslt\n" + e.getMessage()) ;
380 }
381
382 // Combine the skin file and library variables/templates into one document.
383 // Please note: We dont just use xsl:import because the preprocessing stage
384 // needs to know what's available in the library.
385
386 Document skinAndLibraryXsl = null ;
387 Document skinAndLibraryDoc = converter.newDOM();
388 try {
389
390 skinAndLibraryXsl = converter.newDOM();
391 Element root = skinAndLibraryXsl.createElement("skinAndLibraryXsl") ;
392 skinAndLibraryXsl.appendChild(root) ;
393
394 Element s = skinAndLibraryXsl.createElement("skinXsl") ;
395 s.appendChild(skinAndLibraryXsl.importNode(style_doc.getDocumentElement(), true)) ;
396 root.appendChild(s) ;
397
398 Element l = skinAndLibraryXsl.createElement("libraryXsl") ;
399 Element libraryXsl_el = libraryXsl.getDocumentElement();
400 l.appendChild(skinAndLibraryXsl.importNode(libraryXsl_el, true)) ;
401 root.appendChild(l) ;
402 //System.out.println("Skin and Library XSL are now together") ;
403
404
405 //System.out.println("Pre-processing the skin file...") ;
406
407 //pre-process the skin style sheet
408 //In other words, apply the preProcess.xsl to 'skinAndLibraryXsl' in order to
409 //expand all GS-Lib statements into complete XSL statements and also to create
410 //a valid xsl style sheet document.
411
412 Transformer preProcessor = transformerFactory.newTransformer(new DOMSource(preprocessingXsl));
413 preProcessor.setErrorListener(new XMLTransformer.TransformErrorListener());
414 DOMResult result = new DOMResult();
415 result.setNode(skinAndLibraryDoc);
416 preProcessor.transform(new DOMSource(skinAndLibraryXsl), result);
417 //System.out.println("GS-Lib statements are now expanded") ;
418
419 }
420 catch (TransformerException e) {
421 e.printStackTrace() ;
422 System.out.println("TransformerException while preprocessing the skin xslt") ;
423 return XMLTransformer.constructErrorXHTMLPage(e.getMessage()) ;
424 }
425 catch (Exception e) {
426 e.printStackTrace() ;
427 System.out.println("Error while preprocessing the skin xslt") ;
428 return XMLTransformer.constructErrorXHTMLPage(e.getMessage()) ;
429 }
430
431 //The following code is to be uncommented if we need to append the extracted GSF statements
432 //after having extracted the GSLib elements. In case of a problem during postprocessing.
433 /*
434 // put the page into a document - this is necessary for xslt to get
435 // the paths right if you have paths relative to the document root
436 // eg /page.
437 Document doc = this.converter.newDOM();
438 doc.appendChild(doc.importNode(page, true));
439 Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
440 Element format_elem = (Element)GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM);
441 if (output.equals("formatelem")) {
442 return format_elem;
443 }
444 if (format_elem != null) {
445 //page_response.removeChild(format_elem);
446 logger.debug("format elem="+this.converter.getPrettyString(format_elem));
447 // need to transform the format info
448 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");
449 Document configStylesheet_doc = this.converter.getDOM(new File(configStylesheet_file));
450 if (configStylesheet_doc != null) {
451 Document format_doc = this.converter.newDOM();
452 format_doc.appendChild(format_doc.importNode(format_elem, true));
453 Node result = this.transformer.transform(configStylesheet_doc, format_doc);
454
455 // Since we started creating documents with DocTypes, we can end up with
456 // Document objects here. But we will be working with an Element instead,
457 // so we grab the DocumentElement() of the Document object in such a case.
458 Element new_format;
459 if(result.getNodeType() == Node.DOCUMENT_NODE) {
460 new_format = ((Document)result).getDocumentElement();
461 } else {
462 new_format = (Element)result;
463 }
464 logger.debug("new format elem="+this.converter.getPrettyString(new_format));
465 if (output.equals("newformat")) {
466 return new_format;
467 }
468
469 // add extracted GSF statements in to the main stylesheet
470 GSXSLT.mergeStylesheets(skinAndLibraryDoc, new_format);
471 //System.out.println("added extracted GSF statements into the main stylesheet") ;
472
473 // add extracted GSF statements in to the debug test stylesheet
474 //GSXSLT.mergeStylesheets(oldStyle_doc, new_format);
475 } else {
476 logger.error(" couldn't parse the config_format stylesheet, adding the format info as is");
477 GSXSLT.mergeStylesheets(skinAndLibraryDoc, format_elem);
478 // GSXSLT.mergeStylesheets(oldStyle_doc, format_elem);
479 }
480 logger.debug("the converted stylesheet is:");
481 logger.debug(this.converter.getPrettyString(skinAndLibraryDoc.getDocumentElement()));
482 }
483 */
484
485 // there is a thing called a URIResolver which you can set for a
486 // transformer or transformer factory. may be able to use this
487 // instead of this absoluteIncludepaths hack
488
489 GSXSLT.absoluteIncludePaths(skinAndLibraryDoc, GlobalProperties.getGSDL3Home(),
490 (String)this.config_params.get(GSConstants.SITE_NAME),
491 collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME),
492 base_interfaces);
493
494
495 //Same but for the debug version when we want the do the transformation like we use to do
496 //without any gslib elements.
497 GSXSLT.absoluteIncludePaths(oldStyle_doc, GlobalProperties.getGSDL3Home(),
498 (String)this.config_params.get(GSConstants.SITE_NAME),
499 collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME),
500 base_interfaces);
501
502 //Send different stages of the skin xslt to the browser for debug purposes only
503 //using &o=skindoc or &o=skinandlib etc...
504 if (output.equals("skindoc")) {
505 return converter.getDOM(getStringFromDocument(style_doc));
506 }
507 if (output.equals("skinandlib")) {
508 return converter.getDOM(getStringFromDocument(skinAndLibraryXsl));
509 }
510 if (output.equals("skinandlibdoc")) {
511 return converter.getDOM(getStringFromDocument(skinAndLibraryDoc));
512 }
513 if (output.equals("oldskindoc")) {
514 return converter.getDOM(getStringFromDocument(oldStyle_doc));
515 }
516
517 // DocType defaults in case the skin doesn't have an "xsl:output" element
518 String qualifiedName = "html";
519 String publicID = "-//W3C//DTD HTML 4.01 Transitional//EN";
520 String systemID = "http://www.w3.org/TR/html4/loose.dtd";
521
522 // Try to get the system and public ID from the current skin xsl document
523 // otherwise keep the default values.
524 Element root = skinAndLibraryDoc.getDocumentElement();
525 NodeList nodes = root.getElementsByTagName("xsl:output");
526 // If there is at least one "xsl:output" command in the final xsl then...
527 if(nodes.getLength() != 0) {
528 // There should be only one element called xsl:output,
529 // but if this is not the case get the last one
530 Element xsl_output = (Element)nodes.item(nodes.getLength()-1);
531 if (xsl_output != null) {
532 // Qualified name will always be html even for xhtml pages
533 //String attrValue = xsl_output.getAttribute("method");
534 //qualifiedName = attrValue.equals("") ? qualifiedName : attrValue;
535
536 String attrValue = xsl_output.getAttribute("doctype-system");
537 systemID = attrValue.equals("") ? systemID : attrValue;
538
539 attrValue = xsl_output.getAttribute("doctype-public");
540 publicID = attrValue.equals("") ? publicID : attrValue;
541 }
542 }
543
544 // We need to create an empty document with a predefined DocType,
545 // that will then be used for the transformation by the DOMResult
546 Document docWithDoctype = converter.newDOM(qualifiedName, publicID, systemID);
547
548 //System.out.println(converter.getPrettyString(docWithDoctype));
549 //System.out.println("Doctype vals: " + qualifiedName + " " + publicID + " " + systemID) ;
550
551
552 //System.out.println("Generate final HTML from current skin") ;
553 //Transformation of the XML message from the receptionist to HTML with doctype
554 return this.transformer.transform(skinAndLibraryDoc, doc, config_params, docWithDoctype);
555
556
557 // The line below will do the transformation like we use to do before having Skin++ implemented,
558 // it will not contain any GS-Lib statements expanded, and the result will not contain any doctype.
559
560 //return (Element)this.transformer.transform(style_doc, doc, config_params);
561
562 }
563
564
565 // method to convert Document to a proper XML string for debug purposes only
566 protected String getStringFromDocument(Document doc)
567 {
568 String content = "";
569 try
570 {
571 DOMSource domSource = new DOMSource(doc);
572 StringWriter writer = new StringWriter();
573 StreamResult result = new StreamResult(writer);
574 TransformerFactory tf = TransformerFactory.newInstance();
575 Transformer transformer = tf.newTransformer();
576 transformer.transform(domSource, result);
577 content = writer.toString();
578 System.out.println("Change the & to &Amp; for proper debug dispay") ;
579 content = content.replaceAll("&", "&amp;");
580 writer.flush();
581 }
582 catch(TransformerException ex)
583 {
584 ex.printStackTrace();
585 return null;
586 }
587 return content;
588 }
589
590
591 protected Document getPreprocessDoc() throws Exception {
592
593 File xslt_file = new File(preprocess_xsl_filename) ;
594
595 FileReader reader = new FileReader(xslt_file);
596 InputSource xml_source = new InputSource(reader);
597 this.parser.parse(xml_source);
598 Document doc = this.parser.getDocument();
599
600 return doc ;
601 }
602
603 protected Document getLibraryDoc() throws Exception {
604 Document doc = null;
605 File xslt_file = new File(this.getLibraryXSLFilename()) ;
606
607 FileReader reader = new FileReader(xslt_file);
608 InputSource xml_source = new InputSource(reader);
609 this.parser.parse(xml_source);
610
611 doc = this.parser.getDocument();
612 return doc ;
613 }
614
615 protected String getXSLTFileName(String action, String subaction,
616 String collection) {
617
618 String name = null;
619 if (!subaction.equals("")) {
620 String key = action+":"+subaction;
621 name = (String) this.xslt_map.get(key);
622 }
623 // try the action by itself
624 if (name==null) {
625 name = (String) this.xslt_map.get(action);
626 }
627 // now find the absolute path
628 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);
629 if (stylesheet==null) {
630 logger.info(" cant find stylesheet for "+name);
631 }
632 return stylesheet;
633 }
634
635 // returns the library.xsl path of the library file that is applicable for the current interface
636 protected String getLibraryXSLFilename() {
637 return GSFile.xmlTransformDir(GSFile.interfaceHome(
638 GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.INTERFACE_NAME)))
639 + File.separatorChar + "library.xsl";
640 }
641
642 // Call this when a FileNotFoundException could be thrown when loading an xsl (xml) file.
643 // Returns an error xhtml page indicating which xsl (or other xml) file is missing.
644 protected Document fileNotFoundErrorPage(String filenameMessage) {
645 String errorMessage = "ERROR missing file: " + filenameMessage;
646 Element errPage = XMLTransformer.constructErrorXHTMLPage(errorMessage);
647 logger.error(errorMessage);
648 return errPage.getOwnerDocument();
649 }
650}
Note: See TracBrowser for help on using the repository browser.