source: greenstone3/trunk/src/java/org/greenstone/gsdl3/core/SkinnedReceptionist.java@ 16374

Last change on this file since 16374 was 16374, checked in by davidb, 16 years ago

Change to Skin code (returning Node note Element) so XSLT encodes its DocType -- important information for IE to render resultant HTML correctly. This also required Skin.java to be changed from using DomResult to StreamResult. The former is known to have a problem with loosing its DocType info, and as it's then read-only, has no elegant way to put back this info.

File size: 13.6 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import java.util.* ;
4import java.io.* ;
5
6import org.w3c.dom.NodeList;
7import org.w3c.dom.Document;
8import org.w3c.dom.Element;
9import org.w3c.dom.Node;
10
11import org.greenstone.gsdl3.util.*;
12
13import javax.xml.xpath.* ;
14
15public class SkinnedReceptionist extends NZDLReceptionist {
16
17 /** a transformer class to transform xml using xslt */
18 protected XMLTransformer transformer=null;
19
20 public SkinnedReceptionist() {
21 super();
22 System.out.println("Skinned Receptionist created") ;
23 //this.xslt_map = new HashMap();
24 this.transformer = new XMLTransformer();
25 }
26
27 protected Node postProcessPage(Element p) {
28 try {
29 //Avoid the duplicate attribute in the namespaces declaration of the generated html file
30 //String NAMESPACE_URI = "http://www.greenstone.org/greenstone3/schema/gsf";
31 //String NAMESPACE_PREFIX = "gsf";
32 //p.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + NAMESPACE_PREFIX, NAMESPACE_URI);
33
34
35 Page page = new Page(p, this) ;
36 Skin skin = new Skin(page, this) ;
37
38 addExtraInfo(p) ;
39 addCssFiles(page, skin) ;
40 addJsFiles(page, skin) ;
41 addUiElements(page) ;
42 addMetadataElements(page) ;
43
44 String output = page.getPageRequest().getAttribute(GSXML.OUTPUT_ATT);
45
46 // might need to add some data to the page
47 //addExtraInfo(page);
48
49
50 System.out.println("action = " + page.getAction()) ;
51 // if we are displaying a document, specify an attribute for weither a cover image exists.
52 if (page.getAction().equals("d")) {
53
54 System.out.println("doing the document thing") ;
55
56 Element document = (Element)p.getElementsByTagName("document").item(0) ;
57 Element metadataList = (Element)GSXML.getChildByTagName(document, "metadataList") ;
58 String archiveDir = "" ;
59 NodeList metadata = metadataList.getChildNodes() ;
60 for (int i=0 ; i<metadata.getLength() ; i++) {
61 Element m = (Element) metadata.item(i) ;
62 if (m.getAttribute("name").equals("archivedir"))
63 archiveDir = GSXML.getNodeText(m) ;
64 }
65
66 String coverImage_path = page.getCollectionHome()
67 + File.separatorChar + "index"
68 + File.separatorChar + "assoc"
69 + File.separatorChar + archiveDir
70 + File.separatorChar + "cover.jpg" ;
71
72 document.setAttribute("coverImage", coverImage_path) ;
73
74 File coverImage = new File(coverImage_path) ;
75 document.setAttribute("hasCoverImage", String.valueOf(coverImage.canRead())) ;
76 }
77
78
79 Node np = null;
80 try {
81 if (skin != null && !output.equals("xml")) {
82 np = skin.transformPage(page);
83 }
84 else {
85 np = p;
86 }
87 } catch (Exception e) {
88 e.printStackTrace() ;
89 }
90
91
92
93 return np ;
94 } catch (Exception e) {
95 e.printStackTrace() ;
96 return doc.createElement("page") ;
97 }
98 }
99
100 /** overwrite this to add any extra info that might be needed in the page before transformation */
101 protected void addExtraInfo(Element page) {
102
103 //logger.debug("the page before adding extra info is");
104 //logger.debug(this.converter.getPrettyString(page));
105
106 super.addExtraInfo(page);
107
108 // this gets the collection info
109 Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
110 Element page_request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
111 Element collection_info = (Element)GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
112 if (collection_info== null) {
113 return;
114 }
115
116 String collection = collection_info.getAttribute("name"); // should we take this from the request instead??
117 // look through services and see if classifier one is present - need to
118 // get the list of classifiers
119 Element service_list = (Element)GSXML.getChildByTagName(collection_info, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
120 if (service_list == null) {
121 // something weird has gone wrong
122 return;
123 }
124
125 Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
126 String lang = page.getAttribute(GSXML.LANG_ATT);
127
128 boolean do_classifier = false;
129 boolean do_query = false;
130 Element classifier_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "ClassifierBrowse");
131 Element query_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "TextQuery");
132 if (classifier_service != null) {
133 do_classifier = true;
134 // find the list of classifiers
135
136 String to = GSPath.appendLink(collection, "ClassifierBrowse");
137 Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
138 info_message.appendChild(info_request);
139 }
140
141 String action = page_request.getAttribute(GSXML.ACTION_ATT);
142 String subaction = page_request.getAttribute(GSXML.SUBACTION_ATT);
143
144 if (query_service != null && action.equals("p") && subaction.equals("about")) {
145 do_query = true;
146 // get the full description
147 String to = GSPath.appendLink(collection, "TextQuery");
148 Element query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
149 info_message.appendChild(query_request);
150
151 }
152
153 Element info_resp_message = (Element) this.mr.process(info_message);
154 Element info_response = (Element) GSXML.getChildByTagName(info_resp_message, GSXML.RESPONSE_ELEM);
155 // String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM);
156 if (do_classifier) {
157 Element classifier_service_info = (Element)GSXML.getNamedElement(info_response, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "CLassifierBrowse");
158 if (classifier_service_info != null) {
159 service_list.replaceChild(this.doc.importNode(classifier_service_info, true), classifier_service);
160 }
161 }
162 if (do_query) {
163 Element query_service_info = (Element)GSXML.getNamedElement(info_response, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "TextQuery");
164 if (query_service_info != null) {
165 service_list.replaceChild(this.doc.importNode(query_service_info, true), query_service);
166 }
167 }
168 //logger.debug("the final page before transforming is");
169 //logger.debug(this.converter.getPrettyString(page));
170 return;
171 }
172
173 protected void addCssFiles(Page page, Skin skin) {
174
175 Element cssFileList = doc.createElement("cssFileList") ;
176
177 // get skin level files
178 if (skin != null) {
179 File dir = new File(skin.rootDirectory.getPath() + File.separatorChar + "css") ;
180 if (dir.isDirectory()) {
181 File[] files = dir.listFiles() ;
182
183 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
184 if (files[fi].getName().endsWith(".css")) {
185 Element cssFile = doc.createElement("cssFile") ;
186 String path = files[fi].getPath() ;
187 path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
188 //changes to a correct path in case GS3 runs on Windows
189 path = path.replace( '\\', '/' );
190 cssFile.setAttribute("path", path) ;
191 cssFileList.appendChild(cssFile) ;
192 }
193 }
194 }
195 }
196
197 // get site level files
198 String site_home = page.getSiteHome() ;
199 File dir = new File(site_home + File.separatorChar + "ui" + File.separatorChar + "css") ;
200 if (dir.isDirectory()) {
201 File[] files = dir.listFiles() ;
202 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
203 if (files[fi].getName().endsWith(".css")) {
204 Element cssFile = doc.createElement("cssFile") ;
205 String path = files[fi].getPath() ;
206 path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
207 //changes to a correct path in case GS3 runs on Windows
208 path = path.replace( '\\', '/' );
209 cssFile.setAttribute("path", path) ;
210 cssFileList.appendChild(cssFile) ;
211 }
212 }
213 }
214
215 // get collect level files
216 String collect_home = page.getCollectionHome() ;
217 if (collect_home != "") {
218 dir = new File(collect_home + File.separatorChar + "ui" + File.separatorChar + "css") ;
219 if (dir.isDirectory()) {
220 File[] files = dir.listFiles() ;
221 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
222 if (files[fi].getName().endsWith(".css")) {
223 Element cssFile = doc.createElement("cssFile") ;
224 String path = files[fi].getPath() ;
225 path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
226 //changes to a correct path in case GS3 runs on Windows
227 path = path.replace( '\\', '/' );
228 cssFile.setAttribute("path", path) ;
229 cssFileList.appendChild(cssFile) ;
230 }
231 }
232 }
233 }
234
235 page.getPageResponse().appendChild(cssFileList) ;
236 }
237
238 protected void addJsFiles(Page page, Skin skin) {
239
240 Element jsFileList = doc.createElement("jsFileList") ;
241
242 // get skin level files
243 if (skin != null) {
244 File dir = new File(skin.rootDirectory.getPath() + File.separatorChar + "js") ;
245 if (dir.isDirectory()) {
246 File[] files = dir.listFiles() ;
247
248 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
249 if (files[fi].getName().endsWith(".js")) {
250 Element jsFile = doc.createElement("jsFile") ;
251 String path = files[fi].getPath() ;
252 path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
253 //changes to a correct path in case GS3 runs on Windows
254 path = path.replace( '\\', '/' );
255 jsFile.setAttribute("path", path) ;
256 jsFileList.appendChild(jsFile) ;
257 }
258 }
259 }
260 }
261
262 // get site level files
263 String site_home = page.getSiteHome() ;
264 File dir = new File(site_home + File.separatorChar + "ui" + File.separatorChar + "js") ;
265 if (dir.isDirectory()) {
266 File[] files = dir.listFiles() ;
267 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
268 if (files[fi].getName().endsWith(".js")) {
269 Element jsFile = doc.createElement("jsFile") ;
270 String path = files[fi].getPath() ;
271 path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
272 //changes to a correct path in case GS3 runs on Windows
273 path = path.replace( '\\', '/' );
274 jsFile.setAttribute("path", path) ;
275 jsFileList.appendChild(jsFile) ;
276 }
277 }
278 }
279
280 // get collect level files
281 String collect_home = page.getCollectionHome() ;
282 if (collect_home != "") {
283 dir = new File(collect_home + File.separatorChar + "ui" + File.separatorChar + "js") ;
284 if (dir.isDirectory()) {
285 File[] files = dir.listFiles() ;
286 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
287 if (files[fi].getName().endsWith(".js")) {
288 Element jsFile = doc.createElement("jsFile") ;
289 String path = files[fi].getPath() ;
290 path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
291 //changes to a correct path in case GS3 runs on Windows
292 path = path.replace( '\\', '/' );
293 jsFile.setAttribute("path", path) ;
294 jsFileList.appendChild(jsFile) ;
295 }
296 }
297 }
298 }
299
300 page.getPageResponse().appendChild(jsFileList) ;
301 }
302
303 protected void addUiElements(Page page) {
304
305 Document siteUi = page.getSiteUi() ;
306 Document collectUi = page.getCollectUi() ;
307 String lang = page.getLanguage() ;
308
309 HashMap<String, Element> elements = getRelevantElements("uiItem", lang, siteUi, collectUi) ;
310 Element xmlElements = doc.createElement("uiItemList") ;
311
312 Iterator<Element> i = elements.values().iterator() ;
313 while (i.hasNext()) {
314 Element element = i.next() ;
315
316 doc.adoptNode(element) ;
317 xmlElements.appendChild(element) ;
318
319
320 }
321
322 page.getPageResponse().appendChild(xmlElements) ;
323 }
324
325 protected void addMetadataElements(Page page) {
326 Document siteMetadata = page.getSiteMetadata() ;
327 Document collectMetadata = page.getCollectMetadata() ;
328 String lang = page.getLanguage() ;
329
330 HashMap<String, Element> elements = getRelevantElements("metadataItem", lang, siteMetadata, collectMetadata) ;
331 Element xmlElements = doc.createElement("metadataList") ;
332
333 Iterator<Element> i = elements.values().iterator() ;
334 while (i.hasNext()) {
335 Element element = i.next() ;
336 doc.adoptNode(element) ;
337 xmlElements.appendChild(element) ;
338 }
339
340 page.getPageResponse().appendChild(xmlElements) ;
341 }
342
343 protected Node transformPage(Element page) {
344 return super.transformPage(page) ;
345 }
346
347 private HashMap getRelevantElements(String tagName, String lang, Document siteLevel, Document collectLevel) {
348
349 HashMap elements = new HashMap<String, Element>() ;
350
351 // get all relevant elements from collect level
352 if (collectLevel != null) {
353
354 NodeList xmlElements = collectLevel.getElementsByTagName(tagName) ;
355 for (int di = 0, dn = xmlElements.getLength() ; di < dn ; di++) {
356
357 Element xmlElement = (Element)xmlElements.item(di) ;
358
359 // only include elements that are language independent or are the correct language, and do not specify inherit=true
360 if ((xmlElement.getAttribute("lang").equals("") || xmlElement.getAttribute("lang").equalsIgnoreCase(lang)) && !xmlElement.getAttribute("inherit").equalsIgnoreCase("true")) {
361 String name = xmlElement.getAttribute("name") ;
362 //System.out.println(name + " found and added at collect level") ;
363 elements.put(name, xmlElement) ;
364 }
365 }
366 }
367
368 // get all elements from site level
369 if (siteLevel != null) {
370
371 //System.out.println("getting all " + tagName + "'s (" + lang + ") from " + converter.getPrettyString(siteLevel)) ;
372
373 NodeList xmlElements = siteLevel.getElementsByTagName(tagName) ;
374 for (int di = 0, dn = xmlElements.getLength() ; di < dn ; di++) {
375
376 Element xmlElement = (Element)xmlElements.item(di) ;
377
378 // only include elements that are language independent or are the correct language, and which havent already been specified at collect level
379 if (xmlElement.getAttribute("lang").equals("") || xmlElement.getAttribute("lang").equalsIgnoreCase(lang)) {
380 String name = xmlElement.getAttribute("name") ;
381 //System.out.println(name + " found at site level") ;
382
383 if (!elements.containsKey(name)) {
384 elements.put(name, xmlElement) ;
385 //System.out.println(name + " added at site level") ;
386 }
387 }
388 }
389 }
390
391 return elements ;
392 }
393
394
395
396
397
398}
Note: See TracBrowser for help on using the repository browser.