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

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

Merging of skin work with the main trunk

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