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

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

removed some unnecessary stuff

  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 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 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
96 return true;
97 }
98
99
100 protected Element postProcessPage(Element page) {
101
102 // might need to add some data to the page
103 addExtraInfo(page);
104 // transform the page using xslt
105 Element transformed_page = transformPage(page);
106
107 return transformed_page;
108 }
109
110 /** overwrite this to add any extra info that might be needed in the page before transformation */
111 protected void addExtraInfo(Element page) {}
112
113 /** transform the page using xslt
114 * we need to get any format element out of the page and add it to the xslt
115 * before transforming */
116 protected Element transformPage(Element page) {
117
118 Element request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
119 String action = request.getAttribute("action");
120 String subaction = request.getAttribute("subaction");
121
122 Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
123 String collection = "";
124 if (cgi_param_list != null) {
125 HashMap params = GSXML.extractParams(cgi_param_list, false);
126 collection = (String)params.get(GSCGI.COLLECTION_ARG);
127 if (collection == null) collection = "";
128 }
129
130 String xslt_file = getXSLTFileName(action, subaction, collection);
131 if (xslt_file==null) {
132 // cant transform the page - return null or the original?
133 System.err.println(" cant find the xslt file needed, so returning the original page!");
134 return page;
135 }
136 Document style_doc = this.converter.getDOM(new File(xslt_file));
137
138 // look for the format element in the page response
139 Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
140 Element format_elem = (Element)GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM);
141 if (format_elem != null) {
142 page_response.removeChild(format_elem);
143
144 // need to transform the format info
145 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");
146 Document stylesheet = this.converter.getDOM(new File(stylesheet_file));
147 Document format_doc = this.converter.newDOM();
148 format_doc.appendChild(format_doc.importNode(format_elem, true));
149 Element new_format = (Element)this.transformer.transform(stylesheet, format_doc);
150 // add it in to the main stylesheet
151 GSXSLT.mergeStylesheets(style_doc, new_format);
152 ///ystem.out.println("the converted stylesheet is:");
153 ///ystem.out.println(this.converter.getPrettyString(style_doc.getDocumentElement()));
154 }
155
156 // 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
157 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);
158 // 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.
159 Document doc = this.converter.newDOM();
160 doc.appendChild(doc.importNode(page, true));
161 return (Element)this.transformer.transform(style_doc, doc, config_params);
162 }
163
164 protected String getXSLTFileName(String action, String subaction,
165 String collection) {
166
167 String name = null;
168 if (!subaction.equals("")) {
169 String key = action+":"+subaction;
170 name = (String) this.xslt_map.get(key);
171 }
172 // try the action by itself
173 if (name==null) {
174 name = (String) this.xslt_map.get(action);
175 }
176 // now find the absolute path
177 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);
178 if (stylesheet==null) {
179 System.out.println("cant find stylesheet for "+name);
180 }
181 return stylesheet;
182 }
183
184}
Note: See TracBrowser for help on using the repository browser.