source: trunk/gsdl3/src/java/org/greenstone/gsdl3/core/TransformingReceptionist.java@ 5402

Last change on this file since 5402 was 5402, checked in by kjdon, 21 years ago

tidying up println stuff

  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.gsdl3.util.*;
4import org.greenstone.gsdl3.action.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11// other java classes
12import java.io.File;
13import java.util.HashMap;
14import java.util.Enumeration;
15
16/** A receptionist that uses xslt to transform the page_data before returning it. . Receives requests consisting
17 * of an xml representation of cgi args, and returns the page of data - in
18 * html by default. The requests are processed by the appropriate action class
19 *
20 * @see Action
21 */
22public class TransformingReceptionist extends Receptionist{
23
24 /** the list of xslt to use for actions */
25 protected HashMap xslt_map = null;
26
27 /** a transformer class to transform xml using xslt */
28 protected XMLTransformer transformer=null;
29
30 public TransformingReceptionist() {
31 super();
32 this.xslt_map = new HashMap();
33 this.transformer = new XMLTransformer();
34 }
35
36 /** configures the receptionist - overwrite this to set up the xslt map*/
37 public boolean configure() {
38
39 if (this.config_params==null) {
40 System.err.println("TransformingReceptionist Error: config variables must be set before calling configure");
41 return false;
42 }
43 if (this.mr==null) {
44 System.err.println("TransformingReceptionist Error: message router must be set before calling configure");
45 return false;
46 }
47
48 // find the config file containing a list of actions
49 File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome((String)this.config_params.get(GSConstants.GSDL3_HOME), (String)this.config_params.get(GSConstants.INTERFACE_NAME))));
50 if (!interface_config_file.exists()) {
51 System.err.println("TransformingReceptionist: interface config file: "+interface_config_file.getPath()+" not found!");
52 return false;
53 }
54
55 Element config_doc = this.converter.getDOM(interface_config_file, "utf-8").getDocumentElement();
56 Element action_list = (Element)GSXML.getChildByTagName(config_doc, GSXML.ACTION_ELEM+GSXML.LIST_MODIFIER);
57 NodeList actions = action_list.getElementsByTagName(GSXML.ACTION_ELEM);
58
59 for (int i=0; i<actions.getLength(); i++) {
60 Element action = (Element) actions.item(i);
61 String class_name = action.getAttribute("class");
62 String action_name = action.getAttribute("name");
63 Action ac = null;
64 try {
65 ac = (Action)Class.forName("org.greenstone.gsdl3.action."+class_name).newInstance();
66 } catch (Exception e) {
67 System.err.println("TransformingReceptionist Error: couldn't load in action "+class_name);
68 e.printStackTrace();
69 continue;
70 }
71 ac.setConfigParams(this.config_params);
72 ac.setMessageRouter(this.mr);
73 ac.configure();
74 ac.getActionParameters(this.params);
75 this.action_map.put(action_name, ac);
76
77 // now do the xslt map
78 String xslt = action.getAttribute("xslt");
79 if (!xslt.equals("")) {
80 this.xslt_map.put(action_name, xslt);
81 }
82 NodeList subactions = action.getElementsByTagName(GSXML.SUBACTION_ELEM);
83 for (int j=0; j<subactions.getLength(); j++) {
84 Element subaction = (Element)subactions.item(j);
85 String subname = subaction.getAttribute(GSXML.NAME_ATT);
86 String subxslt = subaction.getAttribute("xslt");
87
88 String map_key = action_name+":"+subname;
89 ///ystem.out.println("adding in to xslt map, "+map_key+"->"+subxslt);
90 this.xslt_map.put(map_key, subxslt);
91 }
92
93
94 }
95 Element lang_list = (Element)GSXML.getChildByTagName(config_doc, "languageList");
96 if (lang_list == null) {
97 System.err.println("TransformingReceptionist: didn't find a language list in the config file!!");
98 } else {
99 this.language_list = (Element) this.doc.importNode(lang_list, true);
100 }
101
102 return true;
103 }
104
105
106 protected Element postProcessPage(Element page) {
107
108 // might need to add some data to the page
109 addExtraInfo(page);
110 // transform the page using xslt
111 Element transformed_page = transformPage(page);
112
113 return transformed_page;
114 }
115
116 /** overwrite this to add any extra info that might be needed in the page before transformation */
117 protected void addExtraInfo(Element page) {}
118
119 /** transform the page using xslt
120 * we need to get any format element out of the page and add it to the xslt
121 * before transforming */
122 protected Element transformPage(Element page) {
123
124 Element request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
125 String action = request.getAttribute("action");
126 String subaction = request.getAttribute("subaction");
127
128 Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
129 String collection = "";
130 if (cgi_param_list != null) {
131 HashMap params = GSXML.extractParams(cgi_param_list, false);
132 collection = (String)params.get(GSParams.COLLECTION);
133 if (collection == null) collection = "";
134 }
135
136 String xslt_file = getXSLTFileName(action, subaction, collection);
137 if (xslt_file==null) {
138 // cant transform the page - return null or the original?
139 System.err.println("TransformingReceptionist: cant find the xslt file needed, so returning the original page!");
140 return page;
141 }
142 Document style_doc = this.converter.getDOM(new File(xslt_file));
143
144 // look for the format element in the page response
145 Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
146 Element format_elem = (Element)GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM);
147 if (format_elem != null) {
148 page_response.removeChild(format_elem);
149
150 // need to transform the format info
151 String stylesheet_file = GSFile.stylesheetFile((String)this.config_params.get(GSConstants.GSDL3_HOME), (String)this.config_params.get(GSConstants.SITE_NAME), (String)this.config_params.get(GSConstants.INTERFACE_NAME), "", "config_format.xsl");
152 Document stylesheet = this.converter.getDOM(new File(stylesheet_file));
153 Document format_doc = this.converter.newDOM();
154 format_doc.appendChild(format_doc.importNode(format_elem, true));
155 Element new_format = (Element)this.transformer.transform(stylesheet, format_doc);
156 // add it in to the main stylesheet
157 GSXSLT.mergeStylesheets(style_doc, new_format);
158 ///ystem.out.println("the converted stylesheet is:");
159 ///ystem.out.println(this.converter.getPrettyString(style_doc.getDocumentElement()));
160 }
161
162 // there is a thing called a URIResolver which you can set for a transformer or transformer factory. may be able to use this instead of this absoluteIncludepaths hack
163 GSXSLT.absoluteIncludePaths(style_doc, (String)this.config_params.get(GSConstants.GSDL3_HOME), (String)this.config_params.get(GSConstants.SITE_NAME), (String)this.config_params.get(GSConstants.INTERFACE_NAME), collection);
164 // put the page into a document - this is necessary for xslt to get the paths right if you have paths relative to the document root eg /page.
165 Document doc = this.converter.newDOM();
166 doc.appendChild(doc.importNode(page, true));
167 return (Element)this.transformer.transform(style_doc, doc, config_params);
168 }
169
170 protected String getXSLTFileName(String action, String subaction,
171 String collection) {
172
173 String name = null;
174 if (!subaction.equals("")) {
175 String key = action+":"+subaction;
176 name = (String) this.xslt_map.get(key);
177 }
178 // try the action by itself
179 if (name==null) {
180 name = (String) this.xslt_map.get(action);
181 }
182 // now find the absolute path
183 String stylesheet = GSFile.stylesheetFile((String)this.config_params.get(GSConstants.GSDL3_HOME), (String)this.config_params.get(GSConstants.SITE_NAME), (String)this.config_params.get(GSConstants.INTERFACE_NAME), collection, name);
184 if (stylesheet==null) {
185 System.out.println("TransformingReceptionist: cant find stylesheet for "+name);
186 }
187 return stylesheet;
188 }
189
190}
Note: See TracBrowser for help on using the repository browser.