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

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

we now look for stylesheets in teh collection before the site - note this only works for local sites

  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 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("Receptionist Error: config variables must be set before calling configure");
41 return false;
42 }
43 if (this.mr==null) {
44 System.err.println("Receptionist 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).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("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 this.action_map.put(action_name, ac);
75
76 // now do the xslt map
77 String xslt = action.getAttribute("xslt");
78 if (!xslt.equals("")) {
79 this.xslt_map.put(action_name, xslt);
80 }
81 NodeList subactions = action.getElementsByTagName(GSXML.SUBACTION_ELEM);
82 for (int j=0; j<subactions.getLength(); j++) {
83 Element subaction = (Element)subactions.item(j);
84 String subname = subaction.getAttribute(GSXML.NAME_ATT);
85 String subxslt = subaction.getAttribute("xslt");
86
87 String map_key = action_name+":"+subname;
88 ///ystem.out.println("adding in to xslt map, "+map_key+"->"+subxslt);
89 this.xslt_map.put(map_key, subxslt);
90 }
91
92
93 }
94
95 return true;
96 }
97
98
99 protected Element postProcessPage(Element page) {
100
101 // might need to add some data to the page
102 addExtraInfo(page);
103 // transform the page using xslt
104 Element transformed_page = transformPage(page);
105
106 return transformed_page;
107 }
108
109 /** overwrite this to add any extra info that might be needed in the page before transformation */
110 protected void addExtraInfo(Element page) {}
111
112 /** transform the page using xslt
113 * we need to get any format element out of the page and add it to the xslt
114 * before transforming */
115 protected Element transformPage(Element page) {
116
117 Element request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
118 String action = request.getAttribute("action");
119 String subaction = request.getAttribute("subaction");
120
121 Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
122 String collection = "";
123 if (cgi_param_list != null) {
124 HashMap params = GSXML.extractParams(cgi_param_list, false);
125 collection = (String)params.get(GSCGI.COLLECTION_ARG);
126 if (collection == null) collection = "";
127 }
128
129 String xslt_file = getXSLTFileName(action, subaction, collection);
130 if (xslt_file==null) {
131 // cant transform the page - return null or the original?
132 System.err.println(" cant find the xslt file needed, so returning the original page!");
133 return page;
134 }
135 Document style_doc = this.converter.getDOM(new File(xslt_file));
136
137 // look for the format element in the page response
138 Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
139 Element format_elem = (Element)GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM);
140 if (format_elem != null) {
141 page_response.removeChild(format_elem);
142
143 // need to transform the format info
144 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");
145 Document stylesheet = this.converter.getDOM(new File(stylesheet_file));
146 Document format_doc = this.converter.newDOM();
147 format_doc.appendChild(format_doc.importNode(format_elem, true));
148 Element new_format = (Element)this.transformer.transform(stylesheet, format_doc);
149 // add it in to the main stylesheet
150 GSXSLT.mergeStylesheets(style_doc, new_format);
151 ///ystem.out.println("the converted stylesheet is:");
152 ///ystem.out.println(this.converter.getPrettyString(style_doc.getDocumentElement()));
153 }
154
155 // 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
156 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);
157 // 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.
158 Document doc = this.converter.newDOM();
159 doc.appendChild(doc.importNode(page, true));
160 return (Element)this.transformer.transform(style_doc, doc, config_params);
161 }
162
163 protected String getXSLTFileName(String action, String subaction,
164 String collection) {
165
166 String name = null;
167 if (!subaction.equals("")) {
168 String key = action+":"+subaction;
169 name = (String) this.xslt_map.get(key);
170 }
171 // try the action by itself
172 if (name==null) {
173 name = (String) this.xslt_map.get(action);
174 }
175 // now find the absolute path
176 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);
177 if (stylesheet==null) {
178 System.out.println("cant find stylesheet for "+name);
179 }
180 return stylesheet;
181 }
182 /** returns an xml element containing all the text strings
183 * needed for an interface
184 * currently only uses the current interface - should add in
185 * needed ones from the default interface if current != default */
186 protected Element getDisplayElement(String lang) {
187
188 Element display = this.doc.createElement(GSXML.DISPLAY_ELEM);
189 // looks for properties files based on interface name
190 String resource_name = "interface_"+ (String)this.config_params.get(GSConstants.INTERFACE_NAME);
191
192 Dictionary dict = new Dictionary(resource_name, lang);
193 Enumeration enum = dict.getKeys();
194 while (enum.hasMoreElements()) {
195 String key = (String)enum.nextElement();
196 String value = dict.get(key);
197 Element e = GSXML.createTextElement(this.doc, key, value);
198 display.appendChild(e);
199 }
200 return display;
201 }
202
203}
Note: See TracBrowser for help on using the repository browser.